10b57cec5SDimitry Andric //===- SemaTemplateDeduction.cpp - Template Argument Deduction ------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements C++ template argument deduction. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "clang/Sema/TemplateDeduction.h" 140b57cec5SDimitry Andric #include "TreeTransform.h" 150b57cec5SDimitry Andric #include "TypeLocBuilder.h" 160b57cec5SDimitry Andric #include "clang/AST/ASTContext.h" 170b57cec5SDimitry Andric #include "clang/AST/ASTLambda.h" 180b57cec5SDimitry Andric #include "clang/AST/Decl.h" 190b57cec5SDimitry Andric #include "clang/AST/DeclAccessPair.h" 200b57cec5SDimitry Andric #include "clang/AST/DeclBase.h" 210b57cec5SDimitry Andric #include "clang/AST/DeclCXX.h" 220b57cec5SDimitry Andric #include "clang/AST/DeclTemplate.h" 230b57cec5SDimitry Andric #include "clang/AST/DeclarationName.h" 240b57cec5SDimitry Andric #include "clang/AST/Expr.h" 250b57cec5SDimitry Andric #include "clang/AST/ExprCXX.h" 260b57cec5SDimitry Andric #include "clang/AST/NestedNameSpecifier.h" 27480093f4SDimitry Andric #include "clang/AST/RecursiveASTVisitor.h" 280b57cec5SDimitry Andric #include "clang/AST/TemplateBase.h" 290b57cec5SDimitry Andric #include "clang/AST/TemplateName.h" 300b57cec5SDimitry Andric #include "clang/AST/Type.h" 310b57cec5SDimitry Andric #include "clang/AST/TypeLoc.h" 320b57cec5SDimitry Andric #include "clang/AST/UnresolvedSet.h" 330b57cec5SDimitry Andric #include "clang/Basic/AddressSpaces.h" 340b57cec5SDimitry Andric #include "clang/Basic/ExceptionSpecificationType.h" 350b57cec5SDimitry Andric #include "clang/Basic/LLVM.h" 360b57cec5SDimitry Andric #include "clang/Basic/LangOptions.h" 370b57cec5SDimitry Andric #include "clang/Basic/PartialDiagnostic.h" 380b57cec5SDimitry Andric #include "clang/Basic/SourceLocation.h" 390b57cec5SDimitry Andric #include "clang/Basic/Specifiers.h" 400b57cec5SDimitry Andric #include "clang/Sema/Ownership.h" 410b57cec5SDimitry Andric #include "clang/Sema/Sema.h" 420b57cec5SDimitry Andric #include "clang/Sema/Template.h" 430b57cec5SDimitry Andric #include "llvm/ADT/APInt.h" 440b57cec5SDimitry Andric #include "llvm/ADT/APSInt.h" 450b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 460b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 470b57cec5SDimitry Andric #include "llvm/ADT/FoldingSet.h" 480b57cec5SDimitry Andric #include "llvm/ADT/SmallBitVector.h" 490b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 500b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 510b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 520b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 530b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 540b57cec5SDimitry Andric #include <algorithm> 550b57cec5SDimitry Andric #include <cassert> 56*bdd1243dSDimitry Andric #include <optional> 570b57cec5SDimitry Andric #include <tuple> 58*bdd1243dSDimitry Andric #include <type_traits> 590b57cec5SDimitry Andric #include <utility> 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric namespace clang { 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric /// Various flags that control template argument deduction. 640b57cec5SDimitry Andric /// 650b57cec5SDimitry Andric /// These flags can be bitwise-OR'd together. 660b57cec5SDimitry Andric enum TemplateDeductionFlags { 670b57cec5SDimitry Andric /// No template argument deduction flags, which indicates the 680b57cec5SDimitry Andric /// strictest results for template argument deduction (as used for, e.g., 690b57cec5SDimitry Andric /// matching class template partial specializations). 700b57cec5SDimitry Andric TDF_None = 0, 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric /// Within template argument deduction from a function call, we are 730b57cec5SDimitry Andric /// matching with a parameter type for which the original parameter was 740b57cec5SDimitry Andric /// a reference. 750b57cec5SDimitry Andric TDF_ParamWithReferenceType = 0x1, 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric /// Within template argument deduction from a function call, we 780b57cec5SDimitry Andric /// are matching in a case where we ignore cv-qualifiers. 790b57cec5SDimitry Andric TDF_IgnoreQualifiers = 0x02, 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric /// Within template argument deduction from a function call, 820b57cec5SDimitry Andric /// we are matching in a case where we can perform template argument 830b57cec5SDimitry Andric /// deduction from a template-id of a derived class of the argument type. 840b57cec5SDimitry Andric TDF_DerivedClass = 0x04, 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric /// Allow non-dependent types to differ, e.g., when performing 870b57cec5SDimitry Andric /// template argument deduction from a function call where conversions 880b57cec5SDimitry Andric /// may apply. 890b57cec5SDimitry Andric TDF_SkipNonDependent = 0x08, 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric /// Whether we are performing template argument deduction for 920b57cec5SDimitry Andric /// parameters and arguments in a top-level template argument 930b57cec5SDimitry Andric TDF_TopLevelParameterTypeList = 0x10, 940b57cec5SDimitry Andric 950b57cec5SDimitry Andric /// Within template argument deduction from overload resolution per 960b57cec5SDimitry Andric /// C++ [over.over] allow matching function types that are compatible in 970b57cec5SDimitry Andric /// terms of noreturn and default calling convention adjustments, or 980b57cec5SDimitry Andric /// similarly matching a declared template specialization against a 990b57cec5SDimitry Andric /// possible template, per C++ [temp.deduct.decl]. In either case, permit 1000b57cec5SDimitry Andric /// deduction where the parameter is a function type that can be converted 1010b57cec5SDimitry Andric /// to the argument type. 1020b57cec5SDimitry Andric TDF_AllowCompatibleFunctionType = 0x20, 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric /// Within template argument deduction for a conversion function, we are 1050b57cec5SDimitry Andric /// matching with an argument type for which the original argument was 1060b57cec5SDimitry Andric /// a reference. 1070b57cec5SDimitry Andric TDF_ArgWithReferenceType = 0x40, 1080b57cec5SDimitry Andric }; 1090b57cec5SDimitry Andric } 1100b57cec5SDimitry Andric 1110b57cec5SDimitry Andric using namespace clang; 1120b57cec5SDimitry Andric using namespace sema; 1130b57cec5SDimitry Andric 1140b57cec5SDimitry Andric /// Compare two APSInts, extending and switching the sign as 1150b57cec5SDimitry Andric /// necessary to compare their values regardless of underlying type. 1160b57cec5SDimitry Andric static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) { 1170b57cec5SDimitry Andric if (Y.getBitWidth() > X.getBitWidth()) 1180b57cec5SDimitry Andric X = X.extend(Y.getBitWidth()); 1190b57cec5SDimitry Andric else if (Y.getBitWidth() < X.getBitWidth()) 1200b57cec5SDimitry Andric Y = Y.extend(X.getBitWidth()); 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric // If there is a signedness mismatch, correct it. 1230b57cec5SDimitry Andric if (X.isSigned() != Y.isSigned()) { 1240b57cec5SDimitry Andric // If the signed value is negative, then the values cannot be the same. 1250b57cec5SDimitry Andric if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative())) 1260b57cec5SDimitry Andric return false; 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric Y.setIsSigned(true); 1290b57cec5SDimitry Andric X.setIsSigned(true); 1300b57cec5SDimitry Andric } 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric return X == Y; 1330b57cec5SDimitry Andric } 1340b57cec5SDimitry Andric 135349cc55cSDimitry Andric static Sema::TemplateDeductionResult DeduceTemplateArgumentsByTypeMatch( 136349cc55cSDimitry Andric Sema &S, TemplateParameterList *TemplateParams, QualType Param, 137349cc55cSDimitry Andric QualType Arg, TemplateDeductionInfo &Info, 138349cc55cSDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, unsigned TDF, 139349cc55cSDimitry Andric bool PartialOrdering = false, bool DeducedFromArrayBound = false); 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric static Sema::TemplateDeductionResult 1420b57cec5SDimitry Andric DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, 143349cc55cSDimitry Andric ArrayRef<TemplateArgument> Ps, 144349cc55cSDimitry Andric ArrayRef<TemplateArgument> As, 1450b57cec5SDimitry Andric TemplateDeductionInfo &Info, 1460b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 1470b57cec5SDimitry Andric bool NumberOfArgumentsMustMatch); 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andric static void MarkUsedTemplateParameters(ASTContext &Ctx, 1500b57cec5SDimitry Andric const TemplateArgument &TemplateArg, 1510b57cec5SDimitry Andric bool OnlyDeduced, unsigned Depth, 1520b57cec5SDimitry Andric llvm::SmallBitVector &Used); 1530b57cec5SDimitry Andric 1540b57cec5SDimitry Andric static void MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, 1550b57cec5SDimitry Andric bool OnlyDeduced, unsigned Level, 1560b57cec5SDimitry Andric llvm::SmallBitVector &Deduced); 1570b57cec5SDimitry Andric 1580b57cec5SDimitry Andric /// If the given expression is of a form that permits the deduction 1590b57cec5SDimitry Andric /// of a non-type template parameter, return the declaration of that 1600b57cec5SDimitry Andric /// non-type template parameter. 161e8d8bef9SDimitry Andric static const NonTypeTemplateParmDecl * 162e8d8bef9SDimitry Andric getDeducedParameterFromExpr(const Expr *E, unsigned Depth) { 1630b57cec5SDimitry Andric // If we are within an alias template, the expression may have undergone 1640b57cec5SDimitry Andric // any number of parameter substitutions already. 1650b57cec5SDimitry Andric while (true) { 166e8d8bef9SDimitry Andric if (const auto *IC = dyn_cast<ImplicitCastExpr>(E)) 1670b57cec5SDimitry Andric E = IC->getSubExpr(); 168e8d8bef9SDimitry Andric else if (const auto *CE = dyn_cast<ConstantExpr>(E)) 1690b57cec5SDimitry Andric E = CE->getSubExpr(); 170e8d8bef9SDimitry Andric else if (const auto *Subst = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) 1710b57cec5SDimitry Andric E = Subst->getReplacement(); 172e8d8bef9SDimitry Andric else if (const auto *CCE = dyn_cast<CXXConstructExpr>(E)) { 173e8d8bef9SDimitry Andric // Look through implicit copy construction from an lvalue of the same type. 174e8d8bef9SDimitry Andric if (CCE->getParenOrBraceRange().isValid()) 175e8d8bef9SDimitry Andric break; 176e8d8bef9SDimitry Andric // Note, there could be default arguments. 177e8d8bef9SDimitry Andric assert(CCE->getNumArgs() >= 1 && "implicit construct expr should have 1 arg"); 178e8d8bef9SDimitry Andric E = CCE->getArg(0); 179e8d8bef9SDimitry Andric } else 1800b57cec5SDimitry Andric break; 1810b57cec5SDimitry Andric } 1820b57cec5SDimitry Andric 183e8d8bef9SDimitry Andric if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 184e8d8bef9SDimitry Andric if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) 185e8d8bef9SDimitry Andric if (NTTP->getDepth() == Depth) 1860b57cec5SDimitry Andric return NTTP; 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric return nullptr; 1890b57cec5SDimitry Andric } 1900b57cec5SDimitry Andric 191e8d8bef9SDimitry Andric static const NonTypeTemplateParmDecl * 192e8d8bef9SDimitry Andric getDeducedParameterFromExpr(TemplateDeductionInfo &Info, Expr *E) { 193e8d8bef9SDimitry Andric return getDeducedParameterFromExpr(E, Info.getDeducedDepth()); 194e8d8bef9SDimitry Andric } 195e8d8bef9SDimitry Andric 1960b57cec5SDimitry Andric /// Determine whether two declaration pointers refer to the same 1970b57cec5SDimitry Andric /// declaration. 1980b57cec5SDimitry Andric static bool isSameDeclaration(Decl *X, Decl *Y) { 1990b57cec5SDimitry Andric if (NamedDecl *NX = dyn_cast<NamedDecl>(X)) 2000b57cec5SDimitry Andric X = NX->getUnderlyingDecl(); 2010b57cec5SDimitry Andric if (NamedDecl *NY = dyn_cast<NamedDecl>(Y)) 2020b57cec5SDimitry Andric Y = NY->getUnderlyingDecl(); 2030b57cec5SDimitry Andric 2040b57cec5SDimitry Andric return X->getCanonicalDecl() == Y->getCanonicalDecl(); 2050b57cec5SDimitry Andric } 2060b57cec5SDimitry Andric 2070b57cec5SDimitry Andric /// Verify that the given, deduced template arguments are compatible. 2080b57cec5SDimitry Andric /// 2090b57cec5SDimitry Andric /// \returns The deduced template argument, or a NULL template argument if 2100b57cec5SDimitry Andric /// the deduced template arguments were incompatible. 2110b57cec5SDimitry Andric static DeducedTemplateArgument 2120b57cec5SDimitry Andric checkDeducedTemplateArguments(ASTContext &Context, 2130b57cec5SDimitry Andric const DeducedTemplateArgument &X, 2140b57cec5SDimitry Andric const DeducedTemplateArgument &Y) { 2150b57cec5SDimitry Andric // We have no deduction for one or both of the arguments; they're compatible. 2160b57cec5SDimitry Andric if (X.isNull()) 2170b57cec5SDimitry Andric return Y; 2180b57cec5SDimitry Andric if (Y.isNull()) 2190b57cec5SDimitry Andric return X; 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric // If we have two non-type template argument values deduced for the same 2220b57cec5SDimitry Andric // parameter, they must both match the type of the parameter, and thus must 2230b57cec5SDimitry Andric // match each other's type. As we're only keeping one of them, we must check 2240b57cec5SDimitry Andric // for that now. The exception is that if either was deduced from an array 2250b57cec5SDimitry Andric // bound, the type is permitted to differ. 2260b57cec5SDimitry Andric if (!X.wasDeducedFromArrayBound() && !Y.wasDeducedFromArrayBound()) { 2270b57cec5SDimitry Andric QualType XType = X.getNonTypeTemplateArgumentType(); 2280b57cec5SDimitry Andric if (!XType.isNull()) { 2290b57cec5SDimitry Andric QualType YType = Y.getNonTypeTemplateArgumentType(); 2300b57cec5SDimitry Andric if (YType.isNull() || !Context.hasSameType(XType, YType)) 2310b57cec5SDimitry Andric return DeducedTemplateArgument(); 2320b57cec5SDimitry Andric } 2330b57cec5SDimitry Andric } 2340b57cec5SDimitry Andric 2350b57cec5SDimitry Andric switch (X.getKind()) { 2360b57cec5SDimitry Andric case TemplateArgument::Null: 2370b57cec5SDimitry Andric llvm_unreachable("Non-deduced template arguments handled above"); 2380b57cec5SDimitry Andric 239*bdd1243dSDimitry Andric case TemplateArgument::Type: { 2400b57cec5SDimitry Andric // If two template type arguments have the same type, they're compatible. 241*bdd1243dSDimitry Andric QualType TX = X.getAsType(), TY = Y.getAsType(); 242*bdd1243dSDimitry Andric if (Y.getKind() == TemplateArgument::Type && Context.hasSameType(TX, TY)) 243*bdd1243dSDimitry Andric return DeducedTemplateArgument(Context.getCommonSugaredType(TX, TY), 244*bdd1243dSDimitry Andric X.wasDeducedFromArrayBound() || 245*bdd1243dSDimitry Andric Y.wasDeducedFromArrayBound()); 2460b57cec5SDimitry Andric 2470b57cec5SDimitry Andric // If one of the two arguments was deduced from an array bound, the other 2480b57cec5SDimitry Andric // supersedes it. 2490b57cec5SDimitry Andric if (X.wasDeducedFromArrayBound() != Y.wasDeducedFromArrayBound()) 2500b57cec5SDimitry Andric return X.wasDeducedFromArrayBound() ? Y : X; 2510b57cec5SDimitry Andric 2520b57cec5SDimitry Andric // The arguments are not compatible. 2530b57cec5SDimitry Andric return DeducedTemplateArgument(); 254*bdd1243dSDimitry Andric } 2550b57cec5SDimitry Andric 2560b57cec5SDimitry Andric case TemplateArgument::Integral: 2570b57cec5SDimitry Andric // If we deduced a constant in one case and either a dependent expression or 2580b57cec5SDimitry Andric // declaration in another case, keep the integral constant. 2590b57cec5SDimitry Andric // If both are integral constants with the same value, keep that value. 2600b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::Expression || 2610b57cec5SDimitry Andric Y.getKind() == TemplateArgument::Declaration || 2620b57cec5SDimitry Andric (Y.getKind() == TemplateArgument::Integral && 2630b57cec5SDimitry Andric hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()))) 2640b57cec5SDimitry Andric return X.wasDeducedFromArrayBound() ? Y : X; 2650b57cec5SDimitry Andric 2660b57cec5SDimitry Andric // All other combinations are incompatible. 2670b57cec5SDimitry Andric return DeducedTemplateArgument(); 2680b57cec5SDimitry Andric 2690b57cec5SDimitry Andric case TemplateArgument::Template: 2700b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::Template && 2710b57cec5SDimitry Andric Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate())) 2720b57cec5SDimitry Andric return X; 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric // All other combinations are incompatible. 2750b57cec5SDimitry Andric return DeducedTemplateArgument(); 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric case TemplateArgument::TemplateExpansion: 2780b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::TemplateExpansion && 2790b57cec5SDimitry Andric Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(), 2800b57cec5SDimitry Andric Y.getAsTemplateOrTemplatePattern())) 2810b57cec5SDimitry Andric return X; 2820b57cec5SDimitry Andric 2830b57cec5SDimitry Andric // All other combinations are incompatible. 2840b57cec5SDimitry Andric return DeducedTemplateArgument(); 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric case TemplateArgument::Expression: { 2870b57cec5SDimitry Andric if (Y.getKind() != TemplateArgument::Expression) 2880b57cec5SDimitry Andric return checkDeducedTemplateArguments(Context, Y, X); 2890b57cec5SDimitry Andric 2900b57cec5SDimitry Andric // Compare the expressions for equality 2910b57cec5SDimitry Andric llvm::FoldingSetNodeID ID1, ID2; 2920b57cec5SDimitry Andric X.getAsExpr()->Profile(ID1, Context, true); 2930b57cec5SDimitry Andric Y.getAsExpr()->Profile(ID2, Context, true); 2940b57cec5SDimitry Andric if (ID1 == ID2) 2950b57cec5SDimitry Andric return X.wasDeducedFromArrayBound() ? Y : X; 2960b57cec5SDimitry Andric 2970b57cec5SDimitry Andric // Differing dependent expressions are incompatible. 2980b57cec5SDimitry Andric return DeducedTemplateArgument(); 2990b57cec5SDimitry Andric } 3000b57cec5SDimitry Andric 3010b57cec5SDimitry Andric case TemplateArgument::Declaration: 3020b57cec5SDimitry Andric assert(!X.wasDeducedFromArrayBound()); 3030b57cec5SDimitry Andric 3040b57cec5SDimitry Andric // If we deduced a declaration and a dependent expression, keep the 3050b57cec5SDimitry Andric // declaration. 3060b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::Expression) 3070b57cec5SDimitry Andric return X; 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric // If we deduced a declaration and an integral constant, keep the 3100b57cec5SDimitry Andric // integral constant and whichever type did not come from an array 3110b57cec5SDimitry Andric // bound. 3120b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::Integral) { 3130b57cec5SDimitry Andric if (Y.wasDeducedFromArrayBound()) 3140b57cec5SDimitry Andric return TemplateArgument(Context, Y.getAsIntegral(), 3150b57cec5SDimitry Andric X.getParamTypeForDecl()); 3160b57cec5SDimitry Andric return Y; 3170b57cec5SDimitry Andric } 3180b57cec5SDimitry Andric 3190b57cec5SDimitry Andric // If we deduced two declarations, make sure that they refer to the 3200b57cec5SDimitry Andric // same declaration. 3210b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::Declaration && 3220b57cec5SDimitry Andric isSameDeclaration(X.getAsDecl(), Y.getAsDecl())) 3230b57cec5SDimitry Andric return X; 3240b57cec5SDimitry Andric 3250b57cec5SDimitry Andric // All other combinations are incompatible. 3260b57cec5SDimitry Andric return DeducedTemplateArgument(); 3270b57cec5SDimitry Andric 3280b57cec5SDimitry Andric case TemplateArgument::NullPtr: 3290b57cec5SDimitry Andric // If we deduced a null pointer and a dependent expression, keep the 3300b57cec5SDimitry Andric // null pointer. 3310b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::Expression) 332*bdd1243dSDimitry Andric return TemplateArgument(Context.getCommonSugaredType( 333*bdd1243dSDimitry Andric X.getNullPtrType(), Y.getAsExpr()->getType()), 334*bdd1243dSDimitry Andric true); 3350b57cec5SDimitry Andric 3360b57cec5SDimitry Andric // If we deduced a null pointer and an integral constant, keep the 3370b57cec5SDimitry Andric // integral constant. 3380b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::Integral) 3390b57cec5SDimitry Andric return Y; 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andric // If we deduced two null pointers, they are the same. 3420b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::NullPtr) 343*bdd1243dSDimitry Andric return TemplateArgument( 344*bdd1243dSDimitry Andric Context.getCommonSugaredType(X.getNullPtrType(), Y.getNullPtrType()), 345*bdd1243dSDimitry Andric true); 3460b57cec5SDimitry Andric 3470b57cec5SDimitry Andric // All other combinations are incompatible. 3480b57cec5SDimitry Andric return DeducedTemplateArgument(); 3490b57cec5SDimitry Andric 3500b57cec5SDimitry Andric case TemplateArgument::Pack: { 3510b57cec5SDimitry Andric if (Y.getKind() != TemplateArgument::Pack || 3520b57cec5SDimitry Andric X.pack_size() != Y.pack_size()) 3530b57cec5SDimitry Andric return DeducedTemplateArgument(); 3540b57cec5SDimitry Andric 3550b57cec5SDimitry Andric llvm::SmallVector<TemplateArgument, 8> NewPack; 3560b57cec5SDimitry Andric for (TemplateArgument::pack_iterator XA = X.pack_begin(), 3570b57cec5SDimitry Andric XAEnd = X.pack_end(), 3580b57cec5SDimitry Andric YA = Y.pack_begin(); 3590b57cec5SDimitry Andric XA != XAEnd; ++XA, ++YA) { 3600b57cec5SDimitry Andric TemplateArgument Merged = checkDeducedTemplateArguments( 3610b57cec5SDimitry Andric Context, DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()), 3620b57cec5SDimitry Andric DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound())); 3635ffd83dbSDimitry Andric if (Merged.isNull() && !(XA->isNull() && YA->isNull())) 3640b57cec5SDimitry Andric return DeducedTemplateArgument(); 3650b57cec5SDimitry Andric NewPack.push_back(Merged); 3660b57cec5SDimitry Andric } 3670b57cec5SDimitry Andric 3680b57cec5SDimitry Andric return DeducedTemplateArgument( 3690b57cec5SDimitry Andric TemplateArgument::CreatePackCopy(Context, NewPack), 3700b57cec5SDimitry Andric X.wasDeducedFromArrayBound() && Y.wasDeducedFromArrayBound()); 3710b57cec5SDimitry Andric } 3720b57cec5SDimitry Andric } 3730b57cec5SDimitry Andric 3740b57cec5SDimitry Andric llvm_unreachable("Invalid TemplateArgument Kind!"); 3750b57cec5SDimitry Andric } 3760b57cec5SDimitry Andric 3770b57cec5SDimitry Andric /// Deduce the value of the given non-type template parameter 3780b57cec5SDimitry Andric /// as the given deduced template argument. All non-type template parameter 3790b57cec5SDimitry Andric /// deduction is funneled through here. 3800b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( 3810b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, 382e8d8bef9SDimitry Andric const NonTypeTemplateParmDecl *NTTP, const DeducedTemplateArgument &NewDeduced, 3830b57cec5SDimitry Andric QualType ValueType, TemplateDeductionInfo &Info, 3840b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 3850b57cec5SDimitry Andric assert(NTTP->getDepth() == Info.getDeducedDepth() && 3860b57cec5SDimitry Andric "deducing non-type template argument with wrong depth"); 3870b57cec5SDimitry Andric 3880b57cec5SDimitry Andric DeducedTemplateArgument Result = checkDeducedTemplateArguments( 3890b57cec5SDimitry Andric S.Context, Deduced[NTTP->getIndex()], NewDeduced); 3900b57cec5SDimitry Andric if (Result.isNull()) { 391e8d8bef9SDimitry Andric Info.Param = const_cast<NonTypeTemplateParmDecl*>(NTTP); 3920b57cec5SDimitry Andric Info.FirstArg = Deduced[NTTP->getIndex()]; 3930b57cec5SDimitry Andric Info.SecondArg = NewDeduced; 3940b57cec5SDimitry Andric return Sema::TDK_Inconsistent; 3950b57cec5SDimitry Andric } 3960b57cec5SDimitry Andric 3970b57cec5SDimitry Andric Deduced[NTTP->getIndex()] = Result; 3980b57cec5SDimitry Andric if (!S.getLangOpts().CPlusPlus17) 3990b57cec5SDimitry Andric return Sema::TDK_Success; 4000b57cec5SDimitry Andric 4010b57cec5SDimitry Andric if (NTTP->isExpandedParameterPack()) 4020b57cec5SDimitry Andric // FIXME: We may still need to deduce parts of the type here! But we 4030b57cec5SDimitry Andric // don't have any way to find which slice of the type to use, and the 4040b57cec5SDimitry Andric // type stored on the NTTP itself is nonsense. Perhaps the type of an 4050b57cec5SDimitry Andric // expanded NTTP should be a pack expansion type? 4060b57cec5SDimitry Andric return Sema::TDK_Success; 4070b57cec5SDimitry Andric 4080b57cec5SDimitry Andric // Get the type of the parameter for deduction. If it's a (dependent) array 4090b57cec5SDimitry Andric // or function type, we will not have decayed it yet, so do that now. 4100b57cec5SDimitry Andric QualType ParamType = S.Context.getAdjustedParameterType(NTTP->getType()); 4110b57cec5SDimitry Andric if (auto *Expansion = dyn_cast<PackExpansionType>(ParamType)) 4120b57cec5SDimitry Andric ParamType = Expansion->getPattern(); 4130b57cec5SDimitry Andric 4140b57cec5SDimitry Andric // FIXME: It's not clear how deduction of a parameter of reference 4150b57cec5SDimitry Andric // type from an argument (of non-reference type) should be performed. 4160b57cec5SDimitry Andric // For now, we just remove reference types from both sides and let 4170b57cec5SDimitry Andric // the final check for matching types sort out the mess. 418e8d8bef9SDimitry Andric ValueType = ValueType.getNonReferenceType(); 419e8d8bef9SDimitry Andric if (ParamType->isReferenceType()) 420e8d8bef9SDimitry Andric ParamType = ParamType.getNonReferenceType(); 421e8d8bef9SDimitry Andric else 422e8d8bef9SDimitry Andric // Top-level cv-qualifiers are irrelevant for a non-reference type. 423e8d8bef9SDimitry Andric ValueType = ValueType.getUnqualifiedType(); 424e8d8bef9SDimitry Andric 4250b57cec5SDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 426e8d8bef9SDimitry Andric S, TemplateParams, ParamType, ValueType, Info, Deduced, 427e8d8bef9SDimitry Andric TDF_SkipNonDependent, /*PartialOrdering=*/false, 4280b57cec5SDimitry Andric /*ArrayBound=*/NewDeduced.wasDeducedFromArrayBound()); 4290b57cec5SDimitry Andric } 4300b57cec5SDimitry Andric 4310b57cec5SDimitry Andric /// Deduce the value of the given non-type template parameter 4320b57cec5SDimitry Andric /// from the given integral constant. 4330b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( 4340b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, 435e8d8bef9SDimitry Andric const NonTypeTemplateParmDecl *NTTP, const llvm::APSInt &Value, 4360b57cec5SDimitry Andric QualType ValueType, bool DeducedFromArrayBound, TemplateDeductionInfo &Info, 4370b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 4380b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument( 4390b57cec5SDimitry Andric S, TemplateParams, NTTP, 4400b57cec5SDimitry Andric DeducedTemplateArgument(S.Context, Value, ValueType, 4410b57cec5SDimitry Andric DeducedFromArrayBound), 4420b57cec5SDimitry Andric ValueType, Info, Deduced); 4430b57cec5SDimitry Andric } 4440b57cec5SDimitry Andric 4450b57cec5SDimitry Andric /// Deduce the value of the given non-type template parameter 4460b57cec5SDimitry Andric /// from the given null pointer template argument type. 4470b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceNullPtrTemplateArgument( 4480b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, 449e8d8bef9SDimitry Andric const NonTypeTemplateParmDecl *NTTP, QualType NullPtrType, 4500b57cec5SDimitry Andric TemplateDeductionInfo &Info, 4510b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 452fe6060f1SDimitry Andric Expr *Value = S.ImpCastExprToType( 453fe6060f1SDimitry Andric new (S.Context) CXXNullPtrLiteralExpr(S.Context.NullPtrTy, 454fe6060f1SDimitry Andric NTTP->getLocation()), 455fe6060f1SDimitry Andric NullPtrType, 456fe6060f1SDimitry Andric NullPtrType->isMemberPointerType() ? CK_NullToMemberPointer 457fe6060f1SDimitry Andric : CK_NullToPointer) 4580b57cec5SDimitry Andric .get(); 4590b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, 4600b57cec5SDimitry Andric DeducedTemplateArgument(Value), 4610b57cec5SDimitry Andric Value->getType(), Info, Deduced); 4620b57cec5SDimitry Andric } 4630b57cec5SDimitry Andric 4640b57cec5SDimitry Andric /// Deduce the value of the given non-type template parameter 4650b57cec5SDimitry Andric /// from the given type- or value-dependent expression. 4660b57cec5SDimitry Andric /// 4670b57cec5SDimitry Andric /// \returns true if deduction succeeded, false otherwise. 4680b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( 4690b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, 470e8d8bef9SDimitry Andric const NonTypeTemplateParmDecl *NTTP, Expr *Value, TemplateDeductionInfo &Info, 4710b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 4720b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, 4730b57cec5SDimitry Andric DeducedTemplateArgument(Value), 4740b57cec5SDimitry Andric Value->getType(), Info, Deduced); 4750b57cec5SDimitry Andric } 4760b57cec5SDimitry Andric 4770b57cec5SDimitry Andric /// Deduce the value of the given non-type template parameter 4780b57cec5SDimitry Andric /// from the given declaration. 4790b57cec5SDimitry Andric /// 4800b57cec5SDimitry Andric /// \returns true if deduction succeeded, false otherwise. 4810b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( 4820b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, 483e8d8bef9SDimitry Andric const NonTypeTemplateParmDecl *NTTP, ValueDecl *D, QualType T, 4840b57cec5SDimitry Andric TemplateDeductionInfo &Info, 4850b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 4860b57cec5SDimitry Andric D = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; 4870b57cec5SDimitry Andric TemplateArgument New(D, T); 4880b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument( 4890b57cec5SDimitry Andric S, TemplateParams, NTTP, DeducedTemplateArgument(New), T, Info, Deduced); 4900b57cec5SDimitry Andric } 4910b57cec5SDimitry Andric 4920b57cec5SDimitry Andric static Sema::TemplateDeductionResult 4930b57cec5SDimitry Andric DeduceTemplateArguments(Sema &S, 4940b57cec5SDimitry Andric TemplateParameterList *TemplateParams, 4950b57cec5SDimitry Andric TemplateName Param, 4960b57cec5SDimitry Andric TemplateName Arg, 4970b57cec5SDimitry Andric TemplateDeductionInfo &Info, 4980b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 4990b57cec5SDimitry Andric TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); 5000b57cec5SDimitry Andric if (!ParamDecl) { 5010b57cec5SDimitry Andric // The parameter type is dependent and is not a template template parameter, 5020b57cec5SDimitry Andric // so there is nothing that we can deduce. 5030b57cec5SDimitry Andric return Sema::TDK_Success; 5040b57cec5SDimitry Andric } 5050b57cec5SDimitry Andric 5060b57cec5SDimitry Andric if (TemplateTemplateParmDecl *TempParam 5070b57cec5SDimitry Andric = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) { 5080b57cec5SDimitry Andric // If we're not deducing at this depth, there's nothing to deduce. 5090b57cec5SDimitry Andric if (TempParam->getDepth() != Info.getDeducedDepth()) 5100b57cec5SDimitry Andric return Sema::TDK_Success; 5110b57cec5SDimitry Andric 5120b57cec5SDimitry Andric DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg)); 5130b57cec5SDimitry Andric DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, 5140b57cec5SDimitry Andric Deduced[TempParam->getIndex()], 5150b57cec5SDimitry Andric NewDeduced); 5160b57cec5SDimitry Andric if (Result.isNull()) { 5170b57cec5SDimitry Andric Info.Param = TempParam; 5180b57cec5SDimitry Andric Info.FirstArg = Deduced[TempParam->getIndex()]; 5190b57cec5SDimitry Andric Info.SecondArg = NewDeduced; 5200b57cec5SDimitry Andric return Sema::TDK_Inconsistent; 5210b57cec5SDimitry Andric } 5220b57cec5SDimitry Andric 5230b57cec5SDimitry Andric Deduced[TempParam->getIndex()] = Result; 5240b57cec5SDimitry Andric return Sema::TDK_Success; 5250b57cec5SDimitry Andric } 5260b57cec5SDimitry Andric 5270b57cec5SDimitry Andric // Verify that the two template names are equivalent. 5280b57cec5SDimitry Andric if (S.Context.hasSameTemplateName(Param, Arg)) 5290b57cec5SDimitry Andric return Sema::TDK_Success; 5300b57cec5SDimitry Andric 5310b57cec5SDimitry Andric // Mismatch of non-dependent template parameter to argument. 5320b57cec5SDimitry Andric Info.FirstArg = TemplateArgument(Param); 5330b57cec5SDimitry Andric Info.SecondArg = TemplateArgument(Arg); 5340b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 5350b57cec5SDimitry Andric } 5360b57cec5SDimitry Andric 5370b57cec5SDimitry Andric /// Deduce the template arguments by comparing the template parameter 5380b57cec5SDimitry Andric /// type (which is a template-id) with the template argument type. 5390b57cec5SDimitry Andric /// 5400b57cec5SDimitry Andric /// \param S the Sema 5410b57cec5SDimitry Andric /// 5420b57cec5SDimitry Andric /// \param TemplateParams the template parameters that we are deducing 5430b57cec5SDimitry Andric /// 54481ad6265SDimitry Andric /// \param P the parameter type 5450b57cec5SDimitry Andric /// 54681ad6265SDimitry Andric /// \param A the argument type 5470b57cec5SDimitry Andric /// 5480b57cec5SDimitry Andric /// \param Info information about the template argument deduction itself 5490b57cec5SDimitry Andric /// 5500b57cec5SDimitry Andric /// \param Deduced the deduced template arguments 5510b57cec5SDimitry Andric /// 5520b57cec5SDimitry Andric /// \returns the result of template argument deduction so far. Note that a 5530b57cec5SDimitry Andric /// "success" result means that template argument deduction has not yet failed, 5540b57cec5SDimitry Andric /// but it may still fail, later, for other reasons. 5550b57cec5SDimitry Andric static Sema::TemplateDeductionResult 556349cc55cSDimitry Andric DeduceTemplateSpecArguments(Sema &S, TemplateParameterList *TemplateParams, 557349cc55cSDimitry Andric const QualType P, QualType A, 5580b57cec5SDimitry Andric TemplateDeductionInfo &Info, 5590b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 560349cc55cSDimitry Andric QualType UP = P; 561349cc55cSDimitry Andric if (const auto *IP = P->getAs<InjectedClassNameType>()) 562349cc55cSDimitry Andric UP = IP->getInjectedSpecializationType(); 563349cc55cSDimitry Andric // FIXME: Try to preserve type sugar here, which is hard 564349cc55cSDimitry Andric // because of the unresolved template arguments. 565349cc55cSDimitry Andric const auto *TP = UP.getCanonicalType()->castAs<TemplateSpecializationType>(); 566*bdd1243dSDimitry Andric TemplateName TNP = TP->getTemplateName(); 567*bdd1243dSDimitry Andric 568*bdd1243dSDimitry Andric // If the parameter is an alias template, there is nothing to deduce. 569*bdd1243dSDimitry Andric if (const auto *TD = TNP.getAsTemplateDecl(); TD && TD->isTypeAlias()) 570*bdd1243dSDimitry Andric return Sema::TDK_Success; 571*bdd1243dSDimitry Andric 572349cc55cSDimitry Andric ArrayRef<TemplateArgument> PResolved = TP->template_arguments(); 5730b57cec5SDimitry Andric 574349cc55cSDimitry Andric QualType UA = A; 5750b57cec5SDimitry Andric // Treat an injected-class-name as its underlying template-id. 576349cc55cSDimitry Andric if (const auto *Injected = A->getAs<InjectedClassNameType>()) 577349cc55cSDimitry Andric UA = Injected->getInjectedSpecializationType(); 5780b57cec5SDimitry Andric 5790b57cec5SDimitry Andric // Check whether the template argument is a dependent template-id. 580349cc55cSDimitry Andric // FIXME: Should not lose sugar here. 581349cc55cSDimitry Andric if (const auto *SA = 582349cc55cSDimitry Andric dyn_cast<TemplateSpecializationType>(UA.getCanonicalType())) { 583*bdd1243dSDimitry Andric TemplateName TNA = SA->getTemplateName(); 584*bdd1243dSDimitry Andric 585*bdd1243dSDimitry Andric // If the argument is an alias template, there is nothing to deduce. 586*bdd1243dSDimitry Andric if (const auto *TD = TNA.getAsTemplateDecl(); TD && TD->isTypeAlias()) 587*bdd1243dSDimitry Andric return Sema::TDK_Success; 588*bdd1243dSDimitry Andric 5890b57cec5SDimitry Andric // Perform template argument deduction for the template name. 590349cc55cSDimitry Andric if (auto Result = 591*bdd1243dSDimitry Andric DeduceTemplateArguments(S, TemplateParams, TNP, TNA, Info, Deduced)) 5920b57cec5SDimitry Andric return Result; 5930b57cec5SDimitry Andric // Perform template argument deduction on each template 5940b57cec5SDimitry Andric // argument. Ignore any missing/extra arguments, since they could be 5950b57cec5SDimitry Andric // filled in by default arguments. 596349cc55cSDimitry Andric return DeduceTemplateArguments(S, TemplateParams, PResolved, 597349cc55cSDimitry Andric SA->template_arguments(), Info, Deduced, 5980b57cec5SDimitry Andric /*NumberOfArgumentsMustMatch=*/false); 5990b57cec5SDimitry Andric } 6000b57cec5SDimitry Andric 6010b57cec5SDimitry Andric // If the argument type is a class template specialization, we 6020b57cec5SDimitry Andric // perform template argument deduction using its template 6030b57cec5SDimitry Andric // arguments. 604349cc55cSDimitry Andric const auto *RA = UA->getAs<RecordType>(); 605349cc55cSDimitry Andric const auto *SA = 606349cc55cSDimitry Andric RA ? dyn_cast<ClassTemplateSpecializationDecl>(RA->getDecl()) : nullptr; 607349cc55cSDimitry Andric if (!SA) { 608349cc55cSDimitry Andric Info.FirstArg = TemplateArgument(P); 609349cc55cSDimitry Andric Info.SecondArg = TemplateArgument(A); 6100b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 6110b57cec5SDimitry Andric } 6120b57cec5SDimitry Andric 6130b57cec5SDimitry Andric // Perform template argument deduction for the template name. 614349cc55cSDimitry Andric if (auto Result = DeduceTemplateArguments( 615349cc55cSDimitry Andric S, TemplateParams, TP->getTemplateName(), 616349cc55cSDimitry Andric TemplateName(SA->getSpecializedTemplate()), Info, Deduced)) 6170b57cec5SDimitry Andric return Result; 6180b57cec5SDimitry Andric 6190b57cec5SDimitry Andric // Perform template argument deduction for the template arguments. 620349cc55cSDimitry Andric return DeduceTemplateArguments(S, TemplateParams, PResolved, 621349cc55cSDimitry Andric SA->getTemplateArgs().asArray(), Info, Deduced, 622349cc55cSDimitry Andric /*NumberOfArgumentsMustMatch=*/true); 6230b57cec5SDimitry Andric } 6240b57cec5SDimitry Andric 625349cc55cSDimitry Andric static bool IsPossiblyOpaquelyQualifiedTypeInternal(const Type *T) { 626349cc55cSDimitry Andric assert(T->isCanonicalUnqualified()); 627349cc55cSDimitry Andric 6280b57cec5SDimitry Andric switch (T->getTypeClass()) { 6290b57cec5SDimitry Andric case Type::TypeOfExpr: 6300b57cec5SDimitry Andric case Type::TypeOf: 6310b57cec5SDimitry Andric case Type::DependentName: 6320b57cec5SDimitry Andric case Type::Decltype: 6330b57cec5SDimitry Andric case Type::UnresolvedUsing: 6340b57cec5SDimitry Andric case Type::TemplateTypeParm: 6350b57cec5SDimitry Andric return true; 6360b57cec5SDimitry Andric 6370b57cec5SDimitry Andric case Type::ConstantArray: 6380b57cec5SDimitry Andric case Type::IncompleteArray: 6390b57cec5SDimitry Andric case Type::VariableArray: 6400b57cec5SDimitry Andric case Type::DependentSizedArray: 641349cc55cSDimitry Andric return IsPossiblyOpaquelyQualifiedTypeInternal( 642349cc55cSDimitry Andric cast<ArrayType>(T)->getElementType().getTypePtr()); 6430b57cec5SDimitry Andric 6440b57cec5SDimitry Andric default: 6450b57cec5SDimitry Andric return false; 6460b57cec5SDimitry Andric } 6470b57cec5SDimitry Andric } 6480b57cec5SDimitry Andric 649349cc55cSDimitry Andric /// Determines whether the given type is an opaque type that 650349cc55cSDimitry Andric /// might be more qualified when instantiated. 651349cc55cSDimitry Andric static bool IsPossiblyOpaquelyQualifiedType(QualType T) { 652349cc55cSDimitry Andric return IsPossiblyOpaquelyQualifiedTypeInternal( 653349cc55cSDimitry Andric T->getCanonicalTypeInternal().getTypePtr()); 654349cc55cSDimitry Andric } 655349cc55cSDimitry Andric 6560b57cec5SDimitry Andric /// Helper function to build a TemplateParameter when we don't 6570b57cec5SDimitry Andric /// know its type statically. 6580b57cec5SDimitry Andric static TemplateParameter makeTemplateParameter(Decl *D) { 6590b57cec5SDimitry Andric if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) 6600b57cec5SDimitry Andric return TemplateParameter(TTP); 6610b57cec5SDimitry Andric if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) 6620b57cec5SDimitry Andric return TemplateParameter(NTTP); 6630b57cec5SDimitry Andric 6640b57cec5SDimitry Andric return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); 6650b57cec5SDimitry Andric } 6660b57cec5SDimitry Andric 6670b57cec5SDimitry Andric /// A pack that we're currently deducing. 6680b57cec5SDimitry Andric struct clang::DeducedPack { 6690b57cec5SDimitry Andric // The index of the pack. 6700b57cec5SDimitry Andric unsigned Index; 6710b57cec5SDimitry Andric 6720b57cec5SDimitry Andric // The old value of the pack before we started deducing it. 6730b57cec5SDimitry Andric DeducedTemplateArgument Saved; 6740b57cec5SDimitry Andric 6750b57cec5SDimitry Andric // A deferred value of this pack from an inner deduction, that couldn't be 6760b57cec5SDimitry Andric // deduced because this deduction hadn't happened yet. 6770b57cec5SDimitry Andric DeducedTemplateArgument DeferredDeduction; 6780b57cec5SDimitry Andric 6790b57cec5SDimitry Andric // The new value of the pack. 6800b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> New; 6810b57cec5SDimitry Andric 6820b57cec5SDimitry Andric // The outer deduction for this pack, if any. 6830b57cec5SDimitry Andric DeducedPack *Outer = nullptr; 6840b57cec5SDimitry Andric 6850b57cec5SDimitry Andric DeducedPack(unsigned Index) : Index(Index) {} 6860b57cec5SDimitry Andric }; 6870b57cec5SDimitry Andric 6880b57cec5SDimitry Andric namespace { 6890b57cec5SDimitry Andric 6900b57cec5SDimitry Andric /// A scope in which we're performing pack deduction. 6910b57cec5SDimitry Andric class PackDeductionScope { 6920b57cec5SDimitry Andric public: 6930b57cec5SDimitry Andric /// Prepare to deduce the packs named within Pattern. 6940b57cec5SDimitry Andric PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, 6950b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 6960b57cec5SDimitry Andric TemplateDeductionInfo &Info, TemplateArgument Pattern) 6970b57cec5SDimitry Andric : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { 6980b57cec5SDimitry Andric unsigned NumNamedPacks = addPacks(Pattern); 6990b57cec5SDimitry Andric finishConstruction(NumNamedPacks); 7000b57cec5SDimitry Andric } 7010b57cec5SDimitry Andric 7020b57cec5SDimitry Andric /// Prepare to directly deduce arguments of the parameter with index \p Index. 7030b57cec5SDimitry Andric PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, 7040b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 7050b57cec5SDimitry Andric TemplateDeductionInfo &Info, unsigned Index) 7060b57cec5SDimitry Andric : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { 7070b57cec5SDimitry Andric addPack(Index); 7080b57cec5SDimitry Andric finishConstruction(1); 7090b57cec5SDimitry Andric } 7100b57cec5SDimitry Andric 7110b57cec5SDimitry Andric private: 7120b57cec5SDimitry Andric void addPack(unsigned Index) { 7130b57cec5SDimitry Andric // Save the deduced template argument for the parameter pack expanded 7140b57cec5SDimitry Andric // by this pack expansion, then clear out the deduction. 7150b57cec5SDimitry Andric DeducedPack Pack(Index); 7160b57cec5SDimitry Andric Pack.Saved = Deduced[Index]; 7170b57cec5SDimitry Andric Deduced[Index] = TemplateArgument(); 7180b57cec5SDimitry Andric 7190b57cec5SDimitry Andric // FIXME: What if we encounter multiple packs with different numbers of 7200b57cec5SDimitry Andric // pre-expanded expansions? (This should already have been diagnosed 7210b57cec5SDimitry Andric // during substitution.) 722*bdd1243dSDimitry Andric if (std::optional<unsigned> ExpandedPackExpansions = 7230b57cec5SDimitry Andric getExpandedPackSize(TemplateParams->getParam(Index))) 7240b57cec5SDimitry Andric FixedNumExpansions = ExpandedPackExpansions; 7250b57cec5SDimitry Andric 7260b57cec5SDimitry Andric Packs.push_back(Pack); 7270b57cec5SDimitry Andric } 7280b57cec5SDimitry Andric 7290b57cec5SDimitry Andric unsigned addPacks(TemplateArgument Pattern) { 7300b57cec5SDimitry Andric // Compute the set of template parameter indices that correspond to 7310b57cec5SDimitry Andric // parameter packs expanded by the pack expansion. 7320b57cec5SDimitry Andric llvm::SmallBitVector SawIndices(TemplateParams->size()); 73355e4f9d5SDimitry Andric llvm::SmallVector<TemplateArgument, 4> ExtraDeductions; 7340b57cec5SDimitry Andric 7350b57cec5SDimitry Andric auto AddPack = [&](unsigned Index) { 7360b57cec5SDimitry Andric if (SawIndices[Index]) 7370b57cec5SDimitry Andric return; 7380b57cec5SDimitry Andric SawIndices[Index] = true; 7390b57cec5SDimitry Andric addPack(Index); 74055e4f9d5SDimitry Andric 74155e4f9d5SDimitry Andric // Deducing a parameter pack that is a pack expansion also constrains the 74255e4f9d5SDimitry Andric // packs appearing in that parameter to have the same deduced arity. Also, 74355e4f9d5SDimitry Andric // in C++17 onwards, deducing a non-type template parameter deduces its 74455e4f9d5SDimitry Andric // type, so we need to collect the pending deduced values for those packs. 74555e4f9d5SDimitry Andric if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>( 74655e4f9d5SDimitry Andric TemplateParams->getParam(Index))) { 7475ffd83dbSDimitry Andric if (!NTTP->isExpandedParameterPack()) 74855e4f9d5SDimitry Andric if (auto *Expansion = dyn_cast<PackExpansionType>(NTTP->getType())) 74955e4f9d5SDimitry Andric ExtraDeductions.push_back(Expansion->getPattern()); 75055e4f9d5SDimitry Andric } 75155e4f9d5SDimitry Andric // FIXME: Also collect the unexpanded packs in any type and template 75255e4f9d5SDimitry Andric // parameter packs that are pack expansions. 7530b57cec5SDimitry Andric }; 7540b57cec5SDimitry Andric 75555e4f9d5SDimitry Andric auto Collect = [&](TemplateArgument Pattern) { 7560b57cec5SDimitry Andric SmallVector<UnexpandedParameterPack, 2> Unexpanded; 7570b57cec5SDimitry Andric S.collectUnexpandedParameterPacks(Pattern, Unexpanded); 7580b57cec5SDimitry Andric for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { 759*bdd1243dSDimitry Andric UnexpandedParameterPack U = Unexpanded[I]; 760*bdd1243dSDimitry Andric if (U.first.is<const SubstTemplateTypeParmPackType *>() || 761*bdd1243dSDimitry Andric U.first.is<const SubstNonTypeTemplateParmPackExpr *>()) 762*bdd1243dSDimitry Andric continue; 763*bdd1243dSDimitry Andric auto [Depth, Index] = getDepthAndIndex(U); 7640b57cec5SDimitry Andric if (Depth == Info.getDeducedDepth()) 7650b57cec5SDimitry Andric AddPack(Index); 7660b57cec5SDimitry Andric } 76755e4f9d5SDimitry Andric }; 76855e4f9d5SDimitry Andric 76955e4f9d5SDimitry Andric // Look for unexpanded packs in the pattern. 77055e4f9d5SDimitry Andric Collect(Pattern); 7710b57cec5SDimitry Andric assert(!Packs.empty() && "Pack expansion without unexpanded packs?"); 7720b57cec5SDimitry Andric 7730b57cec5SDimitry Andric unsigned NumNamedPacks = Packs.size(); 7740b57cec5SDimitry Andric 77555e4f9d5SDimitry Andric // Also look for unexpanded packs that are indirectly deduced by deducing 77655e4f9d5SDimitry Andric // the sizes of the packs in this pattern. 77755e4f9d5SDimitry Andric while (!ExtraDeductions.empty()) 77855e4f9d5SDimitry Andric Collect(ExtraDeductions.pop_back_val()); 7790b57cec5SDimitry Andric 7800b57cec5SDimitry Andric return NumNamedPacks; 7810b57cec5SDimitry Andric } 7820b57cec5SDimitry Andric 7830b57cec5SDimitry Andric void finishConstruction(unsigned NumNamedPacks) { 7840b57cec5SDimitry Andric // Dig out the partially-substituted pack, if there is one. 7850b57cec5SDimitry Andric const TemplateArgument *PartialPackArgs = nullptr; 7860b57cec5SDimitry Andric unsigned NumPartialPackArgs = 0; 7870b57cec5SDimitry Andric std::pair<unsigned, unsigned> PartialPackDepthIndex(-1u, -1u); 7880b57cec5SDimitry Andric if (auto *Scope = S.CurrentInstantiationScope) 7890b57cec5SDimitry Andric if (auto *Partial = Scope->getPartiallySubstitutedPack( 7900b57cec5SDimitry Andric &PartialPackArgs, &NumPartialPackArgs)) 7910b57cec5SDimitry Andric PartialPackDepthIndex = getDepthAndIndex(Partial); 7920b57cec5SDimitry Andric 7930b57cec5SDimitry Andric // This pack expansion will have been partially or fully expanded if 7940b57cec5SDimitry Andric // it only names explicitly-specified parameter packs (including the 7950b57cec5SDimitry Andric // partially-substituted one, if any). 7960b57cec5SDimitry Andric bool IsExpanded = true; 7970b57cec5SDimitry Andric for (unsigned I = 0; I != NumNamedPacks; ++I) { 7980b57cec5SDimitry Andric if (Packs[I].Index >= Info.getNumExplicitArgs()) { 7990b57cec5SDimitry Andric IsExpanded = false; 8000b57cec5SDimitry Andric IsPartiallyExpanded = false; 8010b57cec5SDimitry Andric break; 8020b57cec5SDimitry Andric } 8030b57cec5SDimitry Andric if (PartialPackDepthIndex == 8040b57cec5SDimitry Andric std::make_pair(Info.getDeducedDepth(), Packs[I].Index)) { 8050b57cec5SDimitry Andric IsPartiallyExpanded = true; 8060b57cec5SDimitry Andric } 8070b57cec5SDimitry Andric } 8080b57cec5SDimitry Andric 8090b57cec5SDimitry Andric // Skip over the pack elements that were expanded into separate arguments. 8100b57cec5SDimitry Andric // If we partially expanded, this is the number of partial arguments. 8110b57cec5SDimitry Andric if (IsPartiallyExpanded) 8120b57cec5SDimitry Andric PackElements += NumPartialPackArgs; 8130b57cec5SDimitry Andric else if (IsExpanded) 8140b57cec5SDimitry Andric PackElements += *FixedNumExpansions; 8150b57cec5SDimitry Andric 8160b57cec5SDimitry Andric for (auto &Pack : Packs) { 8170b57cec5SDimitry Andric if (Info.PendingDeducedPacks.size() > Pack.Index) 8180b57cec5SDimitry Andric Pack.Outer = Info.PendingDeducedPacks[Pack.Index]; 8190b57cec5SDimitry Andric else 8200b57cec5SDimitry Andric Info.PendingDeducedPacks.resize(Pack.Index + 1); 8210b57cec5SDimitry Andric Info.PendingDeducedPacks[Pack.Index] = &Pack; 8220b57cec5SDimitry Andric 8230b57cec5SDimitry Andric if (PartialPackDepthIndex == 8240b57cec5SDimitry Andric std::make_pair(Info.getDeducedDepth(), Pack.Index)) { 8250b57cec5SDimitry Andric Pack.New.append(PartialPackArgs, PartialPackArgs + NumPartialPackArgs); 8260b57cec5SDimitry Andric // We pre-populate the deduced value of the partially-substituted 8270b57cec5SDimitry Andric // pack with the specified value. This is not entirely correct: the 8280b57cec5SDimitry Andric // value is supposed to have been substituted, not deduced, but the 8290b57cec5SDimitry Andric // cases where this is observable require an exact type match anyway. 8300b57cec5SDimitry Andric // 8310b57cec5SDimitry Andric // FIXME: If we could represent a "depth i, index j, pack elem k" 8320b57cec5SDimitry Andric // parameter, we could substitute the partially-substituted pack 8330b57cec5SDimitry Andric // everywhere and avoid this. 8340b57cec5SDimitry Andric if (!IsPartiallyExpanded) 8350b57cec5SDimitry Andric Deduced[Pack.Index] = Pack.New[PackElements]; 8360b57cec5SDimitry Andric } 8370b57cec5SDimitry Andric } 8380b57cec5SDimitry Andric } 8390b57cec5SDimitry Andric 8400b57cec5SDimitry Andric public: 8410b57cec5SDimitry Andric ~PackDeductionScope() { 8420b57cec5SDimitry Andric for (auto &Pack : Packs) 8430b57cec5SDimitry Andric Info.PendingDeducedPacks[Pack.Index] = Pack.Outer; 8440b57cec5SDimitry Andric } 8450b57cec5SDimitry Andric 8460b57cec5SDimitry Andric /// Determine whether this pack has already been partially expanded into a 8470b57cec5SDimitry Andric /// sequence of (prior) function parameters / template arguments. 8480b57cec5SDimitry Andric bool isPartiallyExpanded() { return IsPartiallyExpanded; } 8490b57cec5SDimitry Andric 8500b57cec5SDimitry Andric /// Determine whether this pack expansion scope has a known, fixed arity. 8510b57cec5SDimitry Andric /// This happens if it involves a pack from an outer template that has 8520b57cec5SDimitry Andric /// (notionally) already been expanded. 85381ad6265SDimitry Andric bool hasFixedArity() { return FixedNumExpansions.has_value(); } 8540b57cec5SDimitry Andric 8550b57cec5SDimitry Andric /// Determine whether the next element of the argument is still part of this 8560b57cec5SDimitry Andric /// pack. This is the case unless the pack is already expanded to a fixed 8570b57cec5SDimitry Andric /// length. 8580b57cec5SDimitry Andric bool hasNextElement() { 8590b57cec5SDimitry Andric return !FixedNumExpansions || *FixedNumExpansions > PackElements; 8600b57cec5SDimitry Andric } 8610b57cec5SDimitry Andric 8620b57cec5SDimitry Andric /// Move to deducing the next element in each pack that is being deduced. 8630b57cec5SDimitry Andric void nextPackElement() { 8640b57cec5SDimitry Andric // Capture the deduced template arguments for each parameter pack expanded 8650b57cec5SDimitry Andric // by this pack expansion, add them to the list of arguments we've deduced 8660b57cec5SDimitry Andric // for that pack, then clear out the deduced argument. 8670b57cec5SDimitry Andric for (auto &Pack : Packs) { 8680b57cec5SDimitry Andric DeducedTemplateArgument &DeducedArg = Deduced[Pack.Index]; 8690b57cec5SDimitry Andric if (!Pack.New.empty() || !DeducedArg.isNull()) { 8700b57cec5SDimitry Andric while (Pack.New.size() < PackElements) 8710b57cec5SDimitry Andric Pack.New.push_back(DeducedTemplateArgument()); 8720b57cec5SDimitry Andric if (Pack.New.size() == PackElements) 8730b57cec5SDimitry Andric Pack.New.push_back(DeducedArg); 8740b57cec5SDimitry Andric else 8750b57cec5SDimitry Andric Pack.New[PackElements] = DeducedArg; 8760b57cec5SDimitry Andric DeducedArg = Pack.New.size() > PackElements + 1 8770b57cec5SDimitry Andric ? Pack.New[PackElements + 1] 8780b57cec5SDimitry Andric : DeducedTemplateArgument(); 8790b57cec5SDimitry Andric } 8800b57cec5SDimitry Andric } 8810b57cec5SDimitry Andric ++PackElements; 8820b57cec5SDimitry Andric } 8830b57cec5SDimitry Andric 8840b57cec5SDimitry Andric /// Finish template argument deduction for a set of argument packs, 8850b57cec5SDimitry Andric /// producing the argument packs and checking for consistency with prior 8860b57cec5SDimitry Andric /// deductions. 887480093f4SDimitry Andric Sema::TemplateDeductionResult finish() { 8880b57cec5SDimitry Andric // Build argument packs for each of the parameter packs expanded by this 8890b57cec5SDimitry Andric // pack expansion. 8900b57cec5SDimitry Andric for (auto &Pack : Packs) { 8910b57cec5SDimitry Andric // Put back the old value for this pack. 8920b57cec5SDimitry Andric Deduced[Pack.Index] = Pack.Saved; 8930b57cec5SDimitry Andric 894480093f4SDimitry Andric // Always make sure the size of this pack is correct, even if we didn't 895480093f4SDimitry Andric // deduce any values for it. 896480093f4SDimitry Andric // 897480093f4SDimitry Andric // FIXME: This isn't required by the normative wording, but substitution 898480093f4SDimitry Andric // and post-substitution checking will always fail if the arity of any 899480093f4SDimitry Andric // pack is not equal to the number of elements we processed. (Either that 900480093f4SDimitry Andric // or something else has gone *very* wrong.) We're permitted to skip any 901480093f4SDimitry Andric // hard errors from those follow-on steps by the intent (but not the 902480093f4SDimitry Andric // wording) of C++ [temp.inst]p8: 903480093f4SDimitry Andric // 904480093f4SDimitry Andric // If the function selected by overload resolution can be determined 905480093f4SDimitry Andric // without instantiating a class template definition, it is unspecified 906480093f4SDimitry Andric // whether that instantiation actually takes place 9070b57cec5SDimitry Andric Pack.New.resize(PackElements); 9080b57cec5SDimitry Andric 9090b57cec5SDimitry Andric // Build or find a new value for this pack. 9100b57cec5SDimitry Andric DeducedTemplateArgument NewPack; 911480093f4SDimitry Andric if (Pack.New.empty()) { 9120b57cec5SDimitry Andric // If we deduced an empty argument pack, create it now. 9130b57cec5SDimitry Andric NewPack = DeducedTemplateArgument(TemplateArgument::getEmptyPack()); 9140b57cec5SDimitry Andric } else { 9150b57cec5SDimitry Andric TemplateArgument *ArgumentPack = 9160b57cec5SDimitry Andric new (S.Context) TemplateArgument[Pack.New.size()]; 9170b57cec5SDimitry Andric std::copy(Pack.New.begin(), Pack.New.end(), ArgumentPack); 9180b57cec5SDimitry Andric NewPack = DeducedTemplateArgument( 919*bdd1243dSDimitry Andric TemplateArgument(llvm::ArrayRef(ArgumentPack, Pack.New.size())), 9200b57cec5SDimitry Andric // FIXME: This is wrong, it's possible that some pack elements are 9210b57cec5SDimitry Andric // deduced from an array bound and others are not: 9220b57cec5SDimitry Andric // template<typename ...T, T ...V> void g(const T (&...p)[V]); 9230b57cec5SDimitry Andric // g({1, 2, 3}, {{}, {}}); 9240b57cec5SDimitry Andric // ... should deduce T = {int, size_t (from array bound)}. 9250b57cec5SDimitry Andric Pack.New[0].wasDeducedFromArrayBound()); 9260b57cec5SDimitry Andric } 9270b57cec5SDimitry Andric 9280b57cec5SDimitry Andric // Pick where we're going to put the merged pack. 9290b57cec5SDimitry Andric DeducedTemplateArgument *Loc; 9300b57cec5SDimitry Andric if (Pack.Outer) { 9310b57cec5SDimitry Andric if (Pack.Outer->DeferredDeduction.isNull()) { 9320b57cec5SDimitry Andric // Defer checking this pack until we have a complete pack to compare 9330b57cec5SDimitry Andric // it against. 9340b57cec5SDimitry Andric Pack.Outer->DeferredDeduction = NewPack; 9350b57cec5SDimitry Andric continue; 9360b57cec5SDimitry Andric } 9370b57cec5SDimitry Andric Loc = &Pack.Outer->DeferredDeduction; 9380b57cec5SDimitry Andric } else { 9390b57cec5SDimitry Andric Loc = &Deduced[Pack.Index]; 9400b57cec5SDimitry Andric } 9410b57cec5SDimitry Andric 9420b57cec5SDimitry Andric // Check the new pack matches any previous value. 9430b57cec5SDimitry Andric DeducedTemplateArgument OldPack = *Loc; 9440b57cec5SDimitry Andric DeducedTemplateArgument Result = 9450b57cec5SDimitry Andric checkDeducedTemplateArguments(S.Context, OldPack, NewPack); 9460b57cec5SDimitry Andric 9470b57cec5SDimitry Andric // If we deferred a deduction of this pack, check that one now too. 9480b57cec5SDimitry Andric if (!Result.isNull() && !Pack.DeferredDeduction.isNull()) { 9490b57cec5SDimitry Andric OldPack = Result; 9500b57cec5SDimitry Andric NewPack = Pack.DeferredDeduction; 9510b57cec5SDimitry Andric Result = checkDeducedTemplateArguments(S.Context, OldPack, NewPack); 9520b57cec5SDimitry Andric } 9530b57cec5SDimitry Andric 9540b57cec5SDimitry Andric NamedDecl *Param = TemplateParams->getParam(Pack.Index); 9550b57cec5SDimitry Andric if (Result.isNull()) { 9560b57cec5SDimitry Andric Info.Param = makeTemplateParameter(Param); 9570b57cec5SDimitry Andric Info.FirstArg = OldPack; 9580b57cec5SDimitry Andric Info.SecondArg = NewPack; 9590b57cec5SDimitry Andric return Sema::TDK_Inconsistent; 9600b57cec5SDimitry Andric } 9610b57cec5SDimitry Andric 9620b57cec5SDimitry Andric // If we have a pre-expanded pack and we didn't deduce enough elements 9630b57cec5SDimitry Andric // for it, fail deduction. 964*bdd1243dSDimitry Andric if (std::optional<unsigned> Expansions = getExpandedPackSize(Param)) { 9650b57cec5SDimitry Andric if (*Expansions != PackElements) { 9660b57cec5SDimitry Andric Info.Param = makeTemplateParameter(Param); 9670b57cec5SDimitry Andric Info.FirstArg = Result; 9680b57cec5SDimitry Andric return Sema::TDK_IncompletePack; 9690b57cec5SDimitry Andric } 9700b57cec5SDimitry Andric } 9710b57cec5SDimitry Andric 9720b57cec5SDimitry Andric *Loc = Result; 9730b57cec5SDimitry Andric } 9740b57cec5SDimitry Andric 9750b57cec5SDimitry Andric return Sema::TDK_Success; 9760b57cec5SDimitry Andric } 9770b57cec5SDimitry Andric 9780b57cec5SDimitry Andric private: 9790b57cec5SDimitry Andric Sema &S; 9800b57cec5SDimitry Andric TemplateParameterList *TemplateParams; 9810b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced; 9820b57cec5SDimitry Andric TemplateDeductionInfo &Info; 9830b57cec5SDimitry Andric unsigned PackElements = 0; 9840b57cec5SDimitry Andric bool IsPartiallyExpanded = false; 9850b57cec5SDimitry Andric /// The number of expansions, if we have a fully-expanded pack in this scope. 986*bdd1243dSDimitry Andric std::optional<unsigned> FixedNumExpansions; 9870b57cec5SDimitry Andric 9880b57cec5SDimitry Andric SmallVector<DeducedPack, 2> Packs; 9890b57cec5SDimitry Andric }; 9900b57cec5SDimitry Andric 9910b57cec5SDimitry Andric } // namespace 9920b57cec5SDimitry Andric 9930b57cec5SDimitry Andric /// Deduce the template arguments by comparing the list of parameter 9940b57cec5SDimitry Andric /// types to the list of argument types, as in the parameter-type-lists of 9950b57cec5SDimitry Andric /// function types (C++ [temp.deduct.type]p10). 9960b57cec5SDimitry Andric /// 9970b57cec5SDimitry Andric /// \param S The semantic analysis object within which we are deducing 9980b57cec5SDimitry Andric /// 9990b57cec5SDimitry Andric /// \param TemplateParams The template parameters that we are deducing 10000b57cec5SDimitry Andric /// 10010b57cec5SDimitry Andric /// \param Params The list of parameter types 10020b57cec5SDimitry Andric /// 10030b57cec5SDimitry Andric /// \param NumParams The number of types in \c Params 10040b57cec5SDimitry Andric /// 10050b57cec5SDimitry Andric /// \param Args The list of argument types 10060b57cec5SDimitry Andric /// 10070b57cec5SDimitry Andric /// \param NumArgs The number of types in \c Args 10080b57cec5SDimitry Andric /// 10090b57cec5SDimitry Andric /// \param Info information about the template argument deduction itself 10100b57cec5SDimitry Andric /// 10110b57cec5SDimitry Andric /// \param Deduced the deduced template arguments 10120b57cec5SDimitry Andric /// 10130b57cec5SDimitry Andric /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe 10140b57cec5SDimitry Andric /// how template argument deduction is performed. 10150b57cec5SDimitry Andric /// 10160b57cec5SDimitry Andric /// \param PartialOrdering If true, we are performing template argument 10170b57cec5SDimitry Andric /// deduction for during partial ordering for a call 10180b57cec5SDimitry Andric /// (C++0x [temp.deduct.partial]). 10190b57cec5SDimitry Andric /// 10200b57cec5SDimitry Andric /// \returns the result of template argument deduction so far. Note that a 10210b57cec5SDimitry Andric /// "success" result means that template argument deduction has not yet failed, 10220b57cec5SDimitry Andric /// but it may still fail, later, for other reasons. 10230b57cec5SDimitry Andric static Sema::TemplateDeductionResult 10240b57cec5SDimitry Andric DeduceTemplateArguments(Sema &S, 10250b57cec5SDimitry Andric TemplateParameterList *TemplateParams, 10260b57cec5SDimitry Andric const QualType *Params, unsigned NumParams, 10270b57cec5SDimitry Andric const QualType *Args, unsigned NumArgs, 10280b57cec5SDimitry Andric TemplateDeductionInfo &Info, 10290b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 10300b57cec5SDimitry Andric unsigned TDF, 10310b57cec5SDimitry Andric bool PartialOrdering = false) { 10320b57cec5SDimitry Andric // C++0x [temp.deduct.type]p10: 10330b57cec5SDimitry Andric // Similarly, if P has a form that contains (T), then each parameter type 10340b57cec5SDimitry Andric // Pi of the respective parameter-type- list of P is compared with the 10350b57cec5SDimitry Andric // corresponding parameter type Ai of the corresponding parameter-type-list 10360b57cec5SDimitry Andric // of A. [...] 10370b57cec5SDimitry Andric unsigned ArgIdx = 0, ParamIdx = 0; 10380b57cec5SDimitry Andric for (; ParamIdx != NumParams; ++ParamIdx) { 10390b57cec5SDimitry Andric // Check argument types. 10400b57cec5SDimitry Andric const PackExpansionType *Expansion 10410b57cec5SDimitry Andric = dyn_cast<PackExpansionType>(Params[ParamIdx]); 10420b57cec5SDimitry Andric if (!Expansion) { 10430b57cec5SDimitry Andric // Simple case: compare the parameter and argument types at this point. 10440b57cec5SDimitry Andric 10450b57cec5SDimitry Andric // Make sure we have an argument. 10460b57cec5SDimitry Andric if (ArgIdx >= NumArgs) 10470b57cec5SDimitry Andric return Sema::TDK_MiscellaneousDeductionFailure; 10480b57cec5SDimitry Andric 10490b57cec5SDimitry Andric if (isa<PackExpansionType>(Args[ArgIdx])) { 10500b57cec5SDimitry Andric // C++0x [temp.deduct.type]p22: 10510b57cec5SDimitry Andric // If the original function parameter associated with A is a function 10520b57cec5SDimitry Andric // parameter pack and the function parameter associated with P is not 10530b57cec5SDimitry Andric // a function parameter pack, then template argument deduction fails. 10540b57cec5SDimitry Andric return Sema::TDK_MiscellaneousDeductionFailure; 10550b57cec5SDimitry Andric } 10560b57cec5SDimitry Andric 1057349cc55cSDimitry Andric if (Sema::TemplateDeductionResult Result = 1058349cc55cSDimitry Andric DeduceTemplateArgumentsByTypeMatch( 1059349cc55cSDimitry Andric S, TemplateParams, Params[ParamIdx].getUnqualifiedType(), 1060349cc55cSDimitry Andric Args[ArgIdx].getUnqualifiedType(), Info, Deduced, TDF, 1061349cc55cSDimitry Andric PartialOrdering, 1062349cc55cSDimitry Andric /*DeducedFromArrayBound=*/false)) 10630b57cec5SDimitry Andric return Result; 10640b57cec5SDimitry Andric 10650b57cec5SDimitry Andric ++ArgIdx; 10660b57cec5SDimitry Andric continue; 10670b57cec5SDimitry Andric } 10680b57cec5SDimitry Andric 10690b57cec5SDimitry Andric // C++0x [temp.deduct.type]p10: 10700b57cec5SDimitry Andric // If the parameter-declaration corresponding to Pi is a function 10710b57cec5SDimitry Andric // parameter pack, then the type of its declarator- id is compared with 10720b57cec5SDimitry Andric // each remaining parameter type in the parameter-type-list of A. Each 10730b57cec5SDimitry Andric // comparison deduces template arguments for subsequent positions in the 10740b57cec5SDimitry Andric // template parameter packs expanded by the function parameter pack. 10750b57cec5SDimitry Andric 10760b57cec5SDimitry Andric QualType Pattern = Expansion->getPattern(); 10770b57cec5SDimitry Andric PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); 10780b57cec5SDimitry Andric 10790b57cec5SDimitry Andric // A pack scope with fixed arity is not really a pack any more, so is not 10800b57cec5SDimitry Andric // a non-deduced context. 10810b57cec5SDimitry Andric if (ParamIdx + 1 == NumParams || PackScope.hasFixedArity()) { 10820b57cec5SDimitry Andric for (; ArgIdx < NumArgs && PackScope.hasNextElement(); ++ArgIdx) { 10830b57cec5SDimitry Andric // Deduce template arguments from the pattern. 1084349cc55cSDimitry Andric if (Sema::TemplateDeductionResult Result = 1085349cc55cSDimitry Andric DeduceTemplateArgumentsByTypeMatch( 1086349cc55cSDimitry Andric S, TemplateParams, Pattern.getUnqualifiedType(), 1087349cc55cSDimitry Andric Args[ArgIdx].getUnqualifiedType(), Info, Deduced, TDF, 1088349cc55cSDimitry Andric PartialOrdering, /*DeducedFromArrayBound=*/false)) 10890b57cec5SDimitry Andric return Result; 10900b57cec5SDimitry Andric 10910b57cec5SDimitry Andric PackScope.nextPackElement(); 10920b57cec5SDimitry Andric } 10930b57cec5SDimitry Andric } else { 10940b57cec5SDimitry Andric // C++0x [temp.deduct.type]p5: 10950b57cec5SDimitry Andric // The non-deduced contexts are: 10960b57cec5SDimitry Andric // - A function parameter pack that does not occur at the end of the 10970b57cec5SDimitry Andric // parameter-declaration-clause. 10980b57cec5SDimitry Andric // 10990b57cec5SDimitry Andric // FIXME: There is no wording to say what we should do in this case. We 11000b57cec5SDimitry Andric // choose to resolve this by applying the same rule that is applied for a 11010b57cec5SDimitry Andric // function call: that is, deduce all contained packs to their 11020b57cec5SDimitry Andric // explicitly-specified values (or to <> if there is no such value). 11030b57cec5SDimitry Andric // 11040b57cec5SDimitry Andric // This is seemingly-arbitrarily different from the case of a template-id 11050b57cec5SDimitry Andric // with a non-trailing pack-expansion in its arguments, which renders the 11060b57cec5SDimitry Andric // entire template-argument-list a non-deduced context. 11070b57cec5SDimitry Andric 11080b57cec5SDimitry Andric // If the parameter type contains an explicitly-specified pack that we 11090b57cec5SDimitry Andric // could not expand, skip the number of parameters notionally created 11100b57cec5SDimitry Andric // by the expansion. 1111*bdd1243dSDimitry Andric std::optional<unsigned> NumExpansions = Expansion->getNumExpansions(); 11120b57cec5SDimitry Andric if (NumExpansions && !PackScope.isPartiallyExpanded()) { 11130b57cec5SDimitry Andric for (unsigned I = 0; I != *NumExpansions && ArgIdx < NumArgs; 11140b57cec5SDimitry Andric ++I, ++ArgIdx) 11150b57cec5SDimitry Andric PackScope.nextPackElement(); 11160b57cec5SDimitry Andric } 11170b57cec5SDimitry Andric } 11180b57cec5SDimitry Andric 11190b57cec5SDimitry Andric // Build argument packs for each of the parameter packs expanded by this 11200b57cec5SDimitry Andric // pack expansion. 11210b57cec5SDimitry Andric if (auto Result = PackScope.finish()) 11220b57cec5SDimitry Andric return Result; 11230b57cec5SDimitry Andric } 11240b57cec5SDimitry Andric 1125*bdd1243dSDimitry Andric // DR692, DR1395 1126*bdd1243dSDimitry Andric // C++0x [temp.deduct.type]p10: 1127*bdd1243dSDimitry Andric // If the parameter-declaration corresponding to P_i ... 1128*bdd1243dSDimitry Andric // During partial ordering, if Ai was originally a function parameter pack: 1129*bdd1243dSDimitry Andric // - if P does not contain a function parameter type corresponding to Ai then 1130*bdd1243dSDimitry Andric // Ai is ignored; 1131*bdd1243dSDimitry Andric if (PartialOrdering && ArgIdx + 1 == NumArgs && 1132*bdd1243dSDimitry Andric isa<PackExpansionType>(Args[ArgIdx])) 1133*bdd1243dSDimitry Andric return Sema::TDK_Success; 1134*bdd1243dSDimitry Andric 11350b57cec5SDimitry Andric // Make sure we don't have any extra arguments. 11360b57cec5SDimitry Andric if (ArgIdx < NumArgs) 11370b57cec5SDimitry Andric return Sema::TDK_MiscellaneousDeductionFailure; 11380b57cec5SDimitry Andric 11390b57cec5SDimitry Andric return Sema::TDK_Success; 11400b57cec5SDimitry Andric } 11410b57cec5SDimitry Andric 11420b57cec5SDimitry Andric /// Determine whether the parameter has qualifiers that the argument 11430b57cec5SDimitry Andric /// lacks. Put another way, determine whether there is no way to add 11440b57cec5SDimitry Andric /// a deduced set of qualifiers to the ParamType that would result in 11450b57cec5SDimitry Andric /// its qualifiers matching those of the ArgType. 11460b57cec5SDimitry Andric static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType, 11470b57cec5SDimitry Andric QualType ArgType) { 11480b57cec5SDimitry Andric Qualifiers ParamQs = ParamType.getQualifiers(); 11490b57cec5SDimitry Andric Qualifiers ArgQs = ArgType.getQualifiers(); 11500b57cec5SDimitry Andric 11510b57cec5SDimitry Andric if (ParamQs == ArgQs) 11520b57cec5SDimitry Andric return false; 11530b57cec5SDimitry Andric 11540b57cec5SDimitry Andric // Mismatched (but not missing) Objective-C GC attributes. 11550b57cec5SDimitry Andric if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() && 11560b57cec5SDimitry Andric ParamQs.hasObjCGCAttr()) 11570b57cec5SDimitry Andric return true; 11580b57cec5SDimitry Andric 11590b57cec5SDimitry Andric // Mismatched (but not missing) address spaces. 11600b57cec5SDimitry Andric if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() && 11610b57cec5SDimitry Andric ParamQs.hasAddressSpace()) 11620b57cec5SDimitry Andric return true; 11630b57cec5SDimitry Andric 11640b57cec5SDimitry Andric // Mismatched (but not missing) Objective-C lifetime qualifiers. 11650b57cec5SDimitry Andric if (ParamQs.getObjCLifetime() != ArgQs.getObjCLifetime() && 11660b57cec5SDimitry Andric ParamQs.hasObjCLifetime()) 11670b57cec5SDimitry Andric return true; 11680b57cec5SDimitry Andric 11690b57cec5SDimitry Andric // CVR qualifiers inconsistent or a superset. 11700b57cec5SDimitry Andric return (ParamQs.getCVRQualifiers() & ~ArgQs.getCVRQualifiers()) != 0; 11710b57cec5SDimitry Andric } 11720b57cec5SDimitry Andric 11730b57cec5SDimitry Andric /// Compare types for equality with respect to possibly compatible 11740b57cec5SDimitry Andric /// function types (noreturn adjustment, implicit calling conventions). If any 11750b57cec5SDimitry Andric /// of parameter and argument is not a function, just perform type comparison. 11760b57cec5SDimitry Andric /// 1177349cc55cSDimitry Andric /// \param P the template parameter type. 11780b57cec5SDimitry Andric /// 1179349cc55cSDimitry Andric /// \param A the argument type. 1180349cc55cSDimitry Andric bool Sema::isSameOrCompatibleFunctionType(QualType P, QualType A) { 1181349cc55cSDimitry Andric const FunctionType *PF = P->getAs<FunctionType>(), 1182349cc55cSDimitry Andric *AF = A->getAs<FunctionType>(); 11830b57cec5SDimitry Andric 11840b57cec5SDimitry Andric // Just compare if not functions. 1185349cc55cSDimitry Andric if (!PF || !AF) 1186349cc55cSDimitry Andric return Context.hasSameType(P, A); 11870b57cec5SDimitry Andric 11880b57cec5SDimitry Andric // Noreturn and noexcept adjustment. 11890b57cec5SDimitry Andric QualType AdjustedParam; 1190349cc55cSDimitry Andric if (IsFunctionConversion(P, A, AdjustedParam)) 1191349cc55cSDimitry Andric return Context.hasSameType(AdjustedParam, A); 11920b57cec5SDimitry Andric 11930b57cec5SDimitry Andric // FIXME: Compatible calling conventions. 11940b57cec5SDimitry Andric 1195349cc55cSDimitry Andric return Context.hasSameType(P, A); 11960b57cec5SDimitry Andric } 11970b57cec5SDimitry Andric 11980b57cec5SDimitry Andric /// Get the index of the first template parameter that was originally from the 11990b57cec5SDimitry Andric /// innermost template-parameter-list. This is 0 except when we concatenate 12000b57cec5SDimitry Andric /// the template parameter lists of a class template and a constructor template 12010b57cec5SDimitry Andric /// when forming an implicit deduction guide. 12020b57cec5SDimitry Andric static unsigned getFirstInnerIndex(FunctionTemplateDecl *FTD) { 12030b57cec5SDimitry Andric auto *Guide = dyn_cast<CXXDeductionGuideDecl>(FTD->getTemplatedDecl()); 12040b57cec5SDimitry Andric if (!Guide || !Guide->isImplicit()) 12050b57cec5SDimitry Andric return 0; 12060b57cec5SDimitry Andric return Guide->getDeducedTemplate()->getTemplateParameters()->size(); 12070b57cec5SDimitry Andric } 12080b57cec5SDimitry Andric 12090b57cec5SDimitry Andric /// Determine whether a type denotes a forwarding reference. 12100b57cec5SDimitry Andric static bool isForwardingReference(QualType Param, unsigned FirstInnerIndex) { 12110b57cec5SDimitry Andric // C++1z [temp.deduct.call]p3: 12120b57cec5SDimitry Andric // A forwarding reference is an rvalue reference to a cv-unqualified 12130b57cec5SDimitry Andric // template parameter that does not represent a template parameter of a 12140b57cec5SDimitry Andric // class template. 12150b57cec5SDimitry Andric if (auto *ParamRef = Param->getAs<RValueReferenceType>()) { 12160b57cec5SDimitry Andric if (ParamRef->getPointeeType().getQualifiers()) 12170b57cec5SDimitry Andric return false; 12180b57cec5SDimitry Andric auto *TypeParm = ParamRef->getPointeeType()->getAs<TemplateTypeParmType>(); 12190b57cec5SDimitry Andric return TypeParm && TypeParm->getIndex() >= FirstInnerIndex; 12200b57cec5SDimitry Andric } 12210b57cec5SDimitry Andric return false; 12220b57cec5SDimitry Andric } 12230b57cec5SDimitry Andric 1224349cc55cSDimitry Andric static CXXRecordDecl *getCanonicalRD(QualType T) { 1225349cc55cSDimitry Andric return cast<CXXRecordDecl>( 1226349cc55cSDimitry Andric T->castAs<RecordType>()->getDecl()->getCanonicalDecl()); 1227349cc55cSDimitry Andric } 1228349cc55cSDimitry Andric 1229e8d8bef9SDimitry Andric /// Attempt to deduce the template arguments by checking the base types 1230e8d8bef9SDimitry Andric /// according to (C++20 [temp.deduct.call] p4b3. 1231e8d8bef9SDimitry Andric /// 1232e8d8bef9SDimitry Andric /// \param S the semantic analysis object within which we are deducing. 1233e8d8bef9SDimitry Andric /// 123481ad6265SDimitry Andric /// \param RD the top level record object we are deducing against. 1235e8d8bef9SDimitry Andric /// 1236e8d8bef9SDimitry Andric /// \param TemplateParams the template parameters that we are deducing. 1237e8d8bef9SDimitry Andric /// 123881ad6265SDimitry Andric /// \param P the template specialization parameter type. 1239e8d8bef9SDimitry Andric /// 1240e8d8bef9SDimitry Andric /// \param Info information about the template argument deduction itself. 1241e8d8bef9SDimitry Andric /// 1242e8d8bef9SDimitry Andric /// \param Deduced the deduced template arguments. 1243e8d8bef9SDimitry Andric /// 1244e8d8bef9SDimitry Andric /// \returns the result of template argument deduction with the bases. "invalid" 1245e8d8bef9SDimitry Andric /// means no matches, "success" found a single item, and the 1246e8d8bef9SDimitry Andric /// "MiscellaneousDeductionFailure" result happens when the match is ambiguous. 1247349cc55cSDimitry Andric static Sema::TemplateDeductionResult 1248349cc55cSDimitry Andric DeduceTemplateBases(Sema &S, const CXXRecordDecl *RD, 1249349cc55cSDimitry Andric TemplateParameterList *TemplateParams, QualType P, 1250349cc55cSDimitry Andric TemplateDeductionInfo &Info, 1251e8d8bef9SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 1252e8d8bef9SDimitry Andric // C++14 [temp.deduct.call] p4b3: 1253e8d8bef9SDimitry Andric // If P is a class and P has the form simple-template-id, then the 1254e8d8bef9SDimitry Andric // transformed A can be a derived class of the deduced A. Likewise if 1255e8d8bef9SDimitry Andric // P is a pointer to a class of the form simple-template-id, the 1256e8d8bef9SDimitry Andric // transformed A can be a pointer to a derived class pointed to by the 1257e8d8bef9SDimitry Andric // deduced A. However, if there is a class C that is a (direct or 1258e8d8bef9SDimitry Andric // indirect) base class of D and derived (directly or indirectly) from a 1259e8d8bef9SDimitry Andric // class B and that would be a valid deduced A, the deduced A cannot be 1260e8d8bef9SDimitry Andric // B or pointer to B, respectively. 1261e8d8bef9SDimitry Andric // 1262e8d8bef9SDimitry Andric // These alternatives are considered only if type deduction would 1263e8d8bef9SDimitry Andric // otherwise fail. If they yield more than one possible deduced A, the 1264e8d8bef9SDimitry Andric // type deduction fails. 1265e8d8bef9SDimitry Andric 1266e8d8bef9SDimitry Andric // Use a breadth-first search through the bases to collect the set of 1267e8d8bef9SDimitry Andric // successful matches. Visited contains the set of nodes we have already 1268e8d8bef9SDimitry Andric // visited, while ToVisit is our stack of records that we still need to 1269e8d8bef9SDimitry Andric // visit. Matches contains a list of matches that have yet to be 1270e8d8bef9SDimitry Andric // disqualified. 1271349cc55cSDimitry Andric llvm::SmallPtrSet<const CXXRecordDecl *, 8> Visited; 1272349cc55cSDimitry Andric SmallVector<QualType, 8> ToVisit; 1273e8d8bef9SDimitry Andric // We iterate over this later, so we have to use MapVector to ensure 1274e8d8bef9SDimitry Andric // determinism. 1275349cc55cSDimitry Andric llvm::MapVector<const CXXRecordDecl *, 1276349cc55cSDimitry Andric SmallVector<DeducedTemplateArgument, 8>> 1277e8d8bef9SDimitry Andric Matches; 1278e8d8bef9SDimitry Andric 1279349cc55cSDimitry Andric auto AddBases = [&Visited, &ToVisit](const CXXRecordDecl *RD) { 1280e8d8bef9SDimitry Andric for (const auto &Base : RD->bases()) { 1281349cc55cSDimitry Andric QualType T = Base.getType(); 1282349cc55cSDimitry Andric assert(T->isRecordType() && "Base class that isn't a record?"); 1283349cc55cSDimitry Andric if (Visited.insert(::getCanonicalRD(T)).second) 1284349cc55cSDimitry Andric ToVisit.push_back(T); 1285e8d8bef9SDimitry Andric } 1286e8d8bef9SDimitry Andric }; 1287e8d8bef9SDimitry Andric 1288e8d8bef9SDimitry Andric // Set up the loop by adding all the bases. 1289349cc55cSDimitry Andric AddBases(RD); 1290e8d8bef9SDimitry Andric 1291e8d8bef9SDimitry Andric // Search each path of bases until we either run into a successful match 1292e8d8bef9SDimitry Andric // (where all bases of it are invalid), or we run out of bases. 1293e8d8bef9SDimitry Andric while (!ToVisit.empty()) { 1294349cc55cSDimitry Andric QualType NextT = ToVisit.pop_back_val(); 1295e8d8bef9SDimitry Andric 1296e8d8bef9SDimitry Andric SmallVector<DeducedTemplateArgument, 8> DeducedCopy(Deduced.begin(), 1297e8d8bef9SDimitry Andric Deduced.end()); 1298e8d8bef9SDimitry Andric TemplateDeductionInfo BaseInfo(TemplateDeductionInfo::ForBase, Info); 1299349cc55cSDimitry Andric Sema::TemplateDeductionResult BaseResult = DeduceTemplateSpecArguments( 1300349cc55cSDimitry Andric S, TemplateParams, P, NextT, BaseInfo, DeducedCopy); 1301e8d8bef9SDimitry Andric 1302e8d8bef9SDimitry Andric // If this was a successful deduction, add it to the list of matches, 1303e8d8bef9SDimitry Andric // otherwise we need to continue searching its bases. 1304349cc55cSDimitry Andric const CXXRecordDecl *RD = ::getCanonicalRD(NextT); 1305e8d8bef9SDimitry Andric if (BaseResult == Sema::TDK_Success) 1306349cc55cSDimitry Andric Matches.insert({RD, DeducedCopy}); 1307e8d8bef9SDimitry Andric else 1308349cc55cSDimitry Andric AddBases(RD); 1309e8d8bef9SDimitry Andric } 1310e8d8bef9SDimitry Andric 1311e8d8bef9SDimitry Andric // At this point, 'Matches' contains a list of seemingly valid bases, however 1312e8d8bef9SDimitry Andric // in the event that we have more than 1 match, it is possible that the base 1313e8d8bef9SDimitry Andric // of one of the matches might be disqualified for being a base of another 1314e8d8bef9SDimitry Andric // valid match. We can count on cyclical instantiations being invalid to 1315e8d8bef9SDimitry Andric // simplify the disqualifications. That is, if A & B are both matches, and B 1316e8d8bef9SDimitry Andric // inherits from A (disqualifying A), we know that A cannot inherit from B. 1317e8d8bef9SDimitry Andric if (Matches.size() > 1) { 1318e8d8bef9SDimitry Andric Visited.clear(); 1319e8d8bef9SDimitry Andric for (const auto &Match : Matches) 1320e8d8bef9SDimitry Andric AddBases(Match.first); 1321e8d8bef9SDimitry Andric 1322e8d8bef9SDimitry Andric // We can give up once we have a single item (or have run out of things to 1323349cc55cSDimitry Andric // search) since cyclical inheritance isn't valid. 1324e8d8bef9SDimitry Andric while (Matches.size() > 1 && !ToVisit.empty()) { 1325349cc55cSDimitry Andric const CXXRecordDecl *RD = ::getCanonicalRD(ToVisit.pop_back_val()); 1326349cc55cSDimitry Andric Matches.erase(RD); 1327e8d8bef9SDimitry Andric 1328349cc55cSDimitry Andric // Always add all bases, since the inheritance tree can contain 1329e8d8bef9SDimitry Andric // disqualifications for multiple matches. 1330349cc55cSDimitry Andric AddBases(RD); 1331e8d8bef9SDimitry Andric } 1332e8d8bef9SDimitry Andric } 1333e8d8bef9SDimitry Andric 1334e8d8bef9SDimitry Andric if (Matches.empty()) 1335e8d8bef9SDimitry Andric return Sema::TDK_Invalid; 1336e8d8bef9SDimitry Andric if (Matches.size() > 1) 1337e8d8bef9SDimitry Andric return Sema::TDK_MiscellaneousDeductionFailure; 1338e8d8bef9SDimitry Andric 1339e8d8bef9SDimitry Andric std::swap(Matches.front().second, Deduced); 1340e8d8bef9SDimitry Andric return Sema::TDK_Success; 1341e8d8bef9SDimitry Andric } 1342e8d8bef9SDimitry Andric 13430b57cec5SDimitry Andric /// Deduce the template arguments by comparing the parameter type and 13440b57cec5SDimitry Andric /// the argument type (C++ [temp.deduct.type]). 13450b57cec5SDimitry Andric /// 13460b57cec5SDimitry Andric /// \param S the semantic analysis object within which we are deducing 13470b57cec5SDimitry Andric /// 13480b57cec5SDimitry Andric /// \param TemplateParams the template parameters that we are deducing 13490b57cec5SDimitry Andric /// 135081ad6265SDimitry Andric /// \param P the parameter type 13510b57cec5SDimitry Andric /// 135281ad6265SDimitry Andric /// \param A the argument type 13530b57cec5SDimitry Andric /// 13540b57cec5SDimitry Andric /// \param Info information about the template argument deduction itself 13550b57cec5SDimitry Andric /// 13560b57cec5SDimitry Andric /// \param Deduced the deduced template arguments 13570b57cec5SDimitry Andric /// 13580b57cec5SDimitry Andric /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe 13590b57cec5SDimitry Andric /// how template argument deduction is performed. 13600b57cec5SDimitry Andric /// 13610b57cec5SDimitry Andric /// \param PartialOrdering Whether we're performing template argument deduction 13620b57cec5SDimitry Andric /// in the context of partial ordering (C++0x [temp.deduct.partial]). 13630b57cec5SDimitry Andric /// 13640b57cec5SDimitry Andric /// \returns the result of template argument deduction so far. Note that a 13650b57cec5SDimitry Andric /// "success" result means that template argument deduction has not yet failed, 13660b57cec5SDimitry Andric /// but it may still fail, later, for other reasons. 1367349cc55cSDimitry Andric static Sema::TemplateDeductionResult DeduceTemplateArgumentsByTypeMatch( 1368349cc55cSDimitry Andric Sema &S, TemplateParameterList *TemplateParams, QualType P, QualType A, 13690b57cec5SDimitry Andric TemplateDeductionInfo &Info, 1370349cc55cSDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, unsigned TDF, 1371349cc55cSDimitry Andric bool PartialOrdering, bool DeducedFromArrayBound) { 13720b57cec5SDimitry Andric 13730b57cec5SDimitry Andric // If the argument type is a pack expansion, look at its pattern. 13740b57cec5SDimitry Andric // This isn't explicitly called out 1375349cc55cSDimitry Andric if (const auto *AExp = dyn_cast<PackExpansionType>(A)) 1376349cc55cSDimitry Andric A = AExp->getPattern(); 1377349cc55cSDimitry Andric assert(!isa<PackExpansionType>(A.getCanonicalType())); 13780b57cec5SDimitry Andric 13790b57cec5SDimitry Andric if (PartialOrdering) { 13800b57cec5SDimitry Andric // C++11 [temp.deduct.partial]p5: 13810b57cec5SDimitry Andric // Before the partial ordering is done, certain transformations are 13820b57cec5SDimitry Andric // performed on the types used for partial ordering: 13830b57cec5SDimitry Andric // - If P is a reference type, P is replaced by the type referred to. 1384349cc55cSDimitry Andric const ReferenceType *PRef = P->getAs<ReferenceType>(); 1385349cc55cSDimitry Andric if (PRef) 1386349cc55cSDimitry Andric P = PRef->getPointeeType(); 13870b57cec5SDimitry Andric 13880b57cec5SDimitry Andric // - If A is a reference type, A is replaced by the type referred to. 1389349cc55cSDimitry Andric const ReferenceType *ARef = A->getAs<ReferenceType>(); 1390349cc55cSDimitry Andric if (ARef) 1391349cc55cSDimitry Andric A = A->getPointeeType(); 13920b57cec5SDimitry Andric 1393349cc55cSDimitry Andric if (PRef && ARef && S.Context.hasSameUnqualifiedType(P, A)) { 13940b57cec5SDimitry Andric // C++11 [temp.deduct.partial]p9: 13950b57cec5SDimitry Andric // If, for a given type, deduction succeeds in both directions (i.e., 13960b57cec5SDimitry Andric // the types are identical after the transformations above) and both 13970b57cec5SDimitry Andric // P and A were reference types [...]: 13980b57cec5SDimitry Andric // - if [one type] was an lvalue reference and [the other type] was 13990b57cec5SDimitry Andric // not, [the other type] is not considered to be at least as 14000b57cec5SDimitry Andric // specialized as [the first type] 14010b57cec5SDimitry Andric // - if [one type] is more cv-qualified than [the other type], 14020b57cec5SDimitry Andric // [the other type] is not considered to be at least as specialized 14030b57cec5SDimitry Andric // as [the first type] 14040b57cec5SDimitry Andric // Objective-C ARC adds: 14050b57cec5SDimitry Andric // - [one type] has non-trivial lifetime, [the other type] has 14060b57cec5SDimitry Andric // __unsafe_unretained lifetime, and the types are otherwise 14070b57cec5SDimitry Andric // identical 14080b57cec5SDimitry Andric // 14090b57cec5SDimitry Andric // A is "considered to be at least as specialized" as P iff deduction 14100b57cec5SDimitry Andric // succeeds, so we model this as a deduction failure. Note that 14110b57cec5SDimitry Andric // [the first type] is P and [the other type] is A here; the standard 14120b57cec5SDimitry Andric // gets this backwards. 1413349cc55cSDimitry Andric Qualifiers PQuals = P.getQualifiers(), AQuals = A.getQualifiers(); 1414349cc55cSDimitry Andric if ((PRef->isLValueReferenceType() && !ARef->isLValueReferenceType()) || 1415349cc55cSDimitry Andric PQuals.isStrictSupersetOf(AQuals) || 1416349cc55cSDimitry Andric (PQuals.hasNonTrivialObjCLifetime() && 1417349cc55cSDimitry Andric AQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone && 1418349cc55cSDimitry Andric PQuals.withoutObjCLifetime() == AQuals.withoutObjCLifetime())) { 1419349cc55cSDimitry Andric Info.FirstArg = TemplateArgument(P); 1420349cc55cSDimitry Andric Info.SecondArg = TemplateArgument(A); 14210b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 14220b57cec5SDimitry Andric } 14230b57cec5SDimitry Andric } 1424349cc55cSDimitry Andric Qualifiers DiscardedQuals; 14250b57cec5SDimitry Andric // C++11 [temp.deduct.partial]p7: 14260b57cec5SDimitry Andric // Remove any top-level cv-qualifiers: 14270b57cec5SDimitry Andric // - If P is a cv-qualified type, P is replaced by the cv-unqualified 14280b57cec5SDimitry Andric // version of P. 1429349cc55cSDimitry Andric P = S.Context.getUnqualifiedArrayType(P, DiscardedQuals); 14300b57cec5SDimitry Andric // - If A is a cv-qualified type, A is replaced by the cv-unqualified 14310b57cec5SDimitry Andric // version of A. 1432349cc55cSDimitry Andric A = S.Context.getUnqualifiedArrayType(A, DiscardedQuals); 14330b57cec5SDimitry Andric } else { 14340b57cec5SDimitry Andric // C++0x [temp.deduct.call]p4 bullet 1: 14350b57cec5SDimitry Andric // - If the original P is a reference type, the deduced A (i.e., the type 14360b57cec5SDimitry Andric // referred to by the reference) can be more cv-qualified than the 14370b57cec5SDimitry Andric // transformed A. 14380b57cec5SDimitry Andric if (TDF & TDF_ParamWithReferenceType) { 14390b57cec5SDimitry Andric Qualifiers Quals; 1440349cc55cSDimitry Andric QualType UnqualP = S.Context.getUnqualifiedArrayType(P, Quals); 1441349cc55cSDimitry Andric Quals.setCVRQualifiers(Quals.getCVRQualifiers() & A.getCVRQualifiers()); 1442349cc55cSDimitry Andric P = S.Context.getQualifiedType(UnqualP, Quals); 14430b57cec5SDimitry Andric } 14440b57cec5SDimitry Andric 1445349cc55cSDimitry Andric if ((TDF & TDF_TopLevelParameterTypeList) && !P->isFunctionType()) { 14460b57cec5SDimitry Andric // C++0x [temp.deduct.type]p10: 14470b57cec5SDimitry Andric // If P and A are function types that originated from deduction when 14480b57cec5SDimitry Andric // taking the address of a function template (14.8.2.2) or when deducing 14490b57cec5SDimitry Andric // template arguments from a function declaration (14.8.2.6) and Pi and 14500b57cec5SDimitry Andric // Ai are parameters of the top-level parameter-type-list of P and A, 14510b57cec5SDimitry Andric // respectively, Pi is adjusted if it is a forwarding reference and Ai 14520b57cec5SDimitry Andric // is an lvalue reference, in 14530b57cec5SDimitry Andric // which case the type of Pi is changed to be the template parameter 14540b57cec5SDimitry Andric // type (i.e., T&& is changed to simply T). [ Note: As a result, when 14550b57cec5SDimitry Andric // Pi is T&& and Ai is X&, the adjusted Pi will be T, causing T to be 14560b57cec5SDimitry Andric // deduced as X&. - end note ] 14570b57cec5SDimitry Andric TDF &= ~TDF_TopLevelParameterTypeList; 1458349cc55cSDimitry Andric if (isForwardingReference(P, /*FirstInnerIndex=*/0) && 1459349cc55cSDimitry Andric A->isLValueReferenceType()) 1460349cc55cSDimitry Andric P = P->getPointeeType(); 14610b57cec5SDimitry Andric } 14620b57cec5SDimitry Andric } 14630b57cec5SDimitry Andric 14640b57cec5SDimitry Andric // C++ [temp.deduct.type]p9: 14650b57cec5SDimitry Andric // A template type argument T, a template template argument TT or a 14660b57cec5SDimitry Andric // template non-type argument i can be deduced if P and A have one of 14670b57cec5SDimitry Andric // the following forms: 14680b57cec5SDimitry Andric // 14690b57cec5SDimitry Andric // T 14700b57cec5SDimitry Andric // cv-list T 1471349cc55cSDimitry Andric if (const auto *TTP = P->getAs<TemplateTypeParmType>()) { 14720b57cec5SDimitry Andric // Just skip any attempts to deduce from a placeholder type or a parameter 14730b57cec5SDimitry Andric // at a different depth. 1474349cc55cSDimitry Andric if (A->isPlaceholderType() || Info.getDeducedDepth() != TTP->getDepth()) 14750b57cec5SDimitry Andric return Sema::TDK_Success; 14760b57cec5SDimitry Andric 1477349cc55cSDimitry Andric unsigned Index = TTP->getIndex(); 14780b57cec5SDimitry Andric 14790b57cec5SDimitry Andric // If the argument type is an array type, move the qualifiers up to the 14800b57cec5SDimitry Andric // top level, so they can be matched with the qualifiers on the parameter. 1481349cc55cSDimitry Andric if (A->isArrayType()) { 14820b57cec5SDimitry Andric Qualifiers Quals; 1483349cc55cSDimitry Andric A = S.Context.getUnqualifiedArrayType(A, Quals); 1484349cc55cSDimitry Andric if (Quals) 1485349cc55cSDimitry Andric A = S.Context.getQualifiedType(A, Quals); 14860b57cec5SDimitry Andric } 14870b57cec5SDimitry Andric 14880b57cec5SDimitry Andric // The argument type can not be less qualified than the parameter 14890b57cec5SDimitry Andric // type. 14900b57cec5SDimitry Andric if (!(TDF & TDF_IgnoreQualifiers) && 1491349cc55cSDimitry Andric hasInconsistentOrSupersetQualifiersOf(P, A)) { 14920b57cec5SDimitry Andric Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); 1493349cc55cSDimitry Andric Info.FirstArg = TemplateArgument(P); 1494349cc55cSDimitry Andric Info.SecondArg = TemplateArgument(A); 14950b57cec5SDimitry Andric return Sema::TDK_Underqualified; 14960b57cec5SDimitry Andric } 14970b57cec5SDimitry Andric 14980b57cec5SDimitry Andric // Do not match a function type with a cv-qualified type. 14990b57cec5SDimitry Andric // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1584 1500349cc55cSDimitry Andric if (A->isFunctionType() && P.hasQualifiers()) 15010b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 15020b57cec5SDimitry Andric 1503349cc55cSDimitry Andric assert(TTP->getDepth() == Info.getDeducedDepth() && 15040b57cec5SDimitry Andric "saw template type parameter with wrong depth"); 1505349cc55cSDimitry Andric assert(A->getCanonicalTypeInternal() != S.Context.OverloadTy && 1506349cc55cSDimitry Andric "Unresolved overloaded function"); 1507349cc55cSDimitry Andric QualType DeducedType = A; 15080b57cec5SDimitry Andric 15090b57cec5SDimitry Andric // Remove any qualifiers on the parameter from the deduced type. 15100b57cec5SDimitry Andric // We checked the qualifiers for consistency above. 15110b57cec5SDimitry Andric Qualifiers DeducedQs = DeducedType.getQualifiers(); 1512349cc55cSDimitry Andric Qualifiers ParamQs = P.getQualifiers(); 15130b57cec5SDimitry Andric DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers()); 15140b57cec5SDimitry Andric if (ParamQs.hasObjCGCAttr()) 15150b57cec5SDimitry Andric DeducedQs.removeObjCGCAttr(); 15160b57cec5SDimitry Andric if (ParamQs.hasAddressSpace()) 15170b57cec5SDimitry Andric DeducedQs.removeAddressSpace(); 15180b57cec5SDimitry Andric if (ParamQs.hasObjCLifetime()) 15190b57cec5SDimitry Andric DeducedQs.removeObjCLifetime(); 15200b57cec5SDimitry Andric 15210b57cec5SDimitry Andric // Objective-C ARC: 15220b57cec5SDimitry Andric // If template deduction would produce a lifetime qualifier on a type 15230b57cec5SDimitry Andric // that is not a lifetime type, template argument deduction fails. 15240b57cec5SDimitry Andric if (ParamQs.hasObjCLifetime() && !DeducedType->isObjCLifetimeType() && 15250b57cec5SDimitry Andric !DeducedType->isDependentType()) { 15260b57cec5SDimitry Andric Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); 1527349cc55cSDimitry Andric Info.FirstArg = TemplateArgument(P); 1528349cc55cSDimitry Andric Info.SecondArg = TemplateArgument(A); 15290b57cec5SDimitry Andric return Sema::TDK_Underqualified; 15300b57cec5SDimitry Andric } 15310b57cec5SDimitry Andric 15320b57cec5SDimitry Andric // Objective-C ARC: 15330b57cec5SDimitry Andric // If template deduction would produce an argument type with lifetime type 15340b57cec5SDimitry Andric // but no lifetime qualifier, the __strong lifetime qualifier is inferred. 1535349cc55cSDimitry Andric if (S.getLangOpts().ObjCAutoRefCount && DeducedType->isObjCLifetimeType() && 15360b57cec5SDimitry Andric !DeducedQs.hasObjCLifetime()) 15370b57cec5SDimitry Andric DeducedQs.setObjCLifetime(Qualifiers::OCL_Strong); 15380b57cec5SDimitry Andric 1539349cc55cSDimitry Andric DeducedType = 1540349cc55cSDimitry Andric S.Context.getQualifiedType(DeducedType.getUnqualifiedType(), DeducedQs); 15410b57cec5SDimitry Andric 15420b57cec5SDimitry Andric DeducedTemplateArgument NewDeduced(DeducedType, DeducedFromArrayBound); 1543349cc55cSDimitry Andric DeducedTemplateArgument Result = 1544349cc55cSDimitry Andric checkDeducedTemplateArguments(S.Context, Deduced[Index], NewDeduced); 15450b57cec5SDimitry Andric if (Result.isNull()) { 15460b57cec5SDimitry Andric Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); 15470b57cec5SDimitry Andric Info.FirstArg = Deduced[Index]; 15480b57cec5SDimitry Andric Info.SecondArg = NewDeduced; 15490b57cec5SDimitry Andric return Sema::TDK_Inconsistent; 15500b57cec5SDimitry Andric } 15510b57cec5SDimitry Andric 15520b57cec5SDimitry Andric Deduced[Index] = Result; 15530b57cec5SDimitry Andric return Sema::TDK_Success; 15540b57cec5SDimitry Andric } 15550b57cec5SDimitry Andric 15560b57cec5SDimitry Andric // Set up the template argument deduction information for a failure. 1557349cc55cSDimitry Andric Info.FirstArg = TemplateArgument(P); 1558349cc55cSDimitry Andric Info.SecondArg = TemplateArgument(A); 15590b57cec5SDimitry Andric 15600b57cec5SDimitry Andric // If the parameter is an already-substituted template parameter 15610b57cec5SDimitry Andric // pack, do nothing: we don't know which of its arguments to look 15620b57cec5SDimitry Andric // at, so we have to wait until all of the parameter packs in this 15630b57cec5SDimitry Andric // expansion have arguments. 1564349cc55cSDimitry Andric if (P->getAs<SubstTemplateTypeParmPackType>()) 15650b57cec5SDimitry Andric return Sema::TDK_Success; 15660b57cec5SDimitry Andric 15670b57cec5SDimitry Andric // Check the cv-qualifiers on the parameter and argument types. 15680b57cec5SDimitry Andric if (!(TDF & TDF_IgnoreQualifiers)) { 15690b57cec5SDimitry Andric if (TDF & TDF_ParamWithReferenceType) { 1570349cc55cSDimitry Andric if (hasInconsistentOrSupersetQualifiersOf(P, A)) 15710b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 15720b57cec5SDimitry Andric } else if (TDF & TDF_ArgWithReferenceType) { 15730b57cec5SDimitry Andric // C++ [temp.deduct.conv]p4: 15740b57cec5SDimitry Andric // If the original A is a reference type, A can be more cv-qualified 15750b57cec5SDimitry Andric // than the deduced A 1576349cc55cSDimitry Andric if (!A.getQualifiers().compatiblyIncludes(P.getQualifiers())) 15770b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 15780b57cec5SDimitry Andric 15790b57cec5SDimitry Andric // Strip out all extra qualifiers from the argument to figure out the 15800b57cec5SDimitry Andric // type we're converting to, prior to the qualification conversion. 15810b57cec5SDimitry Andric Qualifiers Quals; 1582349cc55cSDimitry Andric A = S.Context.getUnqualifiedArrayType(A, Quals); 1583349cc55cSDimitry Andric A = S.Context.getQualifiedType(A, P.getQualifiers()); 1584349cc55cSDimitry Andric } else if (!IsPossiblyOpaquelyQualifiedType(P)) { 1585349cc55cSDimitry Andric if (P.getCVRQualifiers() != A.getCVRQualifiers()) 15860b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 15870b57cec5SDimitry Andric } 1588349cc55cSDimitry Andric } 15890b57cec5SDimitry Andric 15900b57cec5SDimitry Andric // If the parameter type is not dependent, there is nothing to deduce. 1591349cc55cSDimitry Andric if (!P->isDependentType()) { 1592349cc55cSDimitry Andric if (TDF & TDF_SkipNonDependent) 1593349cc55cSDimitry Andric return Sema::TDK_Success; 1594349cc55cSDimitry Andric if ((TDF & TDF_IgnoreQualifiers) ? S.Context.hasSameUnqualifiedType(P, A) 1595349cc55cSDimitry Andric : S.Context.hasSameType(P, A)) 1596349cc55cSDimitry Andric return Sema::TDK_Success; 1597349cc55cSDimitry Andric if (TDF & TDF_AllowCompatibleFunctionType && 1598349cc55cSDimitry Andric S.isSameOrCompatibleFunctionType(P, A)) 1599349cc55cSDimitry Andric return Sema::TDK_Success; 1600349cc55cSDimitry Andric if (!(TDF & TDF_IgnoreQualifiers)) 16010b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1602349cc55cSDimitry Andric // Otherwise, when ignoring qualifiers, the types not having the same 1603349cc55cSDimitry Andric // unqualified type does not mean they do not match, so in this case we 1604349cc55cSDimitry Andric // must keep going and analyze with a non-dependent parameter type. 16050b57cec5SDimitry Andric } 16060b57cec5SDimitry Andric 1607349cc55cSDimitry Andric switch (P.getCanonicalType()->getTypeClass()) { 16080b57cec5SDimitry Andric // Non-canonical types cannot appear here. 16090b57cec5SDimitry Andric #define NON_CANONICAL_TYPE(Class, Base) \ 16100b57cec5SDimitry Andric case Type::Class: llvm_unreachable("deducing non-canonical type: " #Class); 16110b57cec5SDimitry Andric #define TYPE(Class, Base) 1612a7dea167SDimitry Andric #include "clang/AST/TypeNodes.inc" 16130b57cec5SDimitry Andric 16140b57cec5SDimitry Andric case Type::TemplateTypeParm: 16150b57cec5SDimitry Andric case Type::SubstTemplateTypeParmPack: 16160b57cec5SDimitry Andric llvm_unreachable("Type nodes handled above"); 16170b57cec5SDimitry Andric 1618349cc55cSDimitry Andric case Type::Auto: 1619349cc55cSDimitry Andric // FIXME: Implement deduction in dependent case. 1620349cc55cSDimitry Andric if (P->isDependentType()) 1621349cc55cSDimitry Andric return Sema::TDK_Success; 1622*bdd1243dSDimitry Andric [[fallthrough]]; 16230b57cec5SDimitry Andric case Type::Builtin: 16240b57cec5SDimitry Andric case Type::VariableArray: 16250b57cec5SDimitry Andric case Type::Vector: 16260b57cec5SDimitry Andric case Type::FunctionNoProto: 16270b57cec5SDimitry Andric case Type::Record: 16280b57cec5SDimitry Andric case Type::Enum: 16290b57cec5SDimitry Andric case Type::ObjCObject: 16300b57cec5SDimitry Andric case Type::ObjCInterface: 16310b57cec5SDimitry Andric case Type::ObjCObjectPointer: 16320eae32dcSDimitry Andric case Type::BitInt: 1633349cc55cSDimitry Andric return (TDF & TDF_SkipNonDependent) || 1634349cc55cSDimitry Andric ((TDF & TDF_IgnoreQualifiers) 1635349cc55cSDimitry Andric ? S.Context.hasSameUnqualifiedType(P, A) 1636349cc55cSDimitry Andric : S.Context.hasSameType(P, A)) 1637349cc55cSDimitry Andric ? Sema::TDK_Success 1638349cc55cSDimitry Andric : Sema::TDK_NonDeducedMismatch; 16390b57cec5SDimitry Andric 16400b57cec5SDimitry Andric // _Complex T [placeholder extension] 1641349cc55cSDimitry Andric case Type::Complex: { 1642349cc55cSDimitry Andric const auto *CP = P->castAs<ComplexType>(), *CA = A->getAs<ComplexType>(); 1643349cc55cSDimitry Andric if (!CA) 16440b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1645349cc55cSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 1646349cc55cSDimitry Andric S, TemplateParams, CP->getElementType(), CA->getElementType(), Info, 1647349cc55cSDimitry Andric Deduced, TDF); 1648349cc55cSDimitry Andric } 16490b57cec5SDimitry Andric 16500b57cec5SDimitry Andric // _Atomic T [extension] 1651349cc55cSDimitry Andric case Type::Atomic: { 1652349cc55cSDimitry Andric const auto *PA = P->castAs<AtomicType>(), *AA = A->getAs<AtomicType>(); 1653349cc55cSDimitry Andric if (!AA) 16540b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1655349cc55cSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 1656349cc55cSDimitry Andric S, TemplateParams, PA->getValueType(), AA->getValueType(), Info, 1657349cc55cSDimitry Andric Deduced, TDF); 1658349cc55cSDimitry Andric } 16590b57cec5SDimitry Andric 16600b57cec5SDimitry Andric // T * 16610b57cec5SDimitry Andric case Type::Pointer: { 16620b57cec5SDimitry Andric QualType PointeeType; 1663349cc55cSDimitry Andric if (const auto *PA = A->getAs<PointerType>()) { 1664349cc55cSDimitry Andric PointeeType = PA->getPointeeType(); 1665349cc55cSDimitry Andric } else if (const auto *PA = A->getAs<ObjCObjectPointerType>()) { 1666349cc55cSDimitry Andric PointeeType = PA->getPointeeType(); 16670b57cec5SDimitry Andric } else { 16680b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 16690b57cec5SDimitry Andric } 1670349cc55cSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 1671349cc55cSDimitry Andric S, TemplateParams, P->castAs<PointerType>()->getPointeeType(), 1672349cc55cSDimitry Andric PointeeType, Info, Deduced, 1673349cc55cSDimitry Andric TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass)); 16740b57cec5SDimitry Andric } 16750b57cec5SDimitry Andric 16760b57cec5SDimitry Andric // T & 16770b57cec5SDimitry Andric case Type::LValueReference: { 1678349cc55cSDimitry Andric const auto *RP = P->castAs<LValueReferenceType>(), 1679349cc55cSDimitry Andric *RA = A->getAs<LValueReferenceType>(); 1680349cc55cSDimitry Andric if (!RA) 16810b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 16820b57cec5SDimitry Andric 1683349cc55cSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 1684349cc55cSDimitry Andric S, TemplateParams, RP->getPointeeType(), RA->getPointeeType(), Info, 1685349cc55cSDimitry Andric Deduced, 0); 16860b57cec5SDimitry Andric } 16870b57cec5SDimitry Andric 16880b57cec5SDimitry Andric // T && [C++0x] 16890b57cec5SDimitry Andric case Type::RValueReference: { 1690349cc55cSDimitry Andric const auto *RP = P->castAs<RValueReferenceType>(), 1691349cc55cSDimitry Andric *RA = A->getAs<RValueReferenceType>(); 1692349cc55cSDimitry Andric if (!RA) 16930b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 16940b57cec5SDimitry Andric 1695349cc55cSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 1696349cc55cSDimitry Andric S, TemplateParams, RP->getPointeeType(), RA->getPointeeType(), Info, 1697349cc55cSDimitry Andric Deduced, 0); 16980b57cec5SDimitry Andric } 16990b57cec5SDimitry Andric 17000b57cec5SDimitry Andric // T [] (implied, but not stated explicitly) 17010b57cec5SDimitry Andric case Type::IncompleteArray: { 1702349cc55cSDimitry Andric const auto *IAA = S.Context.getAsIncompleteArrayType(A); 1703349cc55cSDimitry Andric if (!IAA) 17040b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 17050b57cec5SDimitry Andric 1706349cc55cSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 1707349cc55cSDimitry Andric S, TemplateParams, 1708349cc55cSDimitry Andric S.Context.getAsIncompleteArrayType(P)->getElementType(), 1709349cc55cSDimitry Andric IAA->getElementType(), Info, Deduced, TDF & TDF_IgnoreQualifiers); 17100b57cec5SDimitry Andric } 17110b57cec5SDimitry Andric 17120b57cec5SDimitry Andric // T [integer-constant] 17130b57cec5SDimitry Andric case Type::ConstantArray: { 1714349cc55cSDimitry Andric const auto *CAA = S.Context.getAsConstantArrayType(A), 1715349cc55cSDimitry Andric *CAP = S.Context.getAsConstantArrayType(P); 1716349cc55cSDimitry Andric assert(CAP); 1717349cc55cSDimitry Andric if (!CAA || CAA->getSize() != CAP->getSize()) 17180b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 17190b57cec5SDimitry Andric 1720349cc55cSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 1721349cc55cSDimitry Andric S, TemplateParams, CAP->getElementType(), CAA->getElementType(), Info, 1722349cc55cSDimitry Andric Deduced, TDF & TDF_IgnoreQualifiers); 17230b57cec5SDimitry Andric } 17240b57cec5SDimitry Andric 17250b57cec5SDimitry Andric // type [i] 17260b57cec5SDimitry Andric case Type::DependentSizedArray: { 1727349cc55cSDimitry Andric const auto *AA = S.Context.getAsArrayType(A); 1728349cc55cSDimitry Andric if (!AA) 17290b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 17300b57cec5SDimitry Andric 17310b57cec5SDimitry Andric // Check the element type of the arrays 1732349cc55cSDimitry Andric const auto *DAP = S.Context.getAsDependentSizedArrayType(P); 1733349cc55cSDimitry Andric assert(DAP); 1734349cc55cSDimitry Andric if (auto Result = DeduceTemplateArgumentsByTypeMatch( 1735349cc55cSDimitry Andric S, TemplateParams, DAP->getElementType(), AA->getElementType(), 1736349cc55cSDimitry Andric Info, Deduced, TDF & TDF_IgnoreQualifiers)) 17370b57cec5SDimitry Andric return Result; 17380b57cec5SDimitry Andric 17390b57cec5SDimitry Andric // Determine the array bound is something we can deduce. 1740349cc55cSDimitry Andric const NonTypeTemplateParmDecl *NTTP = 1741349cc55cSDimitry Andric getDeducedParameterFromExpr(Info, DAP->getSizeExpr()); 17420b57cec5SDimitry Andric if (!NTTP) 17430b57cec5SDimitry Andric return Sema::TDK_Success; 17440b57cec5SDimitry Andric 17450b57cec5SDimitry Andric // We can perform template argument deduction for the given non-type 17460b57cec5SDimitry Andric // template parameter. 17470b57cec5SDimitry Andric assert(NTTP->getDepth() == Info.getDeducedDepth() && 17480b57cec5SDimitry Andric "saw non-type template parameter with wrong depth"); 1749349cc55cSDimitry Andric if (const auto *CAA = dyn_cast<ConstantArrayType>(AA)) { 1750349cc55cSDimitry Andric llvm::APSInt Size(CAA->getSize()); 1751349cc55cSDimitry Andric return DeduceNonTypeTemplateArgument( 1752349cc55cSDimitry Andric S, TemplateParams, NTTP, Size, S.Context.getSizeType(), 1753349cc55cSDimitry Andric /*ArrayBound=*/true, Info, Deduced); 17540b57cec5SDimitry Andric } 1755349cc55cSDimitry Andric if (const auto *DAA = dyn_cast<DependentSizedArrayType>(AA)) 1756349cc55cSDimitry Andric if (DAA->getSizeExpr()) 1757349cc55cSDimitry Andric return DeduceNonTypeTemplateArgument( 1758349cc55cSDimitry Andric S, TemplateParams, NTTP, DAA->getSizeExpr(), Info, Deduced); 17590b57cec5SDimitry Andric 17600b57cec5SDimitry Andric // Incomplete type does not match a dependently-sized array type 17610b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 17620b57cec5SDimitry Andric } 17630b57cec5SDimitry Andric 17640b57cec5SDimitry Andric // type(*)(T) 17650b57cec5SDimitry Andric // T(*)() 17660b57cec5SDimitry Andric // T(*)(T) 17670b57cec5SDimitry Andric case Type::FunctionProto: { 1768349cc55cSDimitry Andric const auto *FPP = P->castAs<FunctionProtoType>(), 1769349cc55cSDimitry Andric *FPA = A->getAs<FunctionProtoType>(); 1770349cc55cSDimitry Andric if (!FPA) 17710b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 17720b57cec5SDimitry Andric 1773349cc55cSDimitry Andric if (FPP->getMethodQuals() != FPA->getMethodQuals() || 1774349cc55cSDimitry Andric FPP->getRefQualifier() != FPA->getRefQualifier() || 1775349cc55cSDimitry Andric FPP->isVariadic() != FPA->isVariadic()) 17760b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 17770b57cec5SDimitry Andric 17780b57cec5SDimitry Andric // Check return types. 17790b57cec5SDimitry Andric if (auto Result = DeduceTemplateArgumentsByTypeMatch( 1780349cc55cSDimitry Andric S, TemplateParams, FPP->getReturnType(), FPA->getReturnType(), 1781349cc55cSDimitry Andric Info, Deduced, 0, 1782349cc55cSDimitry Andric /*PartialOrdering=*/false, 1783349cc55cSDimitry Andric /*DeducedFromArrayBound=*/false)) 17840b57cec5SDimitry Andric return Result; 17850b57cec5SDimitry Andric 17860b57cec5SDimitry Andric // Check parameter types. 17870b57cec5SDimitry Andric if (auto Result = DeduceTemplateArguments( 1788349cc55cSDimitry Andric S, TemplateParams, FPP->param_type_begin(), FPP->getNumParams(), 1789349cc55cSDimitry Andric FPA->param_type_begin(), FPA->getNumParams(), Info, Deduced, 1790*bdd1243dSDimitry Andric TDF & TDF_TopLevelParameterTypeList, PartialOrdering)) 17910b57cec5SDimitry Andric return Result; 17920b57cec5SDimitry Andric 17930b57cec5SDimitry Andric if (TDF & TDF_AllowCompatibleFunctionType) 17940b57cec5SDimitry Andric return Sema::TDK_Success; 17950b57cec5SDimitry Andric 17960b57cec5SDimitry Andric // FIXME: Per core-2016/10/1019 (no corresponding core issue yet), permit 17970b57cec5SDimitry Andric // deducing through the noexcept-specifier if it's part of the canonical 17980b57cec5SDimitry Andric // type. libstdc++ relies on this. 1799349cc55cSDimitry Andric Expr *NoexceptExpr = FPP->getNoexceptExpr(); 1800e8d8bef9SDimitry Andric if (const NonTypeTemplateParmDecl *NTTP = 18010b57cec5SDimitry Andric NoexceptExpr ? getDeducedParameterFromExpr(Info, NoexceptExpr) 18020b57cec5SDimitry Andric : nullptr) { 18030b57cec5SDimitry Andric assert(NTTP->getDepth() == Info.getDeducedDepth() && 18040b57cec5SDimitry Andric "saw non-type template parameter with wrong depth"); 18050b57cec5SDimitry Andric 18060b57cec5SDimitry Andric llvm::APSInt Noexcept(1); 1807349cc55cSDimitry Andric switch (FPA->canThrow()) { 18080b57cec5SDimitry Andric case CT_Cannot: 18090b57cec5SDimitry Andric Noexcept = 1; 1810*bdd1243dSDimitry Andric [[fallthrough]]; 18110b57cec5SDimitry Andric 18120b57cec5SDimitry Andric case CT_Can: 18130b57cec5SDimitry Andric // We give E in noexcept(E) the "deduced from array bound" treatment. 18140b57cec5SDimitry Andric // FIXME: Should we? 18150b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument( 18160b57cec5SDimitry Andric S, TemplateParams, NTTP, Noexcept, S.Context.BoolTy, 1817349cc55cSDimitry Andric /*DeducedFromArrayBound=*/true, Info, Deduced); 18180b57cec5SDimitry Andric 18190b57cec5SDimitry Andric case CT_Dependent: 1820349cc55cSDimitry Andric if (Expr *ArgNoexceptExpr = FPA->getNoexceptExpr()) 18210b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument( 18220b57cec5SDimitry Andric S, TemplateParams, NTTP, ArgNoexceptExpr, Info, Deduced); 18230b57cec5SDimitry Andric // Can't deduce anything from throw(T...). 18240b57cec5SDimitry Andric break; 18250b57cec5SDimitry Andric } 18260b57cec5SDimitry Andric } 18270b57cec5SDimitry Andric // FIXME: Detect non-deduced exception specification mismatches? 18280b57cec5SDimitry Andric // 18290b57cec5SDimitry Andric // Careful about [temp.deduct.call] and [temp.deduct.conv], which allow 18300b57cec5SDimitry Andric // top-level differences in noexcept-specifications. 18310b57cec5SDimitry Andric 18320b57cec5SDimitry Andric return Sema::TDK_Success; 18330b57cec5SDimitry Andric } 18340b57cec5SDimitry Andric 18350b57cec5SDimitry Andric case Type::InjectedClassName: 18360b57cec5SDimitry Andric // Treat a template's injected-class-name as if the template 18370b57cec5SDimitry Andric // specialization type had been used. 18380b57cec5SDimitry Andric 18390b57cec5SDimitry Andric // template-name<T> (where template-name refers to a class template) 18400b57cec5SDimitry Andric // template-name<i> 18410b57cec5SDimitry Andric // TT<T> 18420b57cec5SDimitry Andric // TT<i> 18430b57cec5SDimitry Andric // TT<> 18440b57cec5SDimitry Andric case Type::TemplateSpecialization: { 18450b57cec5SDimitry Andric // When Arg cannot be a derived class, we can just try to deduce template 18460b57cec5SDimitry Andric // arguments from the template-id. 1847349cc55cSDimitry Andric if (!(TDF & TDF_DerivedClass) || !A->isRecordType()) 1848349cc55cSDimitry Andric return DeduceTemplateSpecArguments(S, TemplateParams, P, A, Info, 18490b57cec5SDimitry Andric Deduced); 18500b57cec5SDimitry Andric 18510b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 8> DeducedOrig(Deduced.begin(), 18520b57cec5SDimitry Andric Deduced.end()); 18530b57cec5SDimitry Andric 1854349cc55cSDimitry Andric auto Result = 1855349cc55cSDimitry Andric DeduceTemplateSpecArguments(S, TemplateParams, P, A, Info, Deduced); 18560b57cec5SDimitry Andric if (Result == Sema::TDK_Success) 18570b57cec5SDimitry Andric return Result; 18580b57cec5SDimitry Andric 18590b57cec5SDimitry Andric // We cannot inspect base classes as part of deduction when the type 18600b57cec5SDimitry Andric // is incomplete, so either instantiate any templates necessary to 18610b57cec5SDimitry Andric // complete the type, or skip over it if it cannot be completed. 1862349cc55cSDimitry Andric if (!S.isCompleteType(Info.getLocation(), A)) 18630b57cec5SDimitry Andric return Result; 18640b57cec5SDimitry Andric 18650b57cec5SDimitry Andric // Reset the incorrectly deduced argument from above. 18660b57cec5SDimitry Andric Deduced = DeducedOrig; 18670b57cec5SDimitry Andric 1868e8d8bef9SDimitry Andric // Check bases according to C++14 [temp.deduct.call] p4b3: 1869349cc55cSDimitry Andric auto BaseResult = DeduceTemplateBases(S, getCanonicalRD(A), 1870349cc55cSDimitry Andric TemplateParams, P, Info, Deduced); 1871349cc55cSDimitry Andric return BaseResult != Sema::TDK_Invalid ? BaseResult : Result; 18720b57cec5SDimitry Andric } 18730b57cec5SDimitry Andric 18740b57cec5SDimitry Andric // T type::* 18750b57cec5SDimitry Andric // T T::* 18760b57cec5SDimitry Andric // T (type::*)() 18770b57cec5SDimitry Andric // type (T::*)() 18780b57cec5SDimitry Andric // type (type::*)(T) 18790b57cec5SDimitry Andric // type (T::*)(T) 18800b57cec5SDimitry Andric // T (type::*)(T) 18810b57cec5SDimitry Andric // T (T::*)() 18820b57cec5SDimitry Andric // T (T::*)(T) 18830b57cec5SDimitry Andric case Type::MemberPointer: { 1884349cc55cSDimitry Andric const auto *MPP = P->castAs<MemberPointerType>(), 1885349cc55cSDimitry Andric *MPA = A->getAs<MemberPointerType>(); 1886349cc55cSDimitry Andric if (!MPA) 18870b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 18880b57cec5SDimitry Andric 1889349cc55cSDimitry Andric QualType PPT = MPP->getPointeeType(); 1890349cc55cSDimitry Andric if (PPT->isFunctionType()) 1891349cc55cSDimitry Andric S.adjustMemberFunctionCC(PPT, /*IsStatic=*/true, 18920b57cec5SDimitry Andric /*IsCtorOrDtor=*/false, Info.getLocation()); 1893349cc55cSDimitry Andric QualType APT = MPA->getPointeeType(); 1894349cc55cSDimitry Andric if (APT->isFunctionType()) 1895349cc55cSDimitry Andric S.adjustMemberFunctionCC(APT, /*IsStatic=*/true, 18960b57cec5SDimitry Andric /*IsCtorOrDtor=*/false, Info.getLocation()); 18970b57cec5SDimitry Andric 1898349cc55cSDimitry Andric unsigned SubTDF = TDF & TDF_IgnoreQualifiers; 1899349cc55cSDimitry Andric if (auto Result = DeduceTemplateArgumentsByTypeMatch( 1900349cc55cSDimitry Andric S, TemplateParams, PPT, APT, Info, Deduced, SubTDF)) 19010b57cec5SDimitry Andric return Result; 1902349cc55cSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 1903349cc55cSDimitry Andric S, TemplateParams, QualType(MPP->getClass(), 0), 1904349cc55cSDimitry Andric QualType(MPA->getClass(), 0), Info, Deduced, SubTDF); 19050b57cec5SDimitry Andric } 19060b57cec5SDimitry Andric 19070b57cec5SDimitry Andric // (clang extension) 19080b57cec5SDimitry Andric // 19090b57cec5SDimitry Andric // type(^)(T) 19100b57cec5SDimitry Andric // T(^)() 19110b57cec5SDimitry Andric // T(^)(T) 19120b57cec5SDimitry Andric case Type::BlockPointer: { 1913349cc55cSDimitry Andric const auto *BPP = P->castAs<BlockPointerType>(), 1914349cc55cSDimitry Andric *BPA = A->getAs<BlockPointerType>(); 1915349cc55cSDimitry Andric if (!BPA) 19160b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1917349cc55cSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 1918349cc55cSDimitry Andric S, TemplateParams, BPP->getPointeeType(), BPA->getPointeeType(), Info, 1919349cc55cSDimitry Andric Deduced, 0); 19200b57cec5SDimitry Andric } 19210b57cec5SDimitry Andric 19220b57cec5SDimitry Andric // (clang extension) 19230b57cec5SDimitry Andric // 19240b57cec5SDimitry Andric // T __attribute__(((ext_vector_type(<integral constant>)))) 19250b57cec5SDimitry Andric case Type::ExtVector: { 1926349cc55cSDimitry Andric const auto *VP = P->castAs<ExtVectorType>(); 1927349cc55cSDimitry Andric QualType ElementType; 1928349cc55cSDimitry Andric if (const auto *VA = A->getAs<ExtVectorType>()) { 19290b57cec5SDimitry Andric // Make sure that the vectors have the same number of elements. 1930349cc55cSDimitry Andric if (VP->getNumElements() != VA->getNumElements()) 19310b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1932349cc55cSDimitry Andric ElementType = VA->getElementType(); 1933349cc55cSDimitry Andric } else if (const auto *VA = A->getAs<DependentSizedExtVectorType>()) { 19340b57cec5SDimitry Andric // We can't check the number of elements, since the argument has a 19350b57cec5SDimitry Andric // dependent number of elements. This can only occur during partial 19360b57cec5SDimitry Andric // ordering. 1937349cc55cSDimitry Andric ElementType = VA->getElementType(); 1938349cc55cSDimitry Andric } else { 19390b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 19400b57cec5SDimitry Andric } 1941349cc55cSDimitry Andric // Perform deduction on the element types. 1942349cc55cSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 1943349cc55cSDimitry Andric S, TemplateParams, VP->getElementType(), ElementType, Info, Deduced, 1944349cc55cSDimitry Andric TDF); 1945349cc55cSDimitry Andric } 19460b57cec5SDimitry Andric 19470b57cec5SDimitry Andric case Type::DependentVector: { 1948349cc55cSDimitry Andric const auto *VP = P->castAs<DependentVectorType>(); 19490b57cec5SDimitry Andric 1950349cc55cSDimitry Andric if (const auto *VA = A->getAs<VectorType>()) { 19510b57cec5SDimitry Andric // Perform deduction on the element types. 1952349cc55cSDimitry Andric if (auto Result = DeduceTemplateArgumentsByTypeMatch( 1953349cc55cSDimitry Andric S, TemplateParams, VP->getElementType(), VA->getElementType(), 1954349cc55cSDimitry Andric Info, Deduced, TDF)) 19550b57cec5SDimitry Andric return Result; 19560b57cec5SDimitry Andric 19570b57cec5SDimitry Andric // Perform deduction on the vector size, if we can. 1958e8d8bef9SDimitry Andric const NonTypeTemplateParmDecl *NTTP = 1959349cc55cSDimitry Andric getDeducedParameterFromExpr(Info, VP->getSizeExpr()); 19600b57cec5SDimitry Andric if (!NTTP) 19610b57cec5SDimitry Andric return Sema::TDK_Success; 19620b57cec5SDimitry Andric 19630b57cec5SDimitry Andric llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); 1964349cc55cSDimitry Andric ArgSize = VA->getNumElements(); 19650b57cec5SDimitry Andric // Note that we use the "array bound" rules here; just like in that 19660b57cec5SDimitry Andric // case, we don't have any particular type for the vector size, but 19670b57cec5SDimitry Andric // we can provide one if necessary. 19680b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, 19690b57cec5SDimitry Andric S.Context.UnsignedIntTy, true, 19700b57cec5SDimitry Andric Info, Deduced); 19710b57cec5SDimitry Andric } 19720b57cec5SDimitry Andric 1973349cc55cSDimitry Andric if (const auto *VA = A->getAs<DependentVectorType>()) { 19740b57cec5SDimitry Andric // Perform deduction on the element types. 1975349cc55cSDimitry Andric if (auto Result = DeduceTemplateArgumentsByTypeMatch( 1976349cc55cSDimitry Andric S, TemplateParams, VP->getElementType(), VA->getElementType(), 1977349cc55cSDimitry Andric Info, Deduced, TDF)) 19780b57cec5SDimitry Andric return Result; 19790b57cec5SDimitry Andric 19800b57cec5SDimitry Andric // Perform deduction on the vector size, if we can. 1981349cc55cSDimitry Andric const NonTypeTemplateParmDecl *NTTP = 1982349cc55cSDimitry Andric getDeducedParameterFromExpr(Info, VP->getSizeExpr()); 19830b57cec5SDimitry Andric if (!NTTP) 19840b57cec5SDimitry Andric return Sema::TDK_Success; 19850b57cec5SDimitry Andric 1986349cc55cSDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, 1987349cc55cSDimitry Andric VA->getSizeExpr(), Info, Deduced); 19880b57cec5SDimitry Andric } 19890b57cec5SDimitry Andric 19900b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 19910b57cec5SDimitry Andric } 19920b57cec5SDimitry Andric 19930b57cec5SDimitry Andric // (clang extension) 19940b57cec5SDimitry Andric // 19950b57cec5SDimitry Andric // T __attribute__(((ext_vector_type(N)))) 19960b57cec5SDimitry Andric case Type::DependentSizedExtVector: { 1997349cc55cSDimitry Andric const auto *VP = P->castAs<DependentSizedExtVectorType>(); 19980b57cec5SDimitry Andric 1999349cc55cSDimitry Andric if (const auto *VA = A->getAs<ExtVectorType>()) { 20000b57cec5SDimitry Andric // Perform deduction on the element types. 2001349cc55cSDimitry Andric if (auto Result = DeduceTemplateArgumentsByTypeMatch( 2002349cc55cSDimitry Andric S, TemplateParams, VP->getElementType(), VA->getElementType(), 20030b57cec5SDimitry Andric Info, Deduced, TDF)) 20040b57cec5SDimitry Andric return Result; 20050b57cec5SDimitry Andric 20060b57cec5SDimitry Andric // Perform deduction on the vector size, if we can. 2007e8d8bef9SDimitry Andric const NonTypeTemplateParmDecl *NTTP = 2008349cc55cSDimitry Andric getDeducedParameterFromExpr(Info, VP->getSizeExpr()); 20090b57cec5SDimitry Andric if (!NTTP) 20100b57cec5SDimitry Andric return Sema::TDK_Success; 20110b57cec5SDimitry Andric 20120b57cec5SDimitry Andric llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); 2013349cc55cSDimitry Andric ArgSize = VA->getNumElements(); 20140b57cec5SDimitry Andric // Note that we use the "array bound" rules here; just like in that 20150b57cec5SDimitry Andric // case, we don't have any particular type for the vector size, but 20160b57cec5SDimitry Andric // we can provide one if necessary. 20170b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, 20180b57cec5SDimitry Andric S.Context.IntTy, true, Info, 20190b57cec5SDimitry Andric Deduced); 20200b57cec5SDimitry Andric } 20210b57cec5SDimitry Andric 2022349cc55cSDimitry Andric if (const auto *VA = A->getAs<DependentSizedExtVectorType>()) { 20230b57cec5SDimitry Andric // Perform deduction on the element types. 2024349cc55cSDimitry Andric if (auto Result = DeduceTemplateArgumentsByTypeMatch( 2025349cc55cSDimitry Andric S, TemplateParams, VP->getElementType(), VA->getElementType(), 20260b57cec5SDimitry Andric Info, Deduced, TDF)) 20270b57cec5SDimitry Andric return Result; 20280b57cec5SDimitry Andric 20290b57cec5SDimitry Andric // Perform deduction on the vector size, if we can. 2030e8d8bef9SDimitry Andric const NonTypeTemplateParmDecl *NTTP = 2031349cc55cSDimitry Andric getDeducedParameterFromExpr(Info, VP->getSizeExpr()); 20320b57cec5SDimitry Andric if (!NTTP) 20330b57cec5SDimitry Andric return Sema::TDK_Success; 20340b57cec5SDimitry Andric 20350b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, 2036349cc55cSDimitry Andric VA->getSizeExpr(), Info, Deduced); 20370b57cec5SDimitry Andric } 20380b57cec5SDimitry Andric 20390b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 20400b57cec5SDimitry Andric } 20410b57cec5SDimitry Andric 20420b57cec5SDimitry Andric // (clang extension) 20430b57cec5SDimitry Andric // 20445ffd83dbSDimitry Andric // T __attribute__((matrix_type(<integral constant>, 20455ffd83dbSDimitry Andric // <integral constant>))) 20465ffd83dbSDimitry Andric case Type::ConstantMatrix: { 2047349cc55cSDimitry Andric const auto *MP = P->castAs<ConstantMatrixType>(), 2048349cc55cSDimitry Andric *MA = A->getAs<ConstantMatrixType>(); 2049349cc55cSDimitry Andric if (!MA) 20505ffd83dbSDimitry Andric return Sema::TDK_NonDeducedMismatch; 20515ffd83dbSDimitry Andric 20525ffd83dbSDimitry Andric // Check that the dimensions are the same 2053349cc55cSDimitry Andric if (MP->getNumRows() != MA->getNumRows() || 2054349cc55cSDimitry Andric MP->getNumColumns() != MA->getNumColumns()) { 20555ffd83dbSDimitry Andric return Sema::TDK_NonDeducedMismatch; 20565ffd83dbSDimitry Andric } 20575ffd83dbSDimitry Andric // Perform deduction on element types. 20585ffd83dbSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 2059349cc55cSDimitry Andric S, TemplateParams, MP->getElementType(), MA->getElementType(), Info, 2060349cc55cSDimitry Andric Deduced, TDF); 20615ffd83dbSDimitry Andric } 20625ffd83dbSDimitry Andric 20635ffd83dbSDimitry Andric case Type::DependentSizedMatrix: { 2064349cc55cSDimitry Andric const auto *MP = P->castAs<DependentSizedMatrixType>(); 2065349cc55cSDimitry Andric const auto *MA = A->getAs<MatrixType>(); 2066349cc55cSDimitry Andric if (!MA) 20675ffd83dbSDimitry Andric return Sema::TDK_NonDeducedMismatch; 20685ffd83dbSDimitry Andric 20695ffd83dbSDimitry Andric // Check the element type of the matrixes. 2070349cc55cSDimitry Andric if (auto Result = DeduceTemplateArgumentsByTypeMatch( 2071349cc55cSDimitry Andric S, TemplateParams, MP->getElementType(), MA->getElementType(), 2072349cc55cSDimitry Andric Info, Deduced, TDF)) 20735ffd83dbSDimitry Andric return Result; 20745ffd83dbSDimitry Andric 20755ffd83dbSDimitry Andric // Try to deduce a matrix dimension. 20765ffd83dbSDimitry Andric auto DeduceMatrixArg = 20775ffd83dbSDimitry Andric [&S, &Info, &Deduced, &TemplateParams]( 2078349cc55cSDimitry Andric Expr *ParamExpr, const MatrixType *A, 20795ffd83dbSDimitry Andric unsigned (ConstantMatrixType::*GetArgDimension)() const, 20805ffd83dbSDimitry Andric Expr *(DependentSizedMatrixType::*GetArgDimensionExpr)() const) { 2081349cc55cSDimitry Andric const auto *ACM = dyn_cast<ConstantMatrixType>(A); 2082349cc55cSDimitry Andric const auto *ADM = dyn_cast<DependentSizedMatrixType>(A); 20835ffd83dbSDimitry Andric if (!ParamExpr->isValueDependent()) { 2084*bdd1243dSDimitry Andric std::optional<llvm::APSInt> ParamConst = 2085e8d8bef9SDimitry Andric ParamExpr->getIntegerConstantExpr(S.Context); 2086e8d8bef9SDimitry Andric if (!ParamConst) 20875ffd83dbSDimitry Andric return Sema::TDK_NonDeducedMismatch; 20885ffd83dbSDimitry Andric 2089349cc55cSDimitry Andric if (ACM) { 2090349cc55cSDimitry Andric if ((ACM->*GetArgDimension)() == *ParamConst) 20915ffd83dbSDimitry Andric return Sema::TDK_Success; 20925ffd83dbSDimitry Andric return Sema::TDK_NonDeducedMismatch; 20935ffd83dbSDimitry Andric } 20945ffd83dbSDimitry Andric 2095349cc55cSDimitry Andric Expr *ArgExpr = (ADM->*GetArgDimensionExpr)(); 2096*bdd1243dSDimitry Andric if (std::optional<llvm::APSInt> ArgConst = 2097e8d8bef9SDimitry Andric ArgExpr->getIntegerConstantExpr(S.Context)) 2098e8d8bef9SDimitry Andric if (*ArgConst == *ParamConst) 20995ffd83dbSDimitry Andric return Sema::TDK_Success; 21005ffd83dbSDimitry Andric return Sema::TDK_NonDeducedMismatch; 21015ffd83dbSDimitry Andric } 21025ffd83dbSDimitry Andric 2103e8d8bef9SDimitry Andric const NonTypeTemplateParmDecl *NTTP = 21045ffd83dbSDimitry Andric getDeducedParameterFromExpr(Info, ParamExpr); 21055ffd83dbSDimitry Andric if (!NTTP) 21065ffd83dbSDimitry Andric return Sema::TDK_Success; 21075ffd83dbSDimitry Andric 2108349cc55cSDimitry Andric if (ACM) { 21095ffd83dbSDimitry Andric llvm::APSInt ArgConst( 21105ffd83dbSDimitry Andric S.Context.getTypeSize(S.Context.getSizeType())); 2111349cc55cSDimitry Andric ArgConst = (ACM->*GetArgDimension)(); 21125ffd83dbSDimitry Andric return DeduceNonTypeTemplateArgument( 21135ffd83dbSDimitry Andric S, TemplateParams, NTTP, ArgConst, S.Context.getSizeType(), 21145ffd83dbSDimitry Andric /*ArrayBound=*/true, Info, Deduced); 21155ffd83dbSDimitry Andric } 21165ffd83dbSDimitry Andric 2117349cc55cSDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, 2118349cc55cSDimitry Andric (ADM->*GetArgDimensionExpr)(), 21195ffd83dbSDimitry Andric Info, Deduced); 21205ffd83dbSDimitry Andric }; 21215ffd83dbSDimitry Andric 2122349cc55cSDimitry Andric if (auto Result = DeduceMatrixArg(MP->getRowExpr(), MA, 21235ffd83dbSDimitry Andric &ConstantMatrixType::getNumRows, 2124349cc55cSDimitry Andric &DependentSizedMatrixType::getRowExpr)) 21255ffd83dbSDimitry Andric return Result; 21265ffd83dbSDimitry Andric 2127349cc55cSDimitry Andric return DeduceMatrixArg(MP->getColumnExpr(), MA, 21285ffd83dbSDimitry Andric &ConstantMatrixType::getNumColumns, 21295ffd83dbSDimitry Andric &DependentSizedMatrixType::getColumnExpr); 21305ffd83dbSDimitry Andric } 21315ffd83dbSDimitry Andric 21325ffd83dbSDimitry Andric // (clang extension) 21335ffd83dbSDimitry Andric // 21340b57cec5SDimitry Andric // T __attribute__(((address_space(N)))) 21350b57cec5SDimitry Andric case Type::DependentAddressSpace: { 2136349cc55cSDimitry Andric const auto *ASP = P->castAs<DependentAddressSpaceType>(); 21370b57cec5SDimitry Andric 2138349cc55cSDimitry Andric if (const auto *ASA = A->getAs<DependentAddressSpaceType>()) { 21390b57cec5SDimitry Andric // Perform deduction on the pointer type. 2140349cc55cSDimitry Andric if (auto Result = DeduceTemplateArgumentsByTypeMatch( 2141349cc55cSDimitry Andric S, TemplateParams, ASP->getPointeeType(), ASA->getPointeeType(), 2142349cc55cSDimitry Andric Info, Deduced, TDF)) 21430b57cec5SDimitry Andric return Result; 21440b57cec5SDimitry Andric 21450b57cec5SDimitry Andric // Perform deduction on the address space, if we can. 2146349cc55cSDimitry Andric const NonTypeTemplateParmDecl *NTTP = 2147349cc55cSDimitry Andric getDeducedParameterFromExpr(Info, ASP->getAddrSpaceExpr()); 21480b57cec5SDimitry Andric if (!NTTP) 21490b57cec5SDimitry Andric return Sema::TDK_Success; 21500b57cec5SDimitry Andric 21510b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument( 2152349cc55cSDimitry Andric S, TemplateParams, NTTP, ASA->getAddrSpaceExpr(), Info, Deduced); 21530b57cec5SDimitry Andric } 21540b57cec5SDimitry Andric 2155349cc55cSDimitry Andric if (isTargetAddressSpace(A.getAddressSpace())) { 21560b57cec5SDimitry Andric llvm::APSInt ArgAddressSpace(S.Context.getTypeSize(S.Context.IntTy), 21570b57cec5SDimitry Andric false); 2158349cc55cSDimitry Andric ArgAddressSpace = toTargetAddressSpace(A.getAddressSpace()); 21590b57cec5SDimitry Andric 21600b57cec5SDimitry Andric // Perform deduction on the pointer types. 2161349cc55cSDimitry Andric if (auto Result = DeduceTemplateArgumentsByTypeMatch( 2162349cc55cSDimitry Andric S, TemplateParams, ASP->getPointeeType(), 2163349cc55cSDimitry Andric S.Context.removeAddrSpaceQualType(A), Info, Deduced, TDF)) 21640b57cec5SDimitry Andric return Result; 21650b57cec5SDimitry Andric 21660b57cec5SDimitry Andric // Perform deduction on the address space, if we can. 2167349cc55cSDimitry Andric const NonTypeTemplateParmDecl *NTTP = 2168349cc55cSDimitry Andric getDeducedParameterFromExpr(Info, ASP->getAddrSpaceExpr()); 21690b57cec5SDimitry Andric if (!NTTP) 21700b57cec5SDimitry Andric return Sema::TDK_Success; 21710b57cec5SDimitry Andric 21720b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, 21730b57cec5SDimitry Andric ArgAddressSpace, S.Context.IntTy, 21740b57cec5SDimitry Andric true, Info, Deduced); 21750b57cec5SDimitry Andric } 21760b57cec5SDimitry Andric 21770b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 21780b57cec5SDimitry Andric } 21790eae32dcSDimitry Andric case Type::DependentBitInt: { 21800eae32dcSDimitry Andric const auto *IP = P->castAs<DependentBitIntType>(); 21815ffd83dbSDimitry Andric 21820eae32dcSDimitry Andric if (const auto *IA = A->getAs<BitIntType>()) { 2183349cc55cSDimitry Andric if (IP->isUnsigned() != IA->isUnsigned()) 21845ffd83dbSDimitry Andric return Sema::TDK_NonDeducedMismatch; 21855ffd83dbSDimitry Andric 2186e8d8bef9SDimitry Andric const NonTypeTemplateParmDecl *NTTP = 2187349cc55cSDimitry Andric getDeducedParameterFromExpr(Info, IP->getNumBitsExpr()); 21885ffd83dbSDimitry Andric if (!NTTP) 21895ffd83dbSDimitry Andric return Sema::TDK_Success; 21905ffd83dbSDimitry Andric 21915ffd83dbSDimitry Andric llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); 2192349cc55cSDimitry Andric ArgSize = IA->getNumBits(); 21935ffd83dbSDimitry Andric 21945ffd83dbSDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, 21955ffd83dbSDimitry Andric S.Context.IntTy, true, Info, 21965ffd83dbSDimitry Andric Deduced); 21975ffd83dbSDimitry Andric } 21985ffd83dbSDimitry Andric 21990eae32dcSDimitry Andric if (const auto *IA = A->getAs<DependentBitIntType>()) { 2200349cc55cSDimitry Andric if (IP->isUnsigned() != IA->isUnsigned()) 22015ffd83dbSDimitry Andric return Sema::TDK_NonDeducedMismatch; 22025ffd83dbSDimitry Andric return Sema::TDK_Success; 22035ffd83dbSDimitry Andric } 2204349cc55cSDimitry Andric 22055ffd83dbSDimitry Andric return Sema::TDK_NonDeducedMismatch; 22065ffd83dbSDimitry Andric } 22070b57cec5SDimitry Andric 22080b57cec5SDimitry Andric case Type::TypeOfExpr: 22090b57cec5SDimitry Andric case Type::TypeOf: 22100b57cec5SDimitry Andric case Type::DependentName: 22110b57cec5SDimitry Andric case Type::UnresolvedUsing: 22120b57cec5SDimitry Andric case Type::Decltype: 22130b57cec5SDimitry Andric case Type::UnaryTransform: 22140b57cec5SDimitry Andric case Type::DeducedTemplateSpecialization: 22150b57cec5SDimitry Andric case Type::DependentTemplateSpecialization: 22160b57cec5SDimitry Andric case Type::PackExpansion: 22170b57cec5SDimitry Andric case Type::Pipe: 22180b57cec5SDimitry Andric // No template argument deduction for these types 22190b57cec5SDimitry Andric return Sema::TDK_Success; 22200b57cec5SDimitry Andric } 22210b57cec5SDimitry Andric 22220b57cec5SDimitry Andric llvm_unreachable("Invalid Type Class!"); 22230b57cec5SDimitry Andric } 22240b57cec5SDimitry Andric 22250b57cec5SDimitry Andric static Sema::TemplateDeductionResult 2226349cc55cSDimitry Andric DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, 2227349cc55cSDimitry Andric const TemplateArgument &P, TemplateArgument A, 22280b57cec5SDimitry Andric TemplateDeductionInfo &Info, 22290b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 22300b57cec5SDimitry Andric // If the template argument is a pack expansion, perform template argument 22310b57cec5SDimitry Andric // deduction against the pattern of that expansion. This only occurs during 22320b57cec5SDimitry Andric // partial ordering. 2233349cc55cSDimitry Andric if (A.isPackExpansion()) 2234349cc55cSDimitry Andric A = A.getPackExpansionPattern(); 22350b57cec5SDimitry Andric 2236349cc55cSDimitry Andric switch (P.getKind()) { 22370b57cec5SDimitry Andric case TemplateArgument::Null: 22380b57cec5SDimitry Andric llvm_unreachable("Null template argument in parameter list"); 22390b57cec5SDimitry Andric 22400b57cec5SDimitry Andric case TemplateArgument::Type: 2241349cc55cSDimitry Andric if (A.getKind() == TemplateArgument::Type) 2242349cc55cSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 2243349cc55cSDimitry Andric S, TemplateParams, P.getAsType(), A.getAsType(), Info, Deduced, 0); 2244349cc55cSDimitry Andric Info.FirstArg = P; 2245349cc55cSDimitry Andric Info.SecondArg = A; 22460b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 22470b57cec5SDimitry Andric 22480b57cec5SDimitry Andric case TemplateArgument::Template: 2249349cc55cSDimitry Andric if (A.getKind() == TemplateArgument::Template) 2250349cc55cSDimitry Andric return DeduceTemplateArguments(S, TemplateParams, P.getAsTemplate(), 2251349cc55cSDimitry Andric A.getAsTemplate(), Info, Deduced); 2252349cc55cSDimitry Andric Info.FirstArg = P; 2253349cc55cSDimitry Andric Info.SecondArg = A; 22540b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 22550b57cec5SDimitry Andric 22560b57cec5SDimitry Andric case TemplateArgument::TemplateExpansion: 22570b57cec5SDimitry Andric llvm_unreachable("caller should handle pack expansions"); 22580b57cec5SDimitry Andric 22590b57cec5SDimitry Andric case TemplateArgument::Declaration: 2260349cc55cSDimitry Andric if (A.getKind() == TemplateArgument::Declaration && 2261349cc55cSDimitry Andric isSameDeclaration(P.getAsDecl(), A.getAsDecl())) 22620b57cec5SDimitry Andric return Sema::TDK_Success; 22630b57cec5SDimitry Andric 2264349cc55cSDimitry Andric Info.FirstArg = P; 2265349cc55cSDimitry Andric Info.SecondArg = A; 22660b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 22670b57cec5SDimitry Andric 22680b57cec5SDimitry Andric case TemplateArgument::NullPtr: 2269349cc55cSDimitry Andric if (A.getKind() == TemplateArgument::NullPtr && 2270349cc55cSDimitry Andric S.Context.hasSameType(P.getNullPtrType(), A.getNullPtrType())) 22710b57cec5SDimitry Andric return Sema::TDK_Success; 22720b57cec5SDimitry Andric 2273349cc55cSDimitry Andric Info.FirstArg = P; 2274349cc55cSDimitry Andric Info.SecondArg = A; 22750b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 22760b57cec5SDimitry Andric 22770b57cec5SDimitry Andric case TemplateArgument::Integral: 2278349cc55cSDimitry Andric if (A.getKind() == TemplateArgument::Integral) { 2279349cc55cSDimitry Andric if (hasSameExtendedValue(P.getAsIntegral(), A.getAsIntegral())) 22800b57cec5SDimitry Andric return Sema::TDK_Success; 22810b57cec5SDimitry Andric } 2282349cc55cSDimitry Andric Info.FirstArg = P; 2283349cc55cSDimitry Andric Info.SecondArg = A; 22840b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 22850b57cec5SDimitry Andric 22860b57cec5SDimitry Andric case TemplateArgument::Expression: 2287e8d8bef9SDimitry Andric if (const NonTypeTemplateParmDecl *NTTP = 2288349cc55cSDimitry Andric getDeducedParameterFromExpr(Info, P.getAsExpr())) { 2289349cc55cSDimitry Andric if (A.getKind() == TemplateArgument::Integral) 2290349cc55cSDimitry Andric return DeduceNonTypeTemplateArgument( 2291349cc55cSDimitry Andric S, TemplateParams, NTTP, A.getAsIntegral(), A.getIntegralType(), 2292349cc55cSDimitry Andric /*ArrayBound=*/false, Info, Deduced); 2293349cc55cSDimitry Andric if (A.getKind() == TemplateArgument::NullPtr) 22940b57cec5SDimitry Andric return DeduceNullPtrTemplateArgument(S, TemplateParams, NTTP, 2295349cc55cSDimitry Andric A.getNullPtrType(), Info, Deduced); 2296349cc55cSDimitry Andric if (A.getKind() == TemplateArgument::Expression) 22970b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, 2298349cc55cSDimitry Andric A.getAsExpr(), Info, Deduced); 2299349cc55cSDimitry Andric if (A.getKind() == TemplateArgument::Declaration) 2300349cc55cSDimitry Andric return DeduceNonTypeTemplateArgument( 2301349cc55cSDimitry Andric S, TemplateParams, NTTP, A.getAsDecl(), A.getParamTypeForDecl(), 23020b57cec5SDimitry Andric Info, Deduced); 23030b57cec5SDimitry Andric 2304349cc55cSDimitry Andric Info.FirstArg = P; 2305349cc55cSDimitry Andric Info.SecondArg = A; 23060b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 23070b57cec5SDimitry Andric } 23080b57cec5SDimitry Andric 23090b57cec5SDimitry Andric // Can't deduce anything, but that's okay. 23100b57cec5SDimitry Andric return Sema::TDK_Success; 23110b57cec5SDimitry Andric case TemplateArgument::Pack: 23120b57cec5SDimitry Andric llvm_unreachable("Argument packs should be expanded by the caller!"); 23130b57cec5SDimitry Andric } 23140b57cec5SDimitry Andric 23150b57cec5SDimitry Andric llvm_unreachable("Invalid TemplateArgument Kind!"); 23160b57cec5SDimitry Andric } 23170b57cec5SDimitry Andric 23180b57cec5SDimitry Andric /// Determine whether there is a template argument to be used for 23190b57cec5SDimitry Andric /// deduction. 23200b57cec5SDimitry Andric /// 23210b57cec5SDimitry Andric /// This routine "expands" argument packs in-place, overriding its input 23220b57cec5SDimitry Andric /// parameters so that \c Args[ArgIdx] will be the available template argument. 23230b57cec5SDimitry Andric /// 23240b57cec5SDimitry Andric /// \returns true if there is another template argument (which will be at 23250b57cec5SDimitry Andric /// \c Args[ArgIdx]), false otherwise. 23260b57cec5SDimitry Andric static bool hasTemplateArgumentForDeduction(ArrayRef<TemplateArgument> &Args, 23270b57cec5SDimitry Andric unsigned &ArgIdx) { 23280b57cec5SDimitry Andric if (ArgIdx == Args.size()) 23290b57cec5SDimitry Andric return false; 23300b57cec5SDimitry Andric 23310b57cec5SDimitry Andric const TemplateArgument &Arg = Args[ArgIdx]; 23320b57cec5SDimitry Andric if (Arg.getKind() != TemplateArgument::Pack) 23330b57cec5SDimitry Andric return true; 23340b57cec5SDimitry Andric 23350b57cec5SDimitry Andric assert(ArgIdx == Args.size() - 1 && "Pack not at the end of argument list?"); 23360b57cec5SDimitry Andric Args = Arg.pack_elements(); 23370b57cec5SDimitry Andric ArgIdx = 0; 23380b57cec5SDimitry Andric return ArgIdx < Args.size(); 23390b57cec5SDimitry Andric } 23400b57cec5SDimitry Andric 23410b57cec5SDimitry Andric /// Determine whether the given set of template arguments has a pack 23420b57cec5SDimitry Andric /// expansion that is not the last template argument. 23430b57cec5SDimitry Andric static bool hasPackExpansionBeforeEnd(ArrayRef<TemplateArgument> Args) { 23440b57cec5SDimitry Andric bool FoundPackExpansion = false; 23450b57cec5SDimitry Andric for (const auto &A : Args) { 23460b57cec5SDimitry Andric if (FoundPackExpansion) 23470b57cec5SDimitry Andric return true; 23480b57cec5SDimitry Andric 23490b57cec5SDimitry Andric if (A.getKind() == TemplateArgument::Pack) 23500b57cec5SDimitry Andric return hasPackExpansionBeforeEnd(A.pack_elements()); 23510b57cec5SDimitry Andric 23520b57cec5SDimitry Andric // FIXME: If this is a fixed-arity pack expansion from an outer level of 23530b57cec5SDimitry Andric // templates, it should not be treated as a pack expansion. 23540b57cec5SDimitry Andric if (A.isPackExpansion()) 23550b57cec5SDimitry Andric FoundPackExpansion = true; 23560b57cec5SDimitry Andric } 23570b57cec5SDimitry Andric 23580b57cec5SDimitry Andric return false; 23590b57cec5SDimitry Andric } 23600b57cec5SDimitry Andric 23610b57cec5SDimitry Andric static Sema::TemplateDeductionResult 23620b57cec5SDimitry Andric DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, 2363349cc55cSDimitry Andric ArrayRef<TemplateArgument> Ps, 2364349cc55cSDimitry Andric ArrayRef<TemplateArgument> As, 23650b57cec5SDimitry Andric TemplateDeductionInfo &Info, 23660b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 23670b57cec5SDimitry Andric bool NumberOfArgumentsMustMatch) { 23680b57cec5SDimitry Andric // C++0x [temp.deduct.type]p9: 23690b57cec5SDimitry Andric // If the template argument list of P contains a pack expansion that is not 23700b57cec5SDimitry Andric // the last template argument, the entire template argument list is a 23710b57cec5SDimitry Andric // non-deduced context. 2372349cc55cSDimitry Andric if (hasPackExpansionBeforeEnd(Ps)) 23730b57cec5SDimitry Andric return Sema::TDK_Success; 23740b57cec5SDimitry Andric 23750b57cec5SDimitry Andric // C++0x [temp.deduct.type]p9: 23760b57cec5SDimitry Andric // If P has a form that contains <T> or <i>, then each argument Pi of the 23770b57cec5SDimitry Andric // respective template argument list P is compared with the corresponding 23780b57cec5SDimitry Andric // argument Ai of the corresponding template argument list of A. 23790b57cec5SDimitry Andric unsigned ArgIdx = 0, ParamIdx = 0; 2380349cc55cSDimitry Andric for (; hasTemplateArgumentForDeduction(Ps, ParamIdx); ++ParamIdx) { 2381349cc55cSDimitry Andric const TemplateArgument &P = Ps[ParamIdx]; 2382349cc55cSDimitry Andric if (!P.isPackExpansion()) { 23830b57cec5SDimitry Andric // The simple case: deduce template arguments by matching Pi and Ai. 23840b57cec5SDimitry Andric 23850b57cec5SDimitry Andric // Check whether we have enough arguments. 2386349cc55cSDimitry Andric if (!hasTemplateArgumentForDeduction(As, ArgIdx)) 23870b57cec5SDimitry Andric return NumberOfArgumentsMustMatch 23880b57cec5SDimitry Andric ? Sema::TDK_MiscellaneousDeductionFailure 23890b57cec5SDimitry Andric : Sema::TDK_Success; 23900b57cec5SDimitry Andric 23910b57cec5SDimitry Andric // C++1z [temp.deduct.type]p9: 23920b57cec5SDimitry Andric // During partial ordering, if Ai was originally a pack expansion [and] 23930b57cec5SDimitry Andric // Pi is not a pack expansion, template argument deduction fails. 2394349cc55cSDimitry Andric if (As[ArgIdx].isPackExpansion()) 23950b57cec5SDimitry Andric return Sema::TDK_MiscellaneousDeductionFailure; 23960b57cec5SDimitry Andric 23970b57cec5SDimitry Andric // Perform deduction for this Pi/Ai pair. 2398349cc55cSDimitry Andric if (auto Result = DeduceTemplateArguments(S, TemplateParams, P, 2399349cc55cSDimitry Andric As[ArgIdx], Info, Deduced)) 24000b57cec5SDimitry Andric return Result; 24010b57cec5SDimitry Andric 24020b57cec5SDimitry Andric // Move to the next argument. 24030b57cec5SDimitry Andric ++ArgIdx; 24040b57cec5SDimitry Andric continue; 24050b57cec5SDimitry Andric } 24060b57cec5SDimitry Andric 24070b57cec5SDimitry Andric // The parameter is a pack expansion. 24080b57cec5SDimitry Andric 24090b57cec5SDimitry Andric // C++0x [temp.deduct.type]p9: 24100b57cec5SDimitry Andric // If Pi is a pack expansion, then the pattern of Pi is compared with 24110b57cec5SDimitry Andric // each remaining argument in the template argument list of A. Each 24120b57cec5SDimitry Andric // comparison deduces template arguments for subsequent positions in the 24130b57cec5SDimitry Andric // template parameter packs expanded by Pi. 2414349cc55cSDimitry Andric TemplateArgument Pattern = P.getPackExpansionPattern(); 24150b57cec5SDimitry Andric 24160b57cec5SDimitry Andric // Prepare to deduce the packs within the pattern. 24170b57cec5SDimitry Andric PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); 24180b57cec5SDimitry Andric 24190b57cec5SDimitry Andric // Keep track of the deduced template arguments for each parameter pack 24200b57cec5SDimitry Andric // expanded by this pack expansion (the outer index) and for each 24210b57cec5SDimitry Andric // template argument (the inner SmallVectors). 2422349cc55cSDimitry Andric for (; hasTemplateArgumentForDeduction(As, ArgIdx) && 24230b57cec5SDimitry Andric PackScope.hasNextElement(); 24240b57cec5SDimitry Andric ++ArgIdx) { 24250b57cec5SDimitry Andric // Deduce template arguments from the pattern. 2426349cc55cSDimitry Andric if (auto Result = DeduceTemplateArguments(S, TemplateParams, Pattern, 2427349cc55cSDimitry Andric As[ArgIdx], Info, Deduced)) 24280b57cec5SDimitry Andric return Result; 24290b57cec5SDimitry Andric 24300b57cec5SDimitry Andric PackScope.nextPackElement(); 24310b57cec5SDimitry Andric } 24320b57cec5SDimitry Andric 24330b57cec5SDimitry Andric // Build argument packs for each of the parameter packs expanded by this 24340b57cec5SDimitry Andric // pack expansion. 24350b57cec5SDimitry Andric if (auto Result = PackScope.finish()) 24360b57cec5SDimitry Andric return Result; 24370b57cec5SDimitry Andric } 24380b57cec5SDimitry Andric 24390b57cec5SDimitry Andric return Sema::TDK_Success; 24400b57cec5SDimitry Andric } 24410b57cec5SDimitry Andric 24420b57cec5SDimitry Andric static Sema::TemplateDeductionResult 2443349cc55cSDimitry Andric DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, 24440b57cec5SDimitry Andric const TemplateArgumentList &ParamList, 24450b57cec5SDimitry Andric const TemplateArgumentList &ArgList, 24460b57cec5SDimitry Andric TemplateDeductionInfo &Info, 24470b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 24480b57cec5SDimitry Andric return DeduceTemplateArguments(S, TemplateParams, ParamList.asArray(), 24490b57cec5SDimitry Andric ArgList.asArray(), Info, Deduced, 2450349cc55cSDimitry Andric /*NumberOfArgumentsMustMatch=*/false); 24510b57cec5SDimitry Andric } 24520b57cec5SDimitry Andric 24530b57cec5SDimitry Andric /// Determine whether two template arguments are the same. 24540b57cec5SDimitry Andric static bool isSameTemplateArg(ASTContext &Context, 24550b57cec5SDimitry Andric TemplateArgument X, 24560b57cec5SDimitry Andric const TemplateArgument &Y, 2457*bdd1243dSDimitry Andric bool PartialOrdering, 24580b57cec5SDimitry Andric bool PackExpansionMatchesPack = false) { 24590b57cec5SDimitry Andric // If we're checking deduced arguments (X) against original arguments (Y), 24600b57cec5SDimitry Andric // we will have flattened packs to non-expansions in X. 24610b57cec5SDimitry Andric if (PackExpansionMatchesPack && X.isPackExpansion() && !Y.isPackExpansion()) 24620b57cec5SDimitry Andric X = X.getPackExpansionPattern(); 24630b57cec5SDimitry Andric 24640b57cec5SDimitry Andric if (X.getKind() != Y.getKind()) 24650b57cec5SDimitry Andric return false; 24660b57cec5SDimitry Andric 24670b57cec5SDimitry Andric switch (X.getKind()) { 24680b57cec5SDimitry Andric case TemplateArgument::Null: 24690b57cec5SDimitry Andric llvm_unreachable("Comparing NULL template argument"); 24700b57cec5SDimitry Andric 24710b57cec5SDimitry Andric case TemplateArgument::Type: 24720b57cec5SDimitry Andric return Context.getCanonicalType(X.getAsType()) == 24730b57cec5SDimitry Andric Context.getCanonicalType(Y.getAsType()); 24740b57cec5SDimitry Andric 24750b57cec5SDimitry Andric case TemplateArgument::Declaration: 24760b57cec5SDimitry Andric return isSameDeclaration(X.getAsDecl(), Y.getAsDecl()); 24770b57cec5SDimitry Andric 24780b57cec5SDimitry Andric case TemplateArgument::NullPtr: 24790b57cec5SDimitry Andric return Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType()); 24800b57cec5SDimitry Andric 24810b57cec5SDimitry Andric case TemplateArgument::Template: 24820b57cec5SDimitry Andric case TemplateArgument::TemplateExpansion: 24830b57cec5SDimitry Andric return Context.getCanonicalTemplateName( 24840b57cec5SDimitry Andric X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() == 24850b57cec5SDimitry Andric Context.getCanonicalTemplateName( 24860b57cec5SDimitry Andric Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer(); 24870b57cec5SDimitry Andric 24880b57cec5SDimitry Andric case TemplateArgument::Integral: 24890b57cec5SDimitry Andric return hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()); 24900b57cec5SDimitry Andric 24910b57cec5SDimitry Andric case TemplateArgument::Expression: { 24920b57cec5SDimitry Andric llvm::FoldingSetNodeID XID, YID; 24930b57cec5SDimitry Andric X.getAsExpr()->Profile(XID, Context, true); 24940b57cec5SDimitry Andric Y.getAsExpr()->Profile(YID, Context, true); 24950b57cec5SDimitry Andric return XID == YID; 24960b57cec5SDimitry Andric } 24970b57cec5SDimitry Andric 2498*bdd1243dSDimitry Andric case TemplateArgument::Pack: { 2499*bdd1243dSDimitry Andric unsigned PackIterationSize = X.pack_size(); 2500*bdd1243dSDimitry Andric if (X.pack_size() != Y.pack_size()) { 2501*bdd1243dSDimitry Andric if (!PartialOrdering) 25020b57cec5SDimitry Andric return false; 25030b57cec5SDimitry Andric 2504*bdd1243dSDimitry Andric // C++0x [temp.deduct.type]p9: 2505*bdd1243dSDimitry Andric // During partial ordering, if Ai was originally a pack expansion: 2506*bdd1243dSDimitry Andric // - if P does not contain a template argument corresponding to Ai 2507*bdd1243dSDimitry Andric // then Ai is ignored; 2508*bdd1243dSDimitry Andric bool XHasMoreArg = X.pack_size() > Y.pack_size(); 2509*bdd1243dSDimitry Andric if (!(XHasMoreArg && X.pack_elements().back().isPackExpansion()) && 2510*bdd1243dSDimitry Andric !(!XHasMoreArg && Y.pack_elements().back().isPackExpansion())) 25110b57cec5SDimitry Andric return false; 25120b57cec5SDimitry Andric 2513*bdd1243dSDimitry Andric if (XHasMoreArg) 2514*bdd1243dSDimitry Andric PackIterationSize = Y.pack_size(); 2515*bdd1243dSDimitry Andric } 2516*bdd1243dSDimitry Andric 2517*bdd1243dSDimitry Andric ArrayRef<TemplateArgument> XP = X.pack_elements(); 2518*bdd1243dSDimitry Andric ArrayRef<TemplateArgument> YP = Y.pack_elements(); 2519*bdd1243dSDimitry Andric for (unsigned i = 0; i < PackIterationSize; ++i) 2520*bdd1243dSDimitry Andric if (!isSameTemplateArg(Context, XP[i], YP[i], PartialOrdering, 2521*bdd1243dSDimitry Andric PackExpansionMatchesPack)) 2522*bdd1243dSDimitry Andric return false; 25230b57cec5SDimitry Andric return true; 25240b57cec5SDimitry Andric } 2525*bdd1243dSDimitry Andric } 25260b57cec5SDimitry Andric 25270b57cec5SDimitry Andric llvm_unreachable("Invalid TemplateArgument Kind!"); 25280b57cec5SDimitry Andric } 25290b57cec5SDimitry Andric 25300b57cec5SDimitry Andric /// Allocate a TemplateArgumentLoc where all locations have 25310b57cec5SDimitry Andric /// been initialized to the given location. 25320b57cec5SDimitry Andric /// 25330b57cec5SDimitry Andric /// \param Arg The template argument we are producing template argument 25340b57cec5SDimitry Andric /// location information for. 25350b57cec5SDimitry Andric /// 25360b57cec5SDimitry Andric /// \param NTTPType For a declaration template argument, the type of 25370b57cec5SDimitry Andric /// the non-type template parameter that corresponds to this template 25380b57cec5SDimitry Andric /// argument. Can be null if no type sugar is available to add to the 25390b57cec5SDimitry Andric /// type from the template argument. 25400b57cec5SDimitry Andric /// 25410b57cec5SDimitry Andric /// \param Loc The source location to use for the resulting template 25420b57cec5SDimitry Andric /// argument. 25430b57cec5SDimitry Andric TemplateArgumentLoc 25440b57cec5SDimitry Andric Sema::getTrivialTemplateArgumentLoc(const TemplateArgument &Arg, 25450b57cec5SDimitry Andric QualType NTTPType, SourceLocation Loc) { 25460b57cec5SDimitry Andric switch (Arg.getKind()) { 25470b57cec5SDimitry Andric case TemplateArgument::Null: 25480b57cec5SDimitry Andric llvm_unreachable("Can't get a NULL template argument here"); 25490b57cec5SDimitry Andric 25500b57cec5SDimitry Andric case TemplateArgument::Type: 25510b57cec5SDimitry Andric return TemplateArgumentLoc( 25520b57cec5SDimitry Andric Arg, Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc)); 25530b57cec5SDimitry Andric 25540b57cec5SDimitry Andric case TemplateArgument::Declaration: { 25550b57cec5SDimitry Andric if (NTTPType.isNull()) 25560b57cec5SDimitry Andric NTTPType = Arg.getParamTypeForDecl(); 25570b57cec5SDimitry Andric Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) 25580b57cec5SDimitry Andric .getAs<Expr>(); 25590b57cec5SDimitry Andric return TemplateArgumentLoc(TemplateArgument(E), E); 25600b57cec5SDimitry Andric } 25610b57cec5SDimitry Andric 25620b57cec5SDimitry Andric case TemplateArgument::NullPtr: { 25630b57cec5SDimitry Andric if (NTTPType.isNull()) 25640b57cec5SDimitry Andric NTTPType = Arg.getNullPtrType(); 25650b57cec5SDimitry Andric Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) 25660b57cec5SDimitry Andric .getAs<Expr>(); 25670b57cec5SDimitry Andric return TemplateArgumentLoc(TemplateArgument(NTTPType, /*isNullPtr*/true), 25680b57cec5SDimitry Andric E); 25690b57cec5SDimitry Andric } 25700b57cec5SDimitry Andric 25710b57cec5SDimitry Andric case TemplateArgument::Integral: { 25720b57cec5SDimitry Andric Expr *E = 25730b57cec5SDimitry Andric BuildExpressionFromIntegralTemplateArgument(Arg, Loc).getAs<Expr>(); 25740b57cec5SDimitry Andric return TemplateArgumentLoc(TemplateArgument(E), E); 25750b57cec5SDimitry Andric } 25760b57cec5SDimitry Andric 25770b57cec5SDimitry Andric case TemplateArgument::Template: 25780b57cec5SDimitry Andric case TemplateArgument::TemplateExpansion: { 25790b57cec5SDimitry Andric NestedNameSpecifierLocBuilder Builder; 258013138422SDimitry Andric TemplateName Template = Arg.getAsTemplateOrTemplatePattern(); 25810b57cec5SDimitry Andric if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) 25820b57cec5SDimitry Andric Builder.MakeTrivial(Context, DTN->getQualifier(), Loc); 25830b57cec5SDimitry Andric else if (QualifiedTemplateName *QTN = 25840b57cec5SDimitry Andric Template.getAsQualifiedTemplateName()) 25850b57cec5SDimitry Andric Builder.MakeTrivial(Context, QTN->getQualifier(), Loc); 25860b57cec5SDimitry Andric 25870b57cec5SDimitry Andric if (Arg.getKind() == TemplateArgument::Template) 2588e8d8bef9SDimitry Andric return TemplateArgumentLoc(Context, Arg, 2589e8d8bef9SDimitry Andric Builder.getWithLocInContext(Context), Loc); 25900b57cec5SDimitry Andric 2591e8d8bef9SDimitry Andric return TemplateArgumentLoc( 2592e8d8bef9SDimitry Andric Context, Arg, Builder.getWithLocInContext(Context), Loc, Loc); 25930b57cec5SDimitry Andric } 25940b57cec5SDimitry Andric 25950b57cec5SDimitry Andric case TemplateArgument::Expression: 25960b57cec5SDimitry Andric return TemplateArgumentLoc(Arg, Arg.getAsExpr()); 25970b57cec5SDimitry Andric 25980b57cec5SDimitry Andric case TemplateArgument::Pack: 25990b57cec5SDimitry Andric return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo()); 26000b57cec5SDimitry Andric } 26010b57cec5SDimitry Andric 26020b57cec5SDimitry Andric llvm_unreachable("Invalid TemplateArgument Kind!"); 26030b57cec5SDimitry Andric } 26040b57cec5SDimitry Andric 2605480093f4SDimitry Andric TemplateArgumentLoc 260613138422SDimitry Andric Sema::getIdentityTemplateArgumentLoc(NamedDecl *TemplateParm, 2607480093f4SDimitry Andric SourceLocation Location) { 2608480093f4SDimitry Andric return getTrivialTemplateArgumentLoc( 260913138422SDimitry Andric Context.getInjectedTemplateArg(TemplateParm), QualType(), Location); 2610480093f4SDimitry Andric } 2611480093f4SDimitry Andric 26120b57cec5SDimitry Andric /// Convert the given deduced template argument and add it to the set of 26130b57cec5SDimitry Andric /// fully-converted template arguments. 2614*bdd1243dSDimitry Andric static bool ConvertDeducedTemplateArgument( 2615*bdd1243dSDimitry Andric Sema &S, NamedDecl *Param, DeducedTemplateArgument Arg, NamedDecl *Template, 2616*bdd1243dSDimitry Andric TemplateDeductionInfo &Info, bool IsDeduced, 2617*bdd1243dSDimitry Andric SmallVectorImpl<TemplateArgument> &SugaredOutput, 2618*bdd1243dSDimitry Andric SmallVectorImpl<TemplateArgument> &CanonicalOutput) { 26190b57cec5SDimitry Andric auto ConvertArg = [&](DeducedTemplateArgument Arg, 26200b57cec5SDimitry Andric unsigned ArgumentPackIndex) { 26210b57cec5SDimitry Andric // Convert the deduced template argument into a template 26220b57cec5SDimitry Andric // argument that we can check, almost as if the user had written 26230b57cec5SDimitry Andric // the template argument explicitly. 26240b57cec5SDimitry Andric TemplateArgumentLoc ArgLoc = 26250b57cec5SDimitry Andric S.getTrivialTemplateArgumentLoc(Arg, QualType(), Info.getLocation()); 26260b57cec5SDimitry Andric 26270b57cec5SDimitry Andric // Check the template argument, converting it as necessary. 26280b57cec5SDimitry Andric return S.CheckTemplateArgument( 26290b57cec5SDimitry Andric Param, ArgLoc, Template, Template->getLocation(), 2630*bdd1243dSDimitry Andric Template->getSourceRange().getEnd(), ArgumentPackIndex, SugaredOutput, 2631*bdd1243dSDimitry Andric CanonicalOutput, 26320b57cec5SDimitry Andric IsDeduced 26330b57cec5SDimitry Andric ? (Arg.wasDeducedFromArrayBound() ? Sema::CTAK_DeducedFromArrayBound 26340b57cec5SDimitry Andric : Sema::CTAK_Deduced) 26350b57cec5SDimitry Andric : Sema::CTAK_Specified); 26360b57cec5SDimitry Andric }; 26370b57cec5SDimitry Andric 26380b57cec5SDimitry Andric if (Arg.getKind() == TemplateArgument::Pack) { 26390b57cec5SDimitry Andric // This is a template argument pack, so check each of its arguments against 26400b57cec5SDimitry Andric // the template parameter. 2641*bdd1243dSDimitry Andric SmallVector<TemplateArgument, 2> SugaredPackedArgsBuilder, 2642*bdd1243dSDimitry Andric CanonicalPackedArgsBuilder; 26430b57cec5SDimitry Andric for (const auto &P : Arg.pack_elements()) { 26440b57cec5SDimitry Andric // When converting the deduced template argument, append it to the 26450b57cec5SDimitry Andric // general output list. We need to do this so that the template argument 26460b57cec5SDimitry Andric // checking logic has all of the prior template arguments available. 26470b57cec5SDimitry Andric DeducedTemplateArgument InnerArg(P); 26480b57cec5SDimitry Andric InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound()); 26490b57cec5SDimitry Andric assert(InnerArg.getKind() != TemplateArgument::Pack && 26500b57cec5SDimitry Andric "deduced nested pack"); 26510b57cec5SDimitry Andric if (P.isNull()) { 26520b57cec5SDimitry Andric // We deduced arguments for some elements of this pack, but not for 26530b57cec5SDimitry Andric // all of them. This happens if we get a conditionally-non-deduced 26540b57cec5SDimitry Andric // context in a pack expansion (such as an overload set in one of the 26550b57cec5SDimitry Andric // arguments). 26560b57cec5SDimitry Andric S.Diag(Param->getLocation(), 26570b57cec5SDimitry Andric diag::err_template_arg_deduced_incomplete_pack) 26580b57cec5SDimitry Andric << Arg << Param; 26590b57cec5SDimitry Andric return true; 26600b57cec5SDimitry Andric } 2661*bdd1243dSDimitry Andric if (ConvertArg(InnerArg, SugaredPackedArgsBuilder.size())) 26620b57cec5SDimitry Andric return true; 26630b57cec5SDimitry Andric 26640b57cec5SDimitry Andric // Move the converted template argument into our argument pack. 2665*bdd1243dSDimitry Andric SugaredPackedArgsBuilder.push_back(SugaredOutput.pop_back_val()); 2666*bdd1243dSDimitry Andric CanonicalPackedArgsBuilder.push_back(CanonicalOutput.pop_back_val()); 26670b57cec5SDimitry Andric } 26680b57cec5SDimitry Andric 26690b57cec5SDimitry Andric // If the pack is empty, we still need to substitute into the parameter 26700b57cec5SDimitry Andric // itself, in case that substitution fails. 2671*bdd1243dSDimitry Andric if (SugaredPackedArgsBuilder.empty()) { 26720b57cec5SDimitry Andric LocalInstantiationScope Scope(S); 2673*bdd1243dSDimitry Andric MultiLevelTemplateArgumentList Args(Template, SugaredOutput, 2674*bdd1243dSDimitry Andric /*Final=*/true); 26750b57cec5SDimitry Andric 26760b57cec5SDimitry Andric if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { 26770b57cec5SDimitry Andric Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, 2678*bdd1243dSDimitry Andric NTTP, SugaredOutput, 26790b57cec5SDimitry Andric Template->getSourceRange()); 26800b57cec5SDimitry Andric if (Inst.isInvalid() || 26810b57cec5SDimitry Andric S.SubstType(NTTP->getType(), Args, NTTP->getLocation(), 26820b57cec5SDimitry Andric NTTP->getDeclName()).isNull()) 26830b57cec5SDimitry Andric return true; 26840b57cec5SDimitry Andric } else if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param)) { 26850b57cec5SDimitry Andric Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, 2686*bdd1243dSDimitry Andric TTP, SugaredOutput, 26870b57cec5SDimitry Andric Template->getSourceRange()); 26880b57cec5SDimitry Andric if (Inst.isInvalid() || !S.SubstDecl(TTP, S.CurContext, Args)) 26890b57cec5SDimitry Andric return true; 26900b57cec5SDimitry Andric } 26910b57cec5SDimitry Andric // For type parameters, no substitution is ever required. 26920b57cec5SDimitry Andric } 26930b57cec5SDimitry Andric 26940b57cec5SDimitry Andric // Create the resulting argument pack. 2695*bdd1243dSDimitry Andric SugaredOutput.push_back( 2696*bdd1243dSDimitry Andric TemplateArgument::CreatePackCopy(S.Context, SugaredPackedArgsBuilder)); 2697*bdd1243dSDimitry Andric CanonicalOutput.push_back(TemplateArgument::CreatePackCopy( 2698*bdd1243dSDimitry Andric S.Context, CanonicalPackedArgsBuilder)); 26990b57cec5SDimitry Andric return false; 27000b57cec5SDimitry Andric } 27010b57cec5SDimitry Andric 27020b57cec5SDimitry Andric return ConvertArg(Arg, 0); 27030b57cec5SDimitry Andric } 27040b57cec5SDimitry Andric 27050b57cec5SDimitry Andric // FIXME: This should not be a template, but 27060b57cec5SDimitry Andric // ClassTemplatePartialSpecializationDecl sadly does not derive from 27070b57cec5SDimitry Andric // TemplateDecl. 27080b57cec5SDimitry Andric template <typename TemplateDeclT> 27090b57cec5SDimitry Andric static Sema::TemplateDeductionResult ConvertDeducedTemplateArguments( 27100b57cec5SDimitry Andric Sema &S, TemplateDeclT *Template, bool IsDeduced, 27110b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 2712*bdd1243dSDimitry Andric TemplateDeductionInfo &Info, 2713*bdd1243dSDimitry Andric SmallVectorImpl<TemplateArgument> &SugaredBuilder, 2714*bdd1243dSDimitry Andric SmallVectorImpl<TemplateArgument> &CanonicalBuilder, 27150b57cec5SDimitry Andric LocalInstantiationScope *CurrentInstantiationScope = nullptr, 27160b57cec5SDimitry Andric unsigned NumAlreadyConverted = 0, bool PartialOverloading = false) { 27170b57cec5SDimitry Andric TemplateParameterList *TemplateParams = Template->getTemplateParameters(); 27180b57cec5SDimitry Andric 27190b57cec5SDimitry Andric for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { 27200b57cec5SDimitry Andric NamedDecl *Param = TemplateParams->getParam(I); 27210b57cec5SDimitry Andric 27220b57cec5SDimitry Andric // C++0x [temp.arg.explicit]p3: 27230b57cec5SDimitry Andric // A trailing template parameter pack (14.5.3) not otherwise deduced will 27240b57cec5SDimitry Andric // be deduced to an empty sequence of template arguments. 27250b57cec5SDimitry Andric // FIXME: Where did the word "trailing" come from? 27260b57cec5SDimitry Andric if (Deduced[I].isNull() && Param->isTemplateParameterPack()) { 2727480093f4SDimitry Andric if (auto Result = 2728480093f4SDimitry Andric PackDeductionScope(S, TemplateParams, Deduced, Info, I).finish()) 27290b57cec5SDimitry Andric return Result; 27300b57cec5SDimitry Andric } 27310b57cec5SDimitry Andric 27320b57cec5SDimitry Andric if (!Deduced[I].isNull()) { 27330b57cec5SDimitry Andric if (I < NumAlreadyConverted) { 27340b57cec5SDimitry Andric // We may have had explicitly-specified template arguments for a 27350b57cec5SDimitry Andric // template parameter pack (that may or may not have been extended 27360b57cec5SDimitry Andric // via additional deduced arguments). 27370b57cec5SDimitry Andric if (Param->isParameterPack() && CurrentInstantiationScope && 27380b57cec5SDimitry Andric CurrentInstantiationScope->getPartiallySubstitutedPack() == Param) { 27390b57cec5SDimitry Andric // Forget the partially-substituted pack; its substitution is now 27400b57cec5SDimitry Andric // complete. 27410b57cec5SDimitry Andric CurrentInstantiationScope->ResetPartiallySubstitutedPack(); 27420b57cec5SDimitry Andric // We still need to check the argument in case it was extended by 27430b57cec5SDimitry Andric // deduction. 27440b57cec5SDimitry Andric } else { 27450b57cec5SDimitry Andric // We have already fully type-checked and converted this 27460b57cec5SDimitry Andric // argument, because it was explicitly-specified. Just record the 27470b57cec5SDimitry Andric // presence of this argument. 2748*bdd1243dSDimitry Andric SugaredBuilder.push_back(Deduced[I]); 2749*bdd1243dSDimitry Andric CanonicalBuilder.push_back( 2750*bdd1243dSDimitry Andric S.Context.getCanonicalTemplateArgument(Deduced[I])); 27510b57cec5SDimitry Andric continue; 27520b57cec5SDimitry Andric } 27530b57cec5SDimitry Andric } 27540b57cec5SDimitry Andric 27550b57cec5SDimitry Andric // We may have deduced this argument, so it still needs to be 27560b57cec5SDimitry Andric // checked and converted. 27570b57cec5SDimitry Andric if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Template, Info, 2758*bdd1243dSDimitry Andric IsDeduced, SugaredBuilder, 2759*bdd1243dSDimitry Andric CanonicalBuilder)) { 27600b57cec5SDimitry Andric Info.Param = makeTemplateParameter(Param); 27610b57cec5SDimitry Andric // FIXME: These template arguments are temporary. Free them! 2762*bdd1243dSDimitry Andric Info.reset( 2763*bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(S.Context, SugaredBuilder), 2764*bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(S.Context, CanonicalBuilder)); 27650b57cec5SDimitry Andric return Sema::TDK_SubstitutionFailure; 27660b57cec5SDimitry Andric } 27670b57cec5SDimitry Andric 27680b57cec5SDimitry Andric continue; 27690b57cec5SDimitry Andric } 27700b57cec5SDimitry Andric 27710b57cec5SDimitry Andric // Substitute into the default template argument, if available. 27720b57cec5SDimitry Andric bool HasDefaultArg = false; 27730b57cec5SDimitry Andric TemplateDecl *TD = dyn_cast<TemplateDecl>(Template); 27740b57cec5SDimitry Andric if (!TD) { 27750b57cec5SDimitry Andric assert(isa<ClassTemplatePartialSpecializationDecl>(Template) || 27760b57cec5SDimitry Andric isa<VarTemplatePartialSpecializationDecl>(Template)); 27770b57cec5SDimitry Andric return Sema::TDK_Incomplete; 27780b57cec5SDimitry Andric } 27790b57cec5SDimitry Andric 2780349cc55cSDimitry Andric TemplateArgumentLoc DefArg; 2781349cc55cSDimitry Andric { 2782349cc55cSDimitry Andric Qualifiers ThisTypeQuals; 2783349cc55cSDimitry Andric CXXRecordDecl *ThisContext = nullptr; 2784349cc55cSDimitry Andric if (auto *Rec = dyn_cast<CXXRecordDecl>(TD->getDeclContext())) 2785349cc55cSDimitry Andric if (Rec->isLambda()) 2786349cc55cSDimitry Andric if (auto *Method = dyn_cast<CXXMethodDecl>(Rec->getDeclContext())) { 2787349cc55cSDimitry Andric ThisContext = Method->getParent(); 2788349cc55cSDimitry Andric ThisTypeQuals = Method->getMethodQualifiers(); 2789349cc55cSDimitry Andric } 2790349cc55cSDimitry Andric 2791349cc55cSDimitry Andric Sema::CXXThisScopeRAII ThisScope(S, ThisContext, ThisTypeQuals, 2792349cc55cSDimitry Andric S.getLangOpts().CPlusPlus17); 2793349cc55cSDimitry Andric 2794349cc55cSDimitry Andric DefArg = S.SubstDefaultTemplateArgumentIfAvailable( 2795*bdd1243dSDimitry Andric TD, TD->getLocation(), TD->getSourceRange().getEnd(), Param, 2796*bdd1243dSDimitry Andric SugaredBuilder, CanonicalBuilder, HasDefaultArg); 2797349cc55cSDimitry Andric } 27980b57cec5SDimitry Andric 27990b57cec5SDimitry Andric // If there was no default argument, deduction is incomplete. 28000b57cec5SDimitry Andric if (DefArg.getArgument().isNull()) { 28010b57cec5SDimitry Andric Info.Param = makeTemplateParameter( 28020b57cec5SDimitry Andric const_cast<NamedDecl *>(TemplateParams->getParam(I))); 2803*bdd1243dSDimitry Andric Info.reset(TemplateArgumentList::CreateCopy(S.Context, SugaredBuilder), 2804*bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(S.Context, CanonicalBuilder)); 28050b57cec5SDimitry Andric if (PartialOverloading) break; 28060b57cec5SDimitry Andric 28070b57cec5SDimitry Andric return HasDefaultArg ? Sema::TDK_SubstitutionFailure 28080b57cec5SDimitry Andric : Sema::TDK_Incomplete; 28090b57cec5SDimitry Andric } 28100b57cec5SDimitry Andric 28110b57cec5SDimitry Andric // Check whether we can actually use the default argument. 2812*bdd1243dSDimitry Andric if (S.CheckTemplateArgument( 2813*bdd1243dSDimitry Andric Param, DefArg, TD, TD->getLocation(), TD->getSourceRange().getEnd(), 2814*bdd1243dSDimitry Andric 0, SugaredBuilder, CanonicalBuilder, Sema::CTAK_Specified)) { 28150b57cec5SDimitry Andric Info.Param = makeTemplateParameter( 28160b57cec5SDimitry Andric const_cast<NamedDecl *>(TemplateParams->getParam(I))); 28170b57cec5SDimitry Andric // FIXME: These template arguments are temporary. Free them! 2818*bdd1243dSDimitry Andric Info.reset(TemplateArgumentList::CreateCopy(S.Context, SugaredBuilder), 2819*bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(S.Context, CanonicalBuilder)); 28200b57cec5SDimitry Andric return Sema::TDK_SubstitutionFailure; 28210b57cec5SDimitry Andric } 28220b57cec5SDimitry Andric 28230b57cec5SDimitry Andric // If we get here, we successfully used the default template argument. 28240b57cec5SDimitry Andric } 28250b57cec5SDimitry Andric 28260b57cec5SDimitry Andric return Sema::TDK_Success; 28270b57cec5SDimitry Andric } 28280b57cec5SDimitry Andric 28290b57cec5SDimitry Andric static DeclContext *getAsDeclContextOrEnclosing(Decl *D) { 28300b57cec5SDimitry Andric if (auto *DC = dyn_cast<DeclContext>(D)) 28310b57cec5SDimitry Andric return DC; 28320b57cec5SDimitry Andric return D->getDeclContext(); 28330b57cec5SDimitry Andric } 28340b57cec5SDimitry Andric 28350b57cec5SDimitry Andric template<typename T> struct IsPartialSpecialization { 28360b57cec5SDimitry Andric static constexpr bool value = false; 28370b57cec5SDimitry Andric }; 28380b57cec5SDimitry Andric template<> 28390b57cec5SDimitry Andric struct IsPartialSpecialization<ClassTemplatePartialSpecializationDecl> { 28400b57cec5SDimitry Andric static constexpr bool value = true; 28410b57cec5SDimitry Andric }; 28420b57cec5SDimitry Andric template<> 28430b57cec5SDimitry Andric struct IsPartialSpecialization<VarTemplatePartialSpecializationDecl> { 28440b57cec5SDimitry Andric static constexpr bool value = true; 28450b57cec5SDimitry Andric }; 2846*bdd1243dSDimitry Andric template <typename TemplateDeclT> 2847*bdd1243dSDimitry Andric static bool DeducedArgsNeedReplacement(TemplateDeclT *Template) { 2848*bdd1243dSDimitry Andric return false; 2849*bdd1243dSDimitry Andric } 2850*bdd1243dSDimitry Andric template <> 2851*bdd1243dSDimitry Andric bool DeducedArgsNeedReplacement<VarTemplatePartialSpecializationDecl>( 2852*bdd1243dSDimitry Andric VarTemplatePartialSpecializationDecl *Spec) { 2853*bdd1243dSDimitry Andric return !Spec->isClassScopeExplicitSpecialization(); 2854*bdd1243dSDimitry Andric } 2855*bdd1243dSDimitry Andric template <> 2856*bdd1243dSDimitry Andric bool DeducedArgsNeedReplacement<ClassTemplatePartialSpecializationDecl>( 2857*bdd1243dSDimitry Andric ClassTemplatePartialSpecializationDecl *Spec) { 2858*bdd1243dSDimitry Andric return !Spec->isClassScopeExplicitSpecialization(); 2859*bdd1243dSDimitry Andric } 28600b57cec5SDimitry Andric 2861480093f4SDimitry Andric template <typename TemplateDeclT> 2862480093f4SDimitry Andric static Sema::TemplateDeductionResult 2863480093f4SDimitry Andric CheckDeducedArgumentConstraints(Sema &S, TemplateDeclT *Template, 2864*bdd1243dSDimitry Andric ArrayRef<TemplateArgument> SugaredDeducedArgs, 2865*bdd1243dSDimitry Andric ArrayRef<TemplateArgument> CanonicalDeducedArgs, 2866480093f4SDimitry Andric TemplateDeductionInfo &Info) { 2867480093f4SDimitry Andric llvm::SmallVector<const Expr *, 3> AssociatedConstraints; 2868480093f4SDimitry Andric Template->getAssociatedConstraints(AssociatedConstraints); 2869*bdd1243dSDimitry Andric 2870*bdd1243dSDimitry Andric bool NeedsReplacement = DeducedArgsNeedReplacement(Template); 2871*bdd1243dSDimitry Andric TemplateArgumentList DeducedTAL{TemplateArgumentList::OnStack, 2872*bdd1243dSDimitry Andric CanonicalDeducedArgs}; 2873*bdd1243dSDimitry Andric 2874*bdd1243dSDimitry Andric MultiLevelTemplateArgumentList MLTAL = S.getTemplateInstantiationArgs( 2875*bdd1243dSDimitry Andric Template, /*Final=*/false, 2876*bdd1243dSDimitry Andric /*InnerMost=*/NeedsReplacement ? nullptr : &DeducedTAL, 2877*bdd1243dSDimitry Andric /*RelativeToPrimary=*/true, /*Pattern=*/ 2878*bdd1243dSDimitry Andric nullptr, /*ForConstraintInstantiation=*/true); 2879*bdd1243dSDimitry Andric 2880*bdd1243dSDimitry Andric // getTemplateInstantiationArgs picks up the non-deduced version of the 2881*bdd1243dSDimitry Andric // template args when this is a variable template partial specialization and 2882*bdd1243dSDimitry Andric // not class-scope explicit specialization, so replace with Deduced Args 2883*bdd1243dSDimitry Andric // instead of adding to inner-most. 2884*bdd1243dSDimitry Andric if (NeedsReplacement) 2885*bdd1243dSDimitry Andric MLTAL.replaceInnermostTemplateArguments(CanonicalDeducedArgs); 2886*bdd1243dSDimitry Andric 2887*bdd1243dSDimitry Andric if (S.CheckConstraintSatisfaction(Template, AssociatedConstraints, MLTAL, 2888*bdd1243dSDimitry Andric Info.getLocation(), 2889480093f4SDimitry Andric Info.AssociatedConstraintsSatisfaction) || 2890480093f4SDimitry Andric !Info.AssociatedConstraintsSatisfaction.IsSatisfied) { 2891*bdd1243dSDimitry Andric Info.reset( 2892*bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(S.Context, SugaredDeducedArgs), 2893*bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(S.Context, CanonicalDeducedArgs)); 2894480093f4SDimitry Andric return Sema::TDK_ConstraintsNotSatisfied; 2895480093f4SDimitry Andric } 2896480093f4SDimitry Andric return Sema::TDK_Success; 2897480093f4SDimitry Andric } 2898480093f4SDimitry Andric 28990b57cec5SDimitry Andric /// Complete template argument deduction for a partial specialization. 29000b57cec5SDimitry Andric template <typename T> 29015ffd83dbSDimitry Andric static std::enable_if_t<IsPartialSpecialization<T>::value, 29025ffd83dbSDimitry Andric Sema::TemplateDeductionResult> 29030b57cec5SDimitry Andric FinishTemplateArgumentDeduction( 29040b57cec5SDimitry Andric Sema &S, T *Partial, bool IsPartialOrdering, 29050b57cec5SDimitry Andric const TemplateArgumentList &TemplateArgs, 29060b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 29070b57cec5SDimitry Andric TemplateDeductionInfo &Info) { 29080b57cec5SDimitry Andric // Unevaluated SFINAE context. 29090b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 29100b57cec5SDimitry Andric S, Sema::ExpressionEvaluationContext::Unevaluated); 29110b57cec5SDimitry Andric Sema::SFINAETrap Trap(S); 29120b57cec5SDimitry Andric 29130b57cec5SDimitry Andric Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Partial)); 29140b57cec5SDimitry Andric 29150b57cec5SDimitry Andric // C++ [temp.deduct.type]p2: 29160b57cec5SDimitry Andric // [...] or if any template argument remains neither deduced nor 29170b57cec5SDimitry Andric // explicitly specified, template argument deduction fails. 2918*bdd1243dSDimitry Andric SmallVector<TemplateArgument, 4> SugaredBuilder, CanonicalBuilder; 29190b57cec5SDimitry Andric if (auto Result = ConvertDeducedTemplateArguments( 2920*bdd1243dSDimitry Andric S, Partial, IsPartialOrdering, Deduced, Info, SugaredBuilder, 2921*bdd1243dSDimitry Andric CanonicalBuilder)) 29220b57cec5SDimitry Andric return Result; 29230b57cec5SDimitry Andric 29240b57cec5SDimitry Andric // Form the template argument list from the deduced template arguments. 2925*bdd1243dSDimitry Andric TemplateArgumentList *SugaredDeducedArgumentList = 2926*bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(S.Context, SugaredBuilder); 2927*bdd1243dSDimitry Andric TemplateArgumentList *CanonicalDeducedArgumentList = 2928*bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(S.Context, CanonicalBuilder); 29290b57cec5SDimitry Andric 2930*bdd1243dSDimitry Andric Info.reset(SugaredDeducedArgumentList, CanonicalDeducedArgumentList); 29310b57cec5SDimitry Andric 29320b57cec5SDimitry Andric // Substitute the deduced template arguments into the template 29330b57cec5SDimitry Andric // arguments of the class template partial specialization, and 29340b57cec5SDimitry Andric // verify that the instantiated template arguments are both valid 29350b57cec5SDimitry Andric // and are equivalent to the template arguments originally provided 29360b57cec5SDimitry Andric // to the class template. 29370b57cec5SDimitry Andric LocalInstantiationScope InstScope(S); 29380b57cec5SDimitry Andric auto *Template = Partial->getSpecializedTemplate(); 29390b57cec5SDimitry Andric const ASTTemplateArgumentListInfo *PartialTemplArgInfo = 29400b57cec5SDimitry Andric Partial->getTemplateArgsAsWritten(); 29410b57cec5SDimitry Andric 29420b57cec5SDimitry Andric TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, 29430b57cec5SDimitry Andric PartialTemplArgInfo->RAngleLoc); 29440b57cec5SDimitry Andric 2945*bdd1243dSDimitry Andric if (S.SubstTemplateArguments(PartialTemplArgInfo->arguments(), 2946*bdd1243dSDimitry Andric MultiLevelTemplateArgumentList(Partial, 2947*bdd1243dSDimitry Andric SugaredBuilder, 2948*bdd1243dSDimitry Andric /*Final=*/true), 2949*bdd1243dSDimitry Andric InstArgs)) { 29500b57cec5SDimitry Andric unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; 29510b57cec5SDimitry Andric if (ParamIdx >= Partial->getTemplateParameters()->size()) 29520b57cec5SDimitry Andric ParamIdx = Partial->getTemplateParameters()->size() - 1; 29530b57cec5SDimitry Andric 29540b57cec5SDimitry Andric Decl *Param = const_cast<NamedDecl *>( 29550b57cec5SDimitry Andric Partial->getTemplateParameters()->getParam(ParamIdx)); 29560b57cec5SDimitry Andric Info.Param = makeTemplateParameter(Param); 2957349cc55cSDimitry Andric Info.FirstArg = (*PartialTemplArgInfo)[ArgIdx].getArgument(); 29580b57cec5SDimitry Andric return Sema::TDK_SubstitutionFailure; 29590b57cec5SDimitry Andric } 29600b57cec5SDimitry Andric 2961480093f4SDimitry Andric bool ConstraintsNotSatisfied; 2962*bdd1243dSDimitry Andric SmallVector<TemplateArgument, 4> SugaredConvertedInstArgs, 2963*bdd1243dSDimitry Andric CanonicalConvertedInstArgs; 2964*bdd1243dSDimitry Andric if (S.CheckTemplateArgumentList( 2965*bdd1243dSDimitry Andric Template, Partial->getLocation(), InstArgs, false, 2966*bdd1243dSDimitry Andric SugaredConvertedInstArgs, CanonicalConvertedInstArgs, 2967*bdd1243dSDimitry Andric /*UpdateArgsWithConversions=*/true, &ConstraintsNotSatisfied)) 2968*bdd1243dSDimitry Andric return ConstraintsNotSatisfied ? Sema::TDK_ConstraintsNotSatisfied 2969*bdd1243dSDimitry Andric : Sema::TDK_SubstitutionFailure; 29700b57cec5SDimitry Andric 29710b57cec5SDimitry Andric TemplateParameterList *TemplateParams = Template->getTemplateParameters(); 29720b57cec5SDimitry Andric for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { 2973*bdd1243dSDimitry Andric TemplateArgument InstArg = SugaredConvertedInstArgs.data()[I]; 2974*bdd1243dSDimitry Andric if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg, 2975*bdd1243dSDimitry Andric IsPartialOrdering)) { 29760b57cec5SDimitry Andric Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); 29770b57cec5SDimitry Andric Info.FirstArg = TemplateArgs[I]; 29780b57cec5SDimitry Andric Info.SecondArg = InstArg; 29790b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 29800b57cec5SDimitry Andric } 29810b57cec5SDimitry Andric } 29820b57cec5SDimitry Andric 29830b57cec5SDimitry Andric if (Trap.hasErrorOccurred()) 29840b57cec5SDimitry Andric return Sema::TDK_SubstitutionFailure; 29850b57cec5SDimitry Andric 2986*bdd1243dSDimitry Andric if (auto Result = CheckDeducedArgumentConstraints(S, Partial, SugaredBuilder, 2987*bdd1243dSDimitry Andric CanonicalBuilder, Info)) 2988480093f4SDimitry Andric return Result; 2989480093f4SDimitry Andric 29900b57cec5SDimitry Andric return Sema::TDK_Success; 29910b57cec5SDimitry Andric } 29920b57cec5SDimitry Andric 29930b57cec5SDimitry Andric /// Complete template argument deduction for a class or variable template, 29940b57cec5SDimitry Andric /// when partial ordering against a partial specialization. 29950b57cec5SDimitry Andric // FIXME: Factor out duplication with partial specialization version above. 29960b57cec5SDimitry Andric static Sema::TemplateDeductionResult FinishTemplateArgumentDeduction( 29970b57cec5SDimitry Andric Sema &S, TemplateDecl *Template, bool PartialOrdering, 29980b57cec5SDimitry Andric const TemplateArgumentList &TemplateArgs, 29990b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 30000b57cec5SDimitry Andric TemplateDeductionInfo &Info) { 30010b57cec5SDimitry Andric // Unevaluated SFINAE context. 30020b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 30030b57cec5SDimitry Andric S, Sema::ExpressionEvaluationContext::Unevaluated); 30040b57cec5SDimitry Andric Sema::SFINAETrap Trap(S); 30050b57cec5SDimitry Andric 30060b57cec5SDimitry Andric Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Template)); 30070b57cec5SDimitry Andric 30080b57cec5SDimitry Andric // C++ [temp.deduct.type]p2: 30090b57cec5SDimitry Andric // [...] or if any template argument remains neither deduced nor 30100b57cec5SDimitry Andric // explicitly specified, template argument deduction fails. 3011*bdd1243dSDimitry Andric SmallVector<TemplateArgument, 4> SugaredBuilder, CanonicalBuilder; 30120b57cec5SDimitry Andric if (auto Result = ConvertDeducedTemplateArguments( 3013*bdd1243dSDimitry Andric S, Template, /*IsDeduced*/ PartialOrdering, Deduced, Info, 3014*bdd1243dSDimitry Andric SugaredBuilder, CanonicalBuilder, 3015*bdd1243dSDimitry Andric /*CurrentInstantiationScope=*/nullptr, 3016*bdd1243dSDimitry Andric /*NumAlreadyConverted=*/0U, /*PartialOverloading=*/false)) 30170b57cec5SDimitry Andric return Result; 30180b57cec5SDimitry Andric 30190b57cec5SDimitry Andric // Check that we produced the correct argument list. 30200b57cec5SDimitry Andric TemplateParameterList *TemplateParams = Template->getTemplateParameters(); 30210b57cec5SDimitry Andric for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { 3022*bdd1243dSDimitry Andric TemplateArgument InstArg = CanonicalBuilder[I]; 3023*bdd1243dSDimitry Andric if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg, PartialOrdering, 3024*bdd1243dSDimitry Andric /*PackExpansionMatchesPack=*/true)) { 30250b57cec5SDimitry Andric Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); 30260b57cec5SDimitry Andric Info.FirstArg = TemplateArgs[I]; 30270b57cec5SDimitry Andric Info.SecondArg = InstArg; 30280b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 30290b57cec5SDimitry Andric } 30300b57cec5SDimitry Andric } 30310b57cec5SDimitry Andric 30320b57cec5SDimitry Andric if (Trap.hasErrorOccurred()) 30330b57cec5SDimitry Andric return Sema::TDK_SubstitutionFailure; 30340b57cec5SDimitry Andric 3035*bdd1243dSDimitry Andric if (auto Result = CheckDeducedArgumentConstraints(S, Template, SugaredBuilder, 3036*bdd1243dSDimitry Andric CanonicalBuilder, Info)) 3037480093f4SDimitry Andric return Result; 3038480093f4SDimitry Andric 30390b57cec5SDimitry Andric return Sema::TDK_Success; 30400b57cec5SDimitry Andric } 30410b57cec5SDimitry Andric 30420b57cec5SDimitry Andric /// Perform template argument deduction to determine whether 30430b57cec5SDimitry Andric /// the given template arguments match the given class template 30440b57cec5SDimitry Andric /// partial specialization per C++ [temp.class.spec.match]. 30450b57cec5SDimitry Andric Sema::TemplateDeductionResult 30460b57cec5SDimitry Andric Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, 30470b57cec5SDimitry Andric const TemplateArgumentList &TemplateArgs, 30480b57cec5SDimitry Andric TemplateDeductionInfo &Info) { 30490b57cec5SDimitry Andric if (Partial->isInvalidDecl()) 30500b57cec5SDimitry Andric return TDK_Invalid; 30510b57cec5SDimitry Andric 30520b57cec5SDimitry Andric // C++ [temp.class.spec.match]p2: 30530b57cec5SDimitry Andric // A partial specialization matches a given actual template 30540b57cec5SDimitry Andric // argument list if the template arguments of the partial 30550b57cec5SDimitry Andric // specialization can be deduced from the actual template argument 30560b57cec5SDimitry Andric // list (14.8.2). 30570b57cec5SDimitry Andric 30580b57cec5SDimitry Andric // Unevaluated SFINAE context. 30590b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 30600b57cec5SDimitry Andric *this, Sema::ExpressionEvaluationContext::Unevaluated); 30610b57cec5SDimitry Andric SFINAETrap Trap(*this); 30620b57cec5SDimitry Andric 3063fe6060f1SDimitry Andric // This deduction has no relation to any outer instantiation we might be 3064fe6060f1SDimitry Andric // performing. 3065fe6060f1SDimitry Andric LocalInstantiationScope InstantiationScope(*this); 3066fe6060f1SDimitry Andric 30670b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> Deduced; 30680b57cec5SDimitry Andric Deduced.resize(Partial->getTemplateParameters()->size()); 30690b57cec5SDimitry Andric if (TemplateDeductionResult Result 30700b57cec5SDimitry Andric = ::DeduceTemplateArguments(*this, 30710b57cec5SDimitry Andric Partial->getTemplateParameters(), 30720b57cec5SDimitry Andric Partial->getTemplateArgs(), 30730b57cec5SDimitry Andric TemplateArgs, Info, Deduced)) 30740b57cec5SDimitry Andric return Result; 30750b57cec5SDimitry Andric 30760b57cec5SDimitry Andric SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); 30770b57cec5SDimitry Andric InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, 30780b57cec5SDimitry Andric Info); 30790b57cec5SDimitry Andric if (Inst.isInvalid()) 30800b57cec5SDimitry Andric return TDK_InstantiationDepth; 30810b57cec5SDimitry Andric 30820b57cec5SDimitry Andric if (Trap.hasErrorOccurred()) 30830b57cec5SDimitry Andric return Sema::TDK_SubstitutionFailure; 30840b57cec5SDimitry Andric 30855ffd83dbSDimitry Andric TemplateDeductionResult Result; 30865ffd83dbSDimitry Andric runWithSufficientStackSpace(Info.getLocation(), [&] { 30875ffd83dbSDimitry Andric Result = ::FinishTemplateArgumentDeduction(*this, Partial, 30885ffd83dbSDimitry Andric /*IsPartialOrdering=*/false, 30895ffd83dbSDimitry Andric TemplateArgs, Deduced, Info); 30905ffd83dbSDimitry Andric }); 30915ffd83dbSDimitry Andric return Result; 30920b57cec5SDimitry Andric } 30930b57cec5SDimitry Andric 30940b57cec5SDimitry Andric /// Perform template argument deduction to determine whether 30950b57cec5SDimitry Andric /// the given template arguments match the given variable template 30960b57cec5SDimitry Andric /// partial specialization per C++ [temp.class.spec.match]. 30970b57cec5SDimitry Andric Sema::TemplateDeductionResult 30980b57cec5SDimitry Andric Sema::DeduceTemplateArguments(VarTemplatePartialSpecializationDecl *Partial, 30990b57cec5SDimitry Andric const TemplateArgumentList &TemplateArgs, 31000b57cec5SDimitry Andric TemplateDeductionInfo &Info) { 31010b57cec5SDimitry Andric if (Partial->isInvalidDecl()) 31020b57cec5SDimitry Andric return TDK_Invalid; 31030b57cec5SDimitry Andric 31040b57cec5SDimitry Andric // C++ [temp.class.spec.match]p2: 31050b57cec5SDimitry Andric // A partial specialization matches a given actual template 31060b57cec5SDimitry Andric // argument list if the template arguments of the partial 31070b57cec5SDimitry Andric // specialization can be deduced from the actual template argument 31080b57cec5SDimitry Andric // list (14.8.2). 31090b57cec5SDimitry Andric 31100b57cec5SDimitry Andric // Unevaluated SFINAE context. 31110b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 31120b57cec5SDimitry Andric *this, Sema::ExpressionEvaluationContext::Unevaluated); 31130b57cec5SDimitry Andric SFINAETrap Trap(*this); 31140b57cec5SDimitry Andric 3115fe6060f1SDimitry Andric // This deduction has no relation to any outer instantiation we might be 3116fe6060f1SDimitry Andric // performing. 3117fe6060f1SDimitry Andric LocalInstantiationScope InstantiationScope(*this); 3118fe6060f1SDimitry Andric 31190b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> Deduced; 31200b57cec5SDimitry Andric Deduced.resize(Partial->getTemplateParameters()->size()); 31210b57cec5SDimitry Andric if (TemplateDeductionResult Result = ::DeduceTemplateArguments( 31220b57cec5SDimitry Andric *this, Partial->getTemplateParameters(), Partial->getTemplateArgs(), 31230b57cec5SDimitry Andric TemplateArgs, Info, Deduced)) 31240b57cec5SDimitry Andric return Result; 31250b57cec5SDimitry Andric 31260b57cec5SDimitry Andric SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); 31270b57cec5SDimitry Andric InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, 31280b57cec5SDimitry Andric Info); 31290b57cec5SDimitry Andric if (Inst.isInvalid()) 31300b57cec5SDimitry Andric return TDK_InstantiationDepth; 31310b57cec5SDimitry Andric 31320b57cec5SDimitry Andric if (Trap.hasErrorOccurred()) 31330b57cec5SDimitry Andric return Sema::TDK_SubstitutionFailure; 31340b57cec5SDimitry Andric 31355ffd83dbSDimitry Andric TemplateDeductionResult Result; 31365ffd83dbSDimitry Andric runWithSufficientStackSpace(Info.getLocation(), [&] { 31375ffd83dbSDimitry Andric Result = ::FinishTemplateArgumentDeduction(*this, Partial, 31385ffd83dbSDimitry Andric /*IsPartialOrdering=*/false, 31395ffd83dbSDimitry Andric TemplateArgs, Deduced, Info); 31405ffd83dbSDimitry Andric }); 31415ffd83dbSDimitry Andric return Result; 31420b57cec5SDimitry Andric } 31430b57cec5SDimitry Andric 31440b57cec5SDimitry Andric /// Determine whether the given type T is a simple-template-id type. 31450b57cec5SDimitry Andric static bool isSimpleTemplateIdType(QualType T) { 31460b57cec5SDimitry Andric if (const TemplateSpecializationType *Spec 31470b57cec5SDimitry Andric = T->getAs<TemplateSpecializationType>()) 31480b57cec5SDimitry Andric return Spec->getTemplateName().getAsTemplateDecl() != nullptr; 31490b57cec5SDimitry Andric 31500b57cec5SDimitry Andric // C++17 [temp.local]p2: 31510b57cec5SDimitry Andric // the injected-class-name [...] is equivalent to the template-name followed 31520b57cec5SDimitry Andric // by the template-arguments of the class template specialization or partial 31530b57cec5SDimitry Andric // specialization enclosed in <> 31540b57cec5SDimitry Andric // ... which means it's equivalent to a simple-template-id. 31550b57cec5SDimitry Andric // 31560b57cec5SDimitry Andric // This only arises during class template argument deduction for a copy 31570b57cec5SDimitry Andric // deduction candidate, where it permits slicing. 31580b57cec5SDimitry Andric if (T->getAs<InjectedClassNameType>()) 31590b57cec5SDimitry Andric return true; 31600b57cec5SDimitry Andric 31610b57cec5SDimitry Andric return false; 31620b57cec5SDimitry Andric } 31630b57cec5SDimitry Andric 31640b57cec5SDimitry Andric /// Substitute the explicitly-provided template arguments into the 31650b57cec5SDimitry Andric /// given function template according to C++ [temp.arg.explicit]. 31660b57cec5SDimitry Andric /// 31670b57cec5SDimitry Andric /// \param FunctionTemplate the function template into which the explicit 31680b57cec5SDimitry Andric /// template arguments will be substituted. 31690b57cec5SDimitry Andric /// 31700b57cec5SDimitry Andric /// \param ExplicitTemplateArgs the explicitly-specified template 31710b57cec5SDimitry Andric /// arguments. 31720b57cec5SDimitry Andric /// 31730b57cec5SDimitry Andric /// \param Deduced the deduced template arguments, which will be populated 31740b57cec5SDimitry Andric /// with the converted and checked explicit template arguments. 31750b57cec5SDimitry Andric /// 31760b57cec5SDimitry Andric /// \param ParamTypes will be populated with the instantiated function 31770b57cec5SDimitry Andric /// parameters. 31780b57cec5SDimitry Andric /// 31790b57cec5SDimitry Andric /// \param FunctionType if non-NULL, the result type of the function template 31800b57cec5SDimitry Andric /// will also be instantiated and the pointed-to value will be updated with 31810b57cec5SDimitry Andric /// the instantiated function type. 31820b57cec5SDimitry Andric /// 31830b57cec5SDimitry Andric /// \param Info if substitution fails for any reason, this object will be 31840b57cec5SDimitry Andric /// populated with more information about the failure. 31850b57cec5SDimitry Andric /// 31860b57cec5SDimitry Andric /// \returns TDK_Success if substitution was successful, or some failure 31870b57cec5SDimitry Andric /// condition. 3188*bdd1243dSDimitry Andric Sema::TemplateDeductionResult Sema::SubstituteExplicitTemplateArguments( 31890b57cec5SDimitry Andric FunctionTemplateDecl *FunctionTemplate, 31900b57cec5SDimitry Andric TemplateArgumentListInfo &ExplicitTemplateArgs, 31910b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 3192*bdd1243dSDimitry Andric SmallVectorImpl<QualType> &ParamTypes, QualType *FunctionType, 31930b57cec5SDimitry Andric TemplateDeductionInfo &Info) { 31940b57cec5SDimitry Andric FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 31950b57cec5SDimitry Andric TemplateParameterList *TemplateParams 31960b57cec5SDimitry Andric = FunctionTemplate->getTemplateParameters(); 31970b57cec5SDimitry Andric 31980b57cec5SDimitry Andric if (ExplicitTemplateArgs.size() == 0) { 31990b57cec5SDimitry Andric // No arguments to substitute; just copy over the parameter types and 32000b57cec5SDimitry Andric // fill in the function type. 3201*bdd1243dSDimitry Andric for (auto *P : Function->parameters()) 32020b57cec5SDimitry Andric ParamTypes.push_back(P->getType()); 32030b57cec5SDimitry Andric 32040b57cec5SDimitry Andric if (FunctionType) 32050b57cec5SDimitry Andric *FunctionType = Function->getType(); 32060b57cec5SDimitry Andric return TDK_Success; 32070b57cec5SDimitry Andric } 32080b57cec5SDimitry Andric 32090b57cec5SDimitry Andric // Unevaluated SFINAE context. 32100b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 32110b57cec5SDimitry Andric *this, Sema::ExpressionEvaluationContext::Unevaluated); 32120b57cec5SDimitry Andric SFINAETrap Trap(*this); 32130b57cec5SDimitry Andric 32140b57cec5SDimitry Andric // C++ [temp.arg.explicit]p3: 32150b57cec5SDimitry Andric // Template arguments that are present shall be specified in the 32160b57cec5SDimitry Andric // declaration order of their corresponding template-parameters. The 32170b57cec5SDimitry Andric // template argument list shall not specify more template-arguments than 32180b57cec5SDimitry Andric // there are corresponding template-parameters. 3219*bdd1243dSDimitry Andric SmallVector<TemplateArgument, 4> SugaredBuilder, CanonicalBuilder; 32200b57cec5SDimitry Andric 32210b57cec5SDimitry Andric // Enter a new template instantiation context where we check the 32220b57cec5SDimitry Andric // explicitly-specified template arguments against this function template, 32230b57cec5SDimitry Andric // and then substitute them into the function parameter types. 32240b57cec5SDimitry Andric SmallVector<TemplateArgument, 4> DeducedArgs; 32250b57cec5SDimitry Andric InstantiatingTemplate Inst( 32260b57cec5SDimitry Andric *this, Info.getLocation(), FunctionTemplate, DeducedArgs, 32270b57cec5SDimitry Andric CodeSynthesisContext::ExplicitTemplateArgumentSubstitution, Info); 32280b57cec5SDimitry Andric if (Inst.isInvalid()) 32290b57cec5SDimitry Andric return TDK_InstantiationDepth; 32300b57cec5SDimitry Andric 32310b57cec5SDimitry Andric if (CheckTemplateArgumentList(FunctionTemplate, SourceLocation(), 3232*bdd1243dSDimitry Andric ExplicitTemplateArgs, true, SugaredBuilder, 3233*bdd1243dSDimitry Andric CanonicalBuilder, 3234*bdd1243dSDimitry Andric /*UpdateArgsWithConversions=*/false) || 32350b57cec5SDimitry Andric Trap.hasErrorOccurred()) { 3236*bdd1243dSDimitry Andric unsigned Index = SugaredBuilder.size(); 32370b57cec5SDimitry Andric if (Index >= TemplateParams->size()) 32380b57cec5SDimitry Andric return TDK_SubstitutionFailure; 32390b57cec5SDimitry Andric Info.Param = makeTemplateParameter(TemplateParams->getParam(Index)); 32400b57cec5SDimitry Andric return TDK_InvalidExplicitArguments; 32410b57cec5SDimitry Andric } 32420b57cec5SDimitry Andric 32430b57cec5SDimitry Andric // Form the template argument list from the explicitly-specified 32440b57cec5SDimitry Andric // template arguments. 3245*bdd1243dSDimitry Andric TemplateArgumentList *SugaredExplicitArgumentList = 3246*bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(Context, SugaredBuilder); 3247*bdd1243dSDimitry Andric TemplateArgumentList *CanonicalExplicitArgumentList = 3248*bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(Context, CanonicalBuilder); 3249*bdd1243dSDimitry Andric Info.setExplicitArgs(SugaredExplicitArgumentList, 3250*bdd1243dSDimitry Andric CanonicalExplicitArgumentList); 32510b57cec5SDimitry Andric 32520b57cec5SDimitry Andric // Template argument deduction and the final substitution should be 32530b57cec5SDimitry Andric // done in the context of the templated declaration. Explicit 32540b57cec5SDimitry Andric // argument substitution, on the other hand, needs to happen in the 32550b57cec5SDimitry Andric // calling context. 32560b57cec5SDimitry Andric ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); 32570b57cec5SDimitry Andric 32580b57cec5SDimitry Andric // If we deduced template arguments for a template parameter pack, 32590b57cec5SDimitry Andric // note that the template argument pack is partially substituted and record 32600b57cec5SDimitry Andric // the explicit template arguments. They'll be used as part of deduction 32610b57cec5SDimitry Andric // for this template parameter pack. 32620b57cec5SDimitry Andric unsigned PartiallySubstitutedPackIndex = -1u; 3263*bdd1243dSDimitry Andric if (!CanonicalBuilder.empty()) { 3264*bdd1243dSDimitry Andric const TemplateArgument &Arg = CanonicalBuilder.back(); 32650b57cec5SDimitry Andric if (Arg.getKind() == TemplateArgument::Pack) { 3266*bdd1243dSDimitry Andric auto *Param = TemplateParams->getParam(CanonicalBuilder.size() - 1); 32670b57cec5SDimitry Andric // If this is a fully-saturated fixed-size pack, it should be 32680b57cec5SDimitry Andric // fully-substituted, not partially-substituted. 3269*bdd1243dSDimitry Andric std::optional<unsigned> Expansions = getExpandedPackSize(Param); 32700b57cec5SDimitry Andric if (!Expansions || Arg.pack_size() < *Expansions) { 3271*bdd1243dSDimitry Andric PartiallySubstitutedPackIndex = CanonicalBuilder.size() - 1; 32720b57cec5SDimitry Andric CurrentInstantiationScope->SetPartiallySubstitutedPack( 32730b57cec5SDimitry Andric Param, Arg.pack_begin(), Arg.pack_size()); 32740b57cec5SDimitry Andric } 32750b57cec5SDimitry Andric } 32760b57cec5SDimitry Andric } 32770b57cec5SDimitry Andric 32780b57cec5SDimitry Andric const FunctionProtoType *Proto 32790b57cec5SDimitry Andric = Function->getType()->getAs<FunctionProtoType>(); 32800b57cec5SDimitry Andric assert(Proto && "Function template does not have a prototype?"); 32810b57cec5SDimitry Andric 32820b57cec5SDimitry Andric // Isolate our substituted parameters from our caller. 32830b57cec5SDimitry Andric LocalInstantiationScope InstScope(*this, /*MergeWithOuterScope*/true); 32840b57cec5SDimitry Andric 32850b57cec5SDimitry Andric ExtParameterInfoBuilder ExtParamInfos; 32860b57cec5SDimitry Andric 3287*bdd1243dSDimitry Andric MultiLevelTemplateArgumentList MLTAL(FunctionTemplate, 3288*bdd1243dSDimitry Andric SugaredExplicitArgumentList->asArray(), 3289*bdd1243dSDimitry Andric /*Final=*/true); 3290*bdd1243dSDimitry Andric 32910b57cec5SDimitry Andric // Instantiate the types of each of the function parameters given the 32920b57cec5SDimitry Andric // explicitly-specified template arguments. If the function has a trailing 32930b57cec5SDimitry Andric // return type, substitute it after the arguments to ensure we substitute 32940b57cec5SDimitry Andric // in lexical order. 32950b57cec5SDimitry Andric if (Proto->hasTrailingReturn()) { 32960b57cec5SDimitry Andric if (SubstParmTypes(Function->getLocation(), Function->parameters(), 3297*bdd1243dSDimitry Andric Proto->getExtParameterInfosOrNull(), MLTAL, ParamTypes, 3298*bdd1243dSDimitry Andric /*params=*/nullptr, ExtParamInfos)) 32990b57cec5SDimitry Andric return TDK_SubstitutionFailure; 33000b57cec5SDimitry Andric } 33010b57cec5SDimitry Andric 33020b57cec5SDimitry Andric // Instantiate the return type. 33030b57cec5SDimitry Andric QualType ResultType; 33040b57cec5SDimitry Andric { 33050b57cec5SDimitry Andric // C++11 [expr.prim.general]p3: 33060b57cec5SDimitry Andric // If a declaration declares a member function or member function 33070b57cec5SDimitry Andric // template of a class X, the expression this is a prvalue of type 33080b57cec5SDimitry Andric // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq 33090b57cec5SDimitry Andric // and the end of the function-definition, member-declarator, or 33100b57cec5SDimitry Andric // declarator. 33110b57cec5SDimitry Andric Qualifiers ThisTypeQuals; 33120b57cec5SDimitry Andric CXXRecordDecl *ThisContext = nullptr; 33130b57cec5SDimitry Andric if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { 33140b57cec5SDimitry Andric ThisContext = Method->getParent(); 33150b57cec5SDimitry Andric ThisTypeQuals = Method->getMethodQualifiers(); 33160b57cec5SDimitry Andric } 33170b57cec5SDimitry Andric 33180b57cec5SDimitry Andric CXXThisScopeRAII ThisScope(*this, ThisContext, ThisTypeQuals, 33190b57cec5SDimitry Andric getLangOpts().CPlusPlus11); 33200b57cec5SDimitry Andric 33210b57cec5SDimitry Andric ResultType = 3322*bdd1243dSDimitry Andric SubstType(Proto->getReturnType(), MLTAL, 33230b57cec5SDimitry Andric Function->getTypeSpecStartLoc(), Function->getDeclName()); 33240b57cec5SDimitry Andric if (ResultType.isNull() || Trap.hasErrorOccurred()) 33250b57cec5SDimitry Andric return TDK_SubstitutionFailure; 3326a7dea167SDimitry Andric // CUDA: Kernel function must have 'void' return type. 3327a7dea167SDimitry Andric if (getLangOpts().CUDA) 3328a7dea167SDimitry Andric if (Function->hasAttr<CUDAGlobalAttr>() && !ResultType->isVoidType()) { 3329a7dea167SDimitry Andric Diag(Function->getLocation(), diag::err_kern_type_not_void_return) 3330a7dea167SDimitry Andric << Function->getType() << Function->getSourceRange(); 3331a7dea167SDimitry Andric return TDK_SubstitutionFailure; 3332a7dea167SDimitry Andric } 33330b57cec5SDimitry Andric } 33340b57cec5SDimitry Andric 33350b57cec5SDimitry Andric // Instantiate the types of each of the function parameters given the 33360b57cec5SDimitry Andric // explicitly-specified template arguments if we didn't do so earlier. 33370b57cec5SDimitry Andric if (!Proto->hasTrailingReturn() && 33380b57cec5SDimitry Andric SubstParmTypes(Function->getLocation(), Function->parameters(), 3339*bdd1243dSDimitry Andric Proto->getExtParameterInfosOrNull(), MLTAL, ParamTypes, 3340*bdd1243dSDimitry Andric /*params*/ nullptr, ExtParamInfos)) 33410b57cec5SDimitry Andric return TDK_SubstitutionFailure; 33420b57cec5SDimitry Andric 33430b57cec5SDimitry Andric if (FunctionType) { 33440b57cec5SDimitry Andric auto EPI = Proto->getExtProtoInfo(); 33450b57cec5SDimitry Andric EPI.ExtParameterInfos = ExtParamInfos.getPointerOrNull(ParamTypes.size()); 33460b57cec5SDimitry Andric 33470b57cec5SDimitry Andric // In C++1z onwards, exception specifications are part of the function type, 33480b57cec5SDimitry Andric // so substitution into the type must also substitute into the exception 33490b57cec5SDimitry Andric // specification. 33500b57cec5SDimitry Andric SmallVector<QualType, 4> ExceptionStorage; 33510b57cec5SDimitry Andric if (getLangOpts().CPlusPlus17 && 3352*bdd1243dSDimitry Andric SubstExceptionSpec(Function->getLocation(), EPI.ExceptionSpec, 3353*bdd1243dSDimitry Andric ExceptionStorage, MLTAL)) 33540b57cec5SDimitry Andric return TDK_SubstitutionFailure; 33550b57cec5SDimitry Andric 33560b57cec5SDimitry Andric *FunctionType = BuildFunctionType(ResultType, ParamTypes, 33570b57cec5SDimitry Andric Function->getLocation(), 33580b57cec5SDimitry Andric Function->getDeclName(), 33590b57cec5SDimitry Andric EPI); 33600b57cec5SDimitry Andric if (FunctionType->isNull() || Trap.hasErrorOccurred()) 33610b57cec5SDimitry Andric return TDK_SubstitutionFailure; 33620b57cec5SDimitry Andric } 33630b57cec5SDimitry Andric 33640b57cec5SDimitry Andric // C++ [temp.arg.explicit]p2: 33650b57cec5SDimitry Andric // Trailing template arguments that can be deduced (14.8.2) may be 33660b57cec5SDimitry Andric // omitted from the list of explicit template-arguments. If all of the 33670b57cec5SDimitry Andric // template arguments can be deduced, they may all be omitted; in this 33680b57cec5SDimitry Andric // case, the empty template argument list <> itself may also be omitted. 33690b57cec5SDimitry Andric // 33700b57cec5SDimitry Andric // Take all of the explicitly-specified arguments and put them into 33710b57cec5SDimitry Andric // the set of deduced template arguments. The partially-substituted 33720b57cec5SDimitry Andric // parameter pack, however, will be set to NULL since the deduction 33730b57cec5SDimitry Andric // mechanism handles the partially-substituted argument pack directly. 33740b57cec5SDimitry Andric Deduced.reserve(TemplateParams->size()); 3375*bdd1243dSDimitry Andric for (unsigned I = 0, N = SugaredExplicitArgumentList->size(); I != N; ++I) { 3376*bdd1243dSDimitry Andric const TemplateArgument &Arg = SugaredExplicitArgumentList->get(I); 33770b57cec5SDimitry Andric if (I == PartiallySubstitutedPackIndex) 33780b57cec5SDimitry Andric Deduced.push_back(DeducedTemplateArgument()); 33790b57cec5SDimitry Andric else 33800b57cec5SDimitry Andric Deduced.push_back(Arg); 33810b57cec5SDimitry Andric } 33820b57cec5SDimitry Andric 33830b57cec5SDimitry Andric return TDK_Success; 33840b57cec5SDimitry Andric } 33850b57cec5SDimitry Andric 33860b57cec5SDimitry Andric /// Check whether the deduced argument type for a call to a function 33870b57cec5SDimitry Andric /// template matches the actual argument type per C++ [temp.deduct.call]p4. 33880b57cec5SDimitry Andric static Sema::TemplateDeductionResult 33890b57cec5SDimitry Andric CheckOriginalCallArgDeduction(Sema &S, TemplateDeductionInfo &Info, 33900b57cec5SDimitry Andric Sema::OriginalCallArg OriginalArg, 33910b57cec5SDimitry Andric QualType DeducedA) { 33920b57cec5SDimitry Andric ASTContext &Context = S.Context; 33930b57cec5SDimitry Andric 33940b57cec5SDimitry Andric auto Failed = [&]() -> Sema::TemplateDeductionResult { 33950b57cec5SDimitry Andric Info.FirstArg = TemplateArgument(DeducedA); 33960b57cec5SDimitry Andric Info.SecondArg = TemplateArgument(OriginalArg.OriginalArgType); 33970b57cec5SDimitry Andric Info.CallArgIndex = OriginalArg.ArgIdx; 33980b57cec5SDimitry Andric return OriginalArg.DecomposedParam ? Sema::TDK_DeducedMismatchNested 33990b57cec5SDimitry Andric : Sema::TDK_DeducedMismatch; 34000b57cec5SDimitry Andric }; 34010b57cec5SDimitry Andric 34020b57cec5SDimitry Andric QualType A = OriginalArg.OriginalArgType; 34030b57cec5SDimitry Andric QualType OriginalParamType = OriginalArg.OriginalParamType; 34040b57cec5SDimitry Andric 34050b57cec5SDimitry Andric // Check for type equality (top-level cv-qualifiers are ignored). 34060b57cec5SDimitry Andric if (Context.hasSameUnqualifiedType(A, DeducedA)) 34070b57cec5SDimitry Andric return Sema::TDK_Success; 34080b57cec5SDimitry Andric 34090b57cec5SDimitry Andric // Strip off references on the argument types; they aren't needed for 34100b57cec5SDimitry Andric // the following checks. 34110b57cec5SDimitry Andric if (const ReferenceType *DeducedARef = DeducedA->getAs<ReferenceType>()) 34120b57cec5SDimitry Andric DeducedA = DeducedARef->getPointeeType(); 34130b57cec5SDimitry Andric if (const ReferenceType *ARef = A->getAs<ReferenceType>()) 34140b57cec5SDimitry Andric A = ARef->getPointeeType(); 34150b57cec5SDimitry Andric 34160b57cec5SDimitry Andric // C++ [temp.deduct.call]p4: 34170b57cec5SDimitry Andric // [...] However, there are three cases that allow a difference: 34180b57cec5SDimitry Andric // - If the original P is a reference type, the deduced A (i.e., the 34190b57cec5SDimitry Andric // type referred to by the reference) can be more cv-qualified than 34200b57cec5SDimitry Andric // the transformed A. 34210b57cec5SDimitry Andric if (const ReferenceType *OriginalParamRef 34220b57cec5SDimitry Andric = OriginalParamType->getAs<ReferenceType>()) { 34230b57cec5SDimitry Andric // We don't want to keep the reference around any more. 34240b57cec5SDimitry Andric OriginalParamType = OriginalParamRef->getPointeeType(); 34250b57cec5SDimitry Andric 34260b57cec5SDimitry Andric // FIXME: Resolve core issue (no number yet): if the original P is a 34270b57cec5SDimitry Andric // reference type and the transformed A is function type "noexcept F", 34280b57cec5SDimitry Andric // the deduced A can be F. 34290b57cec5SDimitry Andric QualType Tmp; 34300b57cec5SDimitry Andric if (A->isFunctionType() && S.IsFunctionConversion(A, DeducedA, Tmp)) 34310b57cec5SDimitry Andric return Sema::TDK_Success; 34320b57cec5SDimitry Andric 34330b57cec5SDimitry Andric Qualifiers AQuals = A.getQualifiers(); 34340b57cec5SDimitry Andric Qualifiers DeducedAQuals = DeducedA.getQualifiers(); 34350b57cec5SDimitry Andric 34360b57cec5SDimitry Andric // Under Objective-C++ ARC, the deduced type may have implicitly 34370b57cec5SDimitry Andric // been given strong or (when dealing with a const reference) 34380b57cec5SDimitry Andric // unsafe_unretained lifetime. If so, update the original 34390b57cec5SDimitry Andric // qualifiers to include this lifetime. 34400b57cec5SDimitry Andric if (S.getLangOpts().ObjCAutoRefCount && 34410b57cec5SDimitry Andric ((DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_Strong && 34420b57cec5SDimitry Andric AQuals.getObjCLifetime() == Qualifiers::OCL_None) || 34430b57cec5SDimitry Andric (DeducedAQuals.hasConst() && 34440b57cec5SDimitry Andric DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone))) { 34450b57cec5SDimitry Andric AQuals.setObjCLifetime(DeducedAQuals.getObjCLifetime()); 34460b57cec5SDimitry Andric } 34470b57cec5SDimitry Andric 34480b57cec5SDimitry Andric if (AQuals == DeducedAQuals) { 34490b57cec5SDimitry Andric // Qualifiers match; there's nothing to do. 34500b57cec5SDimitry Andric } else if (!DeducedAQuals.compatiblyIncludes(AQuals)) { 34510b57cec5SDimitry Andric return Failed(); 34520b57cec5SDimitry Andric } else { 34530b57cec5SDimitry Andric // Qualifiers are compatible, so have the argument type adopt the 34540b57cec5SDimitry Andric // deduced argument type's qualifiers as if we had performed the 34550b57cec5SDimitry Andric // qualification conversion. 34560b57cec5SDimitry Andric A = Context.getQualifiedType(A.getUnqualifiedType(), DeducedAQuals); 34570b57cec5SDimitry Andric } 34580b57cec5SDimitry Andric } 34590b57cec5SDimitry Andric 34600b57cec5SDimitry Andric // - The transformed A can be another pointer or pointer to member 34610b57cec5SDimitry Andric // type that can be converted to the deduced A via a function pointer 34620b57cec5SDimitry Andric // conversion and/or a qualification conversion. 34630b57cec5SDimitry Andric // 34640b57cec5SDimitry Andric // Also allow conversions which merely strip __attribute__((noreturn)) from 34650b57cec5SDimitry Andric // function types (recursively). 34660b57cec5SDimitry Andric bool ObjCLifetimeConversion = false; 34670b57cec5SDimitry Andric QualType ResultTy; 34680b57cec5SDimitry Andric if ((A->isAnyPointerType() || A->isMemberPointerType()) && 34690b57cec5SDimitry Andric (S.IsQualificationConversion(A, DeducedA, false, 34700b57cec5SDimitry Andric ObjCLifetimeConversion) || 34710b57cec5SDimitry Andric S.IsFunctionConversion(A, DeducedA, ResultTy))) 34720b57cec5SDimitry Andric return Sema::TDK_Success; 34730b57cec5SDimitry Andric 34740b57cec5SDimitry Andric // - If P is a class and P has the form simple-template-id, then the 34750b57cec5SDimitry Andric // transformed A can be a derived class of the deduced A. [...] 34760b57cec5SDimitry Andric // [...] Likewise, if P is a pointer to a class of the form 34770b57cec5SDimitry Andric // simple-template-id, the transformed A can be a pointer to a 34780b57cec5SDimitry Andric // derived class pointed to by the deduced A. 34790b57cec5SDimitry Andric if (const PointerType *OriginalParamPtr 34800b57cec5SDimitry Andric = OriginalParamType->getAs<PointerType>()) { 34810b57cec5SDimitry Andric if (const PointerType *DeducedAPtr = DeducedA->getAs<PointerType>()) { 34820b57cec5SDimitry Andric if (const PointerType *APtr = A->getAs<PointerType>()) { 34830b57cec5SDimitry Andric if (A->getPointeeType()->isRecordType()) { 34840b57cec5SDimitry Andric OriginalParamType = OriginalParamPtr->getPointeeType(); 34850b57cec5SDimitry Andric DeducedA = DeducedAPtr->getPointeeType(); 34860b57cec5SDimitry Andric A = APtr->getPointeeType(); 34870b57cec5SDimitry Andric } 34880b57cec5SDimitry Andric } 34890b57cec5SDimitry Andric } 34900b57cec5SDimitry Andric } 34910b57cec5SDimitry Andric 34920b57cec5SDimitry Andric if (Context.hasSameUnqualifiedType(A, DeducedA)) 34930b57cec5SDimitry Andric return Sema::TDK_Success; 34940b57cec5SDimitry Andric 34950b57cec5SDimitry Andric if (A->isRecordType() && isSimpleTemplateIdType(OriginalParamType) && 34960b57cec5SDimitry Andric S.IsDerivedFrom(Info.getLocation(), A, DeducedA)) 34970b57cec5SDimitry Andric return Sema::TDK_Success; 34980b57cec5SDimitry Andric 34990b57cec5SDimitry Andric return Failed(); 35000b57cec5SDimitry Andric } 35010b57cec5SDimitry Andric 35020b57cec5SDimitry Andric /// Find the pack index for a particular parameter index in an instantiation of 35030b57cec5SDimitry Andric /// a function template with specific arguments. 35040b57cec5SDimitry Andric /// 35050b57cec5SDimitry Andric /// \return The pack index for whichever pack produced this parameter, or -1 35060b57cec5SDimitry Andric /// if this was not produced by a parameter. Intended to be used as the 35070b57cec5SDimitry Andric /// ArgumentPackSubstitutionIndex for further substitutions. 35080b57cec5SDimitry Andric // FIXME: We should track this in OriginalCallArgs so we don't need to 35090b57cec5SDimitry Andric // reconstruct it here. 35100b57cec5SDimitry Andric static unsigned getPackIndexForParam(Sema &S, 35110b57cec5SDimitry Andric FunctionTemplateDecl *FunctionTemplate, 35120b57cec5SDimitry Andric const MultiLevelTemplateArgumentList &Args, 35130b57cec5SDimitry Andric unsigned ParamIdx) { 35140b57cec5SDimitry Andric unsigned Idx = 0; 35150b57cec5SDimitry Andric for (auto *PD : FunctionTemplate->getTemplatedDecl()->parameters()) { 35160b57cec5SDimitry Andric if (PD->isParameterPack()) { 35170b57cec5SDimitry Andric unsigned NumExpansions = 351881ad6265SDimitry Andric S.getNumArgumentsInExpansion(PD->getType(), Args).value_or(1); 35190b57cec5SDimitry Andric if (Idx + NumExpansions > ParamIdx) 35200b57cec5SDimitry Andric return ParamIdx - Idx; 35210b57cec5SDimitry Andric Idx += NumExpansions; 35220b57cec5SDimitry Andric } else { 35230b57cec5SDimitry Andric if (Idx == ParamIdx) 35240b57cec5SDimitry Andric return -1; // Not a pack expansion 35250b57cec5SDimitry Andric ++Idx; 35260b57cec5SDimitry Andric } 35270b57cec5SDimitry Andric } 35280b57cec5SDimitry Andric 35290b57cec5SDimitry Andric llvm_unreachable("parameter index would not be produced from template"); 35300b57cec5SDimitry Andric } 35310b57cec5SDimitry Andric 35320b57cec5SDimitry Andric /// Finish template argument deduction for a function template, 35330b57cec5SDimitry Andric /// checking the deduced template arguments for completeness and forming 35340b57cec5SDimitry Andric /// the function template specialization. 35350b57cec5SDimitry Andric /// 35360b57cec5SDimitry Andric /// \param OriginalCallArgs If non-NULL, the original call arguments against 35370b57cec5SDimitry Andric /// which the deduced argument types should be compared. 35380b57cec5SDimitry Andric Sema::TemplateDeductionResult Sema::FinishTemplateArgumentDeduction( 35390b57cec5SDimitry Andric FunctionTemplateDecl *FunctionTemplate, 35400b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 35410b57cec5SDimitry Andric unsigned NumExplicitlySpecified, FunctionDecl *&Specialization, 35420b57cec5SDimitry Andric TemplateDeductionInfo &Info, 35430b57cec5SDimitry Andric SmallVectorImpl<OriginalCallArg> const *OriginalCallArgs, 35440b57cec5SDimitry Andric bool PartialOverloading, llvm::function_ref<bool()> CheckNonDependent) { 35450b57cec5SDimitry Andric // Unevaluated SFINAE context. 35460b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 35470b57cec5SDimitry Andric *this, Sema::ExpressionEvaluationContext::Unevaluated); 35480b57cec5SDimitry Andric SFINAETrap Trap(*this); 35490b57cec5SDimitry Andric 35500b57cec5SDimitry Andric // Enter a new template instantiation context while we instantiate the 35510b57cec5SDimitry Andric // actual function declaration. 35520b57cec5SDimitry Andric SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); 35530b57cec5SDimitry Andric InstantiatingTemplate Inst( 35540b57cec5SDimitry Andric *this, Info.getLocation(), FunctionTemplate, DeducedArgs, 35550b57cec5SDimitry Andric CodeSynthesisContext::DeducedTemplateArgumentSubstitution, Info); 35560b57cec5SDimitry Andric if (Inst.isInvalid()) 35570b57cec5SDimitry Andric return TDK_InstantiationDepth; 35580b57cec5SDimitry Andric 35590b57cec5SDimitry Andric ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); 35600b57cec5SDimitry Andric 35610b57cec5SDimitry Andric // C++ [temp.deduct.type]p2: 35620b57cec5SDimitry Andric // [...] or if any template argument remains neither deduced nor 35630b57cec5SDimitry Andric // explicitly specified, template argument deduction fails. 3564*bdd1243dSDimitry Andric SmallVector<TemplateArgument, 4> SugaredBuilder, CanonicalBuilder; 35650b57cec5SDimitry Andric if (auto Result = ConvertDeducedTemplateArguments( 3566*bdd1243dSDimitry Andric *this, FunctionTemplate, /*IsDeduced*/ true, Deduced, Info, 3567*bdd1243dSDimitry Andric SugaredBuilder, CanonicalBuilder, CurrentInstantiationScope, 3568*bdd1243dSDimitry Andric NumExplicitlySpecified, PartialOverloading)) 35690b57cec5SDimitry Andric return Result; 35700b57cec5SDimitry Andric 35710b57cec5SDimitry Andric // C++ [temp.deduct.call]p10: [DR1391] 35720b57cec5SDimitry Andric // If deduction succeeds for all parameters that contain 35730b57cec5SDimitry Andric // template-parameters that participate in template argument deduction, 35740b57cec5SDimitry Andric // and all template arguments are explicitly specified, deduced, or 35750b57cec5SDimitry Andric // obtained from default template arguments, remaining parameters are then 35760b57cec5SDimitry Andric // compared with the corresponding arguments. For each remaining parameter 35770b57cec5SDimitry Andric // P with a type that was non-dependent before substitution of any 35780b57cec5SDimitry Andric // explicitly-specified template arguments, if the corresponding argument 35790b57cec5SDimitry Andric // A cannot be implicitly converted to P, deduction fails. 35800b57cec5SDimitry Andric if (CheckNonDependent()) 35810b57cec5SDimitry Andric return TDK_NonDependentConversionFailure; 35820b57cec5SDimitry Andric 35830b57cec5SDimitry Andric // Form the template argument list from the deduced template arguments. 3584*bdd1243dSDimitry Andric TemplateArgumentList *SugaredDeducedArgumentList = 3585*bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(Context, SugaredBuilder); 3586*bdd1243dSDimitry Andric TemplateArgumentList *CanonicalDeducedArgumentList = 3587*bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(Context, CanonicalBuilder); 3588*bdd1243dSDimitry Andric Info.reset(SugaredDeducedArgumentList, CanonicalDeducedArgumentList); 35890b57cec5SDimitry Andric 35900b57cec5SDimitry Andric // Substitute the deduced template arguments into the function template 35910b57cec5SDimitry Andric // declaration to produce the function template specialization. 35920b57cec5SDimitry Andric DeclContext *Owner = FunctionTemplate->getDeclContext(); 35930b57cec5SDimitry Andric if (FunctionTemplate->getFriendObjectKind()) 35940b57cec5SDimitry Andric Owner = FunctionTemplate->getLexicalDeclContext(); 3595*bdd1243dSDimitry Andric MultiLevelTemplateArgumentList SubstArgs( 3596*bdd1243dSDimitry Andric FunctionTemplate, CanonicalDeducedArgumentList->asArray(), 3597*bdd1243dSDimitry Andric /*Final=*/false); 35980b57cec5SDimitry Andric Specialization = cast_or_null<FunctionDecl>( 35990b57cec5SDimitry Andric SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner, SubstArgs)); 36000b57cec5SDimitry Andric if (!Specialization || Specialization->isInvalidDecl()) 36010b57cec5SDimitry Andric return TDK_SubstitutionFailure; 36020b57cec5SDimitry Andric 36030b57cec5SDimitry Andric assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == 36040b57cec5SDimitry Andric FunctionTemplate->getCanonicalDecl()); 36050b57cec5SDimitry Andric 36060b57cec5SDimitry Andric // If the template argument list is owned by the function template 36070b57cec5SDimitry Andric // specialization, release it. 3608*bdd1243dSDimitry Andric if (Specialization->getTemplateSpecializationArgs() == 3609*bdd1243dSDimitry Andric CanonicalDeducedArgumentList && 36100b57cec5SDimitry Andric !Trap.hasErrorOccurred()) 3611*bdd1243dSDimitry Andric Info.takeCanonical(); 36120b57cec5SDimitry Andric 36130b57cec5SDimitry Andric // There may have been an error that did not prevent us from constructing a 36140b57cec5SDimitry Andric // declaration. Mark the declaration invalid and return with a substitution 36150b57cec5SDimitry Andric // failure. 36160b57cec5SDimitry Andric if (Trap.hasErrorOccurred()) { 36170b57cec5SDimitry Andric Specialization->setInvalidDecl(true); 36180b57cec5SDimitry Andric return TDK_SubstitutionFailure; 36190b57cec5SDimitry Andric } 36200b57cec5SDimitry Andric 3621480093f4SDimitry Andric // C++2a [temp.deduct]p5 3622480093f4SDimitry Andric // [...] When all template arguments have been deduced [...] all uses of 3623480093f4SDimitry Andric // template parameters [...] are replaced with the corresponding deduced 3624480093f4SDimitry Andric // or default argument values. 3625480093f4SDimitry Andric // [...] If the function template has associated constraints 3626480093f4SDimitry Andric // ([temp.constr.decl]), those constraints are checked for satisfaction 3627480093f4SDimitry Andric // ([temp.constr.constr]). If the constraints are not satisfied, type 3628480093f4SDimitry Andric // deduction fails. 362913138422SDimitry Andric if (!PartialOverloading || 3630*bdd1243dSDimitry Andric (CanonicalBuilder.size() == 3631*bdd1243dSDimitry Andric FunctionTemplate->getTemplateParameters()->size())) { 3632*bdd1243dSDimitry Andric if (CheckInstantiatedFunctionTemplateConstraints( 3633*bdd1243dSDimitry Andric Info.getLocation(), Specialization, CanonicalBuilder, 3634*bdd1243dSDimitry Andric Info.AssociatedConstraintsSatisfaction)) 3635480093f4SDimitry Andric return TDK_MiscellaneousDeductionFailure; 3636480093f4SDimitry Andric 3637480093f4SDimitry Andric if (!Info.AssociatedConstraintsSatisfaction.IsSatisfied) { 3638*bdd1243dSDimitry Andric Info.reset(Info.takeSugared(), 3639*bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(Context, CanonicalBuilder)); 3640480093f4SDimitry Andric return TDK_ConstraintsNotSatisfied; 3641480093f4SDimitry Andric } 364213138422SDimitry Andric } 3643480093f4SDimitry Andric 36440b57cec5SDimitry Andric if (OriginalCallArgs) { 36450b57cec5SDimitry Andric // C++ [temp.deduct.call]p4: 36460b57cec5SDimitry Andric // In general, the deduction process attempts to find template argument 36470b57cec5SDimitry Andric // values that will make the deduced A identical to A (after the type A 36480b57cec5SDimitry Andric // is transformed as described above). [...] 36490b57cec5SDimitry Andric llvm::SmallDenseMap<std::pair<unsigned, QualType>, QualType> DeducedATypes; 36500b57cec5SDimitry Andric for (unsigned I = 0, N = OriginalCallArgs->size(); I != N; ++I) { 36510b57cec5SDimitry Andric OriginalCallArg OriginalArg = (*OriginalCallArgs)[I]; 36520b57cec5SDimitry Andric 36530b57cec5SDimitry Andric auto ParamIdx = OriginalArg.ArgIdx; 36540b57cec5SDimitry Andric if (ParamIdx >= Specialization->getNumParams()) 36550b57cec5SDimitry Andric // FIXME: This presumably means a pack ended up smaller than we 36560b57cec5SDimitry Andric // expected while deducing. Should this not result in deduction 36570b57cec5SDimitry Andric // failure? Can it even happen? 36580b57cec5SDimitry Andric continue; 36590b57cec5SDimitry Andric 36600b57cec5SDimitry Andric QualType DeducedA; 36610b57cec5SDimitry Andric if (!OriginalArg.DecomposedParam) { 36620b57cec5SDimitry Andric // P is one of the function parameters, just look up its substituted 36630b57cec5SDimitry Andric // type. 36640b57cec5SDimitry Andric DeducedA = Specialization->getParamDecl(ParamIdx)->getType(); 36650b57cec5SDimitry Andric } else { 36660b57cec5SDimitry Andric // P is a decomposed element of a parameter corresponding to a 36670b57cec5SDimitry Andric // braced-init-list argument. Substitute back into P to find the 36680b57cec5SDimitry Andric // deduced A. 36690b57cec5SDimitry Andric QualType &CacheEntry = 36700b57cec5SDimitry Andric DeducedATypes[{ParamIdx, OriginalArg.OriginalParamType}]; 36710b57cec5SDimitry Andric if (CacheEntry.isNull()) { 36720b57cec5SDimitry Andric ArgumentPackSubstitutionIndexRAII PackIndex( 36730b57cec5SDimitry Andric *this, getPackIndexForParam(*this, FunctionTemplate, SubstArgs, 36740b57cec5SDimitry Andric ParamIdx)); 36750b57cec5SDimitry Andric CacheEntry = 36760b57cec5SDimitry Andric SubstType(OriginalArg.OriginalParamType, SubstArgs, 36770b57cec5SDimitry Andric Specialization->getTypeSpecStartLoc(), 36780b57cec5SDimitry Andric Specialization->getDeclName()); 36790b57cec5SDimitry Andric } 36800b57cec5SDimitry Andric DeducedA = CacheEntry; 36810b57cec5SDimitry Andric } 36820b57cec5SDimitry Andric 36830b57cec5SDimitry Andric if (auto TDK = 36840b57cec5SDimitry Andric CheckOriginalCallArgDeduction(*this, Info, OriginalArg, DeducedA)) 36850b57cec5SDimitry Andric return TDK; 36860b57cec5SDimitry Andric } 36870b57cec5SDimitry Andric } 36880b57cec5SDimitry Andric 36890b57cec5SDimitry Andric // If we suppressed any diagnostics while performing template argument 36900b57cec5SDimitry Andric // deduction, and if we haven't already instantiated this declaration, 36910b57cec5SDimitry Andric // keep track of these diagnostics. They'll be emitted if this specialization 36920b57cec5SDimitry Andric // is actually used. 36930b57cec5SDimitry Andric if (Info.diag_begin() != Info.diag_end()) { 36940b57cec5SDimitry Andric SuppressedDiagnosticsMap::iterator 36950b57cec5SDimitry Andric Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl()); 36960b57cec5SDimitry Andric if (Pos == SuppressedDiagnostics.end()) 36970b57cec5SDimitry Andric SuppressedDiagnostics[Specialization->getCanonicalDecl()] 36980b57cec5SDimitry Andric .append(Info.diag_begin(), Info.diag_end()); 36990b57cec5SDimitry Andric } 37000b57cec5SDimitry Andric 37010b57cec5SDimitry Andric return TDK_Success; 37020b57cec5SDimitry Andric } 37030b57cec5SDimitry Andric 37040b57cec5SDimitry Andric /// Gets the type of a function for template-argument-deducton 37050b57cec5SDimitry Andric /// purposes when it's considered as part of an overload set. 37060b57cec5SDimitry Andric static QualType GetTypeOfFunction(Sema &S, const OverloadExpr::FindResult &R, 37070b57cec5SDimitry Andric FunctionDecl *Fn) { 37080b57cec5SDimitry Andric // We may need to deduce the return type of the function now. 37090b57cec5SDimitry Andric if (S.getLangOpts().CPlusPlus14 && Fn->getReturnType()->isUndeducedType() && 37100b57cec5SDimitry Andric S.DeduceReturnType(Fn, R.Expression->getExprLoc(), /*Diagnose*/ false)) 37110b57cec5SDimitry Andric return {}; 37120b57cec5SDimitry Andric 37130b57cec5SDimitry Andric if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) 37140b57cec5SDimitry Andric if (Method->isInstance()) { 37150b57cec5SDimitry Andric // An instance method that's referenced in a form that doesn't 37160b57cec5SDimitry Andric // look like a member pointer is just invalid. 37170b57cec5SDimitry Andric if (!R.HasFormOfMemberPointer) 37180b57cec5SDimitry Andric return {}; 37190b57cec5SDimitry Andric 37200b57cec5SDimitry Andric return S.Context.getMemberPointerType(Fn->getType(), 37210b57cec5SDimitry Andric S.Context.getTypeDeclType(Method->getParent()).getTypePtr()); 37220b57cec5SDimitry Andric } 37230b57cec5SDimitry Andric 37240b57cec5SDimitry Andric if (!R.IsAddressOfOperand) return Fn->getType(); 37250b57cec5SDimitry Andric return S.Context.getPointerType(Fn->getType()); 37260b57cec5SDimitry Andric } 37270b57cec5SDimitry Andric 37280b57cec5SDimitry Andric /// Apply the deduction rules for overload sets. 37290b57cec5SDimitry Andric /// 37300b57cec5SDimitry Andric /// \return the null type if this argument should be treated as an 37310b57cec5SDimitry Andric /// undeduced context 37320b57cec5SDimitry Andric static QualType 37330b57cec5SDimitry Andric ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams, 37340b57cec5SDimitry Andric Expr *Arg, QualType ParamType, 37350b57cec5SDimitry Andric bool ParamWasReference) { 37360b57cec5SDimitry Andric 37370b57cec5SDimitry Andric OverloadExpr::FindResult R = OverloadExpr::find(Arg); 37380b57cec5SDimitry Andric 37390b57cec5SDimitry Andric OverloadExpr *Ovl = R.Expression; 37400b57cec5SDimitry Andric 37410b57cec5SDimitry Andric // C++0x [temp.deduct.call]p4 37420b57cec5SDimitry Andric unsigned TDF = 0; 37430b57cec5SDimitry Andric if (ParamWasReference) 37440b57cec5SDimitry Andric TDF |= TDF_ParamWithReferenceType; 37450b57cec5SDimitry Andric if (R.IsAddressOfOperand) 37460b57cec5SDimitry Andric TDF |= TDF_IgnoreQualifiers; 37470b57cec5SDimitry Andric 37480b57cec5SDimitry Andric // C++0x [temp.deduct.call]p6: 37490b57cec5SDimitry Andric // When P is a function type, pointer to function type, or pointer 37500b57cec5SDimitry Andric // to member function type: 37510b57cec5SDimitry Andric 37520b57cec5SDimitry Andric if (!ParamType->isFunctionType() && 37530b57cec5SDimitry Andric !ParamType->isFunctionPointerType() && 37540b57cec5SDimitry Andric !ParamType->isMemberFunctionPointerType()) { 37550b57cec5SDimitry Andric if (Ovl->hasExplicitTemplateArgs()) { 37560b57cec5SDimitry Andric // But we can still look for an explicit specialization. 37570b57cec5SDimitry Andric if (FunctionDecl *ExplicitSpec 37580b57cec5SDimitry Andric = S.ResolveSingleFunctionTemplateSpecialization(Ovl)) 37590b57cec5SDimitry Andric return GetTypeOfFunction(S, R, ExplicitSpec); 37600b57cec5SDimitry Andric } 37610b57cec5SDimitry Andric 37620b57cec5SDimitry Andric DeclAccessPair DAP; 37630b57cec5SDimitry Andric if (FunctionDecl *Viable = 3764480093f4SDimitry Andric S.resolveAddressOfSingleOverloadCandidate(Arg, DAP)) 37650b57cec5SDimitry Andric return GetTypeOfFunction(S, R, Viable); 37660b57cec5SDimitry Andric 37670b57cec5SDimitry Andric return {}; 37680b57cec5SDimitry Andric } 37690b57cec5SDimitry Andric 37700b57cec5SDimitry Andric // Gather the explicit template arguments, if any. 37710b57cec5SDimitry Andric TemplateArgumentListInfo ExplicitTemplateArgs; 37720b57cec5SDimitry Andric if (Ovl->hasExplicitTemplateArgs()) 37730b57cec5SDimitry Andric Ovl->copyTemplateArgumentsInto(ExplicitTemplateArgs); 37740b57cec5SDimitry Andric QualType Match; 37750b57cec5SDimitry Andric for (UnresolvedSetIterator I = Ovl->decls_begin(), 37760b57cec5SDimitry Andric E = Ovl->decls_end(); I != E; ++I) { 37770b57cec5SDimitry Andric NamedDecl *D = (*I)->getUnderlyingDecl(); 37780b57cec5SDimitry Andric 37790b57cec5SDimitry Andric if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) { 37800b57cec5SDimitry Andric // - If the argument is an overload set containing one or more 37810b57cec5SDimitry Andric // function templates, the parameter is treated as a 37820b57cec5SDimitry Andric // non-deduced context. 37830b57cec5SDimitry Andric if (!Ovl->hasExplicitTemplateArgs()) 37840b57cec5SDimitry Andric return {}; 37850b57cec5SDimitry Andric 37860b57cec5SDimitry Andric // Otherwise, see if we can resolve a function type 37870b57cec5SDimitry Andric FunctionDecl *Specialization = nullptr; 37880b57cec5SDimitry Andric TemplateDeductionInfo Info(Ovl->getNameLoc()); 37890b57cec5SDimitry Andric if (S.DeduceTemplateArguments(FunTmpl, &ExplicitTemplateArgs, 37900b57cec5SDimitry Andric Specialization, Info)) 37910b57cec5SDimitry Andric continue; 37920b57cec5SDimitry Andric 37930b57cec5SDimitry Andric D = Specialization; 37940b57cec5SDimitry Andric } 37950b57cec5SDimitry Andric 37960b57cec5SDimitry Andric FunctionDecl *Fn = cast<FunctionDecl>(D); 37970b57cec5SDimitry Andric QualType ArgType = GetTypeOfFunction(S, R, Fn); 37980b57cec5SDimitry Andric if (ArgType.isNull()) continue; 37990b57cec5SDimitry Andric 38000b57cec5SDimitry Andric // Function-to-pointer conversion. 38010b57cec5SDimitry Andric if (!ParamWasReference && ParamType->isPointerType() && 38020b57cec5SDimitry Andric ArgType->isFunctionType()) 38030b57cec5SDimitry Andric ArgType = S.Context.getPointerType(ArgType); 38040b57cec5SDimitry Andric 38050b57cec5SDimitry Andric // - If the argument is an overload set (not containing function 38060b57cec5SDimitry Andric // templates), trial argument deduction is attempted using each 38070b57cec5SDimitry Andric // of the members of the set. If deduction succeeds for only one 38080b57cec5SDimitry Andric // of the overload set members, that member is used as the 38090b57cec5SDimitry Andric // argument value for the deduction. If deduction succeeds for 38100b57cec5SDimitry Andric // more than one member of the overload set the parameter is 38110b57cec5SDimitry Andric // treated as a non-deduced context. 38120b57cec5SDimitry Andric 38130b57cec5SDimitry Andric // We do all of this in a fresh context per C++0x [temp.deduct.type]p2: 38140b57cec5SDimitry Andric // Type deduction is done independently for each P/A pair, and 38150b57cec5SDimitry Andric // the deduced template argument values are then combined. 38160b57cec5SDimitry Andric // So we do not reject deductions which were made elsewhere. 38170b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 8> 38180b57cec5SDimitry Andric Deduced(TemplateParams->size()); 38190b57cec5SDimitry Andric TemplateDeductionInfo Info(Ovl->getNameLoc()); 38200b57cec5SDimitry Andric Sema::TemplateDeductionResult Result 38210b57cec5SDimitry Andric = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, 38220b57cec5SDimitry Andric ArgType, Info, Deduced, TDF); 38230b57cec5SDimitry Andric if (Result) continue; 38240b57cec5SDimitry Andric if (!Match.isNull()) 38250b57cec5SDimitry Andric return {}; 38260b57cec5SDimitry Andric Match = ArgType; 38270b57cec5SDimitry Andric } 38280b57cec5SDimitry Andric 38290b57cec5SDimitry Andric return Match; 38300b57cec5SDimitry Andric } 38310b57cec5SDimitry Andric 38320b57cec5SDimitry Andric /// Perform the adjustments to the parameter and argument types 38330b57cec5SDimitry Andric /// described in C++ [temp.deduct.call]. 38340b57cec5SDimitry Andric /// 38350b57cec5SDimitry Andric /// \returns true if the caller should not attempt to perform any template 38360b57cec5SDimitry Andric /// argument deduction based on this P/A pair because the argument is an 38370b57cec5SDimitry Andric /// overloaded function set that could not be resolved. 38380b57cec5SDimitry Andric static bool AdjustFunctionParmAndArgTypesForDeduction( 38390b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, 38400b57cec5SDimitry Andric QualType &ParamType, QualType &ArgType, Expr *Arg, unsigned &TDF) { 38410b57cec5SDimitry Andric // C++0x [temp.deduct.call]p3: 38420b57cec5SDimitry Andric // If P is a cv-qualified type, the top level cv-qualifiers of P's type 38430b57cec5SDimitry Andric // are ignored for type deduction. 38440b57cec5SDimitry Andric if (ParamType.hasQualifiers()) 38450b57cec5SDimitry Andric ParamType = ParamType.getUnqualifiedType(); 38460b57cec5SDimitry Andric 38470b57cec5SDimitry Andric // [...] If P is a reference type, the type referred to by P is 38480b57cec5SDimitry Andric // used for type deduction. 38490b57cec5SDimitry Andric const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>(); 38500b57cec5SDimitry Andric if (ParamRefType) 38510b57cec5SDimitry Andric ParamType = ParamRefType->getPointeeType(); 38520b57cec5SDimitry Andric 38530b57cec5SDimitry Andric // Overload sets usually make this parameter an undeduced context, 38540b57cec5SDimitry Andric // but there are sometimes special circumstances. Typically 38550b57cec5SDimitry Andric // involving a template-id-expr. 38560b57cec5SDimitry Andric if (ArgType == S.Context.OverloadTy) { 38570b57cec5SDimitry Andric ArgType = ResolveOverloadForDeduction(S, TemplateParams, 38580b57cec5SDimitry Andric Arg, ParamType, 38590b57cec5SDimitry Andric ParamRefType != nullptr); 38600b57cec5SDimitry Andric if (ArgType.isNull()) 38610b57cec5SDimitry Andric return true; 38620b57cec5SDimitry Andric } 38630b57cec5SDimitry Andric 38640b57cec5SDimitry Andric if (ParamRefType) { 38650b57cec5SDimitry Andric // If the argument has incomplete array type, try to complete its type. 3866e8d8bef9SDimitry Andric if (ArgType->isIncompleteArrayType()) 3867e8d8bef9SDimitry Andric ArgType = S.getCompletedType(Arg); 38680b57cec5SDimitry Andric 38690b57cec5SDimitry Andric // C++1z [temp.deduct.call]p3: 38700b57cec5SDimitry Andric // If P is a forwarding reference and the argument is an lvalue, the type 38710b57cec5SDimitry Andric // "lvalue reference to A" is used in place of A for type deduction. 38720b57cec5SDimitry Andric if (isForwardingReference(QualType(ParamRefType, 0), FirstInnerIndex) && 3873e8d8bef9SDimitry Andric Arg->isLValue()) { 3874fe6060f1SDimitry Andric if (S.getLangOpts().OpenCL && !ArgType.hasAddressSpace()) 3875349cc55cSDimitry Andric ArgType = S.Context.getAddrSpaceQualType( 3876349cc55cSDimitry Andric ArgType, S.Context.getDefaultOpenCLPointeeAddrSpace()); 38770b57cec5SDimitry Andric ArgType = S.Context.getLValueReferenceType(ArgType); 3878e8d8bef9SDimitry Andric } 38790b57cec5SDimitry Andric } else { 38800b57cec5SDimitry Andric // C++ [temp.deduct.call]p2: 38810b57cec5SDimitry Andric // If P is not a reference type: 38820b57cec5SDimitry Andric // - If A is an array type, the pointer type produced by the 38830b57cec5SDimitry Andric // array-to-pointer standard conversion (4.2) is used in place of 38840b57cec5SDimitry Andric // A for type deduction; otherwise, 38850b57cec5SDimitry Andric // - If A is a function type, the pointer type produced by the 38860b57cec5SDimitry Andric // function-to-pointer standard conversion (4.3) is used in place 38870b57cec5SDimitry Andric // of A for type deduction; otherwise, 3888*bdd1243dSDimitry Andric if (ArgType->canDecayToPointerType()) 3889*bdd1243dSDimitry Andric ArgType = S.Context.getDecayedType(ArgType); 38900b57cec5SDimitry Andric else { 38910b57cec5SDimitry Andric // - If A is a cv-qualified type, the top level cv-qualifiers of A's 38920b57cec5SDimitry Andric // type are ignored for type deduction. 38930b57cec5SDimitry Andric ArgType = ArgType.getUnqualifiedType(); 38940b57cec5SDimitry Andric } 38950b57cec5SDimitry Andric } 38960b57cec5SDimitry Andric 38970b57cec5SDimitry Andric // C++0x [temp.deduct.call]p4: 38980b57cec5SDimitry Andric // In general, the deduction process attempts to find template argument 38990b57cec5SDimitry Andric // values that will make the deduced A identical to A (after the type A 39000b57cec5SDimitry Andric // is transformed as described above). [...] 39010b57cec5SDimitry Andric TDF = TDF_SkipNonDependent; 39020b57cec5SDimitry Andric 39030b57cec5SDimitry Andric // - If the original P is a reference type, the deduced A (i.e., the 39040b57cec5SDimitry Andric // type referred to by the reference) can be more cv-qualified than 39050b57cec5SDimitry Andric // the transformed A. 39060b57cec5SDimitry Andric if (ParamRefType) 39070b57cec5SDimitry Andric TDF |= TDF_ParamWithReferenceType; 39080b57cec5SDimitry Andric // - The transformed A can be another pointer or pointer to member 39090b57cec5SDimitry Andric // type that can be converted to the deduced A via a qualification 39100b57cec5SDimitry Andric // conversion (4.4). 39110b57cec5SDimitry Andric if (ArgType->isPointerType() || ArgType->isMemberPointerType() || 39120b57cec5SDimitry Andric ArgType->isObjCObjectPointerType()) 39130b57cec5SDimitry Andric TDF |= TDF_IgnoreQualifiers; 39140b57cec5SDimitry Andric // - If P is a class and P has the form simple-template-id, then the 39150b57cec5SDimitry Andric // transformed A can be a derived class of the deduced A. Likewise, 39160b57cec5SDimitry Andric // if P is a pointer to a class of the form simple-template-id, the 39170b57cec5SDimitry Andric // transformed A can be a pointer to a derived class pointed to by 39180b57cec5SDimitry Andric // the deduced A. 39190b57cec5SDimitry Andric if (isSimpleTemplateIdType(ParamType) || 39200b57cec5SDimitry Andric (isa<PointerType>(ParamType) && 39210b57cec5SDimitry Andric isSimpleTemplateIdType( 3922fe6060f1SDimitry Andric ParamType->castAs<PointerType>()->getPointeeType()))) 39230b57cec5SDimitry Andric TDF |= TDF_DerivedClass; 39240b57cec5SDimitry Andric 39250b57cec5SDimitry Andric return false; 39260b57cec5SDimitry Andric } 39270b57cec5SDimitry Andric 39280b57cec5SDimitry Andric static bool 39290b57cec5SDimitry Andric hasDeducibleTemplateParameters(Sema &S, FunctionTemplateDecl *FunctionTemplate, 39300b57cec5SDimitry Andric QualType T); 39310b57cec5SDimitry Andric 39320b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceTemplateArgumentsFromCallArgument( 39330b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, 39340b57cec5SDimitry Andric QualType ParamType, Expr *Arg, TemplateDeductionInfo &Info, 39350b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 39360b57cec5SDimitry Andric SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, 39370b57cec5SDimitry Andric bool DecomposedParam, unsigned ArgIdx, unsigned TDF); 39380b57cec5SDimitry Andric 39390b57cec5SDimitry Andric /// Attempt template argument deduction from an initializer list 39400b57cec5SDimitry Andric /// deemed to be an argument in a function call. 39410b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceFromInitializerList( 39420b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, QualType AdjustedParamType, 39430b57cec5SDimitry Andric InitListExpr *ILE, TemplateDeductionInfo &Info, 39440b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 39450b57cec5SDimitry Andric SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, unsigned ArgIdx, 39460b57cec5SDimitry Andric unsigned TDF) { 39470b57cec5SDimitry Andric // C++ [temp.deduct.call]p1: (CWG 1591) 39480b57cec5SDimitry Andric // If removing references and cv-qualifiers from P gives 39490b57cec5SDimitry Andric // std::initializer_list<P0> or P0[N] for some P0 and N and the argument is 39500b57cec5SDimitry Andric // a non-empty initializer list, then deduction is performed instead for 39510b57cec5SDimitry Andric // each element of the initializer list, taking P0 as a function template 39520b57cec5SDimitry Andric // parameter type and the initializer element as its argument 39530b57cec5SDimitry Andric // 39540b57cec5SDimitry Andric // We've already removed references and cv-qualifiers here. 39550b57cec5SDimitry Andric if (!ILE->getNumInits()) 39560b57cec5SDimitry Andric return Sema::TDK_Success; 39570b57cec5SDimitry Andric 39580b57cec5SDimitry Andric QualType ElTy; 39590b57cec5SDimitry Andric auto *ArrTy = S.Context.getAsArrayType(AdjustedParamType); 39600b57cec5SDimitry Andric if (ArrTy) 39610b57cec5SDimitry Andric ElTy = ArrTy->getElementType(); 39620b57cec5SDimitry Andric else if (!S.isStdInitializerList(AdjustedParamType, &ElTy)) { 39630b57cec5SDimitry Andric // Otherwise, an initializer list argument causes the parameter to be 39640b57cec5SDimitry Andric // considered a non-deduced context 39650b57cec5SDimitry Andric return Sema::TDK_Success; 39660b57cec5SDimitry Andric } 39670b57cec5SDimitry Andric 3968a7dea167SDimitry Andric // Resolving a core issue: a braced-init-list containing any designators is 3969a7dea167SDimitry Andric // a non-deduced context. 3970a7dea167SDimitry Andric for (Expr *E : ILE->inits()) 3971a7dea167SDimitry Andric if (isa<DesignatedInitExpr>(E)) 3972a7dea167SDimitry Andric return Sema::TDK_Success; 3973a7dea167SDimitry Andric 39740b57cec5SDimitry Andric // Deduction only needs to be done for dependent types. 39750b57cec5SDimitry Andric if (ElTy->isDependentType()) { 39760b57cec5SDimitry Andric for (Expr *E : ILE->inits()) { 39770b57cec5SDimitry Andric if (auto Result = DeduceTemplateArgumentsFromCallArgument( 39780b57cec5SDimitry Andric S, TemplateParams, 0, ElTy, E, Info, Deduced, OriginalCallArgs, true, 39790b57cec5SDimitry Andric ArgIdx, TDF)) 39800b57cec5SDimitry Andric return Result; 39810b57cec5SDimitry Andric } 39820b57cec5SDimitry Andric } 39830b57cec5SDimitry Andric 39840b57cec5SDimitry Andric // in the P0[N] case, if N is a non-type template parameter, N is deduced 39850b57cec5SDimitry Andric // from the length of the initializer list. 39860b57cec5SDimitry Andric if (auto *DependentArrTy = dyn_cast_or_null<DependentSizedArrayType>(ArrTy)) { 39870b57cec5SDimitry Andric // Determine the array bound is something we can deduce. 3988e8d8bef9SDimitry Andric if (const NonTypeTemplateParmDecl *NTTP = 39890b57cec5SDimitry Andric getDeducedParameterFromExpr(Info, DependentArrTy->getSizeExpr())) { 39900b57cec5SDimitry Andric // We can perform template argument deduction for the given non-type 39910b57cec5SDimitry Andric // template parameter. 39920b57cec5SDimitry Andric // C++ [temp.deduct.type]p13: 39930b57cec5SDimitry Andric // The type of N in the type T[N] is std::size_t. 39940b57cec5SDimitry Andric QualType T = S.Context.getSizeType(); 39950b57cec5SDimitry Andric llvm::APInt Size(S.Context.getIntWidth(T), ILE->getNumInits()); 39960b57cec5SDimitry Andric if (auto Result = DeduceNonTypeTemplateArgument( 39970b57cec5SDimitry Andric S, TemplateParams, NTTP, llvm::APSInt(Size), T, 39980b57cec5SDimitry Andric /*ArrayBound=*/true, Info, Deduced)) 39990b57cec5SDimitry Andric return Result; 40000b57cec5SDimitry Andric } 40010b57cec5SDimitry Andric } 40020b57cec5SDimitry Andric 40030b57cec5SDimitry Andric return Sema::TDK_Success; 40040b57cec5SDimitry Andric } 40050b57cec5SDimitry Andric 40060b57cec5SDimitry Andric /// Perform template argument deduction per [temp.deduct.call] for a 40070b57cec5SDimitry Andric /// single parameter / argument pair. 40080b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceTemplateArgumentsFromCallArgument( 40090b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, 40100b57cec5SDimitry Andric QualType ParamType, Expr *Arg, TemplateDeductionInfo &Info, 40110b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 40120b57cec5SDimitry Andric SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, 40130b57cec5SDimitry Andric bool DecomposedParam, unsigned ArgIdx, unsigned TDF) { 40140b57cec5SDimitry Andric QualType ArgType = Arg->getType(); 40150b57cec5SDimitry Andric QualType OrigParamType = ParamType; 40160b57cec5SDimitry Andric 40170b57cec5SDimitry Andric // If P is a reference type [...] 40180b57cec5SDimitry Andric // If P is a cv-qualified type [...] 40190b57cec5SDimitry Andric if (AdjustFunctionParmAndArgTypesForDeduction( 40200b57cec5SDimitry Andric S, TemplateParams, FirstInnerIndex, ParamType, ArgType, Arg, TDF)) 40210b57cec5SDimitry Andric return Sema::TDK_Success; 40220b57cec5SDimitry Andric 40230b57cec5SDimitry Andric // If [...] the argument is a non-empty initializer list [...] 40240b57cec5SDimitry Andric if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) 40250b57cec5SDimitry Andric return DeduceFromInitializerList(S, TemplateParams, ParamType, ILE, Info, 40260b57cec5SDimitry Andric Deduced, OriginalCallArgs, ArgIdx, TDF); 40270b57cec5SDimitry Andric 40280b57cec5SDimitry Andric // [...] the deduction process attempts to find template argument values 40290b57cec5SDimitry Andric // that will make the deduced A identical to A 40300b57cec5SDimitry Andric // 40310b57cec5SDimitry Andric // Keep track of the argument type and corresponding parameter index, 40320b57cec5SDimitry Andric // so we can check for compatibility between the deduced A and A. 40330b57cec5SDimitry Andric OriginalCallArgs.push_back( 40340b57cec5SDimitry Andric Sema::OriginalCallArg(OrigParamType, DecomposedParam, ArgIdx, ArgType)); 40350b57cec5SDimitry Andric return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, 40360b57cec5SDimitry Andric ArgType, Info, Deduced, TDF); 40370b57cec5SDimitry Andric } 40380b57cec5SDimitry Andric 40390b57cec5SDimitry Andric /// Perform template argument deduction from a function call 40400b57cec5SDimitry Andric /// (C++ [temp.deduct.call]). 40410b57cec5SDimitry Andric /// 40420b57cec5SDimitry Andric /// \param FunctionTemplate the function template for which we are performing 40430b57cec5SDimitry Andric /// template argument deduction. 40440b57cec5SDimitry Andric /// 40450b57cec5SDimitry Andric /// \param ExplicitTemplateArgs the explicit template arguments provided 40460b57cec5SDimitry Andric /// for this call. 40470b57cec5SDimitry Andric /// 40480b57cec5SDimitry Andric /// \param Args the function call arguments 40490b57cec5SDimitry Andric /// 40500b57cec5SDimitry Andric /// \param Specialization if template argument deduction was successful, 40510b57cec5SDimitry Andric /// this will be set to the function template specialization produced by 40520b57cec5SDimitry Andric /// template argument deduction. 40530b57cec5SDimitry Andric /// 40540b57cec5SDimitry Andric /// \param Info the argument will be updated to provide additional information 40550b57cec5SDimitry Andric /// about template argument deduction. 40560b57cec5SDimitry Andric /// 40570b57cec5SDimitry Andric /// \param CheckNonDependent A callback to invoke to check conversions for 40580b57cec5SDimitry Andric /// non-dependent parameters, between deduction and substitution, per DR1391. 40590b57cec5SDimitry Andric /// If this returns true, substitution will be skipped and we return 40600b57cec5SDimitry Andric /// TDK_NonDependentConversionFailure. The callback is passed the parameter 40610b57cec5SDimitry Andric /// types (after substituting explicit template arguments). 40620b57cec5SDimitry Andric /// 40630b57cec5SDimitry Andric /// \returns the result of template argument deduction. 40640b57cec5SDimitry Andric Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( 40650b57cec5SDimitry Andric FunctionTemplateDecl *FunctionTemplate, 40660b57cec5SDimitry Andric TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args, 40670b57cec5SDimitry Andric FunctionDecl *&Specialization, TemplateDeductionInfo &Info, 40680b57cec5SDimitry Andric bool PartialOverloading, 40690b57cec5SDimitry Andric llvm::function_ref<bool(ArrayRef<QualType>)> CheckNonDependent) { 40700b57cec5SDimitry Andric if (FunctionTemplate->isInvalidDecl()) 40710b57cec5SDimitry Andric return TDK_Invalid; 40720b57cec5SDimitry Andric 40730b57cec5SDimitry Andric FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 40740b57cec5SDimitry Andric unsigned NumParams = Function->getNumParams(); 40750b57cec5SDimitry Andric 40760b57cec5SDimitry Andric unsigned FirstInnerIndex = getFirstInnerIndex(FunctionTemplate); 40770b57cec5SDimitry Andric 40780b57cec5SDimitry Andric // C++ [temp.deduct.call]p1: 40790b57cec5SDimitry Andric // Template argument deduction is done by comparing each function template 40800b57cec5SDimitry Andric // parameter type (call it P) with the type of the corresponding argument 40810b57cec5SDimitry Andric // of the call (call it A) as described below. 40820b57cec5SDimitry Andric if (Args.size() < Function->getMinRequiredArguments() && !PartialOverloading) 40830b57cec5SDimitry Andric return TDK_TooFewArguments; 40840b57cec5SDimitry Andric else if (TooManyArguments(NumParams, Args.size(), PartialOverloading)) { 4085a7dea167SDimitry Andric const auto *Proto = Function->getType()->castAs<FunctionProtoType>(); 40860b57cec5SDimitry Andric if (Proto->isTemplateVariadic()) 40870b57cec5SDimitry Andric /* Do nothing */; 40880b57cec5SDimitry Andric else if (!Proto->isVariadic()) 40890b57cec5SDimitry Andric return TDK_TooManyArguments; 40900b57cec5SDimitry Andric } 40910b57cec5SDimitry Andric 40920b57cec5SDimitry Andric // The types of the parameters from which we will perform template argument 40930b57cec5SDimitry Andric // deduction. 40940b57cec5SDimitry Andric LocalInstantiationScope InstScope(*this); 40950b57cec5SDimitry Andric TemplateParameterList *TemplateParams 40960b57cec5SDimitry Andric = FunctionTemplate->getTemplateParameters(); 40970b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> Deduced; 40980b57cec5SDimitry Andric SmallVector<QualType, 8> ParamTypes; 40990b57cec5SDimitry Andric unsigned NumExplicitlySpecified = 0; 41000b57cec5SDimitry Andric if (ExplicitTemplateArgs) { 41015ffd83dbSDimitry Andric TemplateDeductionResult Result; 41025ffd83dbSDimitry Andric runWithSufficientStackSpace(Info.getLocation(), [&] { 41035ffd83dbSDimitry Andric Result = SubstituteExplicitTemplateArguments( 41045ffd83dbSDimitry Andric FunctionTemplate, *ExplicitTemplateArgs, Deduced, ParamTypes, nullptr, 41050b57cec5SDimitry Andric Info); 41065ffd83dbSDimitry Andric }); 41070b57cec5SDimitry Andric if (Result) 41080b57cec5SDimitry Andric return Result; 41090b57cec5SDimitry Andric 41100b57cec5SDimitry Andric NumExplicitlySpecified = Deduced.size(); 41110b57cec5SDimitry Andric } else { 41120b57cec5SDimitry Andric // Just fill in the parameter types from the function declaration. 41130b57cec5SDimitry Andric for (unsigned I = 0; I != NumParams; ++I) 41140b57cec5SDimitry Andric ParamTypes.push_back(Function->getParamDecl(I)->getType()); 41150b57cec5SDimitry Andric } 41160b57cec5SDimitry Andric 41170b57cec5SDimitry Andric SmallVector<OriginalCallArg, 8> OriginalCallArgs; 41180b57cec5SDimitry Andric 41190b57cec5SDimitry Andric // Deduce an argument of type ParamType from an expression with index ArgIdx. 41200b57cec5SDimitry Andric auto DeduceCallArgument = [&](QualType ParamType, unsigned ArgIdx) { 41210b57cec5SDimitry Andric // C++ [demp.deduct.call]p1: (DR1391) 41220b57cec5SDimitry Andric // Template argument deduction is done by comparing each function template 41230b57cec5SDimitry Andric // parameter that contains template-parameters that participate in 41240b57cec5SDimitry Andric // template argument deduction ... 41250b57cec5SDimitry Andric if (!hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) 41260b57cec5SDimitry Andric return Sema::TDK_Success; 41270b57cec5SDimitry Andric 41280b57cec5SDimitry Andric // ... with the type of the corresponding argument 41290b57cec5SDimitry Andric return DeduceTemplateArgumentsFromCallArgument( 41300b57cec5SDimitry Andric *this, TemplateParams, FirstInnerIndex, ParamType, Args[ArgIdx], Info, Deduced, 41310b57cec5SDimitry Andric OriginalCallArgs, /*Decomposed*/false, ArgIdx, /*TDF*/ 0); 41320b57cec5SDimitry Andric }; 41330b57cec5SDimitry Andric 41340b57cec5SDimitry Andric // Deduce template arguments from the function parameters. 41350b57cec5SDimitry Andric Deduced.resize(TemplateParams->size()); 41360b57cec5SDimitry Andric SmallVector<QualType, 8> ParamTypesForArgChecking; 41370b57cec5SDimitry Andric for (unsigned ParamIdx = 0, NumParamTypes = ParamTypes.size(), ArgIdx = 0; 41380b57cec5SDimitry Andric ParamIdx != NumParamTypes; ++ParamIdx) { 41390b57cec5SDimitry Andric QualType ParamType = ParamTypes[ParamIdx]; 41400b57cec5SDimitry Andric 41410b57cec5SDimitry Andric const PackExpansionType *ParamExpansion = 41420b57cec5SDimitry Andric dyn_cast<PackExpansionType>(ParamType); 41430b57cec5SDimitry Andric if (!ParamExpansion) { 41440b57cec5SDimitry Andric // Simple case: matching a function parameter to a function argument. 41450b57cec5SDimitry Andric if (ArgIdx >= Args.size()) 41460b57cec5SDimitry Andric break; 41470b57cec5SDimitry Andric 41480b57cec5SDimitry Andric ParamTypesForArgChecking.push_back(ParamType); 41490b57cec5SDimitry Andric if (auto Result = DeduceCallArgument(ParamType, ArgIdx++)) 41500b57cec5SDimitry Andric return Result; 41510b57cec5SDimitry Andric 41520b57cec5SDimitry Andric continue; 41530b57cec5SDimitry Andric } 41540b57cec5SDimitry Andric 41550b57cec5SDimitry Andric QualType ParamPattern = ParamExpansion->getPattern(); 41560b57cec5SDimitry Andric PackDeductionScope PackScope(*this, TemplateParams, Deduced, Info, 41570b57cec5SDimitry Andric ParamPattern); 41580b57cec5SDimitry Andric 41590b57cec5SDimitry Andric // C++0x [temp.deduct.call]p1: 41600b57cec5SDimitry Andric // For a function parameter pack that occurs at the end of the 41610b57cec5SDimitry Andric // parameter-declaration-list, the type A of each remaining argument of 41620b57cec5SDimitry Andric // the call is compared with the type P of the declarator-id of the 41630b57cec5SDimitry Andric // function parameter pack. Each comparison deduces template arguments 41640b57cec5SDimitry Andric // for subsequent positions in the template parameter packs expanded by 41650b57cec5SDimitry Andric // the function parameter pack. When a function parameter pack appears 41660b57cec5SDimitry Andric // in a non-deduced context [not at the end of the list], the type of 41670b57cec5SDimitry Andric // that parameter pack is never deduced. 41680b57cec5SDimitry Andric // 41690b57cec5SDimitry Andric // FIXME: The above rule allows the size of the parameter pack to change 41700b57cec5SDimitry Andric // after we skip it (in the non-deduced case). That makes no sense, so 41710b57cec5SDimitry Andric // we instead notionally deduce the pack against N arguments, where N is 41720b57cec5SDimitry Andric // the length of the explicitly-specified pack if it's expanded by the 41730b57cec5SDimitry Andric // parameter pack and 0 otherwise, and we treat each deduction as a 41740b57cec5SDimitry Andric // non-deduced context. 41750b57cec5SDimitry Andric if (ParamIdx + 1 == NumParamTypes || PackScope.hasFixedArity()) { 41760b57cec5SDimitry Andric for (; ArgIdx < Args.size() && PackScope.hasNextElement(); 41770b57cec5SDimitry Andric PackScope.nextPackElement(), ++ArgIdx) { 41780b57cec5SDimitry Andric ParamTypesForArgChecking.push_back(ParamPattern); 41790b57cec5SDimitry Andric if (auto Result = DeduceCallArgument(ParamPattern, ArgIdx)) 41800b57cec5SDimitry Andric return Result; 41810b57cec5SDimitry Andric } 41820b57cec5SDimitry Andric } else { 41830b57cec5SDimitry Andric // If the parameter type contains an explicitly-specified pack that we 41840b57cec5SDimitry Andric // could not expand, skip the number of parameters notionally created 41850b57cec5SDimitry Andric // by the expansion. 4186*bdd1243dSDimitry Andric std::optional<unsigned> NumExpansions = 4187*bdd1243dSDimitry Andric ParamExpansion->getNumExpansions(); 41880b57cec5SDimitry Andric if (NumExpansions && !PackScope.isPartiallyExpanded()) { 41890b57cec5SDimitry Andric for (unsigned I = 0; I != *NumExpansions && ArgIdx < Args.size(); 41900b57cec5SDimitry Andric ++I, ++ArgIdx) { 41910b57cec5SDimitry Andric ParamTypesForArgChecking.push_back(ParamPattern); 41920b57cec5SDimitry Andric // FIXME: Should we add OriginalCallArgs for these? What if the 41930b57cec5SDimitry Andric // corresponding argument is a list? 41940b57cec5SDimitry Andric PackScope.nextPackElement(); 41950b57cec5SDimitry Andric } 41960b57cec5SDimitry Andric } 41970b57cec5SDimitry Andric } 41980b57cec5SDimitry Andric 41990b57cec5SDimitry Andric // Build argument packs for each of the parameter packs expanded by this 42000b57cec5SDimitry Andric // pack expansion. 42010b57cec5SDimitry Andric if (auto Result = PackScope.finish()) 42020b57cec5SDimitry Andric return Result; 42030b57cec5SDimitry Andric } 42040b57cec5SDimitry Andric 42050b57cec5SDimitry Andric // Capture the context in which the function call is made. This is the context 42060b57cec5SDimitry Andric // that is needed when the accessibility of template arguments is checked. 42070b57cec5SDimitry Andric DeclContext *CallingCtx = CurContext; 42080b57cec5SDimitry Andric 42095ffd83dbSDimitry Andric TemplateDeductionResult Result; 42105ffd83dbSDimitry Andric runWithSufficientStackSpace(Info.getLocation(), [&] { 42115ffd83dbSDimitry Andric Result = FinishTemplateArgumentDeduction( 42120b57cec5SDimitry Andric FunctionTemplate, Deduced, NumExplicitlySpecified, Specialization, Info, 42130b57cec5SDimitry Andric &OriginalCallArgs, PartialOverloading, [&, CallingCtx]() { 42140b57cec5SDimitry Andric ContextRAII SavedContext(*this, CallingCtx); 42150b57cec5SDimitry Andric return CheckNonDependent(ParamTypesForArgChecking); 42160b57cec5SDimitry Andric }); 42175ffd83dbSDimitry Andric }); 42185ffd83dbSDimitry Andric return Result; 42190b57cec5SDimitry Andric } 42200b57cec5SDimitry Andric 42210b57cec5SDimitry Andric QualType Sema::adjustCCAndNoReturn(QualType ArgFunctionType, 42220b57cec5SDimitry Andric QualType FunctionType, 42230b57cec5SDimitry Andric bool AdjustExceptionSpec) { 42240b57cec5SDimitry Andric if (ArgFunctionType.isNull()) 42250b57cec5SDimitry Andric return ArgFunctionType; 42260b57cec5SDimitry Andric 4227a7dea167SDimitry Andric const auto *FunctionTypeP = FunctionType->castAs<FunctionProtoType>(); 4228a7dea167SDimitry Andric const auto *ArgFunctionTypeP = ArgFunctionType->castAs<FunctionProtoType>(); 42290b57cec5SDimitry Andric FunctionProtoType::ExtProtoInfo EPI = ArgFunctionTypeP->getExtProtoInfo(); 42300b57cec5SDimitry Andric bool Rebuild = false; 42310b57cec5SDimitry Andric 42320b57cec5SDimitry Andric CallingConv CC = FunctionTypeP->getCallConv(); 42330b57cec5SDimitry Andric if (EPI.ExtInfo.getCC() != CC) { 42340b57cec5SDimitry Andric EPI.ExtInfo = EPI.ExtInfo.withCallingConv(CC); 42350b57cec5SDimitry Andric Rebuild = true; 42360b57cec5SDimitry Andric } 42370b57cec5SDimitry Andric 42380b57cec5SDimitry Andric bool NoReturn = FunctionTypeP->getNoReturnAttr(); 42390b57cec5SDimitry Andric if (EPI.ExtInfo.getNoReturn() != NoReturn) { 42400b57cec5SDimitry Andric EPI.ExtInfo = EPI.ExtInfo.withNoReturn(NoReturn); 42410b57cec5SDimitry Andric Rebuild = true; 42420b57cec5SDimitry Andric } 42430b57cec5SDimitry Andric 42440b57cec5SDimitry Andric if (AdjustExceptionSpec && (FunctionTypeP->hasExceptionSpec() || 42450b57cec5SDimitry Andric ArgFunctionTypeP->hasExceptionSpec())) { 42460b57cec5SDimitry Andric EPI.ExceptionSpec = FunctionTypeP->getExtProtoInfo().ExceptionSpec; 42470b57cec5SDimitry Andric Rebuild = true; 42480b57cec5SDimitry Andric } 42490b57cec5SDimitry Andric 42500b57cec5SDimitry Andric if (!Rebuild) 42510b57cec5SDimitry Andric return ArgFunctionType; 42520b57cec5SDimitry Andric 42530b57cec5SDimitry Andric return Context.getFunctionType(ArgFunctionTypeP->getReturnType(), 42540b57cec5SDimitry Andric ArgFunctionTypeP->getParamTypes(), EPI); 42550b57cec5SDimitry Andric } 42560b57cec5SDimitry Andric 42570b57cec5SDimitry Andric /// Deduce template arguments when taking the address of a function 42580b57cec5SDimitry Andric /// template (C++ [temp.deduct.funcaddr]) or matching a specialization to 42590b57cec5SDimitry Andric /// a template. 42600b57cec5SDimitry Andric /// 42610b57cec5SDimitry Andric /// \param FunctionTemplate the function template for which we are performing 42620b57cec5SDimitry Andric /// template argument deduction. 42630b57cec5SDimitry Andric /// 42640b57cec5SDimitry Andric /// \param ExplicitTemplateArgs the explicitly-specified template 42650b57cec5SDimitry Andric /// arguments. 42660b57cec5SDimitry Andric /// 42670b57cec5SDimitry Andric /// \param ArgFunctionType the function type that will be used as the 42680b57cec5SDimitry Andric /// "argument" type (A) when performing template argument deduction from the 42690b57cec5SDimitry Andric /// function template's function type. This type may be NULL, if there is no 42700b57cec5SDimitry Andric /// argument type to compare against, in C++0x [temp.arg.explicit]p3. 42710b57cec5SDimitry Andric /// 42720b57cec5SDimitry Andric /// \param Specialization if template argument deduction was successful, 42730b57cec5SDimitry Andric /// this will be set to the function template specialization produced by 42740b57cec5SDimitry Andric /// template argument deduction. 42750b57cec5SDimitry Andric /// 42760b57cec5SDimitry Andric /// \param Info the argument will be updated to provide additional information 42770b57cec5SDimitry Andric /// about template argument deduction. 42780b57cec5SDimitry Andric /// 42790b57cec5SDimitry Andric /// \param IsAddressOfFunction If \c true, we are deducing as part of taking 42800b57cec5SDimitry Andric /// the address of a function template per [temp.deduct.funcaddr] and 42810b57cec5SDimitry Andric /// [over.over]. If \c false, we are looking up a function template 42820b57cec5SDimitry Andric /// specialization based on its signature, per [temp.deduct.decl]. 42830b57cec5SDimitry Andric /// 42840b57cec5SDimitry Andric /// \returns the result of template argument deduction. 42850b57cec5SDimitry Andric Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( 42860b57cec5SDimitry Andric FunctionTemplateDecl *FunctionTemplate, 42870b57cec5SDimitry Andric TemplateArgumentListInfo *ExplicitTemplateArgs, QualType ArgFunctionType, 42880b57cec5SDimitry Andric FunctionDecl *&Specialization, TemplateDeductionInfo &Info, 42890b57cec5SDimitry Andric bool IsAddressOfFunction) { 42900b57cec5SDimitry Andric if (FunctionTemplate->isInvalidDecl()) 42910b57cec5SDimitry Andric return TDK_Invalid; 42920b57cec5SDimitry Andric 42930b57cec5SDimitry Andric FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 42940b57cec5SDimitry Andric TemplateParameterList *TemplateParams 42950b57cec5SDimitry Andric = FunctionTemplate->getTemplateParameters(); 42960b57cec5SDimitry Andric QualType FunctionType = Function->getType(); 42970b57cec5SDimitry Andric 42980b57cec5SDimitry Andric // Substitute any explicit template arguments. 42990b57cec5SDimitry Andric LocalInstantiationScope InstScope(*this); 43000b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> Deduced; 43010b57cec5SDimitry Andric unsigned NumExplicitlySpecified = 0; 43020b57cec5SDimitry Andric SmallVector<QualType, 4> ParamTypes; 43030b57cec5SDimitry Andric if (ExplicitTemplateArgs) { 43045ffd83dbSDimitry Andric TemplateDeductionResult Result; 43055ffd83dbSDimitry Andric runWithSufficientStackSpace(Info.getLocation(), [&] { 43065ffd83dbSDimitry Andric Result = SubstituteExplicitTemplateArguments( 43075ffd83dbSDimitry Andric FunctionTemplate, *ExplicitTemplateArgs, Deduced, ParamTypes, 43085ffd83dbSDimitry Andric &FunctionType, Info); 43095ffd83dbSDimitry Andric }); 43105ffd83dbSDimitry Andric if (Result) 43110b57cec5SDimitry Andric return Result; 43120b57cec5SDimitry Andric 43130b57cec5SDimitry Andric NumExplicitlySpecified = Deduced.size(); 43140b57cec5SDimitry Andric } 43150b57cec5SDimitry Andric 43160b57cec5SDimitry Andric // When taking the address of a function, we require convertibility of 43170b57cec5SDimitry Andric // the resulting function type. Otherwise, we allow arbitrary mismatches 43180b57cec5SDimitry Andric // of calling convention and noreturn. 43190b57cec5SDimitry Andric if (!IsAddressOfFunction) 43200b57cec5SDimitry Andric ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, FunctionType, 43210b57cec5SDimitry Andric /*AdjustExceptionSpec*/false); 43220b57cec5SDimitry Andric 43230b57cec5SDimitry Andric // Unevaluated SFINAE context. 43240b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 43250b57cec5SDimitry Andric *this, Sema::ExpressionEvaluationContext::Unevaluated); 43260b57cec5SDimitry Andric SFINAETrap Trap(*this); 43270b57cec5SDimitry Andric 43280b57cec5SDimitry Andric Deduced.resize(TemplateParams->size()); 43290b57cec5SDimitry Andric 43300b57cec5SDimitry Andric // If the function has a deduced return type, substitute it for a dependent 43310b57cec5SDimitry Andric // type so that we treat it as a non-deduced context in what follows. If we 43320b57cec5SDimitry Andric // are looking up by signature, the signature type should also have a deduced 43330b57cec5SDimitry Andric // return type, which we instead expect to exactly match. 43340b57cec5SDimitry Andric bool HasDeducedReturnType = false; 43350b57cec5SDimitry Andric if (getLangOpts().CPlusPlus14 && IsAddressOfFunction && 43360b57cec5SDimitry Andric Function->getReturnType()->getContainedAutoType()) { 4337349cc55cSDimitry Andric FunctionType = SubstAutoTypeDependent(FunctionType); 43380b57cec5SDimitry Andric HasDeducedReturnType = true; 43390b57cec5SDimitry Andric } 43400b57cec5SDimitry Andric 4341349cc55cSDimitry Andric if (!ArgFunctionType.isNull() && !FunctionType.isNull()) { 43420b57cec5SDimitry Andric unsigned TDF = 43430b57cec5SDimitry Andric TDF_TopLevelParameterTypeList | TDF_AllowCompatibleFunctionType; 43440b57cec5SDimitry Andric // Deduce template arguments from the function type. 43450b57cec5SDimitry Andric if (TemplateDeductionResult Result 43460b57cec5SDimitry Andric = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, 43470b57cec5SDimitry Andric FunctionType, ArgFunctionType, 43480b57cec5SDimitry Andric Info, Deduced, TDF)) 43490b57cec5SDimitry Andric return Result; 43500b57cec5SDimitry Andric } 43510b57cec5SDimitry Andric 43525ffd83dbSDimitry Andric TemplateDeductionResult Result; 43535ffd83dbSDimitry Andric runWithSufficientStackSpace(Info.getLocation(), [&] { 43545ffd83dbSDimitry Andric Result = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 43550b57cec5SDimitry Andric NumExplicitlySpecified, 43565ffd83dbSDimitry Andric Specialization, Info); 43575ffd83dbSDimitry Andric }); 43585ffd83dbSDimitry Andric if (Result) 43590b57cec5SDimitry Andric return Result; 43600b57cec5SDimitry Andric 43610b57cec5SDimitry Andric // If the function has a deduced return type, deduce it now, so we can check 43620b57cec5SDimitry Andric // that the deduced function type matches the requested type. 43630b57cec5SDimitry Andric if (HasDeducedReturnType && 43640b57cec5SDimitry Andric Specialization->getReturnType()->isUndeducedType() && 43650b57cec5SDimitry Andric DeduceReturnType(Specialization, Info.getLocation(), false)) 43660b57cec5SDimitry Andric return TDK_MiscellaneousDeductionFailure; 43670b57cec5SDimitry Andric 43680b57cec5SDimitry Andric // If the function has a dependent exception specification, resolve it now, 43690b57cec5SDimitry Andric // so we can check that the exception specification matches. 43700b57cec5SDimitry Andric auto *SpecializationFPT = 43710b57cec5SDimitry Andric Specialization->getType()->castAs<FunctionProtoType>(); 43720b57cec5SDimitry Andric if (getLangOpts().CPlusPlus17 && 43730b57cec5SDimitry Andric isUnresolvedExceptionSpec(SpecializationFPT->getExceptionSpecType()) && 43740b57cec5SDimitry Andric !ResolveExceptionSpec(Info.getLocation(), SpecializationFPT)) 43750b57cec5SDimitry Andric return TDK_MiscellaneousDeductionFailure; 43760b57cec5SDimitry Andric 43770b57cec5SDimitry Andric // Adjust the exception specification of the argument to match the 43780b57cec5SDimitry Andric // substituted and resolved type we just formed. (Calling convention and 43790b57cec5SDimitry Andric // noreturn can't be dependent, so we don't actually need this for them 43800b57cec5SDimitry Andric // right now.) 43810b57cec5SDimitry Andric QualType SpecializationType = Specialization->getType(); 43820b57cec5SDimitry Andric if (!IsAddressOfFunction) 43830b57cec5SDimitry Andric ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, SpecializationType, 43840b57cec5SDimitry Andric /*AdjustExceptionSpec*/true); 43850b57cec5SDimitry Andric 43860b57cec5SDimitry Andric // If the requested function type does not match the actual type of the 43870b57cec5SDimitry Andric // specialization with respect to arguments of compatible pointer to function 43880b57cec5SDimitry Andric // types, template argument deduction fails. 43890b57cec5SDimitry Andric if (!ArgFunctionType.isNull()) { 43900b57cec5SDimitry Andric if (IsAddressOfFunction && 43910b57cec5SDimitry Andric !isSameOrCompatibleFunctionType( 43920b57cec5SDimitry Andric Context.getCanonicalType(SpecializationType), 43930b57cec5SDimitry Andric Context.getCanonicalType(ArgFunctionType))) 43940b57cec5SDimitry Andric return TDK_MiscellaneousDeductionFailure; 43950b57cec5SDimitry Andric 43960b57cec5SDimitry Andric if (!IsAddressOfFunction && 43970b57cec5SDimitry Andric !Context.hasSameType(SpecializationType, ArgFunctionType)) 43980b57cec5SDimitry Andric return TDK_MiscellaneousDeductionFailure; 43990b57cec5SDimitry Andric } 44000b57cec5SDimitry Andric 44010b57cec5SDimitry Andric return TDK_Success; 44020b57cec5SDimitry Andric } 44030b57cec5SDimitry Andric 44040b57cec5SDimitry Andric /// Deduce template arguments for a templated conversion 44050b57cec5SDimitry Andric /// function (C++ [temp.deduct.conv]) and, if successful, produce a 44060b57cec5SDimitry Andric /// conversion function template specialization. 44070b57cec5SDimitry Andric Sema::TemplateDeductionResult 44080b57cec5SDimitry Andric Sema::DeduceTemplateArguments(FunctionTemplateDecl *ConversionTemplate, 44090b57cec5SDimitry Andric QualType ToType, 44100b57cec5SDimitry Andric CXXConversionDecl *&Specialization, 44110b57cec5SDimitry Andric TemplateDeductionInfo &Info) { 44120b57cec5SDimitry Andric if (ConversionTemplate->isInvalidDecl()) 44130b57cec5SDimitry Andric return TDK_Invalid; 44140b57cec5SDimitry Andric 44150b57cec5SDimitry Andric CXXConversionDecl *ConversionGeneric 44160b57cec5SDimitry Andric = cast<CXXConversionDecl>(ConversionTemplate->getTemplatedDecl()); 44170b57cec5SDimitry Andric 44180b57cec5SDimitry Andric QualType FromType = ConversionGeneric->getConversionType(); 44190b57cec5SDimitry Andric 44200b57cec5SDimitry Andric // Canonicalize the types for deduction. 44210b57cec5SDimitry Andric QualType P = Context.getCanonicalType(FromType); 44220b57cec5SDimitry Andric QualType A = Context.getCanonicalType(ToType); 44230b57cec5SDimitry Andric 44240b57cec5SDimitry Andric // C++0x [temp.deduct.conv]p2: 44250b57cec5SDimitry Andric // If P is a reference type, the type referred to by P is used for 44260b57cec5SDimitry Andric // type deduction. 44270b57cec5SDimitry Andric if (const ReferenceType *PRef = P->getAs<ReferenceType>()) 44280b57cec5SDimitry Andric P = PRef->getPointeeType(); 44290b57cec5SDimitry Andric 44300b57cec5SDimitry Andric // C++0x [temp.deduct.conv]p4: 44310b57cec5SDimitry Andric // [...] If A is a reference type, the type referred to by A is used 44320b57cec5SDimitry Andric // for type deduction. 44330b57cec5SDimitry Andric if (const ReferenceType *ARef = A->getAs<ReferenceType>()) { 44340b57cec5SDimitry Andric A = ARef->getPointeeType(); 44350b57cec5SDimitry Andric // We work around a defect in the standard here: cv-qualifiers are also 44360b57cec5SDimitry Andric // removed from P and A in this case, unless P was a reference type. This 44370b57cec5SDimitry Andric // seems to mostly match what other compilers are doing. 44380b57cec5SDimitry Andric if (!FromType->getAs<ReferenceType>()) { 44390b57cec5SDimitry Andric A = A.getUnqualifiedType(); 44400b57cec5SDimitry Andric P = P.getUnqualifiedType(); 44410b57cec5SDimitry Andric } 44420b57cec5SDimitry Andric 44430b57cec5SDimitry Andric // C++ [temp.deduct.conv]p3: 44440b57cec5SDimitry Andric // 44450b57cec5SDimitry Andric // If A is not a reference type: 44460b57cec5SDimitry Andric } else { 44470b57cec5SDimitry Andric assert(!A->isReferenceType() && "Reference types were handled above"); 44480b57cec5SDimitry Andric 44490b57cec5SDimitry Andric // - If P is an array type, the pointer type produced by the 44500b57cec5SDimitry Andric // array-to-pointer standard conversion (4.2) is used in place 44510b57cec5SDimitry Andric // of P for type deduction; otherwise, 44520b57cec5SDimitry Andric if (P->isArrayType()) 44530b57cec5SDimitry Andric P = Context.getArrayDecayedType(P); 44540b57cec5SDimitry Andric // - If P is a function type, the pointer type produced by the 44550b57cec5SDimitry Andric // function-to-pointer standard conversion (4.3) is used in 44560b57cec5SDimitry Andric // place of P for type deduction; otherwise, 44570b57cec5SDimitry Andric else if (P->isFunctionType()) 44580b57cec5SDimitry Andric P = Context.getPointerType(P); 44590b57cec5SDimitry Andric // - If P is a cv-qualified type, the top level cv-qualifiers of 44600b57cec5SDimitry Andric // P's type are ignored for type deduction. 44610b57cec5SDimitry Andric else 44620b57cec5SDimitry Andric P = P.getUnqualifiedType(); 44630b57cec5SDimitry Andric 44640b57cec5SDimitry Andric // C++0x [temp.deduct.conv]p4: 44650b57cec5SDimitry Andric // If A is a cv-qualified type, the top level cv-qualifiers of A's 44660b57cec5SDimitry Andric // type are ignored for type deduction. If A is a reference type, the type 44670b57cec5SDimitry Andric // referred to by A is used for type deduction. 44680b57cec5SDimitry Andric A = A.getUnqualifiedType(); 44690b57cec5SDimitry Andric } 44700b57cec5SDimitry Andric 44710b57cec5SDimitry Andric // Unevaluated SFINAE context. 44720b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 44730b57cec5SDimitry Andric *this, Sema::ExpressionEvaluationContext::Unevaluated); 44740b57cec5SDimitry Andric SFINAETrap Trap(*this); 44750b57cec5SDimitry Andric 44760b57cec5SDimitry Andric // C++ [temp.deduct.conv]p1: 44770b57cec5SDimitry Andric // Template argument deduction is done by comparing the return 44780b57cec5SDimitry Andric // type of the template conversion function (call it P) with the 44790b57cec5SDimitry Andric // type that is required as the result of the conversion (call it 44800b57cec5SDimitry Andric // A) as described in 14.8.2.4. 44810b57cec5SDimitry Andric TemplateParameterList *TemplateParams 44820b57cec5SDimitry Andric = ConversionTemplate->getTemplateParameters(); 44830b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> Deduced; 44840b57cec5SDimitry Andric Deduced.resize(TemplateParams->size()); 44850b57cec5SDimitry Andric 44860b57cec5SDimitry Andric // C++0x [temp.deduct.conv]p4: 44870b57cec5SDimitry Andric // In general, the deduction process attempts to find template 44880b57cec5SDimitry Andric // argument values that will make the deduced A identical to 44890b57cec5SDimitry Andric // A. However, there are two cases that allow a difference: 44900b57cec5SDimitry Andric unsigned TDF = 0; 44910b57cec5SDimitry Andric // - If the original A is a reference type, A can be more 44920b57cec5SDimitry Andric // cv-qualified than the deduced A (i.e., the type referred to 44930b57cec5SDimitry Andric // by the reference) 44940b57cec5SDimitry Andric if (ToType->isReferenceType()) 44950b57cec5SDimitry Andric TDF |= TDF_ArgWithReferenceType; 44960b57cec5SDimitry Andric // - The deduced A can be another pointer or pointer to member 44970b57cec5SDimitry Andric // type that can be converted to A via a qualification 44980b57cec5SDimitry Andric // conversion. 44990b57cec5SDimitry Andric // 45000b57cec5SDimitry Andric // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when 45010b57cec5SDimitry Andric // both P and A are pointers or member pointers. In this case, we 45020b57cec5SDimitry Andric // just ignore cv-qualifiers completely). 45030b57cec5SDimitry Andric if ((P->isPointerType() && A->isPointerType()) || 45040b57cec5SDimitry Andric (P->isMemberPointerType() && A->isMemberPointerType())) 45050b57cec5SDimitry Andric TDF |= TDF_IgnoreQualifiers; 45060b57cec5SDimitry Andric if (TemplateDeductionResult Result 45070b57cec5SDimitry Andric = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, 45080b57cec5SDimitry Andric P, A, Info, Deduced, TDF)) 45090b57cec5SDimitry Andric return Result; 45100b57cec5SDimitry Andric 45110b57cec5SDimitry Andric // Create an Instantiation Scope for finalizing the operator. 45120b57cec5SDimitry Andric LocalInstantiationScope InstScope(*this); 45130b57cec5SDimitry Andric // Finish template argument deduction. 45140b57cec5SDimitry Andric FunctionDecl *ConversionSpecialized = nullptr; 45155ffd83dbSDimitry Andric TemplateDeductionResult Result; 45165ffd83dbSDimitry Andric runWithSufficientStackSpace(Info.getLocation(), [&] { 45175ffd83dbSDimitry Andric Result = FinishTemplateArgumentDeduction(ConversionTemplate, Deduced, 0, 45180b57cec5SDimitry Andric ConversionSpecialized, Info); 45195ffd83dbSDimitry Andric }); 45200b57cec5SDimitry Andric Specialization = cast_or_null<CXXConversionDecl>(ConversionSpecialized); 45210b57cec5SDimitry Andric return Result; 45220b57cec5SDimitry Andric } 45230b57cec5SDimitry Andric 45240b57cec5SDimitry Andric /// Deduce template arguments for a function template when there is 45250b57cec5SDimitry Andric /// nothing to deduce against (C++0x [temp.arg.explicit]p3). 45260b57cec5SDimitry Andric /// 45270b57cec5SDimitry Andric /// \param FunctionTemplate the function template for which we are performing 45280b57cec5SDimitry Andric /// template argument deduction. 45290b57cec5SDimitry Andric /// 45300b57cec5SDimitry Andric /// \param ExplicitTemplateArgs the explicitly-specified template 45310b57cec5SDimitry Andric /// arguments. 45320b57cec5SDimitry Andric /// 45330b57cec5SDimitry Andric /// \param Specialization if template argument deduction was successful, 45340b57cec5SDimitry Andric /// this will be set to the function template specialization produced by 45350b57cec5SDimitry Andric /// template argument deduction. 45360b57cec5SDimitry Andric /// 45370b57cec5SDimitry Andric /// \param Info the argument will be updated to provide additional information 45380b57cec5SDimitry Andric /// about template argument deduction. 45390b57cec5SDimitry Andric /// 45400b57cec5SDimitry Andric /// \param IsAddressOfFunction If \c true, we are deducing as part of taking 45410b57cec5SDimitry Andric /// the address of a function template in a context where we do not have a 45420b57cec5SDimitry Andric /// target type, per [over.over]. If \c false, we are looking up a function 45430b57cec5SDimitry Andric /// template specialization based on its signature, which only happens when 45440b57cec5SDimitry Andric /// deducing a function parameter type from an argument that is a template-id 45450b57cec5SDimitry Andric /// naming a function template specialization. 45460b57cec5SDimitry Andric /// 45470b57cec5SDimitry Andric /// \returns the result of template argument deduction. 45480b57cec5SDimitry Andric Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( 45490b57cec5SDimitry Andric FunctionTemplateDecl *FunctionTemplate, 45500b57cec5SDimitry Andric TemplateArgumentListInfo *ExplicitTemplateArgs, 45510b57cec5SDimitry Andric FunctionDecl *&Specialization, TemplateDeductionInfo &Info, 45520b57cec5SDimitry Andric bool IsAddressOfFunction) { 45530b57cec5SDimitry Andric return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, 45540b57cec5SDimitry Andric QualType(), Specialization, Info, 45550b57cec5SDimitry Andric IsAddressOfFunction); 45560b57cec5SDimitry Andric } 45570b57cec5SDimitry Andric 45580b57cec5SDimitry Andric namespace { 45590b57cec5SDimitry Andric struct DependentAuto { bool IsPack; }; 45600b57cec5SDimitry Andric 45610b57cec5SDimitry Andric /// Substitute the 'auto' specifier or deduced template specialization type 45620b57cec5SDimitry Andric /// specifier within a type for a given replacement type. 45630b57cec5SDimitry Andric class SubstituteDeducedTypeTransform : 45640b57cec5SDimitry Andric public TreeTransform<SubstituteDeducedTypeTransform> { 45650b57cec5SDimitry Andric QualType Replacement; 45660b57cec5SDimitry Andric bool ReplacementIsPack; 45670b57cec5SDimitry Andric bool UseTypeSugar; 45680b57cec5SDimitry Andric 45690b57cec5SDimitry Andric public: 45700b57cec5SDimitry Andric SubstituteDeducedTypeTransform(Sema &SemaRef, DependentAuto DA) 457104eeddc0SDimitry Andric : TreeTransform<SubstituteDeducedTypeTransform>(SemaRef), 45720b57cec5SDimitry Andric ReplacementIsPack(DA.IsPack), UseTypeSugar(true) {} 45730b57cec5SDimitry Andric 45740b57cec5SDimitry Andric SubstituteDeducedTypeTransform(Sema &SemaRef, QualType Replacement, 45750b57cec5SDimitry Andric bool UseTypeSugar = true) 45760b57cec5SDimitry Andric : TreeTransform<SubstituteDeducedTypeTransform>(SemaRef), 45770b57cec5SDimitry Andric Replacement(Replacement), ReplacementIsPack(false), 45780b57cec5SDimitry Andric UseTypeSugar(UseTypeSugar) {} 45790b57cec5SDimitry Andric 45800b57cec5SDimitry Andric QualType TransformDesugared(TypeLocBuilder &TLB, DeducedTypeLoc TL) { 45810b57cec5SDimitry Andric assert(isa<TemplateTypeParmType>(Replacement) && 45820b57cec5SDimitry Andric "unexpected unsugared replacement kind"); 45830b57cec5SDimitry Andric QualType Result = Replacement; 45840b57cec5SDimitry Andric TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result); 45850b57cec5SDimitry Andric NewTL.setNameLoc(TL.getNameLoc()); 45860b57cec5SDimitry Andric return Result; 45870b57cec5SDimitry Andric } 45880b57cec5SDimitry Andric 45890b57cec5SDimitry Andric QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) { 45900b57cec5SDimitry Andric // If we're building the type pattern to deduce against, don't wrap the 45910b57cec5SDimitry Andric // substituted type in an AutoType. Certain template deduction rules 45920b57cec5SDimitry Andric // apply only when a template type parameter appears directly (and not if 45930b57cec5SDimitry Andric // the parameter is found through desugaring). For instance: 45940b57cec5SDimitry Andric // auto &&lref = lvalue; 45950b57cec5SDimitry Andric // must transform into "rvalue reference to T" not "rvalue reference to 45960b57cec5SDimitry Andric // auto type deduced as T" in order for [temp.deduct.call]p3 to apply. 45970b57cec5SDimitry Andric // 45980b57cec5SDimitry Andric // FIXME: Is this still necessary? 45990b57cec5SDimitry Andric if (!UseTypeSugar) 46000b57cec5SDimitry Andric return TransformDesugared(TLB, TL); 46010b57cec5SDimitry Andric 46020b57cec5SDimitry Andric QualType Result = SemaRef.Context.getAutoType( 46030b57cec5SDimitry Andric Replacement, TL.getTypePtr()->getKeyword(), Replacement.isNull(), 460455e4f9d5SDimitry Andric ReplacementIsPack, TL.getTypePtr()->getTypeConstraintConcept(), 460555e4f9d5SDimitry Andric TL.getTypePtr()->getTypeConstraintArguments()); 46060b57cec5SDimitry Andric auto NewTL = TLB.push<AutoTypeLoc>(Result); 460755e4f9d5SDimitry Andric NewTL.copy(TL); 46080b57cec5SDimitry Andric return Result; 46090b57cec5SDimitry Andric } 46100b57cec5SDimitry Andric 46110b57cec5SDimitry Andric QualType TransformDeducedTemplateSpecializationType( 46120b57cec5SDimitry Andric TypeLocBuilder &TLB, DeducedTemplateSpecializationTypeLoc TL) { 46130b57cec5SDimitry Andric if (!UseTypeSugar) 46140b57cec5SDimitry Andric return TransformDesugared(TLB, TL); 46150b57cec5SDimitry Andric 46160b57cec5SDimitry Andric QualType Result = SemaRef.Context.getDeducedTemplateSpecializationType( 46170b57cec5SDimitry Andric TL.getTypePtr()->getTemplateName(), 46180b57cec5SDimitry Andric Replacement, Replacement.isNull()); 46190b57cec5SDimitry Andric auto NewTL = TLB.push<DeducedTemplateSpecializationTypeLoc>(Result); 46200b57cec5SDimitry Andric NewTL.setNameLoc(TL.getNameLoc()); 46210b57cec5SDimitry Andric return Result; 46220b57cec5SDimitry Andric } 46230b57cec5SDimitry Andric 46240b57cec5SDimitry Andric ExprResult TransformLambdaExpr(LambdaExpr *E) { 46250b57cec5SDimitry Andric // Lambdas never need to be transformed. 46260b57cec5SDimitry Andric return E; 46270b57cec5SDimitry Andric } 46280b57cec5SDimitry Andric 46290b57cec5SDimitry Andric QualType Apply(TypeLoc TL) { 46300b57cec5SDimitry Andric // Create some scratch storage for the transformed type locations. 46310b57cec5SDimitry Andric // FIXME: We're just going to throw this information away. Don't build it. 46320b57cec5SDimitry Andric TypeLocBuilder TLB; 46330b57cec5SDimitry Andric TLB.reserve(TL.getFullDataSize()); 46340b57cec5SDimitry Andric return TransformType(TLB, TL); 46350b57cec5SDimitry Andric } 46360b57cec5SDimitry Andric }; 46370b57cec5SDimitry Andric 46380b57cec5SDimitry Andric } // namespace 46390b57cec5SDimitry Andric 4640*bdd1243dSDimitry Andric static bool CheckDeducedPlaceholderConstraints(Sema &S, const AutoType &Type, 4641*bdd1243dSDimitry Andric AutoTypeLoc TypeLoc, 4642*bdd1243dSDimitry Andric QualType Deduced) { 464355e4f9d5SDimitry Andric ConstraintSatisfaction Satisfaction; 464455e4f9d5SDimitry Andric ConceptDecl *Concept = Type.getTypeConstraintConcept(); 464555e4f9d5SDimitry Andric TemplateArgumentListInfo TemplateArgs(TypeLoc.getLAngleLoc(), 464655e4f9d5SDimitry Andric TypeLoc.getRAngleLoc()); 464755e4f9d5SDimitry Andric TemplateArgs.addArgument( 464855e4f9d5SDimitry Andric TemplateArgumentLoc(TemplateArgument(Deduced), 464955e4f9d5SDimitry Andric S.Context.getTrivialTypeSourceInfo( 465055e4f9d5SDimitry Andric Deduced, TypeLoc.getNameLoc()))); 465155e4f9d5SDimitry Andric for (unsigned I = 0, C = TypeLoc.getNumArgs(); I != C; ++I) 465255e4f9d5SDimitry Andric TemplateArgs.addArgument(TypeLoc.getArgLoc(I)); 465355e4f9d5SDimitry Andric 4654*bdd1243dSDimitry Andric llvm::SmallVector<TemplateArgument, 4> SugaredConverted, CanonicalConverted; 465555e4f9d5SDimitry Andric if (S.CheckTemplateArgumentList(Concept, SourceLocation(), TemplateArgs, 4656*bdd1243dSDimitry Andric /*PartialTemplateArgs=*/false, 4657*bdd1243dSDimitry Andric SugaredConverted, CanonicalConverted)) 4658*bdd1243dSDimitry Andric return true; 4659*bdd1243dSDimitry Andric MultiLevelTemplateArgumentList MLTAL(Concept, CanonicalConverted, 4660*bdd1243dSDimitry Andric /*Final=*/false); 466155e4f9d5SDimitry Andric if (S.CheckConstraintSatisfaction(Concept, {Concept->getConstraintExpr()}, 4662*bdd1243dSDimitry Andric MLTAL, TypeLoc.getLocalSourceRange(), 466355e4f9d5SDimitry Andric Satisfaction)) 4664*bdd1243dSDimitry Andric return true; 466555e4f9d5SDimitry Andric if (!Satisfaction.IsSatisfied) { 466655e4f9d5SDimitry Andric std::string Buf; 466755e4f9d5SDimitry Andric llvm::raw_string_ostream OS(Buf); 466855e4f9d5SDimitry Andric OS << "'" << Concept->getName(); 466955e4f9d5SDimitry Andric if (TypeLoc.hasExplicitTemplateArgs()) { 4670fe6060f1SDimitry Andric printTemplateArgumentList( 4671fe6060f1SDimitry Andric OS, Type.getTypeConstraintArguments(), S.getPrintingPolicy(), 4672fe6060f1SDimitry Andric Type.getTypeConstraintConcept()->getTemplateParameters()); 467355e4f9d5SDimitry Andric } 467455e4f9d5SDimitry Andric OS << "'"; 467555e4f9d5SDimitry Andric OS.flush(); 467655e4f9d5SDimitry Andric S.Diag(TypeLoc.getConceptNameLoc(), 467755e4f9d5SDimitry Andric diag::err_placeholder_constraints_not_satisfied) 467855e4f9d5SDimitry Andric << Deduced << Buf << TypeLoc.getLocalSourceRange(); 467955e4f9d5SDimitry Andric S.DiagnoseUnsatisfiedConstraint(Satisfaction); 4680*bdd1243dSDimitry Andric return true; 468155e4f9d5SDimitry Andric } 4682*bdd1243dSDimitry Andric return false; 468355e4f9d5SDimitry Andric } 468455e4f9d5SDimitry Andric 46850b57cec5SDimitry Andric /// Deduce the type for an auto type-specifier (C++11 [dcl.spec.auto]p6) 46860b57cec5SDimitry Andric /// 46870b57cec5SDimitry Andric /// Note that this is done even if the initializer is dependent. (This is 46880b57cec5SDimitry Andric /// necessary to support partial ordering of templates using 'auto'.) 46890b57cec5SDimitry Andric /// A dependent type will be produced when deducing from a dependent type. 46900b57cec5SDimitry Andric /// 46910b57cec5SDimitry Andric /// \param Type the type pattern using the auto type-specifier. 46920b57cec5SDimitry Andric /// \param Init the initializer for the variable whose type is to be deduced. 46930b57cec5SDimitry Andric /// \param Result if type deduction was successful, this will be set to the 46940b57cec5SDimitry Andric /// deduced type. 4695*bdd1243dSDimitry Andric /// \param Info the argument will be updated to provide additional information 4696*bdd1243dSDimitry Andric /// about template argument deduction. 4697*bdd1243dSDimitry Andric /// \param DependentDeduction Set if we should permit deduction in 46980b57cec5SDimitry Andric /// dependent cases. This is necessary for template partial ordering with 4699*bdd1243dSDimitry Andric /// 'auto' template parameters. The template parameter depth to be used 4700*bdd1243dSDimitry Andric /// should be specified in the 'Info' parameter. 470155e4f9d5SDimitry Andric /// \param IgnoreConstraints Set if we should not fail if the deduced type does 470255e4f9d5SDimitry Andric /// not satisfy the type-constraint in the auto type. 4703*bdd1243dSDimitry Andric Sema::TemplateDeductionResult Sema::DeduceAutoType(TypeLoc Type, Expr *Init, 4704*bdd1243dSDimitry Andric QualType &Result, 4705*bdd1243dSDimitry Andric TemplateDeductionInfo &Info, 4706*bdd1243dSDimitry Andric bool DependentDeduction, 470755e4f9d5SDimitry Andric bool IgnoreConstraints) { 4708*bdd1243dSDimitry Andric assert(DependentDeduction || Info.getDeducedDepth() == 0); 47095ffd83dbSDimitry Andric if (Init->containsErrors()) 4710*bdd1243dSDimitry Andric return TDK_AlreadyDiagnosed; 4711*bdd1243dSDimitry Andric 4712*bdd1243dSDimitry Andric const AutoType *AT = Type.getType()->getContainedAutoType(); 4713*bdd1243dSDimitry Andric assert(AT); 4714*bdd1243dSDimitry Andric 4715*bdd1243dSDimitry Andric if (Init->getType()->isNonOverloadPlaceholderType() || AT->isDecltypeAuto()) { 47160b57cec5SDimitry Andric ExprResult NonPlaceholder = CheckPlaceholderExpr(Init); 47170b57cec5SDimitry Andric if (NonPlaceholder.isInvalid()) 4718*bdd1243dSDimitry Andric return TDK_AlreadyDiagnosed; 47190b57cec5SDimitry Andric Init = NonPlaceholder.get(); 47200b57cec5SDimitry Andric } 47210b57cec5SDimitry Andric 47220b57cec5SDimitry Andric DependentAuto DependentResult = { 47230b57cec5SDimitry Andric /*.IsPack = */ (bool)Type.getAs<PackExpansionTypeLoc>()}; 47240b57cec5SDimitry Andric 4725*bdd1243dSDimitry Andric if (!DependentDeduction && 4726480093f4SDimitry Andric (Type.getType()->isDependentType() || Init->isTypeDependent() || 4727480093f4SDimitry Andric Init->containsUnexpandedParameterPack())) { 47280b57cec5SDimitry Andric Result = SubstituteDeducedTypeTransform(*this, DependentResult).Apply(Type); 47290b57cec5SDimitry Andric assert(!Result.isNull() && "substituting DependentTy can't fail"); 4730*bdd1243dSDimitry Andric return TDK_Success; 47310b57cec5SDimitry Andric } 47320b57cec5SDimitry Andric 4733*bdd1243dSDimitry Andric auto *InitList = dyn_cast<InitListExpr>(Init); 4734*bdd1243dSDimitry Andric if (!getLangOpts().CPlusPlus && InitList) { 47350b57cec5SDimitry Andric Diag(Init->getBeginLoc(), diag::err_auto_init_list_from_c); 4736*bdd1243dSDimitry Andric return TDK_AlreadyDiagnosed; 47370b57cec5SDimitry Andric } 47380b57cec5SDimitry Andric 47390b57cec5SDimitry Andric // Deduce type of TemplParam in Func(Init) 47400b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 1> Deduced; 47410b57cec5SDimitry Andric Deduced.resize(1); 47420b57cec5SDimitry Andric 47430b57cec5SDimitry Andric // If deduction failed, don't diagnose if the initializer is dependent; it 47440b57cec5SDimitry Andric // might acquire a matching type in the instantiation. 4745*bdd1243dSDimitry Andric auto DeductionFailed = [&](TemplateDeductionResult TDK) { 47460b57cec5SDimitry Andric if (Init->isTypeDependent()) { 47470b57cec5SDimitry Andric Result = 47480b57cec5SDimitry Andric SubstituteDeducedTypeTransform(*this, DependentResult).Apply(Type); 47490b57cec5SDimitry Andric assert(!Result.isNull() && "substituting DependentTy can't fail"); 4750*bdd1243dSDimitry Andric return TDK_Success; 47510b57cec5SDimitry Andric } 4752*bdd1243dSDimitry Andric return TDK; 47530b57cec5SDimitry Andric }; 47540b57cec5SDimitry Andric 47550b57cec5SDimitry Andric SmallVector<OriginalCallArg, 4> OriginalCallArgs; 47560b57cec5SDimitry Andric 4757*bdd1243dSDimitry Andric QualType DeducedType; 4758*bdd1243dSDimitry Andric // If this is a 'decltype(auto)' specifier, do the decltype dance. 4759*bdd1243dSDimitry Andric if (AT->isDecltypeAuto()) { 47600b57cec5SDimitry Andric if (InitList) { 4761*bdd1243dSDimitry Andric Diag(Init->getBeginLoc(), diag::err_decltype_auto_initializer_list); 4762*bdd1243dSDimitry Andric return TDK_AlreadyDiagnosed; 4763*bdd1243dSDimitry Andric } 47640b57cec5SDimitry Andric 4765*bdd1243dSDimitry Andric DeducedType = getDecltypeForExpr(Init); 4766*bdd1243dSDimitry Andric assert(!DeducedType.isNull()); 4767*bdd1243dSDimitry Andric } else { 4768*bdd1243dSDimitry Andric LocalInstantiationScope InstScope(*this); 4769*bdd1243dSDimitry Andric 4770*bdd1243dSDimitry Andric // Build template<class TemplParam> void Func(FuncParam); 4771*bdd1243dSDimitry Andric SourceLocation Loc = Init->getExprLoc(); 4772*bdd1243dSDimitry Andric TemplateTypeParmDecl *TemplParam = TemplateTypeParmDecl::Create( 4773*bdd1243dSDimitry Andric Context, nullptr, SourceLocation(), Loc, Info.getDeducedDepth(), 0, 4774*bdd1243dSDimitry Andric nullptr, false, false, false); 4775*bdd1243dSDimitry Andric QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0); 4776*bdd1243dSDimitry Andric NamedDecl *TemplParamPtr = TemplParam; 4777*bdd1243dSDimitry Andric FixedSizeTemplateParameterListStorage<1, false> TemplateParamsSt( 4778*bdd1243dSDimitry Andric Context, Loc, Loc, TemplParamPtr, Loc, nullptr); 4779*bdd1243dSDimitry Andric 4780*bdd1243dSDimitry Andric if (InitList) { 4781*bdd1243dSDimitry Andric // Notionally, we substitute std::initializer_list<T> for 'auto' and 4782*bdd1243dSDimitry Andric // deduce against that. Such deduction only succeeds if removing 4783*bdd1243dSDimitry Andric // cv-qualifiers and references results in std::initializer_list<T>. 4784*bdd1243dSDimitry Andric if (!Type.getType().getNonReferenceType()->getAs<AutoType>()) 4785*bdd1243dSDimitry Andric return TDK_Invalid; 4786a7dea167SDimitry Andric 47870b57cec5SDimitry Andric SourceRange DeducedFromInitRange; 4788*bdd1243dSDimitry Andric for (Expr *Init : InitList->inits()) { 4789*bdd1243dSDimitry Andric // Resolving a core issue: a braced-init-list containing any designators 4790*bdd1243dSDimitry Andric // is a non-deduced context. 4791*bdd1243dSDimitry Andric if (isa<DesignatedInitExpr>(Init)) 4792*bdd1243dSDimitry Andric return TDK_Invalid; 47930b57cec5SDimitry Andric if (auto TDK = DeduceTemplateArgumentsFromCallArgument( 4794*bdd1243dSDimitry Andric *this, TemplateParamsSt.get(), 0, TemplArg, Init, Info, Deduced, 4795*bdd1243dSDimitry Andric OriginalCallArgs, /*Decomposed=*/true, 4796*bdd1243dSDimitry Andric /*ArgIdx=*/0, /*TDF=*/0)) { 4797*bdd1243dSDimitry Andric if (TDK == TDK_Inconsistent) { 4798*bdd1243dSDimitry Andric Diag(Info.getLocation(), diag::err_auto_inconsistent_deduction) 4799*bdd1243dSDimitry Andric << Info.FirstArg << Info.SecondArg << DeducedFromInitRange 4800*bdd1243dSDimitry Andric << Init->getSourceRange(); 4801*bdd1243dSDimitry Andric return DeductionFailed(TDK_AlreadyDiagnosed); 4802*bdd1243dSDimitry Andric } 4803*bdd1243dSDimitry Andric return DeductionFailed(TDK); 4804*bdd1243dSDimitry Andric } 48050b57cec5SDimitry Andric 48060b57cec5SDimitry Andric if (DeducedFromInitRange.isInvalid() && 48070b57cec5SDimitry Andric Deduced[0].getKind() != TemplateArgument::Null) 48080b57cec5SDimitry Andric DeducedFromInitRange = Init->getSourceRange(); 48090b57cec5SDimitry Andric } 48100b57cec5SDimitry Andric } else { 48110b57cec5SDimitry Andric if (!getLangOpts().CPlusPlus && Init->refersToBitField()) { 48120b57cec5SDimitry Andric Diag(Loc, diag::err_auto_bitfield); 4813*bdd1243dSDimitry Andric return TDK_AlreadyDiagnosed; 48140b57cec5SDimitry Andric } 4815*bdd1243dSDimitry Andric QualType FuncParam = 4816*bdd1243dSDimitry Andric SubstituteDeducedTypeTransform(*this, TemplArg).Apply(Type); 4817*bdd1243dSDimitry Andric assert(!FuncParam.isNull() && 4818*bdd1243dSDimitry Andric "substituting template parameter for 'auto' failed"); 48190b57cec5SDimitry Andric if (auto TDK = DeduceTemplateArgumentsFromCallArgument( 48200b57cec5SDimitry Andric *this, TemplateParamsSt.get(), 0, FuncParam, Init, Info, Deduced, 4821*bdd1243dSDimitry Andric OriginalCallArgs, /*Decomposed=*/false, /*ArgIdx=*/0, /*TDF=*/0)) 4822*bdd1243dSDimitry Andric return DeductionFailed(TDK); 48230b57cec5SDimitry Andric } 48240b57cec5SDimitry Andric 48250b57cec5SDimitry Andric // Could be null if somehow 'auto' appears in a non-deduced context. 48260b57cec5SDimitry Andric if (Deduced[0].getKind() != TemplateArgument::Type) 4827*bdd1243dSDimitry Andric return DeductionFailed(TDK_Incomplete); 4828*bdd1243dSDimitry Andric DeducedType = Deduced[0].getAsType(); 48290b57cec5SDimitry Andric 48300b57cec5SDimitry Andric if (InitList) { 48310b57cec5SDimitry Andric DeducedType = BuildStdInitializerList(DeducedType, Loc); 48320b57cec5SDimitry Andric if (DeducedType.isNull()) 4833*bdd1243dSDimitry Andric return TDK_AlreadyDiagnosed; 4834*bdd1243dSDimitry Andric } 48350b57cec5SDimitry Andric } 48360b57cec5SDimitry Andric 4837*bdd1243dSDimitry Andric if (!Result.isNull()) { 4838*bdd1243dSDimitry Andric if (!Context.hasSameType(DeducedType, Result)) { 4839*bdd1243dSDimitry Andric Info.FirstArg = Result; 4840*bdd1243dSDimitry Andric Info.SecondArg = DeducedType; 4841*bdd1243dSDimitry Andric return DeductionFailed(TDK_Inconsistent); 484255e4f9d5SDimitry Andric } 4843*bdd1243dSDimitry Andric DeducedType = Context.getCommonSugaredType(Result, DeducedType); 484455e4f9d5SDimitry Andric } 484555e4f9d5SDimitry Andric 4846*bdd1243dSDimitry Andric if (AT->isConstrained() && !IgnoreConstraints && 4847*bdd1243dSDimitry Andric CheckDeducedPlaceholderConstraints( 4848*bdd1243dSDimitry Andric *this, *AT, Type.getContainedAutoTypeLoc(), DeducedType)) 4849*bdd1243dSDimitry Andric return TDK_AlreadyDiagnosed; 4850*bdd1243dSDimitry Andric 48510b57cec5SDimitry Andric Result = SubstituteDeducedTypeTransform(*this, DeducedType).Apply(Type); 48520b57cec5SDimitry Andric if (Result.isNull()) 4853*bdd1243dSDimitry Andric return TDK_AlreadyDiagnosed; 48540b57cec5SDimitry Andric 48550b57cec5SDimitry Andric // Check that the deduced argument type is compatible with the original 48560b57cec5SDimitry Andric // argument type per C++ [temp.deduct.call]p4. 48570b57cec5SDimitry Andric QualType DeducedA = InitList ? Deduced[0].getAsType() : Result; 48580b57cec5SDimitry Andric for (const OriginalCallArg &OriginalArg : OriginalCallArgs) { 48590b57cec5SDimitry Andric assert((bool)InitList == OriginalArg.DecomposedParam && 48600b57cec5SDimitry Andric "decomposed non-init-list in auto deduction?"); 48610b57cec5SDimitry Andric if (auto TDK = 48620b57cec5SDimitry Andric CheckOriginalCallArgDeduction(*this, Info, OriginalArg, DeducedA)) { 48630b57cec5SDimitry Andric Result = QualType(); 4864*bdd1243dSDimitry Andric return DeductionFailed(TDK); 48650b57cec5SDimitry Andric } 48660b57cec5SDimitry Andric } 48670b57cec5SDimitry Andric 4868*bdd1243dSDimitry Andric return TDK_Success; 48690b57cec5SDimitry Andric } 48700b57cec5SDimitry Andric 48710b57cec5SDimitry Andric QualType Sema::SubstAutoType(QualType TypeWithAuto, 48720b57cec5SDimitry Andric QualType TypeToReplaceAuto) { 4873349cc55cSDimitry Andric assert(TypeToReplaceAuto != Context.DependentTy); 48740b57cec5SDimitry Andric return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto) 48750b57cec5SDimitry Andric .TransformType(TypeWithAuto); 48760b57cec5SDimitry Andric } 48770b57cec5SDimitry Andric 48780b57cec5SDimitry Andric TypeSourceInfo *Sema::SubstAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto, 48790b57cec5SDimitry Andric QualType TypeToReplaceAuto) { 4880349cc55cSDimitry Andric assert(TypeToReplaceAuto != Context.DependentTy); 48810b57cec5SDimitry Andric return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto) 48820b57cec5SDimitry Andric .TransformType(TypeWithAuto); 48830b57cec5SDimitry Andric } 48840b57cec5SDimitry Andric 4885349cc55cSDimitry Andric QualType Sema::SubstAutoTypeDependent(QualType TypeWithAuto) { 4886349cc55cSDimitry Andric return SubstituteDeducedTypeTransform(*this, DependentAuto{false}) 4887349cc55cSDimitry Andric .TransformType(TypeWithAuto); 4888349cc55cSDimitry Andric } 4889349cc55cSDimitry Andric 4890349cc55cSDimitry Andric TypeSourceInfo * 4891349cc55cSDimitry Andric Sema::SubstAutoTypeSourceInfoDependent(TypeSourceInfo *TypeWithAuto) { 4892349cc55cSDimitry Andric return SubstituteDeducedTypeTransform(*this, DependentAuto{false}) 4893349cc55cSDimitry Andric .TransformType(TypeWithAuto); 4894349cc55cSDimitry Andric } 4895349cc55cSDimitry Andric 48960b57cec5SDimitry Andric QualType Sema::ReplaceAutoType(QualType TypeWithAuto, 48970b57cec5SDimitry Andric QualType TypeToReplaceAuto) { 48980b57cec5SDimitry Andric return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto, 48990b57cec5SDimitry Andric /*UseTypeSugar*/ false) 49000b57cec5SDimitry Andric .TransformType(TypeWithAuto); 49010b57cec5SDimitry Andric } 49020b57cec5SDimitry Andric 4903e63539f3SDimitry Andric TypeSourceInfo *Sema::ReplaceAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto, 4904e63539f3SDimitry Andric QualType TypeToReplaceAuto) { 4905e63539f3SDimitry Andric return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto, 4906e63539f3SDimitry Andric /*UseTypeSugar*/ false) 4907e63539f3SDimitry Andric .TransformType(TypeWithAuto); 4908e63539f3SDimitry Andric } 4909e63539f3SDimitry Andric 49100b57cec5SDimitry Andric void Sema::DiagnoseAutoDeductionFailure(VarDecl *VDecl, Expr *Init) { 49110b57cec5SDimitry Andric if (isa<InitListExpr>(Init)) 49120b57cec5SDimitry Andric Diag(VDecl->getLocation(), 49130b57cec5SDimitry Andric VDecl->isInitCapture() 49140b57cec5SDimitry Andric ? diag::err_init_capture_deduction_failure_from_init_list 49150b57cec5SDimitry Andric : diag::err_auto_var_deduction_failure_from_init_list) 49160b57cec5SDimitry Andric << VDecl->getDeclName() << VDecl->getType() << Init->getSourceRange(); 49170b57cec5SDimitry Andric else 49180b57cec5SDimitry Andric Diag(VDecl->getLocation(), 49190b57cec5SDimitry Andric VDecl->isInitCapture() ? diag::err_init_capture_deduction_failure 49200b57cec5SDimitry Andric : diag::err_auto_var_deduction_failure) 49210b57cec5SDimitry Andric << VDecl->getDeclName() << VDecl->getType() << Init->getType() 49220b57cec5SDimitry Andric << Init->getSourceRange(); 49230b57cec5SDimitry Andric } 49240b57cec5SDimitry Andric 49250b57cec5SDimitry Andric bool Sema::DeduceReturnType(FunctionDecl *FD, SourceLocation Loc, 49260b57cec5SDimitry Andric bool Diagnose) { 49270b57cec5SDimitry Andric assert(FD->getReturnType()->isUndeducedType()); 49280b57cec5SDimitry Andric 49290b57cec5SDimitry Andric // For a lambda's conversion operator, deduce any 'auto' or 'decltype(auto)' 49300b57cec5SDimitry Andric // within the return type from the call operator's type. 49310b57cec5SDimitry Andric if (isLambdaConversionOperator(FD)) { 49320b57cec5SDimitry Andric CXXRecordDecl *Lambda = cast<CXXMethodDecl>(FD)->getParent(); 49330b57cec5SDimitry Andric FunctionDecl *CallOp = Lambda->getLambdaCallOperator(); 49340b57cec5SDimitry Andric 49350b57cec5SDimitry Andric // For a generic lambda, instantiate the call operator if needed. 49360b57cec5SDimitry Andric if (auto *Args = FD->getTemplateSpecializationArgs()) { 49370b57cec5SDimitry Andric CallOp = InstantiateFunctionDeclaration( 49380b57cec5SDimitry Andric CallOp->getDescribedFunctionTemplate(), Args, Loc); 49390b57cec5SDimitry Andric if (!CallOp || CallOp->isInvalidDecl()) 49400b57cec5SDimitry Andric return true; 49410b57cec5SDimitry Andric 49420b57cec5SDimitry Andric // We might need to deduce the return type by instantiating the definition 49430b57cec5SDimitry Andric // of the operator() function. 4944a7dea167SDimitry Andric if (CallOp->getReturnType()->isUndeducedType()) { 4945a7dea167SDimitry Andric runWithSufficientStackSpace(Loc, [&] { 49460b57cec5SDimitry Andric InstantiateFunctionDefinition(Loc, CallOp); 4947a7dea167SDimitry Andric }); 4948a7dea167SDimitry Andric } 49490b57cec5SDimitry Andric } 49500b57cec5SDimitry Andric 49510b57cec5SDimitry Andric if (CallOp->isInvalidDecl()) 49520b57cec5SDimitry Andric return true; 49530b57cec5SDimitry Andric assert(!CallOp->getReturnType()->isUndeducedType() && 49540b57cec5SDimitry Andric "failed to deduce lambda return type"); 49550b57cec5SDimitry Andric 49560b57cec5SDimitry Andric // Build the new return type from scratch. 4957e8d8bef9SDimitry Andric CallingConv RetTyCC = FD->getReturnType() 4958e8d8bef9SDimitry Andric ->getPointeeType() 4959e8d8bef9SDimitry Andric ->castAs<FunctionType>() 4960e8d8bef9SDimitry Andric ->getCallConv(); 49610b57cec5SDimitry Andric QualType RetType = getLambdaConversionFunctionResultType( 4962e8d8bef9SDimitry Andric CallOp->getType()->castAs<FunctionProtoType>(), RetTyCC); 49630b57cec5SDimitry Andric if (FD->getReturnType()->getAs<PointerType>()) 49640b57cec5SDimitry Andric RetType = Context.getPointerType(RetType); 49650b57cec5SDimitry Andric else { 49660b57cec5SDimitry Andric assert(FD->getReturnType()->getAs<BlockPointerType>()); 49670b57cec5SDimitry Andric RetType = Context.getBlockPointerType(RetType); 49680b57cec5SDimitry Andric } 49690b57cec5SDimitry Andric Context.adjustDeducedFunctionResultType(FD, RetType); 49700b57cec5SDimitry Andric return false; 49710b57cec5SDimitry Andric } 49720b57cec5SDimitry Andric 4973a7dea167SDimitry Andric if (FD->getTemplateInstantiationPattern()) { 4974a7dea167SDimitry Andric runWithSufficientStackSpace(Loc, [&] { 49750b57cec5SDimitry Andric InstantiateFunctionDefinition(Loc, FD); 4976a7dea167SDimitry Andric }); 4977a7dea167SDimitry Andric } 49780b57cec5SDimitry Andric 49790b57cec5SDimitry Andric bool StillUndeduced = FD->getReturnType()->isUndeducedType(); 49800b57cec5SDimitry Andric if (StillUndeduced && Diagnose && !FD->isInvalidDecl()) { 49810b57cec5SDimitry Andric Diag(Loc, diag::err_auto_fn_used_before_defined) << FD; 49820b57cec5SDimitry Andric Diag(FD->getLocation(), diag::note_callee_decl) << FD; 49830b57cec5SDimitry Andric } 49840b57cec5SDimitry Andric 49850b57cec5SDimitry Andric return StillUndeduced; 49860b57cec5SDimitry Andric } 49870b57cec5SDimitry Andric 49880b57cec5SDimitry Andric /// If this is a non-static member function, 49890b57cec5SDimitry Andric static void 49900b57cec5SDimitry Andric AddImplicitObjectParameterType(ASTContext &Context, 49910b57cec5SDimitry Andric CXXMethodDecl *Method, 49920b57cec5SDimitry Andric SmallVectorImpl<QualType> &ArgTypes) { 49930b57cec5SDimitry Andric // C++11 [temp.func.order]p3: 49940b57cec5SDimitry Andric // [...] The new parameter is of type "reference to cv A," where cv are 49950b57cec5SDimitry Andric // the cv-qualifiers of the function template (if any) and A is 49960b57cec5SDimitry Andric // the class of which the function template is a member. 49970b57cec5SDimitry Andric // 49980b57cec5SDimitry Andric // The standard doesn't say explicitly, but we pick the appropriate kind of 49990b57cec5SDimitry Andric // reference type based on [over.match.funcs]p4. 50000b57cec5SDimitry Andric QualType ArgTy = Context.getTypeDeclType(Method->getParent()); 50010b57cec5SDimitry Andric ArgTy = Context.getQualifiedType(ArgTy, Method->getMethodQualifiers()); 50020b57cec5SDimitry Andric if (Method->getRefQualifier() == RQ_RValue) 50030b57cec5SDimitry Andric ArgTy = Context.getRValueReferenceType(ArgTy); 50040b57cec5SDimitry Andric else 50050b57cec5SDimitry Andric ArgTy = Context.getLValueReferenceType(ArgTy); 50060b57cec5SDimitry Andric ArgTypes.push_back(ArgTy); 50070b57cec5SDimitry Andric } 50080b57cec5SDimitry Andric 50090b57cec5SDimitry Andric /// Determine whether the function template \p FT1 is at least as 50100b57cec5SDimitry Andric /// specialized as \p FT2. 50110b57cec5SDimitry Andric static bool isAtLeastAsSpecializedAs(Sema &S, 50120b57cec5SDimitry Andric SourceLocation Loc, 50130b57cec5SDimitry Andric FunctionTemplateDecl *FT1, 50140b57cec5SDimitry Andric FunctionTemplateDecl *FT2, 50150b57cec5SDimitry Andric TemplatePartialOrderingContext TPOC, 50165ffd83dbSDimitry Andric unsigned NumCallArguments1, 50175ffd83dbSDimitry Andric bool Reversed) { 50185ffd83dbSDimitry Andric assert(!Reversed || TPOC == TPOC_Call); 50195ffd83dbSDimitry Andric 50200b57cec5SDimitry Andric FunctionDecl *FD1 = FT1->getTemplatedDecl(); 50210b57cec5SDimitry Andric FunctionDecl *FD2 = FT2->getTemplatedDecl(); 50220b57cec5SDimitry Andric const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); 50230b57cec5SDimitry Andric const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); 50240b57cec5SDimitry Andric 50250b57cec5SDimitry Andric assert(Proto1 && Proto2 && "Function templates must have prototypes"); 50260b57cec5SDimitry Andric TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); 50270b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> Deduced; 50280b57cec5SDimitry Andric Deduced.resize(TemplateParams->size()); 50290b57cec5SDimitry Andric 50300b57cec5SDimitry Andric // C++0x [temp.deduct.partial]p3: 50310b57cec5SDimitry Andric // The types used to determine the ordering depend on the context in which 50320b57cec5SDimitry Andric // the partial ordering is done: 50330b57cec5SDimitry Andric TemplateDeductionInfo Info(Loc); 50340b57cec5SDimitry Andric SmallVector<QualType, 4> Args2; 50350b57cec5SDimitry Andric switch (TPOC) { 50360b57cec5SDimitry Andric case TPOC_Call: { 50370b57cec5SDimitry Andric // - In the context of a function call, the function parameter types are 50380b57cec5SDimitry Andric // used. 50390b57cec5SDimitry Andric CXXMethodDecl *Method1 = dyn_cast<CXXMethodDecl>(FD1); 50400b57cec5SDimitry Andric CXXMethodDecl *Method2 = dyn_cast<CXXMethodDecl>(FD2); 50410b57cec5SDimitry Andric 50420b57cec5SDimitry Andric // C++11 [temp.func.order]p3: 50430b57cec5SDimitry Andric // [...] If only one of the function templates is a non-static 50440b57cec5SDimitry Andric // member, that function template is considered to have a new 50450b57cec5SDimitry Andric // first parameter inserted in its function parameter list. The 50460b57cec5SDimitry Andric // new parameter is of type "reference to cv A," where cv are 50470b57cec5SDimitry Andric // the cv-qualifiers of the function template (if any) and A is 50480b57cec5SDimitry Andric // the class of which the function template is a member. 50490b57cec5SDimitry Andric // 50500b57cec5SDimitry Andric // Note that we interpret this to mean "if one of the function 50510b57cec5SDimitry Andric // templates is a non-static member and the other is a non-member"; 50520b57cec5SDimitry Andric // otherwise, the ordering rules for static functions against non-static 50530b57cec5SDimitry Andric // functions don't make any sense. 50540b57cec5SDimitry Andric // 50550b57cec5SDimitry Andric // C++98/03 doesn't have this provision but we've extended DR532 to cover 50560b57cec5SDimitry Andric // it as wording was broken prior to it. 50570b57cec5SDimitry Andric SmallVector<QualType, 4> Args1; 50580b57cec5SDimitry Andric 50590b57cec5SDimitry Andric unsigned NumComparedArguments = NumCallArguments1; 50600b57cec5SDimitry Andric 50610b57cec5SDimitry Andric if (!Method2 && Method1 && !Method1->isStatic()) { 50620b57cec5SDimitry Andric // Compare 'this' from Method1 against first parameter from Method2. 50630b57cec5SDimitry Andric AddImplicitObjectParameterType(S.Context, Method1, Args1); 50640b57cec5SDimitry Andric ++NumComparedArguments; 50650b57cec5SDimitry Andric } else if (!Method1 && Method2 && !Method2->isStatic()) { 50660b57cec5SDimitry Andric // Compare 'this' from Method2 against first parameter from Method1. 50670b57cec5SDimitry Andric AddImplicitObjectParameterType(S.Context, Method2, Args2); 50685ffd83dbSDimitry Andric } else if (Method1 && Method2 && Reversed) { 50695ffd83dbSDimitry Andric // Compare 'this' from Method1 against second parameter from Method2 50705ffd83dbSDimitry Andric // and 'this' from Method2 against second parameter from Method1. 50715ffd83dbSDimitry Andric AddImplicitObjectParameterType(S.Context, Method1, Args1); 50725ffd83dbSDimitry Andric AddImplicitObjectParameterType(S.Context, Method2, Args2); 50735ffd83dbSDimitry Andric ++NumComparedArguments; 50740b57cec5SDimitry Andric } 50750b57cec5SDimitry Andric 50760b57cec5SDimitry Andric Args1.insert(Args1.end(), Proto1->param_type_begin(), 50770b57cec5SDimitry Andric Proto1->param_type_end()); 50780b57cec5SDimitry Andric Args2.insert(Args2.end(), Proto2->param_type_begin(), 50790b57cec5SDimitry Andric Proto2->param_type_end()); 50800b57cec5SDimitry Andric 50810b57cec5SDimitry Andric // C++ [temp.func.order]p5: 50820b57cec5SDimitry Andric // The presence of unused ellipsis and default arguments has no effect on 50830b57cec5SDimitry Andric // the partial ordering of function templates. 50840b57cec5SDimitry Andric if (Args1.size() > NumComparedArguments) 50850b57cec5SDimitry Andric Args1.resize(NumComparedArguments); 50860b57cec5SDimitry Andric if (Args2.size() > NumComparedArguments) 50870b57cec5SDimitry Andric Args2.resize(NumComparedArguments); 50885ffd83dbSDimitry Andric if (Reversed) 50895ffd83dbSDimitry Andric std::reverse(Args2.begin(), Args2.end()); 5090349cc55cSDimitry Andric 50910b57cec5SDimitry Andric if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(), 50920b57cec5SDimitry Andric Args1.data(), Args1.size(), Info, Deduced, 50930b57cec5SDimitry Andric TDF_None, /*PartialOrdering=*/true)) 50940b57cec5SDimitry Andric return false; 50950b57cec5SDimitry Andric 50960b57cec5SDimitry Andric break; 50970b57cec5SDimitry Andric } 50980b57cec5SDimitry Andric 50990b57cec5SDimitry Andric case TPOC_Conversion: 51000b57cec5SDimitry Andric // - In the context of a call to a conversion operator, the return types 51010b57cec5SDimitry Andric // of the conversion function templates are used. 51020b57cec5SDimitry Andric if (DeduceTemplateArgumentsByTypeMatch( 51030b57cec5SDimitry Andric S, TemplateParams, Proto2->getReturnType(), Proto1->getReturnType(), 51040b57cec5SDimitry Andric Info, Deduced, TDF_None, 51050b57cec5SDimitry Andric /*PartialOrdering=*/true)) 51060b57cec5SDimitry Andric return false; 51070b57cec5SDimitry Andric break; 51080b57cec5SDimitry Andric 51090b57cec5SDimitry Andric case TPOC_Other: 51100b57cec5SDimitry Andric // - In other contexts (14.6.6.2) the function template's function type 51110b57cec5SDimitry Andric // is used. 51120b57cec5SDimitry Andric if (DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, 51130b57cec5SDimitry Andric FD2->getType(), FD1->getType(), 51140b57cec5SDimitry Andric Info, Deduced, TDF_None, 51150b57cec5SDimitry Andric /*PartialOrdering=*/true)) 51160b57cec5SDimitry Andric return false; 51170b57cec5SDimitry Andric break; 51180b57cec5SDimitry Andric } 51190b57cec5SDimitry Andric 51200b57cec5SDimitry Andric // C++0x [temp.deduct.partial]p11: 51210b57cec5SDimitry Andric // In most cases, all template parameters must have values in order for 51220b57cec5SDimitry Andric // deduction to succeed, but for partial ordering purposes a template 51230b57cec5SDimitry Andric // parameter may remain without a value provided it is not used in the 51240b57cec5SDimitry Andric // types being used for partial ordering. [ Note: a template parameter used 51250b57cec5SDimitry Andric // in a non-deduced context is considered used. -end note] 51260b57cec5SDimitry Andric unsigned ArgIdx = 0, NumArgs = Deduced.size(); 51270b57cec5SDimitry Andric for (; ArgIdx != NumArgs; ++ArgIdx) 51280b57cec5SDimitry Andric if (Deduced[ArgIdx].isNull()) 51290b57cec5SDimitry Andric break; 51300b57cec5SDimitry Andric 51310b57cec5SDimitry Andric // FIXME: We fail to implement [temp.deduct.type]p1 along this path. We need 51320b57cec5SDimitry Andric // to substitute the deduced arguments back into the template and check that 51330b57cec5SDimitry Andric // we get the right type. 51340b57cec5SDimitry Andric 51350b57cec5SDimitry Andric if (ArgIdx == NumArgs) { 51360b57cec5SDimitry Andric // All template arguments were deduced. FT1 is at least as specialized 51370b57cec5SDimitry Andric // as FT2. 51380b57cec5SDimitry Andric return true; 51390b57cec5SDimitry Andric } 51400b57cec5SDimitry Andric 51410b57cec5SDimitry Andric // Figure out which template parameters were used. 51420b57cec5SDimitry Andric llvm::SmallBitVector UsedParameters(TemplateParams->size()); 51430b57cec5SDimitry Andric switch (TPOC) { 51440b57cec5SDimitry Andric case TPOC_Call: 51450b57cec5SDimitry Andric for (unsigned I = 0, N = Args2.size(); I != N; ++I) 51460b57cec5SDimitry Andric ::MarkUsedTemplateParameters(S.Context, Args2[I], false, 51470b57cec5SDimitry Andric TemplateParams->getDepth(), 51480b57cec5SDimitry Andric UsedParameters); 51490b57cec5SDimitry Andric break; 51500b57cec5SDimitry Andric 51510b57cec5SDimitry Andric case TPOC_Conversion: 51520b57cec5SDimitry Andric ::MarkUsedTemplateParameters(S.Context, Proto2->getReturnType(), false, 51530b57cec5SDimitry Andric TemplateParams->getDepth(), UsedParameters); 51540b57cec5SDimitry Andric break; 51550b57cec5SDimitry Andric 51560b57cec5SDimitry Andric case TPOC_Other: 51570b57cec5SDimitry Andric ::MarkUsedTemplateParameters(S.Context, FD2->getType(), false, 51580b57cec5SDimitry Andric TemplateParams->getDepth(), 51590b57cec5SDimitry Andric UsedParameters); 51600b57cec5SDimitry Andric break; 51610b57cec5SDimitry Andric } 51620b57cec5SDimitry Andric 51630b57cec5SDimitry Andric for (; ArgIdx != NumArgs; ++ArgIdx) 51640b57cec5SDimitry Andric // If this argument had no value deduced but was used in one of the types 51650b57cec5SDimitry Andric // used for partial ordering, then deduction fails. 51660b57cec5SDimitry Andric if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) 51670b57cec5SDimitry Andric return false; 51680b57cec5SDimitry Andric 51690b57cec5SDimitry Andric return true; 51700b57cec5SDimitry Andric } 51710b57cec5SDimitry Andric 51720b57cec5SDimitry Andric /// Returns the more specialized function template according 51730b57cec5SDimitry Andric /// to the rules of function template partial ordering (C++ [temp.func.order]). 51740b57cec5SDimitry Andric /// 51750b57cec5SDimitry Andric /// \param FT1 the first function template 51760b57cec5SDimitry Andric /// 51770b57cec5SDimitry Andric /// \param FT2 the second function template 51780b57cec5SDimitry Andric /// 51790b57cec5SDimitry Andric /// \param TPOC the context in which we are performing partial ordering of 51800b57cec5SDimitry Andric /// function templates. 51810b57cec5SDimitry Andric /// 51820b57cec5SDimitry Andric /// \param NumCallArguments1 The number of arguments in the call to FT1, used 51830b57cec5SDimitry Andric /// only when \c TPOC is \c TPOC_Call. 51840b57cec5SDimitry Andric /// 51850b57cec5SDimitry Andric /// \param NumCallArguments2 The number of arguments in the call to FT2, used 51860b57cec5SDimitry Andric /// only when \c TPOC is \c TPOC_Call. 51870b57cec5SDimitry Andric /// 51885ffd83dbSDimitry Andric /// \param Reversed If \c true, exactly one of FT1 and FT2 is an overload 51895ffd83dbSDimitry Andric /// candidate with a reversed parameter order. In this case, the corresponding 51905ffd83dbSDimitry Andric /// P/A pairs between FT1 and FT2 are reversed. 51915ffd83dbSDimitry Andric /// 51920b57cec5SDimitry Andric /// \returns the more specialized function template. If neither 51930b57cec5SDimitry Andric /// template is more specialized, returns NULL. 519481ad6265SDimitry Andric FunctionTemplateDecl *Sema::getMoreSpecializedTemplate( 519581ad6265SDimitry Andric FunctionTemplateDecl *FT1, FunctionTemplateDecl *FT2, SourceLocation Loc, 519681ad6265SDimitry Andric TemplatePartialOrderingContext TPOC, unsigned NumCallArguments1, 5197*bdd1243dSDimitry Andric unsigned NumCallArguments2, bool Reversed) { 5198480093f4SDimitry Andric 5199*bdd1243dSDimitry Andric bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, 5200*bdd1243dSDimitry Andric NumCallArguments1, Reversed); 5201*bdd1243dSDimitry Andric bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC, 5202*bdd1243dSDimitry Andric NumCallArguments2, Reversed); 5203*bdd1243dSDimitry Andric 5204*bdd1243dSDimitry Andric // C++ [temp.deduct.partial]p10: 5205*bdd1243dSDimitry Andric // F is more specialized than G if F is at least as specialized as G and G 5206*bdd1243dSDimitry Andric // is not at least as specialized as F. 5207*bdd1243dSDimitry Andric if (Better1 != Better2) // We have a clear winner 5208*bdd1243dSDimitry Andric return Better1 ? FT1 : FT2; 5209*bdd1243dSDimitry Andric 5210*bdd1243dSDimitry Andric if (!Better1 && !Better2) // Neither is better than the other 521181ad6265SDimitry Andric return nullptr; 5212*bdd1243dSDimitry Andric 5213*bdd1243dSDimitry Andric // C++ [temp.deduct.partial]p11: 5214*bdd1243dSDimitry Andric // ... and if G has a trailing function parameter pack for which F does not 5215*bdd1243dSDimitry Andric // have a corresponding parameter, and if F does not have a trailing 5216*bdd1243dSDimitry Andric // function parameter pack, then F is more specialized than G. 5217*bdd1243dSDimitry Andric FunctionDecl *FD1 = FT1->getTemplatedDecl(); 5218*bdd1243dSDimitry Andric FunctionDecl *FD2 = FT2->getTemplatedDecl(); 5219*bdd1243dSDimitry Andric unsigned NumParams1 = FD1->getNumParams(); 5220*bdd1243dSDimitry Andric unsigned NumParams2 = FD2->getNumParams(); 5221*bdd1243dSDimitry Andric bool Variadic1 = NumParams1 && FD1->parameters().back()->isParameterPack(); 5222*bdd1243dSDimitry Andric bool Variadic2 = NumParams2 && FD2->parameters().back()->isParameterPack(); 5223*bdd1243dSDimitry Andric if (Variadic1 != Variadic2) { 5224*bdd1243dSDimitry Andric if (Variadic1 && NumParams1 > NumParams2) 5225*bdd1243dSDimitry Andric return FT2; 5226*bdd1243dSDimitry Andric if (Variadic2 && NumParams2 > NumParams1) 5227*bdd1243dSDimitry Andric return FT1; 5228*bdd1243dSDimitry Andric } 5229*bdd1243dSDimitry Andric 5230*bdd1243dSDimitry Andric // This a speculative fix for CWG1432 (Similar to the fix for CWG1395) that 5231*bdd1243dSDimitry Andric // there is no wording or even resolution for this issue. 5232*bdd1243dSDimitry Andric for (int i = 0, e = std::min(NumParams1, NumParams2); i < e; ++i) { 5233*bdd1243dSDimitry Andric QualType T1 = FD1->getParamDecl(i)->getType().getCanonicalType(); 5234*bdd1243dSDimitry Andric QualType T2 = FD2->getParamDecl(i)->getType().getCanonicalType(); 5235*bdd1243dSDimitry Andric auto *TST1 = dyn_cast<TemplateSpecializationType>(T1); 5236*bdd1243dSDimitry Andric auto *TST2 = dyn_cast<TemplateSpecializationType>(T2); 5237*bdd1243dSDimitry Andric if (!TST1 || !TST2) 5238*bdd1243dSDimitry Andric continue; 5239*bdd1243dSDimitry Andric const TemplateArgument &TA1 = TST1->template_arguments().back(); 5240*bdd1243dSDimitry Andric if (TA1.getKind() == TemplateArgument::Pack) { 5241*bdd1243dSDimitry Andric assert(TST1->template_arguments().size() == 5242*bdd1243dSDimitry Andric TST2->template_arguments().size()); 5243*bdd1243dSDimitry Andric const TemplateArgument &TA2 = TST2->template_arguments().back(); 5244*bdd1243dSDimitry Andric assert(TA2.getKind() == TemplateArgument::Pack); 5245*bdd1243dSDimitry Andric unsigned PackSize1 = TA1.pack_size(); 5246*bdd1243dSDimitry Andric unsigned PackSize2 = TA2.pack_size(); 5247*bdd1243dSDimitry Andric bool IsPackExpansion1 = 5248*bdd1243dSDimitry Andric PackSize1 && TA1.pack_elements().back().isPackExpansion(); 5249*bdd1243dSDimitry Andric bool IsPackExpansion2 = 5250*bdd1243dSDimitry Andric PackSize2 && TA2.pack_elements().back().isPackExpansion(); 5251*bdd1243dSDimitry Andric if (PackSize1 != PackSize2 && IsPackExpansion1 != IsPackExpansion2) { 5252*bdd1243dSDimitry Andric if (PackSize1 > PackSize2 && IsPackExpansion1) 5253*bdd1243dSDimitry Andric return FT2; 5254*bdd1243dSDimitry Andric if (PackSize1 < PackSize2 && IsPackExpansion2) 5255*bdd1243dSDimitry Andric return FT1; 5256*bdd1243dSDimitry Andric } 5257*bdd1243dSDimitry Andric } 5258*bdd1243dSDimitry Andric } 5259*bdd1243dSDimitry Andric 5260*bdd1243dSDimitry Andric if (!Context.getLangOpts().CPlusPlus20) 5261*bdd1243dSDimitry Andric return nullptr; 5262*bdd1243dSDimitry Andric 5263*bdd1243dSDimitry Andric // Match GCC on not implementing [temp.func.order]p6.2.1. 5264*bdd1243dSDimitry Andric 5265*bdd1243dSDimitry Andric // C++20 [temp.func.order]p6: 5266*bdd1243dSDimitry Andric // If deduction against the other template succeeds for both transformed 5267*bdd1243dSDimitry Andric // templates, constraints can be considered as follows: 5268*bdd1243dSDimitry Andric 5269*bdd1243dSDimitry Andric // C++20 [temp.func.order]p6.1: 5270*bdd1243dSDimitry Andric // If their template-parameter-lists (possibly including template-parameters 5271*bdd1243dSDimitry Andric // invented for an abbreviated function template ([dcl.fct])) or function 5272*bdd1243dSDimitry Andric // parameter lists differ in length, neither template is more specialized 5273*bdd1243dSDimitry Andric // than the other. 5274*bdd1243dSDimitry Andric TemplateParameterList *TPL1 = FT1->getTemplateParameters(); 5275*bdd1243dSDimitry Andric TemplateParameterList *TPL2 = FT2->getTemplateParameters(); 5276*bdd1243dSDimitry Andric if (TPL1->size() != TPL2->size() || NumParams1 != NumParams2) 5277*bdd1243dSDimitry Andric return nullptr; 5278*bdd1243dSDimitry Andric 5279*bdd1243dSDimitry Andric // C++20 [temp.func.order]p6.2.2: 5280*bdd1243dSDimitry Andric // Otherwise, if the corresponding template-parameters of the 5281*bdd1243dSDimitry Andric // template-parameter-lists are not equivalent ([temp.over.link]) or if the 5282*bdd1243dSDimitry Andric // function parameters that positionally correspond between the two 5283*bdd1243dSDimitry Andric // templates are not of the same type, neither template is more specialized 5284*bdd1243dSDimitry Andric // than the other. 5285*bdd1243dSDimitry Andric if (!TemplateParameterListsAreEqual( 5286*bdd1243dSDimitry Andric TPL1, TPL2, false, Sema::TPL_TemplateMatch, SourceLocation(), true)) 5287*bdd1243dSDimitry Andric return nullptr; 5288*bdd1243dSDimitry Andric 5289*bdd1243dSDimitry Andric for (unsigned i = 0; i < NumParams1; ++i) 5290*bdd1243dSDimitry Andric if (!Context.hasSameType(FD1->getParamDecl(i)->getType(), 5291*bdd1243dSDimitry Andric FD2->getParamDecl(i)->getType())) 5292*bdd1243dSDimitry Andric return nullptr; 5293*bdd1243dSDimitry Andric 5294*bdd1243dSDimitry Andric // C++20 [temp.func.order]p6.3: 5295*bdd1243dSDimitry Andric // Otherwise, if the context in which the partial ordering is done is 5296*bdd1243dSDimitry Andric // that of a call to a conversion function and the return types of the 5297*bdd1243dSDimitry Andric // templates are not the same, then neither template is more specialized 5298*bdd1243dSDimitry Andric // than the other. 5299*bdd1243dSDimitry Andric if (TPOC == TPOC_Conversion && 5300*bdd1243dSDimitry Andric !Context.hasSameType(FD1->getReturnType(), FD2->getReturnType())) 5301*bdd1243dSDimitry Andric return nullptr; 5302*bdd1243dSDimitry Andric 5303480093f4SDimitry Andric llvm::SmallVector<const Expr *, 3> AC1, AC2; 5304480093f4SDimitry Andric FT1->getAssociatedConstraints(AC1); 5305480093f4SDimitry Andric FT2->getAssociatedConstraints(AC2); 5306480093f4SDimitry Andric bool AtLeastAsConstrained1, AtLeastAsConstrained2; 5307480093f4SDimitry Andric if (IsAtLeastAsConstrained(FT1, AC1, FT2, AC2, AtLeastAsConstrained1)) 5308480093f4SDimitry Andric return nullptr; 5309480093f4SDimitry Andric if (IsAtLeastAsConstrained(FT2, AC2, FT1, AC1, AtLeastAsConstrained2)) 5310480093f4SDimitry Andric return nullptr; 5311480093f4SDimitry Andric if (AtLeastAsConstrained1 == AtLeastAsConstrained2) 5312480093f4SDimitry Andric return nullptr; 5313480093f4SDimitry Andric return AtLeastAsConstrained1 ? FT1 : FT2; 53140b57cec5SDimitry Andric } 53150b57cec5SDimitry Andric 53160b57cec5SDimitry Andric /// Determine if the two templates are equivalent. 53170b57cec5SDimitry Andric static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) { 53180b57cec5SDimitry Andric if (T1 == T2) 53190b57cec5SDimitry Andric return true; 53200b57cec5SDimitry Andric 53210b57cec5SDimitry Andric if (!T1 || !T2) 53220b57cec5SDimitry Andric return false; 53230b57cec5SDimitry Andric 53240b57cec5SDimitry Andric return T1->getCanonicalDecl() == T2->getCanonicalDecl(); 53250b57cec5SDimitry Andric } 53260b57cec5SDimitry Andric 53270b57cec5SDimitry Andric /// Retrieve the most specialized of the given function template 53280b57cec5SDimitry Andric /// specializations. 53290b57cec5SDimitry Andric /// 53300b57cec5SDimitry Andric /// \param SpecBegin the start iterator of the function template 53310b57cec5SDimitry Andric /// specializations that we will be comparing. 53320b57cec5SDimitry Andric /// 53330b57cec5SDimitry Andric /// \param SpecEnd the end iterator of the function template 53340b57cec5SDimitry Andric /// specializations, paired with \p SpecBegin. 53350b57cec5SDimitry Andric /// 53360b57cec5SDimitry Andric /// \param Loc the location where the ambiguity or no-specializations 53370b57cec5SDimitry Andric /// diagnostic should occur. 53380b57cec5SDimitry Andric /// 53390b57cec5SDimitry Andric /// \param NoneDiag partial diagnostic used to diagnose cases where there are 53400b57cec5SDimitry Andric /// no matching candidates. 53410b57cec5SDimitry Andric /// 53420b57cec5SDimitry Andric /// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one 53430b57cec5SDimitry Andric /// occurs. 53440b57cec5SDimitry Andric /// 53450b57cec5SDimitry Andric /// \param CandidateDiag partial diagnostic used for each function template 53460b57cec5SDimitry Andric /// specialization that is a candidate in the ambiguous ordering. One parameter 53470b57cec5SDimitry Andric /// in this diagnostic should be unbound, which will correspond to the string 53480b57cec5SDimitry Andric /// describing the template arguments for the function template specialization. 53490b57cec5SDimitry Andric /// 53500b57cec5SDimitry Andric /// \returns the most specialized function template specialization, if 53510b57cec5SDimitry Andric /// found. Otherwise, returns SpecEnd. 53520b57cec5SDimitry Andric UnresolvedSetIterator Sema::getMostSpecialized( 53530b57cec5SDimitry Andric UnresolvedSetIterator SpecBegin, UnresolvedSetIterator SpecEnd, 53540b57cec5SDimitry Andric TemplateSpecCandidateSet &FailedCandidates, 53550b57cec5SDimitry Andric SourceLocation Loc, const PartialDiagnostic &NoneDiag, 53560b57cec5SDimitry Andric const PartialDiagnostic &AmbigDiag, const PartialDiagnostic &CandidateDiag, 53570b57cec5SDimitry Andric bool Complain, QualType TargetType) { 53580b57cec5SDimitry Andric if (SpecBegin == SpecEnd) { 53590b57cec5SDimitry Andric if (Complain) { 53600b57cec5SDimitry Andric Diag(Loc, NoneDiag); 53610b57cec5SDimitry Andric FailedCandidates.NoteCandidates(*this, Loc); 53620b57cec5SDimitry Andric } 53630b57cec5SDimitry Andric return SpecEnd; 53640b57cec5SDimitry Andric } 53650b57cec5SDimitry Andric 53660b57cec5SDimitry Andric if (SpecBegin + 1 == SpecEnd) 53670b57cec5SDimitry Andric return SpecBegin; 53680b57cec5SDimitry Andric 53690b57cec5SDimitry Andric // Find the function template that is better than all of the templates it 53700b57cec5SDimitry Andric // has been compared to. 53710b57cec5SDimitry Andric UnresolvedSetIterator Best = SpecBegin; 53720b57cec5SDimitry Andric FunctionTemplateDecl *BestTemplate 53730b57cec5SDimitry Andric = cast<FunctionDecl>(*Best)->getPrimaryTemplate(); 53740b57cec5SDimitry Andric assert(BestTemplate && "Not a function template specialization?"); 53750b57cec5SDimitry Andric for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) { 53760b57cec5SDimitry Andric FunctionTemplateDecl *Challenger 53770b57cec5SDimitry Andric = cast<FunctionDecl>(*I)->getPrimaryTemplate(); 53780b57cec5SDimitry Andric assert(Challenger && "Not a function template specialization?"); 53790b57cec5SDimitry Andric if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, 53800b57cec5SDimitry Andric Loc, TPOC_Other, 0, 0), 53810b57cec5SDimitry Andric Challenger)) { 53820b57cec5SDimitry Andric Best = I; 53830b57cec5SDimitry Andric BestTemplate = Challenger; 53840b57cec5SDimitry Andric } 53850b57cec5SDimitry Andric } 53860b57cec5SDimitry Andric 53870b57cec5SDimitry Andric // Make sure that the "best" function template is more specialized than all 53880b57cec5SDimitry Andric // of the others. 53890b57cec5SDimitry Andric bool Ambiguous = false; 53900b57cec5SDimitry Andric for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { 53910b57cec5SDimitry Andric FunctionTemplateDecl *Challenger 53920b57cec5SDimitry Andric = cast<FunctionDecl>(*I)->getPrimaryTemplate(); 53930b57cec5SDimitry Andric if (I != Best && 53940b57cec5SDimitry Andric !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, 53950b57cec5SDimitry Andric Loc, TPOC_Other, 0, 0), 53960b57cec5SDimitry Andric BestTemplate)) { 53970b57cec5SDimitry Andric Ambiguous = true; 53980b57cec5SDimitry Andric break; 53990b57cec5SDimitry Andric } 54000b57cec5SDimitry Andric } 54010b57cec5SDimitry Andric 54020b57cec5SDimitry Andric if (!Ambiguous) { 54030b57cec5SDimitry Andric // We found an answer. Return it. 54040b57cec5SDimitry Andric return Best; 54050b57cec5SDimitry Andric } 54060b57cec5SDimitry Andric 54070b57cec5SDimitry Andric // Diagnose the ambiguity. 54080b57cec5SDimitry Andric if (Complain) { 54090b57cec5SDimitry Andric Diag(Loc, AmbigDiag); 54100b57cec5SDimitry Andric 54110b57cec5SDimitry Andric // FIXME: Can we order the candidates in some sane way? 54120b57cec5SDimitry Andric for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { 54130b57cec5SDimitry Andric PartialDiagnostic PD = CandidateDiag; 54140b57cec5SDimitry Andric const auto *FD = cast<FunctionDecl>(*I); 54150b57cec5SDimitry Andric PD << FD << getTemplateArgumentBindingsText( 54160b57cec5SDimitry Andric FD->getPrimaryTemplate()->getTemplateParameters(), 54170b57cec5SDimitry Andric *FD->getTemplateSpecializationArgs()); 54180b57cec5SDimitry Andric if (!TargetType.isNull()) 54190b57cec5SDimitry Andric HandleFunctionTypeMismatch(PD, FD->getType(), TargetType); 54200b57cec5SDimitry Andric Diag((*I)->getLocation(), PD); 54210b57cec5SDimitry Andric } 54220b57cec5SDimitry Andric } 54230b57cec5SDimitry Andric 54240b57cec5SDimitry Andric return SpecEnd; 54250b57cec5SDimitry Andric } 54260b57cec5SDimitry Andric 54270b57cec5SDimitry Andric /// Determine whether one partial specialization, P1, is at least as 54280b57cec5SDimitry Andric /// specialized than another, P2. 54290b57cec5SDimitry Andric /// 54300b57cec5SDimitry Andric /// \tparam TemplateLikeDecl The kind of P2, which must be a 54310b57cec5SDimitry Andric /// TemplateDecl or {Class,Var}TemplatePartialSpecializationDecl. 54320b57cec5SDimitry Andric /// \param T1 The injected-class-name of P1 (faked for a variable template). 54330b57cec5SDimitry Andric /// \param T2 The injected-class-name of P2 (faked for a variable template). 54340b57cec5SDimitry Andric template<typename TemplateLikeDecl> 54350b57cec5SDimitry Andric static bool isAtLeastAsSpecializedAs(Sema &S, QualType T1, QualType T2, 54360b57cec5SDimitry Andric TemplateLikeDecl *P2, 54370b57cec5SDimitry Andric TemplateDeductionInfo &Info) { 54380b57cec5SDimitry Andric // C++ [temp.class.order]p1: 54390b57cec5SDimitry Andric // For two class template partial specializations, the first is at least as 54400b57cec5SDimitry Andric // specialized as the second if, given the following rewrite to two 54410b57cec5SDimitry Andric // function templates, the first function template is at least as 54420b57cec5SDimitry Andric // specialized as the second according to the ordering rules for function 54430b57cec5SDimitry Andric // templates (14.6.6.2): 54440b57cec5SDimitry Andric // - the first function template has the same template parameters as the 54450b57cec5SDimitry Andric // first partial specialization and has a single function parameter 54460b57cec5SDimitry Andric // whose type is a class template specialization with the template 54470b57cec5SDimitry Andric // arguments of the first partial specialization, and 54480b57cec5SDimitry Andric // - the second function template has the same template parameters as the 54490b57cec5SDimitry Andric // second partial specialization and has a single function parameter 54500b57cec5SDimitry Andric // whose type is a class template specialization with the template 54510b57cec5SDimitry Andric // arguments of the second partial specialization. 54520b57cec5SDimitry Andric // 54530b57cec5SDimitry Andric // Rather than synthesize function templates, we merely perform the 54540b57cec5SDimitry Andric // equivalent partial ordering by performing deduction directly on 54550b57cec5SDimitry Andric // the template arguments of the class template partial 54560b57cec5SDimitry Andric // specializations. This computation is slightly simpler than the 54570b57cec5SDimitry Andric // general problem of function template partial ordering, because 54580b57cec5SDimitry Andric // class template partial specializations are more constrained. We 54590b57cec5SDimitry Andric // know that every template parameter is deducible from the class 54600b57cec5SDimitry Andric // template partial specialization's template arguments, for 54610b57cec5SDimitry Andric // example. 54620b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> Deduced; 54630b57cec5SDimitry Andric 54640b57cec5SDimitry Andric // Determine whether P1 is at least as specialized as P2. 54650b57cec5SDimitry Andric Deduced.resize(P2->getTemplateParameters()->size()); 54660b57cec5SDimitry Andric if (DeduceTemplateArgumentsByTypeMatch(S, P2->getTemplateParameters(), 54670b57cec5SDimitry Andric T2, T1, Info, Deduced, TDF_None, 54680b57cec5SDimitry Andric /*PartialOrdering=*/true)) 54690b57cec5SDimitry Andric return false; 54700b57cec5SDimitry Andric 54710b57cec5SDimitry Andric SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), 54720b57cec5SDimitry Andric Deduced.end()); 54730b57cec5SDimitry Andric Sema::InstantiatingTemplate Inst(S, Info.getLocation(), P2, DeducedArgs, 54740b57cec5SDimitry Andric Info); 5475fe6060f1SDimitry Andric if (Inst.isInvalid()) 5476fe6060f1SDimitry Andric return false; 5477fe6060f1SDimitry Andric 5478*bdd1243dSDimitry Andric const auto *TST1 = cast<TemplateSpecializationType>(T1); 54795ffd83dbSDimitry Andric bool AtLeastAsSpecialized; 54805ffd83dbSDimitry Andric S.runWithSufficientStackSpace(Info.getLocation(), [&] { 54815ffd83dbSDimitry Andric AtLeastAsSpecialized = !FinishTemplateArgumentDeduction( 54820b57cec5SDimitry Andric S, P2, /*IsPartialOrdering=*/true, 54830b57cec5SDimitry Andric TemplateArgumentList(TemplateArgumentList::OnStack, 54840b57cec5SDimitry Andric TST1->template_arguments()), 54855ffd83dbSDimitry Andric Deduced, Info); 54865ffd83dbSDimitry Andric }); 54875ffd83dbSDimitry Andric return AtLeastAsSpecialized; 54880b57cec5SDimitry Andric } 54890b57cec5SDimitry Andric 5490*bdd1243dSDimitry Andric namespace { 5491*bdd1243dSDimitry Andric // A dummy class to return nullptr instead of P2 when performing "more 5492*bdd1243dSDimitry Andric // specialized than primary" check. 5493*bdd1243dSDimitry Andric struct GetP2 { 5494*bdd1243dSDimitry Andric template <typename T1, typename T2, 5495*bdd1243dSDimitry Andric std::enable_if_t<std::is_same_v<T1, T2>, bool> = true> 5496*bdd1243dSDimitry Andric T2 *operator()(T1 *, T2 *P2) { 5497*bdd1243dSDimitry Andric return P2; 5498*bdd1243dSDimitry Andric } 5499*bdd1243dSDimitry Andric template <typename T1, typename T2, 5500*bdd1243dSDimitry Andric std::enable_if_t<!std::is_same_v<T1, T2>, bool> = true> 5501*bdd1243dSDimitry Andric T1 *operator()(T1 *, T2 *) { 5502*bdd1243dSDimitry Andric return nullptr; 5503*bdd1243dSDimitry Andric } 5504*bdd1243dSDimitry Andric }; 5505*bdd1243dSDimitry Andric 5506*bdd1243dSDimitry Andric // The assumption is that two template argument lists have the same size. 5507*bdd1243dSDimitry Andric struct TemplateArgumentListAreEqual { 5508*bdd1243dSDimitry Andric ASTContext &Ctx; 5509*bdd1243dSDimitry Andric TemplateArgumentListAreEqual(ASTContext &Ctx) : Ctx(Ctx) {} 5510*bdd1243dSDimitry Andric 5511*bdd1243dSDimitry Andric template <typename T1, typename T2, 5512*bdd1243dSDimitry Andric std::enable_if_t<std::is_same_v<T1, T2>, bool> = true> 5513*bdd1243dSDimitry Andric bool operator()(T1 *PS1, T2 *PS2) { 5514*bdd1243dSDimitry Andric ArrayRef<TemplateArgument> Args1 = PS1->getTemplateArgs().asArray(), 5515*bdd1243dSDimitry Andric Args2 = PS2->getTemplateArgs().asArray(); 5516*bdd1243dSDimitry Andric 5517*bdd1243dSDimitry Andric for (unsigned I = 0, E = Args1.size(); I < E; ++I) { 5518*bdd1243dSDimitry Andric // We use profile, instead of structural comparison of the arguments, 5519*bdd1243dSDimitry Andric // because canonicalization can't do the right thing for dependent 5520*bdd1243dSDimitry Andric // expressions. 5521*bdd1243dSDimitry Andric llvm::FoldingSetNodeID IDA, IDB; 5522*bdd1243dSDimitry Andric Args1[I].Profile(IDA, Ctx); 5523*bdd1243dSDimitry Andric Args2[I].Profile(IDB, Ctx); 5524*bdd1243dSDimitry Andric if (IDA != IDB) 5525*bdd1243dSDimitry Andric return false; 5526*bdd1243dSDimitry Andric } 5527*bdd1243dSDimitry Andric return true; 5528*bdd1243dSDimitry Andric } 5529*bdd1243dSDimitry Andric 5530*bdd1243dSDimitry Andric template <typename T1, typename T2, 5531*bdd1243dSDimitry Andric std::enable_if_t<!std::is_same_v<T1, T2>, bool> = true> 5532*bdd1243dSDimitry Andric bool operator()(T1 *Spec, T2 *Primary) { 5533*bdd1243dSDimitry Andric ArrayRef<TemplateArgument> Args1 = Spec->getTemplateArgs().asArray(), 5534*bdd1243dSDimitry Andric Args2 = Primary->getInjectedTemplateArgs(); 5535*bdd1243dSDimitry Andric 5536*bdd1243dSDimitry Andric for (unsigned I = 0, E = Args1.size(); I < E; ++I) { 5537*bdd1243dSDimitry Andric // We use profile, instead of structural comparison of the arguments, 5538*bdd1243dSDimitry Andric // because canonicalization can't do the right thing for dependent 5539*bdd1243dSDimitry Andric // expressions. 5540*bdd1243dSDimitry Andric llvm::FoldingSetNodeID IDA, IDB; 5541*bdd1243dSDimitry Andric Args1[I].Profile(IDA, Ctx); 5542*bdd1243dSDimitry Andric // Unlike the specialization arguments, the injected arguments are not 5543*bdd1243dSDimitry Andric // always canonical. 5544*bdd1243dSDimitry Andric Ctx.getCanonicalTemplateArgument(Args2[I]).Profile(IDB, Ctx); 5545*bdd1243dSDimitry Andric if (IDA != IDB) 5546*bdd1243dSDimitry Andric return false; 5547*bdd1243dSDimitry Andric } 5548*bdd1243dSDimitry Andric return true; 5549*bdd1243dSDimitry Andric } 5550*bdd1243dSDimitry Andric }; 5551*bdd1243dSDimitry Andric } // namespace 5552*bdd1243dSDimitry Andric 5553*bdd1243dSDimitry Andric /// Returns the more specialized template specialization between T1/P1 and 5554*bdd1243dSDimitry Andric /// T2/P2. 5555*bdd1243dSDimitry Andric /// - If IsMoreSpecialThanPrimaryCheck is true, T1/P1 is the partial 5556*bdd1243dSDimitry Andric /// specialization and T2/P2 is the primary template. 5557*bdd1243dSDimitry Andric /// - otherwise, both T1/P1 and T2/P2 are the partial specialization. 5558*bdd1243dSDimitry Andric /// 5559*bdd1243dSDimitry Andric /// \param T1 the type of the first template partial specialization 5560*bdd1243dSDimitry Andric /// 5561*bdd1243dSDimitry Andric /// \param T2 if IsMoreSpecialThanPrimaryCheck is true, the type of the second 5562*bdd1243dSDimitry Andric /// template partial specialization; otherwise, the type of the 5563*bdd1243dSDimitry Andric /// primary template. 5564*bdd1243dSDimitry Andric /// 5565*bdd1243dSDimitry Andric /// \param P1 the first template partial specialization 5566*bdd1243dSDimitry Andric /// 5567*bdd1243dSDimitry Andric /// \param P2 if IsMoreSpecialThanPrimaryCheck is true, the second template 5568*bdd1243dSDimitry Andric /// partial specialization; otherwise, the primary template. 5569*bdd1243dSDimitry Andric /// 5570*bdd1243dSDimitry Andric /// \returns - If IsMoreSpecialThanPrimaryCheck is true, returns P1 if P1 is 5571*bdd1243dSDimitry Andric /// more specialized, returns nullptr if P1 is not more specialized. 5572*bdd1243dSDimitry Andric /// - otherwise, returns the more specialized template partial 5573*bdd1243dSDimitry Andric /// specialization. If neither partial specialization is more 5574*bdd1243dSDimitry Andric /// specialized, returns NULL. 5575*bdd1243dSDimitry Andric template <typename TemplateLikeDecl, typename PrimaryDel> 5576*bdd1243dSDimitry Andric static TemplateLikeDecl * 5577*bdd1243dSDimitry Andric getMoreSpecialized(Sema &S, QualType T1, QualType T2, TemplateLikeDecl *P1, 5578*bdd1243dSDimitry Andric PrimaryDel *P2, TemplateDeductionInfo &Info) { 5579*bdd1243dSDimitry Andric constexpr bool IsMoreSpecialThanPrimaryCheck = 5580*bdd1243dSDimitry Andric !std::is_same_v<TemplateLikeDecl, PrimaryDel>; 5581*bdd1243dSDimitry Andric 5582*bdd1243dSDimitry Andric bool Better1 = isAtLeastAsSpecializedAs(S, T1, T2, P2, Info); 5583*bdd1243dSDimitry Andric if (IsMoreSpecialThanPrimaryCheck && !Better1) 5584*bdd1243dSDimitry Andric return nullptr; 5585*bdd1243dSDimitry Andric 5586*bdd1243dSDimitry Andric bool Better2 = isAtLeastAsSpecializedAs(S, T2, T1, P1, Info); 5587*bdd1243dSDimitry Andric if (IsMoreSpecialThanPrimaryCheck && !Better2) 5588*bdd1243dSDimitry Andric return P1; 5589*bdd1243dSDimitry Andric 5590*bdd1243dSDimitry Andric // C++ [temp.deduct.partial]p10: 5591*bdd1243dSDimitry Andric // F is more specialized than G if F is at least as specialized as G and G 5592*bdd1243dSDimitry Andric // is not at least as specialized as F. 5593*bdd1243dSDimitry Andric if (Better1 != Better2) // We have a clear winner 5594*bdd1243dSDimitry Andric return Better1 ? P1 : GetP2()(P1, P2); 5595*bdd1243dSDimitry Andric 5596*bdd1243dSDimitry Andric if (!Better1 && !Better2) 5597*bdd1243dSDimitry Andric return nullptr; 5598*bdd1243dSDimitry Andric 5599*bdd1243dSDimitry Andric // This a speculative fix for CWG1432 (Similar to the fix for CWG1395) that 5600*bdd1243dSDimitry Andric // there is no wording or even resolution for this issue. 5601*bdd1243dSDimitry Andric auto *TST1 = cast<TemplateSpecializationType>(T1); 5602*bdd1243dSDimitry Andric auto *TST2 = cast<TemplateSpecializationType>(T2); 5603*bdd1243dSDimitry Andric const TemplateArgument &TA1 = TST1->template_arguments().back(); 5604*bdd1243dSDimitry Andric if (TA1.getKind() == TemplateArgument::Pack) { 5605*bdd1243dSDimitry Andric assert(TST1->template_arguments().size() == 5606*bdd1243dSDimitry Andric TST2->template_arguments().size()); 5607*bdd1243dSDimitry Andric const TemplateArgument &TA2 = TST2->template_arguments().back(); 5608*bdd1243dSDimitry Andric assert(TA2.getKind() == TemplateArgument::Pack); 5609*bdd1243dSDimitry Andric unsigned PackSize1 = TA1.pack_size(); 5610*bdd1243dSDimitry Andric unsigned PackSize2 = TA2.pack_size(); 5611*bdd1243dSDimitry Andric bool IsPackExpansion1 = 5612*bdd1243dSDimitry Andric PackSize1 && TA1.pack_elements().back().isPackExpansion(); 5613*bdd1243dSDimitry Andric bool IsPackExpansion2 = 5614*bdd1243dSDimitry Andric PackSize2 && TA2.pack_elements().back().isPackExpansion(); 5615*bdd1243dSDimitry Andric if (PackSize1 != PackSize2 && IsPackExpansion1 != IsPackExpansion2) { 5616*bdd1243dSDimitry Andric if (PackSize1 > PackSize2 && IsPackExpansion1) 5617*bdd1243dSDimitry Andric return GetP2()(P1, P2); 5618*bdd1243dSDimitry Andric if (PackSize1 < PackSize2 && IsPackExpansion2) 5619*bdd1243dSDimitry Andric return P1; 5620*bdd1243dSDimitry Andric } 5621*bdd1243dSDimitry Andric } 5622*bdd1243dSDimitry Andric 5623*bdd1243dSDimitry Andric if (!S.Context.getLangOpts().CPlusPlus20) 5624*bdd1243dSDimitry Andric return nullptr; 5625*bdd1243dSDimitry Andric 5626*bdd1243dSDimitry Andric // Match GCC on not implementing [temp.func.order]p6.2.1. 5627*bdd1243dSDimitry Andric 5628*bdd1243dSDimitry Andric // C++20 [temp.func.order]p6: 5629*bdd1243dSDimitry Andric // If deduction against the other template succeeds for both transformed 5630*bdd1243dSDimitry Andric // templates, constraints can be considered as follows: 5631*bdd1243dSDimitry Andric 5632*bdd1243dSDimitry Andric TemplateParameterList *TPL1 = P1->getTemplateParameters(); 5633*bdd1243dSDimitry Andric TemplateParameterList *TPL2 = P2->getTemplateParameters(); 5634*bdd1243dSDimitry Andric if (TPL1->size() != TPL2->size()) 5635*bdd1243dSDimitry Andric return nullptr; 5636*bdd1243dSDimitry Andric 5637*bdd1243dSDimitry Andric // C++20 [temp.func.order]p6.2.2: 5638*bdd1243dSDimitry Andric // Otherwise, if the corresponding template-parameters of the 5639*bdd1243dSDimitry Andric // template-parameter-lists are not equivalent ([temp.over.link]) or if the 5640*bdd1243dSDimitry Andric // function parameters that positionally correspond between the two 5641*bdd1243dSDimitry Andric // templates are not of the same type, neither template is more specialized 5642*bdd1243dSDimitry Andric // than the other. 5643*bdd1243dSDimitry Andric if (!S.TemplateParameterListsAreEqual( 5644*bdd1243dSDimitry Andric TPL1, TPL2, false, Sema::TPL_TemplateMatch, SourceLocation(), true)) 5645*bdd1243dSDimitry Andric return nullptr; 5646*bdd1243dSDimitry Andric 5647*bdd1243dSDimitry Andric if (!TemplateArgumentListAreEqual(S.getASTContext())(P1, P2)) 5648*bdd1243dSDimitry Andric return nullptr; 5649*bdd1243dSDimitry Andric 5650*bdd1243dSDimitry Andric llvm::SmallVector<const Expr *, 3> AC1, AC2; 5651*bdd1243dSDimitry Andric P1->getAssociatedConstraints(AC1); 5652*bdd1243dSDimitry Andric P2->getAssociatedConstraints(AC2); 5653*bdd1243dSDimitry Andric bool AtLeastAsConstrained1, AtLeastAsConstrained2; 5654*bdd1243dSDimitry Andric if (S.IsAtLeastAsConstrained(P1, AC1, P2, AC2, AtLeastAsConstrained1) || 5655*bdd1243dSDimitry Andric (IsMoreSpecialThanPrimaryCheck && !AtLeastAsConstrained1)) 5656*bdd1243dSDimitry Andric return nullptr; 5657*bdd1243dSDimitry Andric if (S.IsAtLeastAsConstrained(P2, AC2, P1, AC1, AtLeastAsConstrained2)) 5658*bdd1243dSDimitry Andric return nullptr; 5659*bdd1243dSDimitry Andric if (AtLeastAsConstrained1 == AtLeastAsConstrained2) 5660*bdd1243dSDimitry Andric return nullptr; 5661*bdd1243dSDimitry Andric return AtLeastAsConstrained1 ? P1 : GetP2()(P1, P2); 5662*bdd1243dSDimitry Andric } 5663*bdd1243dSDimitry Andric 56640b57cec5SDimitry Andric /// Returns the more specialized class template partial specialization 56650b57cec5SDimitry Andric /// according to the rules of partial ordering of class template partial 56660b57cec5SDimitry Andric /// specializations (C++ [temp.class.order]). 56670b57cec5SDimitry Andric /// 56680b57cec5SDimitry Andric /// \param PS1 the first class template partial specialization 56690b57cec5SDimitry Andric /// 56700b57cec5SDimitry Andric /// \param PS2 the second class template partial specialization 56710b57cec5SDimitry Andric /// 56720b57cec5SDimitry Andric /// \returns the more specialized class template partial specialization. If 56730b57cec5SDimitry Andric /// neither partial specialization is more specialized, returns NULL. 56740b57cec5SDimitry Andric ClassTemplatePartialSpecializationDecl * 56750b57cec5SDimitry Andric Sema::getMoreSpecializedPartialSpecialization( 56760b57cec5SDimitry Andric ClassTemplatePartialSpecializationDecl *PS1, 56770b57cec5SDimitry Andric ClassTemplatePartialSpecializationDecl *PS2, 56780b57cec5SDimitry Andric SourceLocation Loc) { 56790b57cec5SDimitry Andric QualType PT1 = PS1->getInjectedSpecializationType(); 56800b57cec5SDimitry Andric QualType PT2 = PS2->getInjectedSpecializationType(); 56810b57cec5SDimitry Andric 56820b57cec5SDimitry Andric TemplateDeductionInfo Info(Loc); 5683*bdd1243dSDimitry Andric return getMoreSpecialized(*this, PT1, PT2, PS1, PS2, Info); 56840b57cec5SDimitry Andric } 56850b57cec5SDimitry Andric 56860b57cec5SDimitry Andric bool Sema::isMoreSpecializedThanPrimary( 56870b57cec5SDimitry Andric ClassTemplatePartialSpecializationDecl *Spec, TemplateDeductionInfo &Info) { 56880b57cec5SDimitry Andric ClassTemplateDecl *Primary = Spec->getSpecializedTemplate(); 56890b57cec5SDimitry Andric QualType PrimaryT = Primary->getInjectedClassNameSpecialization(); 56900b57cec5SDimitry Andric QualType PartialT = Spec->getInjectedSpecializationType(); 5691*bdd1243dSDimitry Andric 5692*bdd1243dSDimitry Andric ClassTemplatePartialSpecializationDecl *MaybeSpec = 5693*bdd1243dSDimitry Andric getMoreSpecialized(*this, PartialT, PrimaryT, Spec, Primary, Info); 5694*bdd1243dSDimitry Andric if (MaybeSpec) 5695480093f4SDimitry Andric Info.clearSFINAEDiagnostic(); 5696*bdd1243dSDimitry Andric return MaybeSpec; 56970b57cec5SDimitry Andric } 56980b57cec5SDimitry Andric 56990b57cec5SDimitry Andric VarTemplatePartialSpecializationDecl * 57000b57cec5SDimitry Andric Sema::getMoreSpecializedPartialSpecialization( 57010b57cec5SDimitry Andric VarTemplatePartialSpecializationDecl *PS1, 57020b57cec5SDimitry Andric VarTemplatePartialSpecializationDecl *PS2, SourceLocation Loc) { 57030b57cec5SDimitry Andric // Pretend the variable template specializations are class template 57040b57cec5SDimitry Andric // specializations and form a fake injected class name type for comparison. 57050b57cec5SDimitry Andric assert(PS1->getSpecializedTemplate() == PS2->getSpecializedTemplate() && 57060b57cec5SDimitry Andric "the partial specializations being compared should specialize" 57070b57cec5SDimitry Andric " the same template."); 57080b57cec5SDimitry Andric TemplateName Name(PS1->getSpecializedTemplate()); 57090b57cec5SDimitry Andric TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); 57100b57cec5SDimitry Andric QualType PT1 = Context.getTemplateSpecializationType( 57110b57cec5SDimitry Andric CanonTemplate, PS1->getTemplateArgs().asArray()); 57120b57cec5SDimitry Andric QualType PT2 = Context.getTemplateSpecializationType( 57130b57cec5SDimitry Andric CanonTemplate, PS2->getTemplateArgs().asArray()); 57140b57cec5SDimitry Andric 57150b57cec5SDimitry Andric TemplateDeductionInfo Info(Loc); 5716*bdd1243dSDimitry Andric return getMoreSpecialized(*this, PT1, PT2, PS1, PS2, Info); 57170b57cec5SDimitry Andric } 57180b57cec5SDimitry Andric 57190b57cec5SDimitry Andric bool Sema::isMoreSpecializedThanPrimary( 57200b57cec5SDimitry Andric VarTemplatePartialSpecializationDecl *Spec, TemplateDeductionInfo &Info) { 5721*bdd1243dSDimitry Andric VarTemplateDecl *Primary = Spec->getSpecializedTemplate(); 57220b57cec5SDimitry Andric TemplateName CanonTemplate = 57230b57cec5SDimitry Andric Context.getCanonicalTemplateName(TemplateName(Primary)); 57240b57cec5SDimitry Andric QualType PrimaryT = Context.getTemplateSpecializationType( 5725*bdd1243dSDimitry Andric CanonTemplate, Primary->getInjectedTemplateArgs()); 57260b57cec5SDimitry Andric QualType PartialT = Context.getTemplateSpecializationType( 57270b57cec5SDimitry Andric CanonTemplate, Spec->getTemplateArgs().asArray()); 5728480093f4SDimitry Andric 5729*bdd1243dSDimitry Andric VarTemplatePartialSpecializationDecl *MaybeSpec = 5730*bdd1243dSDimitry Andric getMoreSpecialized(*this, PartialT, PrimaryT, Spec, Primary, Info); 5731*bdd1243dSDimitry Andric if (MaybeSpec) 5732480093f4SDimitry Andric Info.clearSFINAEDiagnostic(); 5733*bdd1243dSDimitry Andric return MaybeSpec; 57340b57cec5SDimitry Andric } 57350b57cec5SDimitry Andric 57360b57cec5SDimitry Andric bool Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs( 57370b57cec5SDimitry Andric TemplateParameterList *P, TemplateDecl *AArg, SourceLocation Loc) { 57380b57cec5SDimitry Andric // C++1z [temp.arg.template]p4: (DR 150) 57390b57cec5SDimitry Andric // A template template-parameter P is at least as specialized as a 57400b57cec5SDimitry Andric // template template-argument A if, given the following rewrite to two 57410b57cec5SDimitry Andric // function templates... 57420b57cec5SDimitry Andric 57430b57cec5SDimitry Andric // Rather than synthesize function templates, we merely perform the 57440b57cec5SDimitry Andric // equivalent partial ordering by performing deduction directly on 57450b57cec5SDimitry Andric // the template parameter lists of the template template parameters. 57460b57cec5SDimitry Andric // 57470b57cec5SDimitry Andric // Given an invented class template X with the template parameter list of 57480b57cec5SDimitry Andric // A (including default arguments): 57490b57cec5SDimitry Andric TemplateName X = Context.getCanonicalTemplateName(TemplateName(AArg)); 57500b57cec5SDimitry Andric TemplateParameterList *A = AArg->getTemplateParameters(); 57510b57cec5SDimitry Andric 57520b57cec5SDimitry Andric // - Each function template has a single function parameter whose type is 57530b57cec5SDimitry Andric // a specialization of X with template arguments corresponding to the 57540b57cec5SDimitry Andric // template parameters from the respective function template 57550b57cec5SDimitry Andric SmallVector<TemplateArgument, 8> AArgs; 57560b57cec5SDimitry Andric Context.getInjectedTemplateArgs(A, AArgs); 57570b57cec5SDimitry Andric 57580b57cec5SDimitry Andric // Check P's arguments against A's parameter list. This will fill in default 57590b57cec5SDimitry Andric // template arguments as needed. AArgs are already correct by construction. 57600b57cec5SDimitry Andric // We can't just use CheckTemplateIdType because that will expand alias 57610b57cec5SDimitry Andric // templates. 57620b57cec5SDimitry Andric SmallVector<TemplateArgument, 4> PArgs; 57630b57cec5SDimitry Andric { 57640b57cec5SDimitry Andric SFINAETrap Trap(*this); 57650b57cec5SDimitry Andric 57660b57cec5SDimitry Andric Context.getInjectedTemplateArgs(P, PArgs); 5767480093f4SDimitry Andric TemplateArgumentListInfo PArgList(P->getLAngleLoc(), 5768480093f4SDimitry Andric P->getRAngleLoc()); 57690b57cec5SDimitry Andric for (unsigned I = 0, N = P->size(); I != N; ++I) { 57700b57cec5SDimitry Andric // Unwrap packs that getInjectedTemplateArgs wrapped around pack 57710b57cec5SDimitry Andric // expansions, to form an "as written" argument list. 57720b57cec5SDimitry Andric TemplateArgument Arg = PArgs[I]; 57730b57cec5SDimitry Andric if (Arg.getKind() == TemplateArgument::Pack) { 57740b57cec5SDimitry Andric assert(Arg.pack_size() == 1 && Arg.pack_begin()->isPackExpansion()); 57750b57cec5SDimitry Andric Arg = *Arg.pack_begin(); 57760b57cec5SDimitry Andric } 57770b57cec5SDimitry Andric PArgList.addArgument(getTrivialTemplateArgumentLoc( 57780b57cec5SDimitry Andric Arg, QualType(), P->getParam(I)->getLocation())); 57790b57cec5SDimitry Andric } 57800b57cec5SDimitry Andric PArgs.clear(); 57810b57cec5SDimitry Andric 57820b57cec5SDimitry Andric // C++1z [temp.arg.template]p3: 57830b57cec5SDimitry Andric // If the rewrite produces an invalid type, then P is not at least as 57840b57cec5SDimitry Andric // specialized as A. 5785*bdd1243dSDimitry Andric SmallVector<TemplateArgument, 4> SugaredPArgs; 5786*bdd1243dSDimitry Andric if (CheckTemplateArgumentList(AArg, Loc, PArgList, false, SugaredPArgs, 5787*bdd1243dSDimitry Andric PArgs) || 57880b57cec5SDimitry Andric Trap.hasErrorOccurred()) 57890b57cec5SDimitry Andric return false; 57900b57cec5SDimitry Andric } 57910b57cec5SDimitry Andric 5792*bdd1243dSDimitry Andric QualType AType = Context.getCanonicalTemplateSpecializationType(X, AArgs); 5793*bdd1243dSDimitry Andric QualType PType = Context.getCanonicalTemplateSpecializationType(X, PArgs); 57940b57cec5SDimitry Andric 57950b57cec5SDimitry Andric // ... the function template corresponding to P is at least as specialized 57960b57cec5SDimitry Andric // as the function template corresponding to A according to the partial 57970b57cec5SDimitry Andric // ordering rules for function templates. 57980b57cec5SDimitry Andric TemplateDeductionInfo Info(Loc, A->getDepth()); 57990b57cec5SDimitry Andric return isAtLeastAsSpecializedAs(*this, PType, AType, AArg, Info); 58000b57cec5SDimitry Andric } 58010b57cec5SDimitry Andric 5802480093f4SDimitry Andric namespace { 5803480093f4SDimitry Andric struct MarkUsedTemplateParameterVisitor : 5804480093f4SDimitry Andric RecursiveASTVisitor<MarkUsedTemplateParameterVisitor> { 5805480093f4SDimitry Andric llvm::SmallBitVector &Used; 5806480093f4SDimitry Andric unsigned Depth; 5807480093f4SDimitry Andric 5808480093f4SDimitry Andric MarkUsedTemplateParameterVisitor(llvm::SmallBitVector &Used, 5809480093f4SDimitry Andric unsigned Depth) 5810480093f4SDimitry Andric : Used(Used), Depth(Depth) { } 5811480093f4SDimitry Andric 5812480093f4SDimitry Andric bool VisitTemplateTypeParmType(TemplateTypeParmType *T) { 5813480093f4SDimitry Andric if (T->getDepth() == Depth) 5814480093f4SDimitry Andric Used[T->getIndex()] = true; 5815480093f4SDimitry Andric return true; 5816480093f4SDimitry Andric } 5817480093f4SDimitry Andric 5818480093f4SDimitry Andric bool TraverseTemplateName(TemplateName Template) { 5819*bdd1243dSDimitry Andric if (auto *TTP = llvm::dyn_cast_or_null<TemplateTemplateParmDecl>( 5820*bdd1243dSDimitry Andric Template.getAsTemplateDecl())) 5821480093f4SDimitry Andric if (TTP->getDepth() == Depth) 5822480093f4SDimitry Andric Used[TTP->getIndex()] = true; 5823480093f4SDimitry Andric RecursiveASTVisitor<MarkUsedTemplateParameterVisitor>:: 5824480093f4SDimitry Andric TraverseTemplateName(Template); 5825480093f4SDimitry Andric return true; 5826480093f4SDimitry Andric } 5827480093f4SDimitry Andric 5828480093f4SDimitry Andric bool VisitDeclRefExpr(DeclRefExpr *E) { 5829480093f4SDimitry Andric if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) 5830480093f4SDimitry Andric if (NTTP->getDepth() == Depth) 5831480093f4SDimitry Andric Used[NTTP->getIndex()] = true; 5832480093f4SDimitry Andric return true; 5833480093f4SDimitry Andric } 5834480093f4SDimitry Andric }; 5835480093f4SDimitry Andric } 5836480093f4SDimitry Andric 58370b57cec5SDimitry Andric /// Mark the template parameters that are used by the given 58380b57cec5SDimitry Andric /// expression. 58390b57cec5SDimitry Andric static void 58400b57cec5SDimitry Andric MarkUsedTemplateParameters(ASTContext &Ctx, 58410b57cec5SDimitry Andric const Expr *E, 58420b57cec5SDimitry Andric bool OnlyDeduced, 58430b57cec5SDimitry Andric unsigned Depth, 58440b57cec5SDimitry Andric llvm::SmallBitVector &Used) { 5845480093f4SDimitry Andric if (!OnlyDeduced) { 5846480093f4SDimitry Andric MarkUsedTemplateParameterVisitor(Used, Depth) 5847480093f4SDimitry Andric .TraverseStmt(const_cast<Expr *>(E)); 5848480093f4SDimitry Andric return; 5849480093f4SDimitry Andric } 5850480093f4SDimitry Andric 58510b57cec5SDimitry Andric // We can deduce from a pack expansion. 58520b57cec5SDimitry Andric if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E)) 58530b57cec5SDimitry Andric E = Expansion->getPattern(); 58540b57cec5SDimitry Andric 5855e8d8bef9SDimitry Andric const NonTypeTemplateParmDecl *NTTP = getDeducedParameterFromExpr(E, Depth); 58560b57cec5SDimitry Andric if (!NTTP) 58570b57cec5SDimitry Andric return; 58580b57cec5SDimitry Andric 58590b57cec5SDimitry Andric if (NTTP->getDepth() == Depth) 58600b57cec5SDimitry Andric Used[NTTP->getIndex()] = true; 58610b57cec5SDimitry Andric 58620b57cec5SDimitry Andric // In C++17 mode, additional arguments may be deduced from the type of a 58630b57cec5SDimitry Andric // non-type argument. 58640b57cec5SDimitry Andric if (Ctx.getLangOpts().CPlusPlus17) 58650b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, NTTP->getType(), OnlyDeduced, Depth, Used); 58660b57cec5SDimitry Andric } 58670b57cec5SDimitry Andric 58680b57cec5SDimitry Andric /// Mark the template parameters that are used by the given 58690b57cec5SDimitry Andric /// nested name specifier. 58700b57cec5SDimitry Andric static void 58710b57cec5SDimitry Andric MarkUsedTemplateParameters(ASTContext &Ctx, 58720b57cec5SDimitry Andric NestedNameSpecifier *NNS, 58730b57cec5SDimitry Andric bool OnlyDeduced, 58740b57cec5SDimitry Andric unsigned Depth, 58750b57cec5SDimitry Andric llvm::SmallBitVector &Used) { 58760b57cec5SDimitry Andric if (!NNS) 58770b57cec5SDimitry Andric return; 58780b57cec5SDimitry Andric 58790b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, NNS->getPrefix(), OnlyDeduced, Depth, 58800b57cec5SDimitry Andric Used); 58810b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, QualType(NNS->getAsType(), 0), 58820b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 58830b57cec5SDimitry Andric } 58840b57cec5SDimitry Andric 58850b57cec5SDimitry Andric /// Mark the template parameters that are used by the given 58860b57cec5SDimitry Andric /// template name. 58870b57cec5SDimitry Andric static void 58880b57cec5SDimitry Andric MarkUsedTemplateParameters(ASTContext &Ctx, 58890b57cec5SDimitry Andric TemplateName Name, 58900b57cec5SDimitry Andric bool OnlyDeduced, 58910b57cec5SDimitry Andric unsigned Depth, 58920b57cec5SDimitry Andric llvm::SmallBitVector &Used) { 58930b57cec5SDimitry Andric if (TemplateDecl *Template = Name.getAsTemplateDecl()) { 58940b57cec5SDimitry Andric if (TemplateTemplateParmDecl *TTP 58950b57cec5SDimitry Andric = dyn_cast<TemplateTemplateParmDecl>(Template)) { 58960b57cec5SDimitry Andric if (TTP->getDepth() == Depth) 58970b57cec5SDimitry Andric Used[TTP->getIndex()] = true; 58980b57cec5SDimitry Andric } 58990b57cec5SDimitry Andric return; 59000b57cec5SDimitry Andric } 59010b57cec5SDimitry Andric 59020b57cec5SDimitry Andric if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) 59030b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, QTN->getQualifier(), OnlyDeduced, 59040b57cec5SDimitry Andric Depth, Used); 59050b57cec5SDimitry Andric if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) 59060b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, DTN->getQualifier(), OnlyDeduced, 59070b57cec5SDimitry Andric Depth, Used); 59080b57cec5SDimitry Andric } 59090b57cec5SDimitry Andric 59100b57cec5SDimitry Andric /// Mark the template parameters that are used by the given 59110b57cec5SDimitry Andric /// type. 59120b57cec5SDimitry Andric static void 59130b57cec5SDimitry Andric MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, 59140b57cec5SDimitry Andric bool OnlyDeduced, 59150b57cec5SDimitry Andric unsigned Depth, 59160b57cec5SDimitry Andric llvm::SmallBitVector &Used) { 59170b57cec5SDimitry Andric if (T.isNull()) 59180b57cec5SDimitry Andric return; 59190b57cec5SDimitry Andric 59200b57cec5SDimitry Andric // Non-dependent types have nothing deducible 59210b57cec5SDimitry Andric if (!T->isDependentType()) 59220b57cec5SDimitry Andric return; 59230b57cec5SDimitry Andric 59240b57cec5SDimitry Andric T = Ctx.getCanonicalType(T); 59250b57cec5SDimitry Andric switch (T->getTypeClass()) { 59260b57cec5SDimitry Andric case Type::Pointer: 59270b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 59280b57cec5SDimitry Andric cast<PointerType>(T)->getPointeeType(), 59290b57cec5SDimitry Andric OnlyDeduced, 59300b57cec5SDimitry Andric Depth, 59310b57cec5SDimitry Andric Used); 59320b57cec5SDimitry Andric break; 59330b57cec5SDimitry Andric 59340b57cec5SDimitry Andric case Type::BlockPointer: 59350b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 59360b57cec5SDimitry Andric cast<BlockPointerType>(T)->getPointeeType(), 59370b57cec5SDimitry Andric OnlyDeduced, 59380b57cec5SDimitry Andric Depth, 59390b57cec5SDimitry Andric Used); 59400b57cec5SDimitry Andric break; 59410b57cec5SDimitry Andric 59420b57cec5SDimitry Andric case Type::LValueReference: 59430b57cec5SDimitry Andric case Type::RValueReference: 59440b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 59450b57cec5SDimitry Andric cast<ReferenceType>(T)->getPointeeType(), 59460b57cec5SDimitry Andric OnlyDeduced, 59470b57cec5SDimitry Andric Depth, 59480b57cec5SDimitry Andric Used); 59490b57cec5SDimitry Andric break; 59500b57cec5SDimitry Andric 59510b57cec5SDimitry Andric case Type::MemberPointer: { 59520b57cec5SDimitry Andric const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); 59530b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, MemPtr->getPointeeType(), OnlyDeduced, 59540b57cec5SDimitry Andric Depth, Used); 59550b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, QualType(MemPtr->getClass(), 0), 59560b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 59570b57cec5SDimitry Andric break; 59580b57cec5SDimitry Andric } 59590b57cec5SDimitry Andric 59600b57cec5SDimitry Andric case Type::DependentSizedArray: 59610b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 59620b57cec5SDimitry Andric cast<DependentSizedArrayType>(T)->getSizeExpr(), 59630b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 59640b57cec5SDimitry Andric // Fall through to check the element type 5965*bdd1243dSDimitry Andric [[fallthrough]]; 59660b57cec5SDimitry Andric 59670b57cec5SDimitry Andric case Type::ConstantArray: 59680b57cec5SDimitry Andric case Type::IncompleteArray: 59690b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 59700b57cec5SDimitry Andric cast<ArrayType>(T)->getElementType(), 59710b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 59720b57cec5SDimitry Andric break; 59730b57cec5SDimitry Andric 59740b57cec5SDimitry Andric case Type::Vector: 59750b57cec5SDimitry Andric case Type::ExtVector: 59760b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 59770b57cec5SDimitry Andric cast<VectorType>(T)->getElementType(), 59780b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 59790b57cec5SDimitry Andric break; 59800b57cec5SDimitry Andric 59810b57cec5SDimitry Andric case Type::DependentVector: { 59820b57cec5SDimitry Andric const auto *VecType = cast<DependentVectorType>(T); 59830b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, 59840b57cec5SDimitry Andric Depth, Used); 59850b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, Depth, 59860b57cec5SDimitry Andric Used); 59870b57cec5SDimitry Andric break; 59880b57cec5SDimitry Andric } 59890b57cec5SDimitry Andric case Type::DependentSizedExtVector: { 59900b57cec5SDimitry Andric const DependentSizedExtVectorType *VecType 59910b57cec5SDimitry Andric = cast<DependentSizedExtVectorType>(T); 59920b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, 59930b57cec5SDimitry Andric Depth, Used); 59940b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, 59950b57cec5SDimitry Andric Depth, Used); 59960b57cec5SDimitry Andric break; 59970b57cec5SDimitry Andric } 59980b57cec5SDimitry Andric 59990b57cec5SDimitry Andric case Type::DependentAddressSpace: { 60000b57cec5SDimitry Andric const DependentAddressSpaceType *DependentASType = 60010b57cec5SDimitry Andric cast<DependentAddressSpaceType>(T); 60020b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, DependentASType->getPointeeType(), 60030b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 60040b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 60050b57cec5SDimitry Andric DependentASType->getAddrSpaceExpr(), 60060b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 60070b57cec5SDimitry Andric break; 60080b57cec5SDimitry Andric } 60090b57cec5SDimitry Andric 60105ffd83dbSDimitry Andric case Type::ConstantMatrix: { 60115ffd83dbSDimitry Andric const ConstantMatrixType *MatType = cast<ConstantMatrixType>(T); 60125ffd83dbSDimitry Andric MarkUsedTemplateParameters(Ctx, MatType->getElementType(), OnlyDeduced, 60135ffd83dbSDimitry Andric Depth, Used); 60145ffd83dbSDimitry Andric break; 60155ffd83dbSDimitry Andric } 60165ffd83dbSDimitry Andric 60175ffd83dbSDimitry Andric case Type::DependentSizedMatrix: { 60185ffd83dbSDimitry Andric const DependentSizedMatrixType *MatType = cast<DependentSizedMatrixType>(T); 60195ffd83dbSDimitry Andric MarkUsedTemplateParameters(Ctx, MatType->getElementType(), OnlyDeduced, 60205ffd83dbSDimitry Andric Depth, Used); 60215ffd83dbSDimitry Andric MarkUsedTemplateParameters(Ctx, MatType->getRowExpr(), OnlyDeduced, Depth, 60225ffd83dbSDimitry Andric Used); 60235ffd83dbSDimitry Andric MarkUsedTemplateParameters(Ctx, MatType->getColumnExpr(), OnlyDeduced, 60245ffd83dbSDimitry Andric Depth, Used); 60255ffd83dbSDimitry Andric break; 60265ffd83dbSDimitry Andric } 60275ffd83dbSDimitry Andric 60280b57cec5SDimitry Andric case Type::FunctionProto: { 60290b57cec5SDimitry Andric const FunctionProtoType *Proto = cast<FunctionProtoType>(T); 60300b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, Proto->getReturnType(), OnlyDeduced, Depth, 60310b57cec5SDimitry Andric Used); 60320b57cec5SDimitry Andric for (unsigned I = 0, N = Proto->getNumParams(); I != N; ++I) { 60330b57cec5SDimitry Andric // C++17 [temp.deduct.type]p5: 60340b57cec5SDimitry Andric // The non-deduced contexts are: [...] 60350b57cec5SDimitry Andric // -- A function parameter pack that does not occur at the end of the 60360b57cec5SDimitry Andric // parameter-declaration-list. 60370b57cec5SDimitry Andric if (!OnlyDeduced || I + 1 == N || 60380b57cec5SDimitry Andric !Proto->getParamType(I)->getAs<PackExpansionType>()) { 60390b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, Proto->getParamType(I), OnlyDeduced, 60400b57cec5SDimitry Andric Depth, Used); 60410b57cec5SDimitry Andric } else { 60420b57cec5SDimitry Andric // FIXME: C++17 [temp.deduct.call]p1: 60430b57cec5SDimitry Andric // When a function parameter pack appears in a non-deduced context, 60440b57cec5SDimitry Andric // the type of that pack is never deduced. 60450b57cec5SDimitry Andric // 60460b57cec5SDimitry Andric // We should also track a set of "never deduced" parameters, and 60470b57cec5SDimitry Andric // subtract that from the list of deduced parameters after marking. 60480b57cec5SDimitry Andric } 60490b57cec5SDimitry Andric } 60500b57cec5SDimitry Andric if (auto *E = Proto->getNoexceptExpr()) 60510b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, E, OnlyDeduced, Depth, Used); 60520b57cec5SDimitry Andric break; 60530b57cec5SDimitry Andric } 60540b57cec5SDimitry Andric 60550b57cec5SDimitry Andric case Type::TemplateTypeParm: { 60560b57cec5SDimitry Andric const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T); 60570b57cec5SDimitry Andric if (TTP->getDepth() == Depth) 60580b57cec5SDimitry Andric Used[TTP->getIndex()] = true; 60590b57cec5SDimitry Andric break; 60600b57cec5SDimitry Andric } 60610b57cec5SDimitry Andric 60620b57cec5SDimitry Andric case Type::SubstTemplateTypeParmPack: { 60630b57cec5SDimitry Andric const SubstTemplateTypeParmPackType *Subst 60640b57cec5SDimitry Andric = cast<SubstTemplateTypeParmPackType>(T); 6065*bdd1243dSDimitry Andric if (Subst->getReplacedParameter()->getDepth() == Depth) 6066*bdd1243dSDimitry Andric Used[Subst->getIndex()] = true; 60670b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, Subst->getArgumentPack(), 60680b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 60690b57cec5SDimitry Andric break; 60700b57cec5SDimitry Andric } 60710b57cec5SDimitry Andric 60720b57cec5SDimitry Andric case Type::InjectedClassName: 60730b57cec5SDimitry Andric T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); 6074*bdd1243dSDimitry Andric [[fallthrough]]; 60750b57cec5SDimitry Andric 60760b57cec5SDimitry Andric case Type::TemplateSpecialization: { 60770b57cec5SDimitry Andric const TemplateSpecializationType *Spec 60780b57cec5SDimitry Andric = cast<TemplateSpecializationType>(T); 60790b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, Spec->getTemplateName(), OnlyDeduced, 60800b57cec5SDimitry Andric Depth, Used); 60810b57cec5SDimitry Andric 60820b57cec5SDimitry Andric // C++0x [temp.deduct.type]p9: 60830b57cec5SDimitry Andric // If the template argument list of P contains a pack expansion that is 60840b57cec5SDimitry Andric // not the last template argument, the entire template argument list is a 60850b57cec5SDimitry Andric // non-deduced context. 60860b57cec5SDimitry Andric if (OnlyDeduced && 60870b57cec5SDimitry Andric hasPackExpansionBeforeEnd(Spec->template_arguments())) 60880b57cec5SDimitry Andric break; 60890b57cec5SDimitry Andric 6090*bdd1243dSDimitry Andric for (const auto &Arg : Spec->template_arguments()) 6091*bdd1243dSDimitry Andric MarkUsedTemplateParameters(Ctx, Arg, OnlyDeduced, Depth, Used); 60920b57cec5SDimitry Andric break; 60930b57cec5SDimitry Andric } 60940b57cec5SDimitry Andric 60950b57cec5SDimitry Andric case Type::Complex: 60960b57cec5SDimitry Andric if (!OnlyDeduced) 60970b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 60980b57cec5SDimitry Andric cast<ComplexType>(T)->getElementType(), 60990b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 61000b57cec5SDimitry Andric break; 61010b57cec5SDimitry Andric 61020b57cec5SDimitry Andric case Type::Atomic: 61030b57cec5SDimitry Andric if (!OnlyDeduced) 61040b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 61050b57cec5SDimitry Andric cast<AtomicType>(T)->getValueType(), 61060b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 61070b57cec5SDimitry Andric break; 61080b57cec5SDimitry Andric 61090b57cec5SDimitry Andric case Type::DependentName: 61100b57cec5SDimitry Andric if (!OnlyDeduced) 61110b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 61120b57cec5SDimitry Andric cast<DependentNameType>(T)->getQualifier(), 61130b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 61140b57cec5SDimitry Andric break; 61150b57cec5SDimitry Andric 61160b57cec5SDimitry Andric case Type::DependentTemplateSpecialization: { 61170b57cec5SDimitry Andric // C++14 [temp.deduct.type]p5: 61180b57cec5SDimitry Andric // The non-deduced contexts are: 61190b57cec5SDimitry Andric // -- The nested-name-specifier of a type that was specified using a 61200b57cec5SDimitry Andric // qualified-id 61210b57cec5SDimitry Andric // 61220b57cec5SDimitry Andric // C++14 [temp.deduct.type]p6: 61230b57cec5SDimitry Andric // When a type name is specified in a way that includes a non-deduced 61240b57cec5SDimitry Andric // context, all of the types that comprise that type name are also 61250b57cec5SDimitry Andric // non-deduced. 61260b57cec5SDimitry Andric if (OnlyDeduced) 61270b57cec5SDimitry Andric break; 61280b57cec5SDimitry Andric 61290b57cec5SDimitry Andric const DependentTemplateSpecializationType *Spec 61300b57cec5SDimitry Andric = cast<DependentTemplateSpecializationType>(T); 61310b57cec5SDimitry Andric 61320b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, Spec->getQualifier(), 61330b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 61340b57cec5SDimitry Andric 6135*bdd1243dSDimitry Andric for (const auto &Arg : Spec->template_arguments()) 6136*bdd1243dSDimitry Andric MarkUsedTemplateParameters(Ctx, Arg, OnlyDeduced, Depth, Used); 61370b57cec5SDimitry Andric break; 61380b57cec5SDimitry Andric } 61390b57cec5SDimitry Andric 61400b57cec5SDimitry Andric case Type::TypeOf: 61410b57cec5SDimitry Andric if (!OnlyDeduced) 6142*bdd1243dSDimitry Andric MarkUsedTemplateParameters(Ctx, cast<TypeOfType>(T)->getUnmodifiedType(), 61430b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 61440b57cec5SDimitry Andric break; 61450b57cec5SDimitry Andric 61460b57cec5SDimitry Andric case Type::TypeOfExpr: 61470b57cec5SDimitry Andric if (!OnlyDeduced) 61480b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 61490b57cec5SDimitry Andric cast<TypeOfExprType>(T)->getUnderlyingExpr(), 61500b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 61510b57cec5SDimitry Andric break; 61520b57cec5SDimitry Andric 61530b57cec5SDimitry Andric case Type::Decltype: 61540b57cec5SDimitry Andric if (!OnlyDeduced) 61550b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 61560b57cec5SDimitry Andric cast<DecltypeType>(T)->getUnderlyingExpr(), 61570b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 61580b57cec5SDimitry Andric break; 61590b57cec5SDimitry Andric 61600b57cec5SDimitry Andric case Type::UnaryTransform: 61610b57cec5SDimitry Andric if (!OnlyDeduced) 61620b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 61630b57cec5SDimitry Andric cast<UnaryTransformType>(T)->getUnderlyingType(), 61640b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 61650b57cec5SDimitry Andric break; 61660b57cec5SDimitry Andric 61670b57cec5SDimitry Andric case Type::PackExpansion: 61680b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 61690b57cec5SDimitry Andric cast<PackExpansionType>(T)->getPattern(), 61700b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 61710b57cec5SDimitry Andric break; 61720b57cec5SDimitry Andric 61730b57cec5SDimitry Andric case Type::Auto: 61740b57cec5SDimitry Andric case Type::DeducedTemplateSpecialization: 61750b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 61760b57cec5SDimitry Andric cast<DeducedType>(T)->getDeducedType(), 61770b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 61780b57cec5SDimitry Andric break; 61790eae32dcSDimitry Andric case Type::DependentBitInt: 61805ffd83dbSDimitry Andric MarkUsedTemplateParameters(Ctx, 61810eae32dcSDimitry Andric cast<DependentBitIntType>(T)->getNumBitsExpr(), 61825ffd83dbSDimitry Andric OnlyDeduced, Depth, Used); 61835ffd83dbSDimitry Andric break; 61840b57cec5SDimitry Andric 61850b57cec5SDimitry Andric // None of these types have any template parameters in them. 61860b57cec5SDimitry Andric case Type::Builtin: 61870b57cec5SDimitry Andric case Type::VariableArray: 61880b57cec5SDimitry Andric case Type::FunctionNoProto: 61890b57cec5SDimitry Andric case Type::Record: 61900b57cec5SDimitry Andric case Type::Enum: 61910b57cec5SDimitry Andric case Type::ObjCInterface: 61920b57cec5SDimitry Andric case Type::ObjCObject: 61930b57cec5SDimitry Andric case Type::ObjCObjectPointer: 61940b57cec5SDimitry Andric case Type::UnresolvedUsing: 61950b57cec5SDimitry Andric case Type::Pipe: 61960eae32dcSDimitry Andric case Type::BitInt: 61970b57cec5SDimitry Andric #define TYPE(Class, Base) 61980b57cec5SDimitry Andric #define ABSTRACT_TYPE(Class, Base) 61990b57cec5SDimitry Andric #define DEPENDENT_TYPE(Class, Base) 62000b57cec5SDimitry Andric #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 6201a7dea167SDimitry Andric #include "clang/AST/TypeNodes.inc" 62020b57cec5SDimitry Andric break; 62030b57cec5SDimitry Andric } 62040b57cec5SDimitry Andric } 62050b57cec5SDimitry Andric 62060b57cec5SDimitry Andric /// Mark the template parameters that are used by this 62070b57cec5SDimitry Andric /// template argument. 62080b57cec5SDimitry Andric static void 62090b57cec5SDimitry Andric MarkUsedTemplateParameters(ASTContext &Ctx, 62100b57cec5SDimitry Andric const TemplateArgument &TemplateArg, 62110b57cec5SDimitry Andric bool OnlyDeduced, 62120b57cec5SDimitry Andric unsigned Depth, 62130b57cec5SDimitry Andric llvm::SmallBitVector &Used) { 62140b57cec5SDimitry Andric switch (TemplateArg.getKind()) { 62150b57cec5SDimitry Andric case TemplateArgument::Null: 62160b57cec5SDimitry Andric case TemplateArgument::Integral: 62170b57cec5SDimitry Andric case TemplateArgument::Declaration: 62180b57cec5SDimitry Andric break; 62190b57cec5SDimitry Andric 62200b57cec5SDimitry Andric case TemplateArgument::NullPtr: 62210b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, TemplateArg.getNullPtrType(), OnlyDeduced, 62220b57cec5SDimitry Andric Depth, Used); 62230b57cec5SDimitry Andric break; 62240b57cec5SDimitry Andric 62250b57cec5SDimitry Andric case TemplateArgument::Type: 62260b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, TemplateArg.getAsType(), OnlyDeduced, 62270b57cec5SDimitry Andric Depth, Used); 62280b57cec5SDimitry Andric break; 62290b57cec5SDimitry Andric 62300b57cec5SDimitry Andric case TemplateArgument::Template: 62310b57cec5SDimitry Andric case TemplateArgument::TemplateExpansion: 62320b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 62330b57cec5SDimitry Andric TemplateArg.getAsTemplateOrTemplatePattern(), 62340b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 62350b57cec5SDimitry Andric break; 62360b57cec5SDimitry Andric 62370b57cec5SDimitry Andric case TemplateArgument::Expression: 62380b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, TemplateArg.getAsExpr(), OnlyDeduced, 62390b57cec5SDimitry Andric Depth, Used); 62400b57cec5SDimitry Andric break; 62410b57cec5SDimitry Andric 62420b57cec5SDimitry Andric case TemplateArgument::Pack: 62430b57cec5SDimitry Andric for (const auto &P : TemplateArg.pack_elements()) 62440b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, P, OnlyDeduced, Depth, Used); 62450b57cec5SDimitry Andric break; 62460b57cec5SDimitry Andric } 62470b57cec5SDimitry Andric } 62480b57cec5SDimitry Andric 6249480093f4SDimitry Andric /// Mark which template parameters are used in a given expression. 6250480093f4SDimitry Andric /// 6251480093f4SDimitry Andric /// \param E the expression from which template parameters will be deduced. 6252480093f4SDimitry Andric /// 6253480093f4SDimitry Andric /// \param Used a bit vector whose elements will be set to \c true 6254480093f4SDimitry Andric /// to indicate when the corresponding template parameter will be 6255480093f4SDimitry Andric /// deduced. 6256480093f4SDimitry Andric void 6257480093f4SDimitry Andric Sema::MarkUsedTemplateParameters(const Expr *E, bool OnlyDeduced, 6258480093f4SDimitry Andric unsigned Depth, 6259480093f4SDimitry Andric llvm::SmallBitVector &Used) { 6260480093f4SDimitry Andric ::MarkUsedTemplateParameters(Context, E, OnlyDeduced, Depth, Used); 6261480093f4SDimitry Andric } 6262480093f4SDimitry Andric 62630b57cec5SDimitry Andric /// Mark which template parameters can be deduced from a given 62640b57cec5SDimitry Andric /// template argument list. 62650b57cec5SDimitry Andric /// 62660b57cec5SDimitry Andric /// \param TemplateArgs the template argument list from which template 62670b57cec5SDimitry Andric /// parameters will be deduced. 62680b57cec5SDimitry Andric /// 62690b57cec5SDimitry Andric /// \param Used a bit vector whose elements will be set to \c true 62700b57cec5SDimitry Andric /// to indicate when the corresponding template parameter will be 62710b57cec5SDimitry Andric /// deduced. 62720b57cec5SDimitry Andric void 62730b57cec5SDimitry Andric Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, 62740b57cec5SDimitry Andric bool OnlyDeduced, unsigned Depth, 62750b57cec5SDimitry Andric llvm::SmallBitVector &Used) { 62760b57cec5SDimitry Andric // C++0x [temp.deduct.type]p9: 62770b57cec5SDimitry Andric // If the template argument list of P contains a pack expansion that is not 62780b57cec5SDimitry Andric // the last template argument, the entire template argument list is a 62790b57cec5SDimitry Andric // non-deduced context. 62800b57cec5SDimitry Andric if (OnlyDeduced && 62810b57cec5SDimitry Andric hasPackExpansionBeforeEnd(TemplateArgs.asArray())) 62820b57cec5SDimitry Andric return; 62830b57cec5SDimitry Andric 62840b57cec5SDimitry Andric for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) 62850b57cec5SDimitry Andric ::MarkUsedTemplateParameters(Context, TemplateArgs[I], OnlyDeduced, 62860b57cec5SDimitry Andric Depth, Used); 62870b57cec5SDimitry Andric } 62880b57cec5SDimitry Andric 62890b57cec5SDimitry Andric /// Marks all of the template parameters that will be deduced by a 62900b57cec5SDimitry Andric /// call to the given function template. 62910b57cec5SDimitry Andric void Sema::MarkDeducedTemplateParameters( 62920b57cec5SDimitry Andric ASTContext &Ctx, const FunctionTemplateDecl *FunctionTemplate, 62930b57cec5SDimitry Andric llvm::SmallBitVector &Deduced) { 62940b57cec5SDimitry Andric TemplateParameterList *TemplateParams 62950b57cec5SDimitry Andric = FunctionTemplate->getTemplateParameters(); 62960b57cec5SDimitry Andric Deduced.clear(); 62970b57cec5SDimitry Andric Deduced.resize(TemplateParams->size()); 62980b57cec5SDimitry Andric 62990b57cec5SDimitry Andric FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 63000b57cec5SDimitry Andric for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) 63010b57cec5SDimitry Andric ::MarkUsedTemplateParameters(Ctx, Function->getParamDecl(I)->getType(), 63020b57cec5SDimitry Andric true, TemplateParams->getDepth(), Deduced); 63030b57cec5SDimitry Andric } 63040b57cec5SDimitry Andric 63050b57cec5SDimitry Andric bool hasDeducibleTemplateParameters(Sema &S, 63060b57cec5SDimitry Andric FunctionTemplateDecl *FunctionTemplate, 63070b57cec5SDimitry Andric QualType T) { 63080b57cec5SDimitry Andric if (!T->isDependentType()) 63090b57cec5SDimitry Andric return false; 63100b57cec5SDimitry Andric 63110b57cec5SDimitry Andric TemplateParameterList *TemplateParams 63120b57cec5SDimitry Andric = FunctionTemplate->getTemplateParameters(); 63130b57cec5SDimitry Andric llvm::SmallBitVector Deduced(TemplateParams->size()); 63140b57cec5SDimitry Andric ::MarkUsedTemplateParameters(S.Context, T, true, TemplateParams->getDepth(), 63150b57cec5SDimitry Andric Deduced); 63160b57cec5SDimitry Andric 63170b57cec5SDimitry Andric return Deduced.any(); 63180b57cec5SDimitry Andric } 6319