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 "TreeTransform.h" 140b57cec5SDimitry Andric #include "TypeLocBuilder.h" 150b57cec5SDimitry Andric #include "clang/AST/ASTContext.h" 160b57cec5SDimitry Andric #include "clang/AST/ASTLambda.h" 170b57cec5SDimitry Andric #include "clang/AST/Decl.h" 180b57cec5SDimitry Andric #include "clang/AST/DeclAccessPair.h" 190b57cec5SDimitry Andric #include "clang/AST/DeclBase.h" 200b57cec5SDimitry Andric #include "clang/AST/DeclCXX.h" 210b57cec5SDimitry Andric #include "clang/AST/DeclTemplate.h" 220b57cec5SDimitry Andric #include "clang/AST/DeclarationName.h" 230b57cec5SDimitry Andric #include "clang/AST/Expr.h" 240b57cec5SDimitry Andric #include "clang/AST/ExprCXX.h" 250b57cec5SDimitry Andric #include "clang/AST/NestedNameSpecifier.h" 26480093f4SDimitry Andric #include "clang/AST/RecursiveASTVisitor.h" 270b57cec5SDimitry Andric #include "clang/AST/TemplateBase.h" 280b57cec5SDimitry Andric #include "clang/AST/TemplateName.h" 290b57cec5SDimitry Andric #include "clang/AST/Type.h" 300b57cec5SDimitry Andric #include "clang/AST/TypeLoc.h" 310b57cec5SDimitry Andric #include "clang/AST/UnresolvedSet.h" 320b57cec5SDimitry Andric #include "clang/Basic/AddressSpaces.h" 330b57cec5SDimitry Andric #include "clang/Basic/ExceptionSpecificationType.h" 340b57cec5SDimitry Andric #include "clang/Basic/LLVM.h" 350b57cec5SDimitry Andric #include "clang/Basic/LangOptions.h" 360b57cec5SDimitry Andric #include "clang/Basic/PartialDiagnostic.h" 370b57cec5SDimitry Andric #include "clang/Basic/SourceLocation.h" 380b57cec5SDimitry Andric #include "clang/Basic/Specifiers.h" 3906c3fb27SDimitry Andric #include "clang/Sema/EnterExpressionEvaluationContext.h" 400b57cec5SDimitry Andric #include "clang/Sema/Ownership.h" 410b57cec5SDimitry Andric #include "clang/Sema/Sema.h" 420b57cec5SDimitry Andric #include "clang/Sema/Template.h" 4306c3fb27SDimitry Andric #include "clang/Sema/TemplateDeduction.h" 440b57cec5SDimitry Andric #include "llvm/ADT/APInt.h" 450b57cec5SDimitry Andric #include "llvm/ADT/APSInt.h" 460b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 470b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 480b57cec5SDimitry Andric #include "llvm/ADT/FoldingSet.h" 490b57cec5SDimitry Andric #include "llvm/ADT/SmallBitVector.h" 500b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 510b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 520b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 530b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 540b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 550b57cec5SDimitry Andric #include <algorithm> 560b57cec5SDimitry Andric #include <cassert> 57bdd1243dSDimitry Andric #include <optional> 580b57cec5SDimitry Andric #include <tuple> 59bdd1243dSDimitry Andric #include <type_traits> 600b57cec5SDimitry Andric #include <utility> 610b57cec5SDimitry Andric 620b57cec5SDimitry Andric namespace clang { 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric /// Various flags that control template argument deduction. 650b57cec5SDimitry Andric /// 660b57cec5SDimitry Andric /// These flags can be bitwise-OR'd together. 670b57cec5SDimitry Andric enum TemplateDeductionFlags { 680b57cec5SDimitry Andric /// No template argument deduction flags, which indicates the 690b57cec5SDimitry Andric /// strictest results for template argument deduction (as used for, e.g., 700b57cec5SDimitry Andric /// matching class template partial specializations). 710b57cec5SDimitry Andric TDF_None = 0, 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric /// Within template argument deduction from a function call, we are 740b57cec5SDimitry Andric /// matching with a parameter type for which the original parameter was 750b57cec5SDimitry Andric /// a reference. 760b57cec5SDimitry Andric TDF_ParamWithReferenceType = 0x1, 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric /// Within template argument deduction from a function call, we 790b57cec5SDimitry Andric /// are matching in a case where we ignore cv-qualifiers. 800b57cec5SDimitry Andric TDF_IgnoreQualifiers = 0x02, 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric /// Within template argument deduction from a function call, 830b57cec5SDimitry Andric /// we are matching in a case where we can perform template argument 840b57cec5SDimitry Andric /// deduction from a template-id of a derived class of the argument type. 850b57cec5SDimitry Andric TDF_DerivedClass = 0x04, 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric /// Allow non-dependent types to differ, e.g., when performing 880b57cec5SDimitry Andric /// template argument deduction from a function call where conversions 890b57cec5SDimitry Andric /// may apply. 900b57cec5SDimitry Andric TDF_SkipNonDependent = 0x08, 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric /// Whether we are performing template argument deduction for 930b57cec5SDimitry Andric /// parameters and arguments in a top-level template argument 940b57cec5SDimitry Andric TDF_TopLevelParameterTypeList = 0x10, 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric /// Within template argument deduction from overload resolution per 970b57cec5SDimitry Andric /// C++ [over.over] allow matching function types that are compatible in 980b57cec5SDimitry Andric /// terms of noreturn and default calling convention adjustments, or 990b57cec5SDimitry Andric /// similarly matching a declared template specialization against a 1000b57cec5SDimitry Andric /// possible template, per C++ [temp.deduct.decl]. In either case, permit 1010b57cec5SDimitry Andric /// deduction where the parameter is a function type that can be converted 1020b57cec5SDimitry Andric /// to the argument type. 1030b57cec5SDimitry Andric TDF_AllowCompatibleFunctionType = 0x20, 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric /// Within template argument deduction for a conversion function, we are 1060b57cec5SDimitry Andric /// matching with an argument type for which the original argument was 1070b57cec5SDimitry Andric /// a reference. 1080b57cec5SDimitry Andric TDF_ArgWithReferenceType = 0x40, 1090b57cec5SDimitry Andric }; 1100b57cec5SDimitry Andric } 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric using namespace clang; 1130b57cec5SDimitry Andric using namespace sema; 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric /// Compare two APSInts, extending and switching the sign as 1160b57cec5SDimitry Andric /// necessary to compare their values regardless of underlying type. 1170b57cec5SDimitry Andric static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) { 1180b57cec5SDimitry Andric if (Y.getBitWidth() > X.getBitWidth()) 1190b57cec5SDimitry Andric X = X.extend(Y.getBitWidth()); 1200b57cec5SDimitry Andric else if (Y.getBitWidth() < X.getBitWidth()) 1210b57cec5SDimitry Andric Y = Y.extend(X.getBitWidth()); 1220b57cec5SDimitry Andric 1230b57cec5SDimitry Andric // If there is a signedness mismatch, correct it. 1240b57cec5SDimitry Andric if (X.isSigned() != Y.isSigned()) { 1250b57cec5SDimitry Andric // If the signed value is negative, then the values cannot be the same. 1260b57cec5SDimitry Andric if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative())) 1270b57cec5SDimitry Andric return false; 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric Y.setIsSigned(true); 1300b57cec5SDimitry Andric X.setIsSigned(true); 1310b57cec5SDimitry Andric } 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric return X == Y; 1340b57cec5SDimitry Andric } 1350b57cec5SDimitry Andric 136349cc55cSDimitry Andric static Sema::TemplateDeductionResult DeduceTemplateArgumentsByTypeMatch( 137349cc55cSDimitry Andric Sema &S, TemplateParameterList *TemplateParams, QualType Param, 138349cc55cSDimitry Andric QualType Arg, TemplateDeductionInfo &Info, 139349cc55cSDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, unsigned TDF, 140349cc55cSDimitry Andric bool PartialOrdering = false, bool DeducedFromArrayBound = false); 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric static Sema::TemplateDeductionResult 1430b57cec5SDimitry Andric DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, 144349cc55cSDimitry Andric ArrayRef<TemplateArgument> Ps, 145349cc55cSDimitry Andric ArrayRef<TemplateArgument> As, 1460b57cec5SDimitry Andric TemplateDeductionInfo &Info, 1470b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 1480b57cec5SDimitry Andric bool NumberOfArgumentsMustMatch); 1490b57cec5SDimitry Andric 1500b57cec5SDimitry Andric static void MarkUsedTemplateParameters(ASTContext &Ctx, 1510b57cec5SDimitry Andric const TemplateArgument &TemplateArg, 1520b57cec5SDimitry Andric bool OnlyDeduced, unsigned Depth, 1530b57cec5SDimitry Andric llvm::SmallBitVector &Used); 1540b57cec5SDimitry Andric 1550b57cec5SDimitry Andric static void MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, 1560b57cec5SDimitry Andric bool OnlyDeduced, unsigned Level, 1570b57cec5SDimitry Andric llvm::SmallBitVector &Deduced); 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric /// If the given expression is of a form that permits the deduction 1600b57cec5SDimitry Andric /// of a non-type template parameter, return the declaration of that 1610b57cec5SDimitry Andric /// non-type template parameter. 162e8d8bef9SDimitry Andric static const NonTypeTemplateParmDecl * 163e8d8bef9SDimitry Andric getDeducedParameterFromExpr(const Expr *E, unsigned Depth) { 1640b57cec5SDimitry Andric // If we are within an alias template, the expression may have undergone 1650b57cec5SDimitry Andric // any number of parameter substitutions already. 1660b57cec5SDimitry Andric while (true) { 167e8d8bef9SDimitry Andric if (const auto *IC = dyn_cast<ImplicitCastExpr>(E)) 1680b57cec5SDimitry Andric E = IC->getSubExpr(); 169e8d8bef9SDimitry Andric else if (const auto *CE = dyn_cast<ConstantExpr>(E)) 1700b57cec5SDimitry Andric E = CE->getSubExpr(); 171e8d8bef9SDimitry Andric else if (const auto *Subst = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) 1720b57cec5SDimitry Andric E = Subst->getReplacement(); 173e8d8bef9SDimitry Andric else if (const auto *CCE = dyn_cast<CXXConstructExpr>(E)) { 174e8d8bef9SDimitry Andric // Look through implicit copy construction from an lvalue of the same type. 175e8d8bef9SDimitry Andric if (CCE->getParenOrBraceRange().isValid()) 176e8d8bef9SDimitry Andric break; 177e8d8bef9SDimitry Andric // Note, there could be default arguments. 178e8d8bef9SDimitry Andric assert(CCE->getNumArgs() >= 1 && "implicit construct expr should have 1 arg"); 179e8d8bef9SDimitry Andric E = CCE->getArg(0); 180e8d8bef9SDimitry Andric } else 1810b57cec5SDimitry Andric break; 1820b57cec5SDimitry Andric } 1830b57cec5SDimitry Andric 184e8d8bef9SDimitry Andric if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) 185e8d8bef9SDimitry Andric if (const auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) 186e8d8bef9SDimitry Andric if (NTTP->getDepth() == Depth) 1870b57cec5SDimitry Andric return NTTP; 1880b57cec5SDimitry Andric 1890b57cec5SDimitry Andric return nullptr; 1900b57cec5SDimitry Andric } 1910b57cec5SDimitry Andric 192e8d8bef9SDimitry Andric static const NonTypeTemplateParmDecl * 193e8d8bef9SDimitry Andric getDeducedParameterFromExpr(TemplateDeductionInfo &Info, Expr *E) { 194e8d8bef9SDimitry Andric return getDeducedParameterFromExpr(E, Info.getDeducedDepth()); 195e8d8bef9SDimitry Andric } 196e8d8bef9SDimitry Andric 1970b57cec5SDimitry Andric /// Determine whether two declaration pointers refer to the same 1980b57cec5SDimitry Andric /// declaration. 1990b57cec5SDimitry Andric static bool isSameDeclaration(Decl *X, Decl *Y) { 2000b57cec5SDimitry Andric if (NamedDecl *NX = dyn_cast<NamedDecl>(X)) 2010b57cec5SDimitry Andric X = NX->getUnderlyingDecl(); 2020b57cec5SDimitry Andric if (NamedDecl *NY = dyn_cast<NamedDecl>(Y)) 2030b57cec5SDimitry Andric Y = NY->getUnderlyingDecl(); 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric return X->getCanonicalDecl() == Y->getCanonicalDecl(); 2060b57cec5SDimitry Andric } 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric /// Verify that the given, deduced template arguments are compatible. 2090b57cec5SDimitry Andric /// 2100b57cec5SDimitry Andric /// \returns The deduced template argument, or a NULL template argument if 2110b57cec5SDimitry Andric /// the deduced template arguments were incompatible. 2120b57cec5SDimitry Andric static DeducedTemplateArgument 2130b57cec5SDimitry Andric checkDeducedTemplateArguments(ASTContext &Context, 2140b57cec5SDimitry Andric const DeducedTemplateArgument &X, 21506c3fb27SDimitry Andric const DeducedTemplateArgument &Y, 21606c3fb27SDimitry Andric bool AggregateCandidateDeduction = false) { 2170b57cec5SDimitry Andric // We have no deduction for one or both of the arguments; they're compatible. 2180b57cec5SDimitry Andric if (X.isNull()) 2190b57cec5SDimitry Andric return Y; 2200b57cec5SDimitry Andric if (Y.isNull()) 2210b57cec5SDimitry Andric return X; 2220b57cec5SDimitry Andric 2230b57cec5SDimitry Andric // If we have two non-type template argument values deduced for the same 2240b57cec5SDimitry Andric // parameter, they must both match the type of the parameter, and thus must 2250b57cec5SDimitry Andric // match each other's type. As we're only keeping one of them, we must check 2260b57cec5SDimitry Andric // for that now. The exception is that if either was deduced from an array 2270b57cec5SDimitry Andric // bound, the type is permitted to differ. 2280b57cec5SDimitry Andric if (!X.wasDeducedFromArrayBound() && !Y.wasDeducedFromArrayBound()) { 2290b57cec5SDimitry Andric QualType XType = X.getNonTypeTemplateArgumentType(); 2300b57cec5SDimitry Andric if (!XType.isNull()) { 2310b57cec5SDimitry Andric QualType YType = Y.getNonTypeTemplateArgumentType(); 2320b57cec5SDimitry Andric if (YType.isNull() || !Context.hasSameType(XType, YType)) 2330b57cec5SDimitry Andric return DeducedTemplateArgument(); 2340b57cec5SDimitry Andric } 2350b57cec5SDimitry Andric } 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric switch (X.getKind()) { 2380b57cec5SDimitry Andric case TemplateArgument::Null: 2390b57cec5SDimitry Andric llvm_unreachable("Non-deduced template arguments handled above"); 2400b57cec5SDimitry Andric 241bdd1243dSDimitry Andric case TemplateArgument::Type: { 2420b57cec5SDimitry Andric // If two template type arguments have the same type, they're compatible. 243bdd1243dSDimitry Andric QualType TX = X.getAsType(), TY = Y.getAsType(); 244bdd1243dSDimitry Andric if (Y.getKind() == TemplateArgument::Type && Context.hasSameType(TX, TY)) 245bdd1243dSDimitry Andric return DeducedTemplateArgument(Context.getCommonSugaredType(TX, TY), 246bdd1243dSDimitry Andric X.wasDeducedFromArrayBound() || 247bdd1243dSDimitry Andric Y.wasDeducedFromArrayBound()); 2480b57cec5SDimitry Andric 2490b57cec5SDimitry Andric // If one of the two arguments was deduced from an array bound, the other 2500b57cec5SDimitry Andric // supersedes it. 2510b57cec5SDimitry Andric if (X.wasDeducedFromArrayBound() != Y.wasDeducedFromArrayBound()) 2520b57cec5SDimitry Andric return X.wasDeducedFromArrayBound() ? Y : X; 2530b57cec5SDimitry Andric 2540b57cec5SDimitry Andric // The arguments are not compatible. 2550b57cec5SDimitry Andric return DeducedTemplateArgument(); 256bdd1243dSDimitry Andric } 2570b57cec5SDimitry Andric 2580b57cec5SDimitry Andric case TemplateArgument::Integral: 2590b57cec5SDimitry Andric // If we deduced a constant in one case and either a dependent expression or 2600b57cec5SDimitry Andric // declaration in another case, keep the integral constant. 2610b57cec5SDimitry Andric // If both are integral constants with the same value, keep that value. 2620b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::Expression || 2630b57cec5SDimitry Andric Y.getKind() == TemplateArgument::Declaration || 2640b57cec5SDimitry Andric (Y.getKind() == TemplateArgument::Integral && 2650b57cec5SDimitry Andric hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()))) 2660b57cec5SDimitry Andric return X.wasDeducedFromArrayBound() ? Y : X; 2670b57cec5SDimitry Andric 2680b57cec5SDimitry Andric // All other combinations are incompatible. 2690b57cec5SDimitry Andric return DeducedTemplateArgument(); 2700b57cec5SDimitry Andric 271*7a6dacacSDimitry Andric case TemplateArgument::StructuralValue: 272*7a6dacacSDimitry Andric // If we deduced a value and a dependent expression, keep the value. 273*7a6dacacSDimitry Andric if (Y.getKind() == TemplateArgument::Expression || 274*7a6dacacSDimitry Andric (Y.getKind() == TemplateArgument::StructuralValue && 275*7a6dacacSDimitry Andric X.structurallyEquals(Y))) 276*7a6dacacSDimitry Andric return X; 277*7a6dacacSDimitry Andric 278*7a6dacacSDimitry Andric // All other combinations are incompatible. 279*7a6dacacSDimitry Andric return DeducedTemplateArgument(); 280*7a6dacacSDimitry Andric 2810b57cec5SDimitry Andric case TemplateArgument::Template: 2820b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::Template && 2830b57cec5SDimitry Andric Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate())) 2840b57cec5SDimitry Andric return X; 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric // All other combinations are incompatible. 2870b57cec5SDimitry Andric return DeducedTemplateArgument(); 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric case TemplateArgument::TemplateExpansion: 2900b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::TemplateExpansion && 2910b57cec5SDimitry Andric Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(), 2920b57cec5SDimitry Andric Y.getAsTemplateOrTemplatePattern())) 2930b57cec5SDimitry Andric return X; 2940b57cec5SDimitry Andric 2950b57cec5SDimitry Andric // All other combinations are incompatible. 2960b57cec5SDimitry Andric return DeducedTemplateArgument(); 2970b57cec5SDimitry Andric 2980b57cec5SDimitry Andric case TemplateArgument::Expression: { 2990b57cec5SDimitry Andric if (Y.getKind() != TemplateArgument::Expression) 3000b57cec5SDimitry Andric return checkDeducedTemplateArguments(Context, Y, X); 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andric // Compare the expressions for equality 3030b57cec5SDimitry Andric llvm::FoldingSetNodeID ID1, ID2; 3040b57cec5SDimitry Andric X.getAsExpr()->Profile(ID1, Context, true); 3050b57cec5SDimitry Andric Y.getAsExpr()->Profile(ID2, Context, true); 3060b57cec5SDimitry Andric if (ID1 == ID2) 3070b57cec5SDimitry Andric return X.wasDeducedFromArrayBound() ? Y : X; 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric // Differing dependent expressions are incompatible. 3100b57cec5SDimitry Andric return DeducedTemplateArgument(); 3110b57cec5SDimitry Andric } 3120b57cec5SDimitry Andric 3130b57cec5SDimitry Andric case TemplateArgument::Declaration: 3140b57cec5SDimitry Andric assert(!X.wasDeducedFromArrayBound()); 3150b57cec5SDimitry Andric 3160b57cec5SDimitry Andric // If we deduced a declaration and a dependent expression, keep the 3170b57cec5SDimitry Andric // declaration. 3180b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::Expression) 3190b57cec5SDimitry Andric return X; 3200b57cec5SDimitry Andric 3210b57cec5SDimitry Andric // If we deduced a declaration and an integral constant, keep the 3220b57cec5SDimitry Andric // integral constant and whichever type did not come from an array 3230b57cec5SDimitry Andric // bound. 3240b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::Integral) { 3250b57cec5SDimitry Andric if (Y.wasDeducedFromArrayBound()) 3260b57cec5SDimitry Andric return TemplateArgument(Context, Y.getAsIntegral(), 3270b57cec5SDimitry Andric X.getParamTypeForDecl()); 3280b57cec5SDimitry Andric return Y; 3290b57cec5SDimitry Andric } 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andric // If we deduced two declarations, make sure that they refer to the 3320b57cec5SDimitry Andric // same declaration. 3330b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::Declaration && 3340b57cec5SDimitry Andric isSameDeclaration(X.getAsDecl(), Y.getAsDecl())) 3350b57cec5SDimitry Andric return X; 3360b57cec5SDimitry Andric 3370b57cec5SDimitry Andric // All other combinations are incompatible. 3380b57cec5SDimitry Andric return DeducedTemplateArgument(); 3390b57cec5SDimitry Andric 3400b57cec5SDimitry Andric case TemplateArgument::NullPtr: 3410b57cec5SDimitry Andric // If we deduced a null pointer and a dependent expression, keep the 3420b57cec5SDimitry Andric // null pointer. 3430b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::Expression) 344bdd1243dSDimitry Andric return TemplateArgument(Context.getCommonSugaredType( 345bdd1243dSDimitry Andric X.getNullPtrType(), Y.getAsExpr()->getType()), 346bdd1243dSDimitry Andric true); 3470b57cec5SDimitry Andric 3480b57cec5SDimitry Andric // If we deduced a null pointer and an integral constant, keep the 3490b57cec5SDimitry Andric // integral constant. 3500b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::Integral) 3510b57cec5SDimitry Andric return Y; 3520b57cec5SDimitry Andric 3530b57cec5SDimitry Andric // If we deduced two null pointers, they are the same. 3540b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::NullPtr) 355bdd1243dSDimitry Andric return TemplateArgument( 356bdd1243dSDimitry Andric Context.getCommonSugaredType(X.getNullPtrType(), Y.getNullPtrType()), 357bdd1243dSDimitry Andric true); 3580b57cec5SDimitry Andric 3590b57cec5SDimitry Andric // All other combinations are incompatible. 3600b57cec5SDimitry Andric return DeducedTemplateArgument(); 3610b57cec5SDimitry Andric 3620b57cec5SDimitry Andric case TemplateArgument::Pack: { 3630b57cec5SDimitry Andric if (Y.getKind() != TemplateArgument::Pack || 36406c3fb27SDimitry Andric (!AggregateCandidateDeduction && X.pack_size() != Y.pack_size())) 3650b57cec5SDimitry Andric return DeducedTemplateArgument(); 3660b57cec5SDimitry Andric 3670b57cec5SDimitry Andric llvm::SmallVector<TemplateArgument, 8> NewPack; 36806c3fb27SDimitry Andric for (TemplateArgument::pack_iterator 36906c3fb27SDimitry Andric XA = X.pack_begin(), 37006c3fb27SDimitry Andric XAEnd = X.pack_end(), YA = Y.pack_begin(), YAEnd = Y.pack_end(); 3710b57cec5SDimitry Andric XA != XAEnd; ++XA, ++YA) { 37206c3fb27SDimitry Andric if (YA != YAEnd) { 3730b57cec5SDimitry Andric TemplateArgument Merged = checkDeducedTemplateArguments( 3740b57cec5SDimitry Andric Context, DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()), 3750b57cec5SDimitry Andric DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound())); 3765ffd83dbSDimitry Andric if (Merged.isNull() && !(XA->isNull() && YA->isNull())) 3770b57cec5SDimitry Andric return DeducedTemplateArgument(); 3780b57cec5SDimitry Andric NewPack.push_back(Merged); 37906c3fb27SDimitry Andric } else { 38006c3fb27SDimitry Andric NewPack.push_back(*XA); 38106c3fb27SDimitry Andric } 3820b57cec5SDimitry Andric } 3830b57cec5SDimitry Andric 3840b57cec5SDimitry Andric return DeducedTemplateArgument( 3850b57cec5SDimitry Andric TemplateArgument::CreatePackCopy(Context, NewPack), 3860b57cec5SDimitry Andric X.wasDeducedFromArrayBound() && Y.wasDeducedFromArrayBound()); 3870b57cec5SDimitry Andric } 3880b57cec5SDimitry Andric } 3890b57cec5SDimitry Andric 3900b57cec5SDimitry Andric llvm_unreachable("Invalid TemplateArgument Kind!"); 3910b57cec5SDimitry Andric } 3920b57cec5SDimitry Andric 3930b57cec5SDimitry Andric /// Deduce the value of the given non-type template parameter 3940b57cec5SDimitry Andric /// as the given deduced template argument. All non-type template parameter 3950b57cec5SDimitry Andric /// deduction is funneled through here. 3960b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( 3970b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, 398e8d8bef9SDimitry Andric const NonTypeTemplateParmDecl *NTTP, const DeducedTemplateArgument &NewDeduced, 3990b57cec5SDimitry Andric QualType ValueType, TemplateDeductionInfo &Info, 4000b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 4010b57cec5SDimitry Andric assert(NTTP->getDepth() == Info.getDeducedDepth() && 4020b57cec5SDimitry Andric "deducing non-type template argument with wrong depth"); 4030b57cec5SDimitry Andric 4040b57cec5SDimitry Andric DeducedTemplateArgument Result = checkDeducedTemplateArguments( 4050b57cec5SDimitry Andric S.Context, Deduced[NTTP->getIndex()], NewDeduced); 4060b57cec5SDimitry Andric if (Result.isNull()) { 407e8d8bef9SDimitry Andric Info.Param = const_cast<NonTypeTemplateParmDecl*>(NTTP); 4080b57cec5SDimitry Andric Info.FirstArg = Deduced[NTTP->getIndex()]; 4090b57cec5SDimitry Andric Info.SecondArg = NewDeduced; 4100b57cec5SDimitry Andric return Sema::TDK_Inconsistent; 4110b57cec5SDimitry Andric } 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andric Deduced[NTTP->getIndex()] = Result; 4140b57cec5SDimitry Andric if (!S.getLangOpts().CPlusPlus17) 4150b57cec5SDimitry Andric return Sema::TDK_Success; 4160b57cec5SDimitry Andric 4170b57cec5SDimitry Andric if (NTTP->isExpandedParameterPack()) 4180b57cec5SDimitry Andric // FIXME: We may still need to deduce parts of the type here! But we 4190b57cec5SDimitry Andric // don't have any way to find which slice of the type to use, and the 4200b57cec5SDimitry Andric // type stored on the NTTP itself is nonsense. Perhaps the type of an 4210b57cec5SDimitry Andric // expanded NTTP should be a pack expansion type? 4220b57cec5SDimitry Andric return Sema::TDK_Success; 4230b57cec5SDimitry Andric 4240b57cec5SDimitry Andric // Get the type of the parameter for deduction. If it's a (dependent) array 4250b57cec5SDimitry Andric // or function type, we will not have decayed it yet, so do that now. 4260b57cec5SDimitry Andric QualType ParamType = S.Context.getAdjustedParameterType(NTTP->getType()); 4270b57cec5SDimitry Andric if (auto *Expansion = dyn_cast<PackExpansionType>(ParamType)) 4280b57cec5SDimitry Andric ParamType = Expansion->getPattern(); 4290b57cec5SDimitry Andric 4300b57cec5SDimitry Andric // FIXME: It's not clear how deduction of a parameter of reference 4310b57cec5SDimitry Andric // type from an argument (of non-reference type) should be performed. 4320b57cec5SDimitry Andric // For now, we just remove reference types from both sides and let 4330b57cec5SDimitry Andric // the final check for matching types sort out the mess. 434e8d8bef9SDimitry Andric ValueType = ValueType.getNonReferenceType(); 435e8d8bef9SDimitry Andric if (ParamType->isReferenceType()) 436e8d8bef9SDimitry Andric ParamType = ParamType.getNonReferenceType(); 437e8d8bef9SDimitry Andric else 438e8d8bef9SDimitry Andric // Top-level cv-qualifiers are irrelevant for a non-reference type. 439e8d8bef9SDimitry Andric ValueType = ValueType.getUnqualifiedType(); 440e8d8bef9SDimitry Andric 4410b57cec5SDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 442e8d8bef9SDimitry Andric S, TemplateParams, ParamType, ValueType, Info, Deduced, 443e8d8bef9SDimitry Andric TDF_SkipNonDependent, /*PartialOrdering=*/false, 4440b57cec5SDimitry Andric /*ArrayBound=*/NewDeduced.wasDeducedFromArrayBound()); 4450b57cec5SDimitry Andric } 4460b57cec5SDimitry Andric 4470b57cec5SDimitry Andric /// Deduce the value of the given non-type template parameter 4480b57cec5SDimitry Andric /// from the given integral constant. 4490b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( 4500b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, 451e8d8bef9SDimitry Andric const NonTypeTemplateParmDecl *NTTP, const llvm::APSInt &Value, 4520b57cec5SDimitry Andric QualType ValueType, bool DeducedFromArrayBound, TemplateDeductionInfo &Info, 4530b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 4540b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument( 4550b57cec5SDimitry Andric S, TemplateParams, NTTP, 4560b57cec5SDimitry Andric DeducedTemplateArgument(S.Context, Value, ValueType, 4570b57cec5SDimitry Andric DeducedFromArrayBound), 4580b57cec5SDimitry Andric ValueType, Info, Deduced); 4590b57cec5SDimitry Andric } 4600b57cec5SDimitry Andric 4610b57cec5SDimitry Andric /// Deduce the value of the given non-type template parameter 4620b57cec5SDimitry Andric /// from the given null pointer template argument type. 4630b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceNullPtrTemplateArgument( 4640b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, 465e8d8bef9SDimitry Andric const NonTypeTemplateParmDecl *NTTP, QualType NullPtrType, 4660b57cec5SDimitry Andric TemplateDeductionInfo &Info, 4670b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 468fe6060f1SDimitry Andric Expr *Value = S.ImpCastExprToType( 469fe6060f1SDimitry Andric new (S.Context) CXXNullPtrLiteralExpr(S.Context.NullPtrTy, 470fe6060f1SDimitry Andric NTTP->getLocation()), 471fe6060f1SDimitry Andric NullPtrType, 472fe6060f1SDimitry Andric NullPtrType->isMemberPointerType() ? CK_NullToMemberPointer 473fe6060f1SDimitry Andric : CK_NullToPointer) 4740b57cec5SDimitry Andric .get(); 4750b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, 4760b57cec5SDimitry Andric DeducedTemplateArgument(Value), 4770b57cec5SDimitry Andric Value->getType(), Info, Deduced); 4780b57cec5SDimitry Andric } 4790b57cec5SDimitry Andric 4800b57cec5SDimitry Andric /// Deduce the value of the given non-type template parameter 4810b57cec5SDimitry Andric /// from the given type- or value-dependent expression. 4820b57cec5SDimitry Andric /// 4830b57cec5SDimitry Andric /// \returns true if deduction succeeded, false otherwise. 4840b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( 4850b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, 486e8d8bef9SDimitry Andric const NonTypeTemplateParmDecl *NTTP, Expr *Value, TemplateDeductionInfo &Info, 4870b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 4880b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, 4890b57cec5SDimitry Andric DeducedTemplateArgument(Value), 4900b57cec5SDimitry Andric Value->getType(), Info, Deduced); 4910b57cec5SDimitry Andric } 4920b57cec5SDimitry Andric 4930b57cec5SDimitry Andric /// Deduce the value of the given non-type template parameter 4940b57cec5SDimitry Andric /// from the given declaration. 4950b57cec5SDimitry Andric /// 4960b57cec5SDimitry Andric /// \returns true if deduction succeeded, false otherwise. 4970b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( 4980b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, 499e8d8bef9SDimitry Andric const NonTypeTemplateParmDecl *NTTP, ValueDecl *D, QualType T, 5000b57cec5SDimitry Andric TemplateDeductionInfo &Info, 5010b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 5020b57cec5SDimitry Andric D = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; 5030b57cec5SDimitry Andric TemplateArgument New(D, T); 5040b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument( 5050b57cec5SDimitry Andric S, TemplateParams, NTTP, DeducedTemplateArgument(New), T, Info, Deduced); 5060b57cec5SDimitry Andric } 5070b57cec5SDimitry Andric 5080b57cec5SDimitry Andric static Sema::TemplateDeductionResult 5090b57cec5SDimitry Andric DeduceTemplateArguments(Sema &S, 5100b57cec5SDimitry Andric TemplateParameterList *TemplateParams, 5110b57cec5SDimitry Andric TemplateName Param, 5120b57cec5SDimitry Andric TemplateName Arg, 5130b57cec5SDimitry Andric TemplateDeductionInfo &Info, 5140b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 5150b57cec5SDimitry Andric TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); 5160b57cec5SDimitry Andric if (!ParamDecl) { 5170b57cec5SDimitry Andric // The parameter type is dependent and is not a template template parameter, 5180b57cec5SDimitry Andric // so there is nothing that we can deduce. 5190b57cec5SDimitry Andric return Sema::TDK_Success; 5200b57cec5SDimitry Andric } 5210b57cec5SDimitry Andric 5220b57cec5SDimitry Andric if (TemplateTemplateParmDecl *TempParam 5230b57cec5SDimitry Andric = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) { 5240b57cec5SDimitry Andric // If we're not deducing at this depth, there's nothing to deduce. 5250b57cec5SDimitry Andric if (TempParam->getDepth() != Info.getDeducedDepth()) 5260b57cec5SDimitry Andric return Sema::TDK_Success; 5270b57cec5SDimitry Andric 5280b57cec5SDimitry Andric DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg)); 5290b57cec5SDimitry Andric DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, 5300b57cec5SDimitry Andric Deduced[TempParam->getIndex()], 5310b57cec5SDimitry Andric NewDeduced); 5320b57cec5SDimitry Andric if (Result.isNull()) { 5330b57cec5SDimitry Andric Info.Param = TempParam; 5340b57cec5SDimitry Andric Info.FirstArg = Deduced[TempParam->getIndex()]; 5350b57cec5SDimitry Andric Info.SecondArg = NewDeduced; 5360b57cec5SDimitry Andric return Sema::TDK_Inconsistent; 5370b57cec5SDimitry Andric } 5380b57cec5SDimitry Andric 5390b57cec5SDimitry Andric Deduced[TempParam->getIndex()] = Result; 5400b57cec5SDimitry Andric return Sema::TDK_Success; 5410b57cec5SDimitry Andric } 5420b57cec5SDimitry Andric 5430b57cec5SDimitry Andric // Verify that the two template names are equivalent. 5440b57cec5SDimitry Andric if (S.Context.hasSameTemplateName(Param, Arg)) 5450b57cec5SDimitry Andric return Sema::TDK_Success; 5460b57cec5SDimitry Andric 5470b57cec5SDimitry Andric // Mismatch of non-dependent template parameter to argument. 5480b57cec5SDimitry Andric Info.FirstArg = TemplateArgument(Param); 5490b57cec5SDimitry Andric Info.SecondArg = TemplateArgument(Arg); 5500b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 5510b57cec5SDimitry Andric } 5520b57cec5SDimitry Andric 5530b57cec5SDimitry Andric /// Deduce the template arguments by comparing the template parameter 5540b57cec5SDimitry Andric /// type (which is a template-id) with the template argument type. 5550b57cec5SDimitry Andric /// 5560b57cec5SDimitry Andric /// \param S the Sema 5570b57cec5SDimitry Andric /// 5580b57cec5SDimitry Andric /// \param TemplateParams the template parameters that we are deducing 5590b57cec5SDimitry Andric /// 56081ad6265SDimitry Andric /// \param P the parameter type 5610b57cec5SDimitry Andric /// 56281ad6265SDimitry Andric /// \param A the argument type 5630b57cec5SDimitry Andric /// 5640b57cec5SDimitry Andric /// \param Info information about the template argument deduction itself 5650b57cec5SDimitry Andric /// 5660b57cec5SDimitry Andric /// \param Deduced the deduced template arguments 5670b57cec5SDimitry Andric /// 5680b57cec5SDimitry Andric /// \returns the result of template argument deduction so far. Note that a 5690b57cec5SDimitry Andric /// "success" result means that template argument deduction has not yet failed, 5700b57cec5SDimitry Andric /// but it may still fail, later, for other reasons. 5710b57cec5SDimitry Andric static Sema::TemplateDeductionResult 572349cc55cSDimitry Andric DeduceTemplateSpecArguments(Sema &S, TemplateParameterList *TemplateParams, 573349cc55cSDimitry Andric const QualType P, QualType A, 5740b57cec5SDimitry Andric TemplateDeductionInfo &Info, 5750b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 576349cc55cSDimitry Andric QualType UP = P; 577349cc55cSDimitry Andric if (const auto *IP = P->getAs<InjectedClassNameType>()) 578349cc55cSDimitry Andric UP = IP->getInjectedSpecializationType(); 579349cc55cSDimitry Andric // FIXME: Try to preserve type sugar here, which is hard 580349cc55cSDimitry Andric // because of the unresolved template arguments. 581349cc55cSDimitry Andric const auto *TP = UP.getCanonicalType()->castAs<TemplateSpecializationType>(); 582bdd1243dSDimitry Andric TemplateName TNP = TP->getTemplateName(); 583bdd1243dSDimitry Andric 584bdd1243dSDimitry Andric // If the parameter is an alias template, there is nothing to deduce. 585bdd1243dSDimitry Andric if (const auto *TD = TNP.getAsTemplateDecl(); TD && TD->isTypeAlias()) 586bdd1243dSDimitry Andric return Sema::TDK_Success; 587bdd1243dSDimitry Andric 588349cc55cSDimitry Andric ArrayRef<TemplateArgument> PResolved = TP->template_arguments(); 5890b57cec5SDimitry Andric 590349cc55cSDimitry Andric QualType UA = A; 5910b57cec5SDimitry Andric // Treat an injected-class-name as its underlying template-id. 592349cc55cSDimitry Andric if (const auto *Injected = A->getAs<InjectedClassNameType>()) 593349cc55cSDimitry Andric UA = Injected->getInjectedSpecializationType(); 5940b57cec5SDimitry Andric 5950b57cec5SDimitry Andric // Check whether the template argument is a dependent template-id. 596349cc55cSDimitry Andric // FIXME: Should not lose sugar here. 597349cc55cSDimitry Andric if (const auto *SA = 598349cc55cSDimitry Andric dyn_cast<TemplateSpecializationType>(UA.getCanonicalType())) { 599bdd1243dSDimitry Andric TemplateName TNA = SA->getTemplateName(); 600bdd1243dSDimitry Andric 601bdd1243dSDimitry Andric // If the argument is an alias template, there is nothing to deduce. 602bdd1243dSDimitry Andric if (const auto *TD = TNA.getAsTemplateDecl(); TD && TD->isTypeAlias()) 603bdd1243dSDimitry Andric return Sema::TDK_Success; 604bdd1243dSDimitry Andric 6050b57cec5SDimitry Andric // Perform template argument deduction for the template name. 606349cc55cSDimitry Andric if (auto Result = 607bdd1243dSDimitry Andric DeduceTemplateArguments(S, TemplateParams, TNP, TNA, Info, Deduced)) 6080b57cec5SDimitry Andric return Result; 6090b57cec5SDimitry Andric // Perform template argument deduction on each template 6100b57cec5SDimitry Andric // argument. Ignore any missing/extra arguments, since they could be 6110b57cec5SDimitry Andric // filled in by default arguments. 612349cc55cSDimitry Andric return DeduceTemplateArguments(S, TemplateParams, PResolved, 613349cc55cSDimitry Andric SA->template_arguments(), Info, Deduced, 6140b57cec5SDimitry Andric /*NumberOfArgumentsMustMatch=*/false); 6150b57cec5SDimitry Andric } 6160b57cec5SDimitry Andric 6170b57cec5SDimitry Andric // If the argument type is a class template specialization, we 6180b57cec5SDimitry Andric // perform template argument deduction using its template 6190b57cec5SDimitry Andric // arguments. 620349cc55cSDimitry Andric const auto *RA = UA->getAs<RecordType>(); 621349cc55cSDimitry Andric const auto *SA = 622349cc55cSDimitry Andric RA ? dyn_cast<ClassTemplateSpecializationDecl>(RA->getDecl()) : nullptr; 623349cc55cSDimitry Andric if (!SA) { 624349cc55cSDimitry Andric Info.FirstArg = TemplateArgument(P); 625349cc55cSDimitry Andric Info.SecondArg = TemplateArgument(A); 6260b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 6270b57cec5SDimitry Andric } 6280b57cec5SDimitry Andric 6290b57cec5SDimitry Andric // Perform template argument deduction for the template name. 630349cc55cSDimitry Andric if (auto Result = DeduceTemplateArguments( 631349cc55cSDimitry Andric S, TemplateParams, TP->getTemplateName(), 632349cc55cSDimitry Andric TemplateName(SA->getSpecializedTemplate()), Info, Deduced)) 6330b57cec5SDimitry Andric return Result; 6340b57cec5SDimitry Andric 6350b57cec5SDimitry Andric // Perform template argument deduction for the template arguments. 636349cc55cSDimitry Andric return DeduceTemplateArguments(S, TemplateParams, PResolved, 637349cc55cSDimitry Andric SA->getTemplateArgs().asArray(), Info, Deduced, 638349cc55cSDimitry Andric /*NumberOfArgumentsMustMatch=*/true); 6390b57cec5SDimitry Andric } 6400b57cec5SDimitry Andric 641349cc55cSDimitry Andric static bool IsPossiblyOpaquelyQualifiedTypeInternal(const Type *T) { 642349cc55cSDimitry Andric assert(T->isCanonicalUnqualified()); 643349cc55cSDimitry Andric 6440b57cec5SDimitry Andric switch (T->getTypeClass()) { 6450b57cec5SDimitry Andric case Type::TypeOfExpr: 6460b57cec5SDimitry Andric case Type::TypeOf: 6470b57cec5SDimitry Andric case Type::DependentName: 6480b57cec5SDimitry Andric case Type::Decltype: 6490b57cec5SDimitry Andric case Type::UnresolvedUsing: 6500b57cec5SDimitry Andric case Type::TemplateTypeParm: 6510b57cec5SDimitry Andric return true; 6520b57cec5SDimitry Andric 6530b57cec5SDimitry Andric case Type::ConstantArray: 6540b57cec5SDimitry Andric case Type::IncompleteArray: 6550b57cec5SDimitry Andric case Type::VariableArray: 6560b57cec5SDimitry Andric case Type::DependentSizedArray: 657349cc55cSDimitry Andric return IsPossiblyOpaquelyQualifiedTypeInternal( 658349cc55cSDimitry Andric cast<ArrayType>(T)->getElementType().getTypePtr()); 6590b57cec5SDimitry Andric 6600b57cec5SDimitry Andric default: 6610b57cec5SDimitry Andric return false; 6620b57cec5SDimitry Andric } 6630b57cec5SDimitry Andric } 6640b57cec5SDimitry Andric 665349cc55cSDimitry Andric /// Determines whether the given type is an opaque type that 666349cc55cSDimitry Andric /// might be more qualified when instantiated. 667349cc55cSDimitry Andric static bool IsPossiblyOpaquelyQualifiedType(QualType T) { 668349cc55cSDimitry Andric return IsPossiblyOpaquelyQualifiedTypeInternal( 669349cc55cSDimitry Andric T->getCanonicalTypeInternal().getTypePtr()); 670349cc55cSDimitry Andric } 671349cc55cSDimitry Andric 6720b57cec5SDimitry Andric /// Helper function to build a TemplateParameter when we don't 6730b57cec5SDimitry Andric /// know its type statically. 6740b57cec5SDimitry Andric static TemplateParameter makeTemplateParameter(Decl *D) { 6750b57cec5SDimitry Andric if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) 6760b57cec5SDimitry Andric return TemplateParameter(TTP); 6770b57cec5SDimitry Andric if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) 6780b57cec5SDimitry Andric return TemplateParameter(NTTP); 6790b57cec5SDimitry Andric 6800b57cec5SDimitry Andric return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); 6810b57cec5SDimitry Andric } 6820b57cec5SDimitry Andric 6830b57cec5SDimitry Andric /// A pack that we're currently deducing. 6840b57cec5SDimitry Andric struct clang::DeducedPack { 6850b57cec5SDimitry Andric // The index of the pack. 6860b57cec5SDimitry Andric unsigned Index; 6870b57cec5SDimitry Andric 6880b57cec5SDimitry Andric // The old value of the pack before we started deducing it. 6890b57cec5SDimitry Andric DeducedTemplateArgument Saved; 6900b57cec5SDimitry Andric 6910b57cec5SDimitry Andric // A deferred value of this pack from an inner deduction, that couldn't be 6920b57cec5SDimitry Andric // deduced because this deduction hadn't happened yet. 6930b57cec5SDimitry Andric DeducedTemplateArgument DeferredDeduction; 6940b57cec5SDimitry Andric 6950b57cec5SDimitry Andric // The new value of the pack. 6960b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> New; 6970b57cec5SDimitry Andric 6980b57cec5SDimitry Andric // The outer deduction for this pack, if any. 6990b57cec5SDimitry Andric DeducedPack *Outer = nullptr; 7000b57cec5SDimitry Andric 7010b57cec5SDimitry Andric DeducedPack(unsigned Index) : Index(Index) {} 7020b57cec5SDimitry Andric }; 7030b57cec5SDimitry Andric 7040b57cec5SDimitry Andric namespace { 7050b57cec5SDimitry Andric 7060b57cec5SDimitry Andric /// A scope in which we're performing pack deduction. 7070b57cec5SDimitry Andric class PackDeductionScope { 7080b57cec5SDimitry Andric public: 7090b57cec5SDimitry Andric /// Prepare to deduce the packs named within Pattern. 7100b57cec5SDimitry Andric PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, 7110b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 71206c3fb27SDimitry Andric TemplateDeductionInfo &Info, TemplateArgument Pattern, 71306c3fb27SDimitry Andric bool DeducePackIfNotAlreadyDeduced = false) 71406c3fb27SDimitry Andric : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info), 71506c3fb27SDimitry Andric DeducePackIfNotAlreadyDeduced(DeducePackIfNotAlreadyDeduced){ 7160b57cec5SDimitry Andric unsigned NumNamedPacks = addPacks(Pattern); 7170b57cec5SDimitry Andric finishConstruction(NumNamedPacks); 7180b57cec5SDimitry Andric } 7190b57cec5SDimitry Andric 7200b57cec5SDimitry Andric /// Prepare to directly deduce arguments of the parameter with index \p Index. 7210b57cec5SDimitry Andric PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, 7220b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 7230b57cec5SDimitry Andric TemplateDeductionInfo &Info, unsigned Index) 7240b57cec5SDimitry Andric : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { 7250b57cec5SDimitry Andric addPack(Index); 7260b57cec5SDimitry Andric finishConstruction(1); 7270b57cec5SDimitry Andric } 7280b57cec5SDimitry Andric 7290b57cec5SDimitry Andric private: 7300b57cec5SDimitry Andric void addPack(unsigned Index) { 7310b57cec5SDimitry Andric // Save the deduced template argument for the parameter pack expanded 7320b57cec5SDimitry Andric // by this pack expansion, then clear out the deduction. 7330b57cec5SDimitry Andric DeducedPack Pack(Index); 7340b57cec5SDimitry Andric Pack.Saved = Deduced[Index]; 7350b57cec5SDimitry Andric Deduced[Index] = TemplateArgument(); 7360b57cec5SDimitry Andric 7370b57cec5SDimitry Andric // FIXME: What if we encounter multiple packs with different numbers of 7380b57cec5SDimitry Andric // pre-expanded expansions? (This should already have been diagnosed 7390b57cec5SDimitry Andric // during substitution.) 740bdd1243dSDimitry Andric if (std::optional<unsigned> ExpandedPackExpansions = 7410b57cec5SDimitry Andric getExpandedPackSize(TemplateParams->getParam(Index))) 7420b57cec5SDimitry Andric FixedNumExpansions = ExpandedPackExpansions; 7430b57cec5SDimitry Andric 7440b57cec5SDimitry Andric Packs.push_back(Pack); 7450b57cec5SDimitry Andric } 7460b57cec5SDimitry Andric 7470b57cec5SDimitry Andric unsigned addPacks(TemplateArgument Pattern) { 7480b57cec5SDimitry Andric // Compute the set of template parameter indices that correspond to 7490b57cec5SDimitry Andric // parameter packs expanded by the pack expansion. 7500b57cec5SDimitry Andric llvm::SmallBitVector SawIndices(TemplateParams->size()); 75155e4f9d5SDimitry Andric llvm::SmallVector<TemplateArgument, 4> ExtraDeductions; 7520b57cec5SDimitry Andric 7530b57cec5SDimitry Andric auto AddPack = [&](unsigned Index) { 7540b57cec5SDimitry Andric if (SawIndices[Index]) 7550b57cec5SDimitry Andric return; 7560b57cec5SDimitry Andric SawIndices[Index] = true; 7570b57cec5SDimitry Andric addPack(Index); 75855e4f9d5SDimitry Andric 75955e4f9d5SDimitry Andric // Deducing a parameter pack that is a pack expansion also constrains the 76055e4f9d5SDimitry Andric // packs appearing in that parameter to have the same deduced arity. Also, 76155e4f9d5SDimitry Andric // in C++17 onwards, deducing a non-type template parameter deduces its 76255e4f9d5SDimitry Andric // type, so we need to collect the pending deduced values for those packs. 76355e4f9d5SDimitry Andric if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>( 76455e4f9d5SDimitry Andric TemplateParams->getParam(Index))) { 7655ffd83dbSDimitry Andric if (!NTTP->isExpandedParameterPack()) 76655e4f9d5SDimitry Andric if (auto *Expansion = dyn_cast<PackExpansionType>(NTTP->getType())) 76755e4f9d5SDimitry Andric ExtraDeductions.push_back(Expansion->getPattern()); 76855e4f9d5SDimitry Andric } 76955e4f9d5SDimitry Andric // FIXME: Also collect the unexpanded packs in any type and template 77055e4f9d5SDimitry Andric // parameter packs that are pack expansions. 7710b57cec5SDimitry Andric }; 7720b57cec5SDimitry Andric 77355e4f9d5SDimitry Andric auto Collect = [&](TemplateArgument Pattern) { 7740b57cec5SDimitry Andric SmallVector<UnexpandedParameterPack, 2> Unexpanded; 7750b57cec5SDimitry Andric S.collectUnexpandedParameterPacks(Pattern, Unexpanded); 7760b57cec5SDimitry Andric for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { 7771ac55f4cSDimitry Andric unsigned Depth, Index; 7781ac55f4cSDimitry Andric std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); 7790b57cec5SDimitry Andric if (Depth == Info.getDeducedDepth()) 7800b57cec5SDimitry Andric AddPack(Index); 7810b57cec5SDimitry Andric } 78255e4f9d5SDimitry Andric }; 78355e4f9d5SDimitry Andric 78455e4f9d5SDimitry Andric // Look for unexpanded packs in the pattern. 78555e4f9d5SDimitry Andric Collect(Pattern); 7860b57cec5SDimitry Andric assert(!Packs.empty() && "Pack expansion without unexpanded packs?"); 7870b57cec5SDimitry Andric 7880b57cec5SDimitry Andric unsigned NumNamedPacks = Packs.size(); 7890b57cec5SDimitry Andric 79055e4f9d5SDimitry Andric // Also look for unexpanded packs that are indirectly deduced by deducing 79155e4f9d5SDimitry Andric // the sizes of the packs in this pattern. 79255e4f9d5SDimitry Andric while (!ExtraDeductions.empty()) 79355e4f9d5SDimitry Andric Collect(ExtraDeductions.pop_back_val()); 7940b57cec5SDimitry Andric 7950b57cec5SDimitry Andric return NumNamedPacks; 7960b57cec5SDimitry Andric } 7970b57cec5SDimitry Andric 7980b57cec5SDimitry Andric void finishConstruction(unsigned NumNamedPacks) { 7990b57cec5SDimitry Andric // Dig out the partially-substituted pack, if there is one. 8000b57cec5SDimitry Andric const TemplateArgument *PartialPackArgs = nullptr; 8010b57cec5SDimitry Andric unsigned NumPartialPackArgs = 0; 8020b57cec5SDimitry Andric std::pair<unsigned, unsigned> PartialPackDepthIndex(-1u, -1u); 8030b57cec5SDimitry Andric if (auto *Scope = S.CurrentInstantiationScope) 8040b57cec5SDimitry Andric if (auto *Partial = Scope->getPartiallySubstitutedPack( 8050b57cec5SDimitry Andric &PartialPackArgs, &NumPartialPackArgs)) 8060b57cec5SDimitry Andric PartialPackDepthIndex = getDepthAndIndex(Partial); 8070b57cec5SDimitry Andric 8080b57cec5SDimitry Andric // This pack expansion will have been partially or fully expanded if 8090b57cec5SDimitry Andric // it only names explicitly-specified parameter packs (including the 8100b57cec5SDimitry Andric // partially-substituted one, if any). 8110b57cec5SDimitry Andric bool IsExpanded = true; 8120b57cec5SDimitry Andric for (unsigned I = 0; I != NumNamedPacks; ++I) { 8130b57cec5SDimitry Andric if (Packs[I].Index >= Info.getNumExplicitArgs()) { 8140b57cec5SDimitry Andric IsExpanded = false; 8150b57cec5SDimitry Andric IsPartiallyExpanded = false; 8160b57cec5SDimitry Andric break; 8170b57cec5SDimitry Andric } 8180b57cec5SDimitry Andric if (PartialPackDepthIndex == 8190b57cec5SDimitry Andric std::make_pair(Info.getDeducedDepth(), Packs[I].Index)) { 8200b57cec5SDimitry Andric IsPartiallyExpanded = true; 8210b57cec5SDimitry Andric } 8220b57cec5SDimitry Andric } 8230b57cec5SDimitry Andric 8240b57cec5SDimitry Andric // Skip over the pack elements that were expanded into separate arguments. 8250b57cec5SDimitry Andric // If we partially expanded, this is the number of partial arguments. 8260b57cec5SDimitry Andric if (IsPartiallyExpanded) 8270b57cec5SDimitry Andric PackElements += NumPartialPackArgs; 8280b57cec5SDimitry Andric else if (IsExpanded) 8290b57cec5SDimitry Andric PackElements += *FixedNumExpansions; 8300b57cec5SDimitry Andric 8310b57cec5SDimitry Andric for (auto &Pack : Packs) { 8320b57cec5SDimitry Andric if (Info.PendingDeducedPacks.size() > Pack.Index) 8330b57cec5SDimitry Andric Pack.Outer = Info.PendingDeducedPacks[Pack.Index]; 8340b57cec5SDimitry Andric else 8350b57cec5SDimitry Andric Info.PendingDeducedPacks.resize(Pack.Index + 1); 8360b57cec5SDimitry Andric Info.PendingDeducedPacks[Pack.Index] = &Pack; 8370b57cec5SDimitry Andric 8380b57cec5SDimitry Andric if (PartialPackDepthIndex == 8390b57cec5SDimitry Andric std::make_pair(Info.getDeducedDepth(), Pack.Index)) { 8400b57cec5SDimitry Andric Pack.New.append(PartialPackArgs, PartialPackArgs + NumPartialPackArgs); 8410b57cec5SDimitry Andric // We pre-populate the deduced value of the partially-substituted 8420b57cec5SDimitry Andric // pack with the specified value. This is not entirely correct: the 8430b57cec5SDimitry Andric // value is supposed to have been substituted, not deduced, but the 8440b57cec5SDimitry Andric // cases where this is observable require an exact type match anyway. 8450b57cec5SDimitry Andric // 8460b57cec5SDimitry Andric // FIXME: If we could represent a "depth i, index j, pack elem k" 8470b57cec5SDimitry Andric // parameter, we could substitute the partially-substituted pack 8480b57cec5SDimitry Andric // everywhere and avoid this. 8490b57cec5SDimitry Andric if (!IsPartiallyExpanded) 8500b57cec5SDimitry Andric Deduced[Pack.Index] = Pack.New[PackElements]; 8510b57cec5SDimitry Andric } 8520b57cec5SDimitry Andric } 8530b57cec5SDimitry Andric } 8540b57cec5SDimitry Andric 8550b57cec5SDimitry Andric public: 8560b57cec5SDimitry Andric ~PackDeductionScope() { 8570b57cec5SDimitry Andric for (auto &Pack : Packs) 8580b57cec5SDimitry Andric Info.PendingDeducedPacks[Pack.Index] = Pack.Outer; 8590b57cec5SDimitry Andric } 8600b57cec5SDimitry Andric 8610b57cec5SDimitry Andric /// Determine whether this pack has already been partially expanded into a 8620b57cec5SDimitry Andric /// sequence of (prior) function parameters / template arguments. 8630b57cec5SDimitry Andric bool isPartiallyExpanded() { return IsPartiallyExpanded; } 8640b57cec5SDimitry Andric 8650b57cec5SDimitry Andric /// Determine whether this pack expansion scope has a known, fixed arity. 8660b57cec5SDimitry Andric /// This happens if it involves a pack from an outer template that has 8670b57cec5SDimitry Andric /// (notionally) already been expanded. 86881ad6265SDimitry Andric bool hasFixedArity() { return FixedNumExpansions.has_value(); } 8690b57cec5SDimitry Andric 8700b57cec5SDimitry Andric /// Determine whether the next element of the argument is still part of this 8710b57cec5SDimitry Andric /// pack. This is the case unless the pack is already expanded to a fixed 8720b57cec5SDimitry Andric /// length. 8730b57cec5SDimitry Andric bool hasNextElement() { 8740b57cec5SDimitry Andric return !FixedNumExpansions || *FixedNumExpansions > PackElements; 8750b57cec5SDimitry Andric } 8760b57cec5SDimitry Andric 8770b57cec5SDimitry Andric /// Move to deducing the next element in each pack that is being deduced. 8780b57cec5SDimitry Andric void nextPackElement() { 8790b57cec5SDimitry Andric // Capture the deduced template arguments for each parameter pack expanded 8800b57cec5SDimitry Andric // by this pack expansion, add them to the list of arguments we've deduced 8810b57cec5SDimitry Andric // for that pack, then clear out the deduced argument. 8820b57cec5SDimitry Andric for (auto &Pack : Packs) { 8830b57cec5SDimitry Andric DeducedTemplateArgument &DeducedArg = Deduced[Pack.Index]; 8840b57cec5SDimitry Andric if (!Pack.New.empty() || !DeducedArg.isNull()) { 8850b57cec5SDimitry Andric while (Pack.New.size() < PackElements) 8860b57cec5SDimitry Andric Pack.New.push_back(DeducedTemplateArgument()); 8870b57cec5SDimitry Andric if (Pack.New.size() == PackElements) 8880b57cec5SDimitry Andric Pack.New.push_back(DeducedArg); 8890b57cec5SDimitry Andric else 8900b57cec5SDimitry Andric Pack.New[PackElements] = DeducedArg; 8910b57cec5SDimitry Andric DeducedArg = Pack.New.size() > PackElements + 1 8920b57cec5SDimitry Andric ? Pack.New[PackElements + 1] 8930b57cec5SDimitry Andric : DeducedTemplateArgument(); 8940b57cec5SDimitry Andric } 8950b57cec5SDimitry Andric } 8960b57cec5SDimitry Andric ++PackElements; 8970b57cec5SDimitry Andric } 8980b57cec5SDimitry Andric 8990b57cec5SDimitry Andric /// Finish template argument deduction for a set of argument packs, 9000b57cec5SDimitry Andric /// producing the argument packs and checking for consistency with prior 9010b57cec5SDimitry Andric /// deductions. 902480093f4SDimitry Andric Sema::TemplateDeductionResult finish() { 9030b57cec5SDimitry Andric // Build argument packs for each of the parameter packs expanded by this 9040b57cec5SDimitry Andric // pack expansion. 9050b57cec5SDimitry Andric for (auto &Pack : Packs) { 9060b57cec5SDimitry Andric // Put back the old value for this pack. 9070b57cec5SDimitry Andric Deduced[Pack.Index] = Pack.Saved; 9080b57cec5SDimitry Andric 909480093f4SDimitry Andric // Always make sure the size of this pack is correct, even if we didn't 910480093f4SDimitry Andric // deduce any values for it. 911480093f4SDimitry Andric // 912480093f4SDimitry Andric // FIXME: This isn't required by the normative wording, but substitution 913480093f4SDimitry Andric // and post-substitution checking will always fail if the arity of any 914480093f4SDimitry Andric // pack is not equal to the number of elements we processed. (Either that 915480093f4SDimitry Andric // or something else has gone *very* wrong.) We're permitted to skip any 916480093f4SDimitry Andric // hard errors from those follow-on steps by the intent (but not the 917480093f4SDimitry Andric // wording) of C++ [temp.inst]p8: 918480093f4SDimitry Andric // 919480093f4SDimitry Andric // If the function selected by overload resolution can be determined 920480093f4SDimitry Andric // without instantiating a class template definition, it is unspecified 921480093f4SDimitry Andric // whether that instantiation actually takes place 9220b57cec5SDimitry Andric Pack.New.resize(PackElements); 9230b57cec5SDimitry Andric 9240b57cec5SDimitry Andric // Build or find a new value for this pack. 9250b57cec5SDimitry Andric DeducedTemplateArgument NewPack; 926480093f4SDimitry Andric if (Pack.New.empty()) { 9270b57cec5SDimitry Andric // If we deduced an empty argument pack, create it now. 9280b57cec5SDimitry Andric NewPack = DeducedTemplateArgument(TemplateArgument::getEmptyPack()); 9290b57cec5SDimitry Andric } else { 9300b57cec5SDimitry Andric TemplateArgument *ArgumentPack = 9310b57cec5SDimitry Andric new (S.Context) TemplateArgument[Pack.New.size()]; 9320b57cec5SDimitry Andric std::copy(Pack.New.begin(), Pack.New.end(), ArgumentPack); 9330b57cec5SDimitry Andric NewPack = DeducedTemplateArgument( 934bdd1243dSDimitry Andric TemplateArgument(llvm::ArrayRef(ArgumentPack, Pack.New.size())), 9350b57cec5SDimitry Andric // FIXME: This is wrong, it's possible that some pack elements are 9360b57cec5SDimitry Andric // deduced from an array bound and others are not: 9370b57cec5SDimitry Andric // template<typename ...T, T ...V> void g(const T (&...p)[V]); 9380b57cec5SDimitry Andric // g({1, 2, 3}, {{}, {}}); 9390b57cec5SDimitry Andric // ... should deduce T = {int, size_t (from array bound)}. 9400b57cec5SDimitry Andric Pack.New[0].wasDeducedFromArrayBound()); 9410b57cec5SDimitry Andric } 9420b57cec5SDimitry Andric 9430b57cec5SDimitry Andric // Pick where we're going to put the merged pack. 9440b57cec5SDimitry Andric DeducedTemplateArgument *Loc; 9450b57cec5SDimitry Andric if (Pack.Outer) { 9460b57cec5SDimitry Andric if (Pack.Outer->DeferredDeduction.isNull()) { 9470b57cec5SDimitry Andric // Defer checking this pack until we have a complete pack to compare 9480b57cec5SDimitry Andric // it against. 9490b57cec5SDimitry Andric Pack.Outer->DeferredDeduction = NewPack; 9500b57cec5SDimitry Andric continue; 9510b57cec5SDimitry Andric } 9520b57cec5SDimitry Andric Loc = &Pack.Outer->DeferredDeduction; 9530b57cec5SDimitry Andric } else { 9540b57cec5SDimitry Andric Loc = &Deduced[Pack.Index]; 9550b57cec5SDimitry Andric } 9560b57cec5SDimitry Andric 9570b57cec5SDimitry Andric // Check the new pack matches any previous value. 9580b57cec5SDimitry Andric DeducedTemplateArgument OldPack = *Loc; 95906c3fb27SDimitry Andric DeducedTemplateArgument Result = checkDeducedTemplateArguments( 96006c3fb27SDimitry Andric S.Context, OldPack, NewPack, DeducePackIfNotAlreadyDeduced); 96106c3fb27SDimitry Andric 96206c3fb27SDimitry Andric Info.AggregateDeductionCandidateHasMismatchedArity = 96306c3fb27SDimitry Andric OldPack.getKind() == TemplateArgument::Pack && 96406c3fb27SDimitry Andric NewPack.getKind() == TemplateArgument::Pack && 96506c3fb27SDimitry Andric OldPack.pack_size() != NewPack.pack_size() && !Result.isNull(); 9660b57cec5SDimitry Andric 9670b57cec5SDimitry Andric // If we deferred a deduction of this pack, check that one now too. 9680b57cec5SDimitry Andric if (!Result.isNull() && !Pack.DeferredDeduction.isNull()) { 9690b57cec5SDimitry Andric OldPack = Result; 9700b57cec5SDimitry Andric NewPack = Pack.DeferredDeduction; 9710b57cec5SDimitry Andric Result = checkDeducedTemplateArguments(S.Context, OldPack, NewPack); 9720b57cec5SDimitry Andric } 9730b57cec5SDimitry Andric 9740b57cec5SDimitry Andric NamedDecl *Param = TemplateParams->getParam(Pack.Index); 9750b57cec5SDimitry Andric if (Result.isNull()) { 9760b57cec5SDimitry Andric Info.Param = makeTemplateParameter(Param); 9770b57cec5SDimitry Andric Info.FirstArg = OldPack; 9780b57cec5SDimitry Andric Info.SecondArg = NewPack; 9790b57cec5SDimitry Andric return Sema::TDK_Inconsistent; 9800b57cec5SDimitry Andric } 9810b57cec5SDimitry Andric 9820b57cec5SDimitry Andric // If we have a pre-expanded pack and we didn't deduce enough elements 9830b57cec5SDimitry Andric // for it, fail deduction. 984bdd1243dSDimitry Andric if (std::optional<unsigned> Expansions = getExpandedPackSize(Param)) { 9850b57cec5SDimitry Andric if (*Expansions != PackElements) { 9860b57cec5SDimitry Andric Info.Param = makeTemplateParameter(Param); 9870b57cec5SDimitry Andric Info.FirstArg = Result; 9880b57cec5SDimitry Andric return Sema::TDK_IncompletePack; 9890b57cec5SDimitry Andric } 9900b57cec5SDimitry Andric } 9910b57cec5SDimitry Andric 9920b57cec5SDimitry Andric *Loc = Result; 9930b57cec5SDimitry Andric } 9940b57cec5SDimitry Andric 9950b57cec5SDimitry Andric return Sema::TDK_Success; 9960b57cec5SDimitry Andric } 9970b57cec5SDimitry Andric 9980b57cec5SDimitry Andric private: 9990b57cec5SDimitry Andric Sema &S; 10000b57cec5SDimitry Andric TemplateParameterList *TemplateParams; 10010b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced; 10020b57cec5SDimitry Andric TemplateDeductionInfo &Info; 10030b57cec5SDimitry Andric unsigned PackElements = 0; 10040b57cec5SDimitry Andric bool IsPartiallyExpanded = false; 100506c3fb27SDimitry Andric bool DeducePackIfNotAlreadyDeduced = false; 10060b57cec5SDimitry Andric /// The number of expansions, if we have a fully-expanded pack in this scope. 1007bdd1243dSDimitry Andric std::optional<unsigned> FixedNumExpansions; 10080b57cec5SDimitry Andric 10090b57cec5SDimitry Andric SmallVector<DeducedPack, 2> Packs; 10100b57cec5SDimitry Andric }; 10110b57cec5SDimitry Andric 10120b57cec5SDimitry Andric } // namespace 10130b57cec5SDimitry Andric 10140b57cec5SDimitry Andric /// Deduce the template arguments by comparing the list of parameter 10150b57cec5SDimitry Andric /// types to the list of argument types, as in the parameter-type-lists of 10160b57cec5SDimitry Andric /// function types (C++ [temp.deduct.type]p10). 10170b57cec5SDimitry Andric /// 10180b57cec5SDimitry Andric /// \param S The semantic analysis object within which we are deducing 10190b57cec5SDimitry Andric /// 10200b57cec5SDimitry Andric /// \param TemplateParams The template parameters that we are deducing 10210b57cec5SDimitry Andric /// 10220b57cec5SDimitry Andric /// \param Params The list of parameter types 10230b57cec5SDimitry Andric /// 10240b57cec5SDimitry Andric /// \param NumParams The number of types in \c Params 10250b57cec5SDimitry Andric /// 10260b57cec5SDimitry Andric /// \param Args The list of argument types 10270b57cec5SDimitry Andric /// 10280b57cec5SDimitry Andric /// \param NumArgs The number of types in \c Args 10290b57cec5SDimitry Andric /// 10300b57cec5SDimitry Andric /// \param Info information about the template argument deduction itself 10310b57cec5SDimitry Andric /// 10320b57cec5SDimitry Andric /// \param Deduced the deduced template arguments 10330b57cec5SDimitry Andric /// 10340b57cec5SDimitry Andric /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe 10350b57cec5SDimitry Andric /// how template argument deduction is performed. 10360b57cec5SDimitry Andric /// 10370b57cec5SDimitry Andric /// \param PartialOrdering If true, we are performing template argument 10380b57cec5SDimitry Andric /// deduction for during partial ordering for a call 10390b57cec5SDimitry Andric /// (C++0x [temp.deduct.partial]). 10400b57cec5SDimitry Andric /// 10410b57cec5SDimitry Andric /// \returns the result of template argument deduction so far. Note that a 10420b57cec5SDimitry Andric /// "success" result means that template argument deduction has not yet failed, 10430b57cec5SDimitry Andric /// but it may still fail, later, for other reasons. 10440b57cec5SDimitry Andric static Sema::TemplateDeductionResult 10450b57cec5SDimitry Andric DeduceTemplateArguments(Sema &S, 10460b57cec5SDimitry Andric TemplateParameterList *TemplateParams, 10470b57cec5SDimitry Andric const QualType *Params, unsigned NumParams, 10480b57cec5SDimitry Andric const QualType *Args, unsigned NumArgs, 10490b57cec5SDimitry Andric TemplateDeductionInfo &Info, 10500b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 10510b57cec5SDimitry Andric unsigned TDF, 10520b57cec5SDimitry Andric bool PartialOrdering = false) { 10530b57cec5SDimitry Andric // C++0x [temp.deduct.type]p10: 10540b57cec5SDimitry Andric // Similarly, if P has a form that contains (T), then each parameter type 10550b57cec5SDimitry Andric // Pi of the respective parameter-type- list of P is compared with the 10560b57cec5SDimitry Andric // corresponding parameter type Ai of the corresponding parameter-type-list 10570b57cec5SDimitry Andric // of A. [...] 10580b57cec5SDimitry Andric unsigned ArgIdx = 0, ParamIdx = 0; 10590b57cec5SDimitry Andric for (; ParamIdx != NumParams; ++ParamIdx) { 10600b57cec5SDimitry Andric // Check argument types. 10610b57cec5SDimitry Andric const PackExpansionType *Expansion 10620b57cec5SDimitry Andric = dyn_cast<PackExpansionType>(Params[ParamIdx]); 10630b57cec5SDimitry Andric if (!Expansion) { 10640b57cec5SDimitry Andric // Simple case: compare the parameter and argument types at this point. 10650b57cec5SDimitry Andric 10660b57cec5SDimitry Andric // Make sure we have an argument. 10670b57cec5SDimitry Andric if (ArgIdx >= NumArgs) 10680b57cec5SDimitry Andric return Sema::TDK_MiscellaneousDeductionFailure; 10690b57cec5SDimitry Andric 10700b57cec5SDimitry Andric if (isa<PackExpansionType>(Args[ArgIdx])) { 10710b57cec5SDimitry Andric // C++0x [temp.deduct.type]p22: 10720b57cec5SDimitry Andric // If the original function parameter associated with A is a function 10730b57cec5SDimitry Andric // parameter pack and the function parameter associated with P is not 10740b57cec5SDimitry Andric // a function parameter pack, then template argument deduction fails. 10750b57cec5SDimitry Andric return Sema::TDK_MiscellaneousDeductionFailure; 10760b57cec5SDimitry Andric } 10770b57cec5SDimitry Andric 1078349cc55cSDimitry Andric if (Sema::TemplateDeductionResult Result = 1079349cc55cSDimitry Andric DeduceTemplateArgumentsByTypeMatch( 1080349cc55cSDimitry Andric S, TemplateParams, Params[ParamIdx].getUnqualifiedType(), 1081349cc55cSDimitry Andric Args[ArgIdx].getUnqualifiedType(), Info, Deduced, TDF, 1082349cc55cSDimitry Andric PartialOrdering, 1083349cc55cSDimitry Andric /*DeducedFromArrayBound=*/false)) 10840b57cec5SDimitry Andric return Result; 10850b57cec5SDimitry Andric 10860b57cec5SDimitry Andric ++ArgIdx; 10870b57cec5SDimitry Andric continue; 10880b57cec5SDimitry Andric } 10890b57cec5SDimitry Andric 10900b57cec5SDimitry Andric // C++0x [temp.deduct.type]p10: 10910b57cec5SDimitry Andric // If the parameter-declaration corresponding to Pi is a function 10920b57cec5SDimitry Andric // parameter pack, then the type of its declarator- id is compared with 10930b57cec5SDimitry Andric // each remaining parameter type in the parameter-type-list of A. Each 10940b57cec5SDimitry Andric // comparison deduces template arguments for subsequent positions in the 10950b57cec5SDimitry Andric // template parameter packs expanded by the function parameter pack. 10960b57cec5SDimitry Andric 10970b57cec5SDimitry Andric QualType Pattern = Expansion->getPattern(); 10980b57cec5SDimitry Andric PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); 10990b57cec5SDimitry Andric 11000b57cec5SDimitry Andric // A pack scope with fixed arity is not really a pack any more, so is not 11010b57cec5SDimitry Andric // a non-deduced context. 11020b57cec5SDimitry Andric if (ParamIdx + 1 == NumParams || PackScope.hasFixedArity()) { 11030b57cec5SDimitry Andric for (; ArgIdx < NumArgs && PackScope.hasNextElement(); ++ArgIdx) { 11040b57cec5SDimitry Andric // Deduce template arguments from the pattern. 1105349cc55cSDimitry Andric if (Sema::TemplateDeductionResult Result = 1106349cc55cSDimitry Andric DeduceTemplateArgumentsByTypeMatch( 1107349cc55cSDimitry Andric S, TemplateParams, Pattern.getUnqualifiedType(), 1108349cc55cSDimitry Andric Args[ArgIdx].getUnqualifiedType(), Info, Deduced, TDF, 1109349cc55cSDimitry Andric PartialOrdering, /*DeducedFromArrayBound=*/false)) 11100b57cec5SDimitry Andric return Result; 11110b57cec5SDimitry Andric 11120b57cec5SDimitry Andric PackScope.nextPackElement(); 11130b57cec5SDimitry Andric } 11140b57cec5SDimitry Andric } else { 11150b57cec5SDimitry Andric // C++0x [temp.deduct.type]p5: 11160b57cec5SDimitry Andric // The non-deduced contexts are: 11170b57cec5SDimitry Andric // - A function parameter pack that does not occur at the end of the 11180b57cec5SDimitry Andric // parameter-declaration-clause. 11190b57cec5SDimitry Andric // 11200b57cec5SDimitry Andric // FIXME: There is no wording to say what we should do in this case. We 11210b57cec5SDimitry Andric // choose to resolve this by applying the same rule that is applied for a 11220b57cec5SDimitry Andric // function call: that is, deduce all contained packs to their 11230b57cec5SDimitry Andric // explicitly-specified values (or to <> if there is no such value). 11240b57cec5SDimitry Andric // 11250b57cec5SDimitry Andric // This is seemingly-arbitrarily different from the case of a template-id 11260b57cec5SDimitry Andric // with a non-trailing pack-expansion in its arguments, which renders the 11270b57cec5SDimitry Andric // entire template-argument-list a non-deduced context. 11280b57cec5SDimitry Andric 11290b57cec5SDimitry Andric // If the parameter type contains an explicitly-specified pack that we 11300b57cec5SDimitry Andric // could not expand, skip the number of parameters notionally created 11310b57cec5SDimitry Andric // by the expansion. 1132bdd1243dSDimitry Andric std::optional<unsigned> NumExpansions = Expansion->getNumExpansions(); 11330b57cec5SDimitry Andric if (NumExpansions && !PackScope.isPartiallyExpanded()) { 11340b57cec5SDimitry Andric for (unsigned I = 0; I != *NumExpansions && ArgIdx < NumArgs; 11350b57cec5SDimitry Andric ++I, ++ArgIdx) 11360b57cec5SDimitry Andric PackScope.nextPackElement(); 11370b57cec5SDimitry Andric } 11380b57cec5SDimitry Andric } 11390b57cec5SDimitry Andric 11400b57cec5SDimitry Andric // Build argument packs for each of the parameter packs expanded by this 11410b57cec5SDimitry Andric // pack expansion. 11420b57cec5SDimitry Andric if (auto Result = PackScope.finish()) 11430b57cec5SDimitry Andric return Result; 11440b57cec5SDimitry Andric } 11450b57cec5SDimitry Andric 1146bdd1243dSDimitry Andric // DR692, DR1395 1147bdd1243dSDimitry Andric // C++0x [temp.deduct.type]p10: 1148bdd1243dSDimitry Andric // If the parameter-declaration corresponding to P_i ... 1149bdd1243dSDimitry Andric // During partial ordering, if Ai was originally a function parameter pack: 1150bdd1243dSDimitry Andric // - if P does not contain a function parameter type corresponding to Ai then 1151bdd1243dSDimitry Andric // Ai is ignored; 1152bdd1243dSDimitry Andric if (PartialOrdering && ArgIdx + 1 == NumArgs && 1153bdd1243dSDimitry Andric isa<PackExpansionType>(Args[ArgIdx])) 1154bdd1243dSDimitry Andric return Sema::TDK_Success; 1155bdd1243dSDimitry Andric 11560b57cec5SDimitry Andric // Make sure we don't have any extra arguments. 11570b57cec5SDimitry Andric if (ArgIdx < NumArgs) 11580b57cec5SDimitry Andric return Sema::TDK_MiscellaneousDeductionFailure; 11590b57cec5SDimitry Andric 11600b57cec5SDimitry Andric return Sema::TDK_Success; 11610b57cec5SDimitry Andric } 11620b57cec5SDimitry Andric 11630b57cec5SDimitry Andric /// Determine whether the parameter has qualifiers that the argument 11640b57cec5SDimitry Andric /// lacks. Put another way, determine whether there is no way to add 11650b57cec5SDimitry Andric /// a deduced set of qualifiers to the ParamType that would result in 11660b57cec5SDimitry Andric /// its qualifiers matching those of the ArgType. 11670b57cec5SDimitry Andric static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType, 11680b57cec5SDimitry Andric QualType ArgType) { 11690b57cec5SDimitry Andric Qualifiers ParamQs = ParamType.getQualifiers(); 11700b57cec5SDimitry Andric Qualifiers ArgQs = ArgType.getQualifiers(); 11710b57cec5SDimitry Andric 11720b57cec5SDimitry Andric if (ParamQs == ArgQs) 11730b57cec5SDimitry Andric return false; 11740b57cec5SDimitry Andric 11750b57cec5SDimitry Andric // Mismatched (but not missing) Objective-C GC attributes. 11760b57cec5SDimitry Andric if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() && 11770b57cec5SDimitry Andric ParamQs.hasObjCGCAttr()) 11780b57cec5SDimitry Andric return true; 11790b57cec5SDimitry Andric 11800b57cec5SDimitry Andric // Mismatched (but not missing) address spaces. 11810b57cec5SDimitry Andric if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() && 11820b57cec5SDimitry Andric ParamQs.hasAddressSpace()) 11830b57cec5SDimitry Andric return true; 11840b57cec5SDimitry Andric 11850b57cec5SDimitry Andric // Mismatched (but not missing) Objective-C lifetime qualifiers. 11860b57cec5SDimitry Andric if (ParamQs.getObjCLifetime() != ArgQs.getObjCLifetime() && 11870b57cec5SDimitry Andric ParamQs.hasObjCLifetime()) 11880b57cec5SDimitry Andric return true; 11890b57cec5SDimitry Andric 11900b57cec5SDimitry Andric // CVR qualifiers inconsistent or a superset. 11910b57cec5SDimitry Andric return (ParamQs.getCVRQualifiers() & ~ArgQs.getCVRQualifiers()) != 0; 11920b57cec5SDimitry Andric } 11930b57cec5SDimitry Andric 11940b57cec5SDimitry Andric /// Compare types for equality with respect to possibly compatible 11950b57cec5SDimitry Andric /// function types (noreturn adjustment, implicit calling conventions). If any 11960b57cec5SDimitry Andric /// of parameter and argument is not a function, just perform type comparison. 11970b57cec5SDimitry Andric /// 1198349cc55cSDimitry Andric /// \param P the template parameter type. 11990b57cec5SDimitry Andric /// 1200349cc55cSDimitry Andric /// \param A the argument type. 1201349cc55cSDimitry Andric bool Sema::isSameOrCompatibleFunctionType(QualType P, QualType A) { 1202349cc55cSDimitry Andric const FunctionType *PF = P->getAs<FunctionType>(), 1203349cc55cSDimitry Andric *AF = A->getAs<FunctionType>(); 12040b57cec5SDimitry Andric 12050b57cec5SDimitry Andric // Just compare if not functions. 1206349cc55cSDimitry Andric if (!PF || !AF) 1207349cc55cSDimitry Andric return Context.hasSameType(P, A); 12080b57cec5SDimitry Andric 12090b57cec5SDimitry Andric // Noreturn and noexcept adjustment. 12100b57cec5SDimitry Andric QualType AdjustedParam; 1211349cc55cSDimitry Andric if (IsFunctionConversion(P, A, AdjustedParam)) 1212349cc55cSDimitry Andric return Context.hasSameType(AdjustedParam, A); 12130b57cec5SDimitry Andric 12140b57cec5SDimitry Andric // FIXME: Compatible calling conventions. 12150b57cec5SDimitry Andric 1216349cc55cSDimitry Andric return Context.hasSameType(P, A); 12170b57cec5SDimitry Andric } 12180b57cec5SDimitry Andric 12190b57cec5SDimitry Andric /// Get the index of the first template parameter that was originally from the 12200b57cec5SDimitry Andric /// innermost template-parameter-list. This is 0 except when we concatenate 12210b57cec5SDimitry Andric /// the template parameter lists of a class template and a constructor template 12220b57cec5SDimitry Andric /// when forming an implicit deduction guide. 12230b57cec5SDimitry Andric static unsigned getFirstInnerIndex(FunctionTemplateDecl *FTD) { 12240b57cec5SDimitry Andric auto *Guide = dyn_cast<CXXDeductionGuideDecl>(FTD->getTemplatedDecl()); 12250b57cec5SDimitry Andric if (!Guide || !Guide->isImplicit()) 12260b57cec5SDimitry Andric return 0; 12270b57cec5SDimitry Andric return Guide->getDeducedTemplate()->getTemplateParameters()->size(); 12280b57cec5SDimitry Andric } 12290b57cec5SDimitry Andric 12300b57cec5SDimitry Andric /// Determine whether a type denotes a forwarding reference. 12310b57cec5SDimitry Andric static bool isForwardingReference(QualType Param, unsigned FirstInnerIndex) { 12320b57cec5SDimitry Andric // C++1z [temp.deduct.call]p3: 12330b57cec5SDimitry Andric // A forwarding reference is an rvalue reference to a cv-unqualified 12340b57cec5SDimitry Andric // template parameter that does not represent a template parameter of a 12350b57cec5SDimitry Andric // class template. 12360b57cec5SDimitry Andric if (auto *ParamRef = Param->getAs<RValueReferenceType>()) { 12370b57cec5SDimitry Andric if (ParamRef->getPointeeType().getQualifiers()) 12380b57cec5SDimitry Andric return false; 12390b57cec5SDimitry Andric auto *TypeParm = ParamRef->getPointeeType()->getAs<TemplateTypeParmType>(); 12400b57cec5SDimitry Andric return TypeParm && TypeParm->getIndex() >= FirstInnerIndex; 12410b57cec5SDimitry Andric } 12420b57cec5SDimitry Andric return false; 12430b57cec5SDimitry Andric } 12440b57cec5SDimitry Andric 1245349cc55cSDimitry Andric static CXXRecordDecl *getCanonicalRD(QualType T) { 1246349cc55cSDimitry Andric return cast<CXXRecordDecl>( 1247349cc55cSDimitry Andric T->castAs<RecordType>()->getDecl()->getCanonicalDecl()); 1248349cc55cSDimitry Andric } 1249349cc55cSDimitry Andric 1250e8d8bef9SDimitry Andric /// Attempt to deduce the template arguments by checking the base types 1251e8d8bef9SDimitry Andric /// according to (C++20 [temp.deduct.call] p4b3. 1252e8d8bef9SDimitry Andric /// 1253e8d8bef9SDimitry Andric /// \param S the semantic analysis object within which we are deducing. 1254e8d8bef9SDimitry Andric /// 125581ad6265SDimitry Andric /// \param RD the top level record object we are deducing against. 1256e8d8bef9SDimitry Andric /// 1257e8d8bef9SDimitry Andric /// \param TemplateParams the template parameters that we are deducing. 1258e8d8bef9SDimitry Andric /// 125981ad6265SDimitry Andric /// \param P the template specialization parameter type. 1260e8d8bef9SDimitry Andric /// 1261e8d8bef9SDimitry Andric /// \param Info information about the template argument deduction itself. 1262e8d8bef9SDimitry Andric /// 1263e8d8bef9SDimitry Andric /// \param Deduced the deduced template arguments. 1264e8d8bef9SDimitry Andric /// 1265e8d8bef9SDimitry Andric /// \returns the result of template argument deduction with the bases. "invalid" 1266e8d8bef9SDimitry Andric /// means no matches, "success" found a single item, and the 1267e8d8bef9SDimitry Andric /// "MiscellaneousDeductionFailure" result happens when the match is ambiguous. 1268349cc55cSDimitry Andric static Sema::TemplateDeductionResult 1269349cc55cSDimitry Andric DeduceTemplateBases(Sema &S, const CXXRecordDecl *RD, 1270349cc55cSDimitry Andric TemplateParameterList *TemplateParams, QualType P, 1271349cc55cSDimitry Andric TemplateDeductionInfo &Info, 1272e8d8bef9SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 1273e8d8bef9SDimitry Andric // C++14 [temp.deduct.call] p4b3: 1274e8d8bef9SDimitry Andric // If P is a class and P has the form simple-template-id, then the 1275e8d8bef9SDimitry Andric // transformed A can be a derived class of the deduced A. Likewise if 1276e8d8bef9SDimitry Andric // P is a pointer to a class of the form simple-template-id, the 1277e8d8bef9SDimitry Andric // transformed A can be a pointer to a derived class pointed to by the 1278e8d8bef9SDimitry Andric // deduced A. However, if there is a class C that is a (direct or 1279e8d8bef9SDimitry Andric // indirect) base class of D and derived (directly or indirectly) from a 1280e8d8bef9SDimitry Andric // class B and that would be a valid deduced A, the deduced A cannot be 1281e8d8bef9SDimitry Andric // B or pointer to B, respectively. 1282e8d8bef9SDimitry Andric // 1283e8d8bef9SDimitry Andric // These alternatives are considered only if type deduction would 1284e8d8bef9SDimitry Andric // otherwise fail. If they yield more than one possible deduced A, the 1285e8d8bef9SDimitry Andric // type deduction fails. 1286e8d8bef9SDimitry Andric 1287e8d8bef9SDimitry Andric // Use a breadth-first search through the bases to collect the set of 1288e8d8bef9SDimitry Andric // successful matches. Visited contains the set of nodes we have already 1289e8d8bef9SDimitry Andric // visited, while ToVisit is our stack of records that we still need to 1290e8d8bef9SDimitry Andric // visit. Matches contains a list of matches that have yet to be 1291e8d8bef9SDimitry Andric // disqualified. 1292349cc55cSDimitry Andric llvm::SmallPtrSet<const CXXRecordDecl *, 8> Visited; 1293349cc55cSDimitry Andric SmallVector<QualType, 8> ToVisit; 1294e8d8bef9SDimitry Andric // We iterate over this later, so we have to use MapVector to ensure 1295e8d8bef9SDimitry Andric // determinism. 1296349cc55cSDimitry Andric llvm::MapVector<const CXXRecordDecl *, 1297349cc55cSDimitry Andric SmallVector<DeducedTemplateArgument, 8>> 1298e8d8bef9SDimitry Andric Matches; 1299e8d8bef9SDimitry Andric 1300349cc55cSDimitry Andric auto AddBases = [&Visited, &ToVisit](const CXXRecordDecl *RD) { 1301e8d8bef9SDimitry Andric for (const auto &Base : RD->bases()) { 1302349cc55cSDimitry Andric QualType T = Base.getType(); 1303349cc55cSDimitry Andric assert(T->isRecordType() && "Base class that isn't a record?"); 1304349cc55cSDimitry Andric if (Visited.insert(::getCanonicalRD(T)).second) 1305349cc55cSDimitry Andric ToVisit.push_back(T); 1306e8d8bef9SDimitry Andric } 1307e8d8bef9SDimitry Andric }; 1308e8d8bef9SDimitry Andric 1309e8d8bef9SDimitry Andric // Set up the loop by adding all the bases. 1310349cc55cSDimitry Andric AddBases(RD); 1311e8d8bef9SDimitry Andric 1312e8d8bef9SDimitry Andric // Search each path of bases until we either run into a successful match 1313e8d8bef9SDimitry Andric // (where all bases of it are invalid), or we run out of bases. 1314e8d8bef9SDimitry Andric while (!ToVisit.empty()) { 1315349cc55cSDimitry Andric QualType NextT = ToVisit.pop_back_val(); 1316e8d8bef9SDimitry Andric 1317e8d8bef9SDimitry Andric SmallVector<DeducedTemplateArgument, 8> DeducedCopy(Deduced.begin(), 1318e8d8bef9SDimitry Andric Deduced.end()); 1319e8d8bef9SDimitry Andric TemplateDeductionInfo BaseInfo(TemplateDeductionInfo::ForBase, Info); 1320349cc55cSDimitry Andric Sema::TemplateDeductionResult BaseResult = DeduceTemplateSpecArguments( 1321349cc55cSDimitry Andric S, TemplateParams, P, NextT, BaseInfo, DeducedCopy); 1322e8d8bef9SDimitry Andric 1323e8d8bef9SDimitry Andric // If this was a successful deduction, add it to the list of matches, 1324e8d8bef9SDimitry Andric // otherwise we need to continue searching its bases. 1325349cc55cSDimitry Andric const CXXRecordDecl *RD = ::getCanonicalRD(NextT); 1326e8d8bef9SDimitry Andric if (BaseResult == Sema::TDK_Success) 1327349cc55cSDimitry Andric Matches.insert({RD, DeducedCopy}); 1328e8d8bef9SDimitry Andric else 1329349cc55cSDimitry Andric AddBases(RD); 1330e8d8bef9SDimitry Andric } 1331e8d8bef9SDimitry Andric 1332e8d8bef9SDimitry Andric // At this point, 'Matches' contains a list of seemingly valid bases, however 1333e8d8bef9SDimitry Andric // in the event that we have more than 1 match, it is possible that the base 1334e8d8bef9SDimitry Andric // of one of the matches might be disqualified for being a base of another 1335e8d8bef9SDimitry Andric // valid match. We can count on cyclical instantiations being invalid to 1336e8d8bef9SDimitry Andric // simplify the disqualifications. That is, if A & B are both matches, and B 1337e8d8bef9SDimitry Andric // inherits from A (disqualifying A), we know that A cannot inherit from B. 1338e8d8bef9SDimitry Andric if (Matches.size() > 1) { 1339e8d8bef9SDimitry Andric Visited.clear(); 1340e8d8bef9SDimitry Andric for (const auto &Match : Matches) 1341e8d8bef9SDimitry Andric AddBases(Match.first); 1342e8d8bef9SDimitry Andric 1343e8d8bef9SDimitry Andric // We can give up once we have a single item (or have run out of things to 1344349cc55cSDimitry Andric // search) since cyclical inheritance isn't valid. 1345e8d8bef9SDimitry Andric while (Matches.size() > 1 && !ToVisit.empty()) { 1346349cc55cSDimitry Andric const CXXRecordDecl *RD = ::getCanonicalRD(ToVisit.pop_back_val()); 1347349cc55cSDimitry Andric Matches.erase(RD); 1348e8d8bef9SDimitry Andric 1349349cc55cSDimitry Andric // Always add all bases, since the inheritance tree can contain 1350e8d8bef9SDimitry Andric // disqualifications for multiple matches. 1351349cc55cSDimitry Andric AddBases(RD); 1352e8d8bef9SDimitry Andric } 1353e8d8bef9SDimitry Andric } 1354e8d8bef9SDimitry Andric 1355e8d8bef9SDimitry Andric if (Matches.empty()) 1356e8d8bef9SDimitry Andric return Sema::TDK_Invalid; 1357e8d8bef9SDimitry Andric if (Matches.size() > 1) 1358e8d8bef9SDimitry Andric return Sema::TDK_MiscellaneousDeductionFailure; 1359e8d8bef9SDimitry Andric 1360e8d8bef9SDimitry Andric std::swap(Matches.front().second, Deduced); 1361e8d8bef9SDimitry Andric return Sema::TDK_Success; 1362e8d8bef9SDimitry Andric } 1363e8d8bef9SDimitry Andric 13640b57cec5SDimitry Andric /// Deduce the template arguments by comparing the parameter type and 13650b57cec5SDimitry Andric /// the argument type (C++ [temp.deduct.type]). 13660b57cec5SDimitry Andric /// 13670b57cec5SDimitry Andric /// \param S the semantic analysis object within which we are deducing 13680b57cec5SDimitry Andric /// 13690b57cec5SDimitry Andric /// \param TemplateParams the template parameters that we are deducing 13700b57cec5SDimitry Andric /// 137181ad6265SDimitry Andric /// \param P the parameter type 13720b57cec5SDimitry Andric /// 137381ad6265SDimitry Andric /// \param A the argument type 13740b57cec5SDimitry Andric /// 13750b57cec5SDimitry Andric /// \param Info information about the template argument deduction itself 13760b57cec5SDimitry Andric /// 13770b57cec5SDimitry Andric /// \param Deduced the deduced template arguments 13780b57cec5SDimitry Andric /// 13790b57cec5SDimitry Andric /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe 13800b57cec5SDimitry Andric /// how template argument deduction is performed. 13810b57cec5SDimitry Andric /// 13820b57cec5SDimitry Andric /// \param PartialOrdering Whether we're performing template argument deduction 13830b57cec5SDimitry Andric /// in the context of partial ordering (C++0x [temp.deduct.partial]). 13840b57cec5SDimitry Andric /// 13850b57cec5SDimitry Andric /// \returns the result of template argument deduction so far. Note that a 13860b57cec5SDimitry Andric /// "success" result means that template argument deduction has not yet failed, 13870b57cec5SDimitry Andric /// but it may still fail, later, for other reasons. 1388349cc55cSDimitry Andric static Sema::TemplateDeductionResult DeduceTemplateArgumentsByTypeMatch( 1389349cc55cSDimitry Andric Sema &S, TemplateParameterList *TemplateParams, QualType P, QualType A, 13900b57cec5SDimitry Andric TemplateDeductionInfo &Info, 1391349cc55cSDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, unsigned TDF, 1392349cc55cSDimitry Andric bool PartialOrdering, bool DeducedFromArrayBound) { 13930b57cec5SDimitry Andric 13940b57cec5SDimitry Andric // If the argument type is a pack expansion, look at its pattern. 13950b57cec5SDimitry Andric // This isn't explicitly called out 1396349cc55cSDimitry Andric if (const auto *AExp = dyn_cast<PackExpansionType>(A)) 1397349cc55cSDimitry Andric A = AExp->getPattern(); 1398349cc55cSDimitry Andric assert(!isa<PackExpansionType>(A.getCanonicalType())); 13990b57cec5SDimitry Andric 14000b57cec5SDimitry Andric if (PartialOrdering) { 14010b57cec5SDimitry Andric // C++11 [temp.deduct.partial]p5: 14020b57cec5SDimitry Andric // Before the partial ordering is done, certain transformations are 14030b57cec5SDimitry Andric // performed on the types used for partial ordering: 14040b57cec5SDimitry Andric // - If P is a reference type, P is replaced by the type referred to. 1405349cc55cSDimitry Andric const ReferenceType *PRef = P->getAs<ReferenceType>(); 1406349cc55cSDimitry Andric if (PRef) 1407349cc55cSDimitry Andric P = PRef->getPointeeType(); 14080b57cec5SDimitry Andric 14090b57cec5SDimitry Andric // - If A is a reference type, A is replaced by the type referred to. 1410349cc55cSDimitry Andric const ReferenceType *ARef = A->getAs<ReferenceType>(); 1411349cc55cSDimitry Andric if (ARef) 1412349cc55cSDimitry Andric A = A->getPointeeType(); 14130b57cec5SDimitry Andric 1414349cc55cSDimitry Andric if (PRef && ARef && S.Context.hasSameUnqualifiedType(P, A)) { 14150b57cec5SDimitry Andric // C++11 [temp.deduct.partial]p9: 14160b57cec5SDimitry Andric // If, for a given type, deduction succeeds in both directions (i.e., 14170b57cec5SDimitry Andric // the types are identical after the transformations above) and both 14180b57cec5SDimitry Andric // P and A were reference types [...]: 14190b57cec5SDimitry Andric // - if [one type] was an lvalue reference and [the other type] was 14200b57cec5SDimitry Andric // not, [the other type] is not considered to be at least as 14210b57cec5SDimitry Andric // specialized as [the first type] 14220b57cec5SDimitry Andric // - if [one type] is more cv-qualified than [the other type], 14230b57cec5SDimitry Andric // [the other type] is not considered to be at least as specialized 14240b57cec5SDimitry Andric // as [the first type] 14250b57cec5SDimitry Andric // Objective-C ARC adds: 14260b57cec5SDimitry Andric // - [one type] has non-trivial lifetime, [the other type] has 14270b57cec5SDimitry Andric // __unsafe_unretained lifetime, and the types are otherwise 14280b57cec5SDimitry Andric // identical 14290b57cec5SDimitry Andric // 14300b57cec5SDimitry Andric // A is "considered to be at least as specialized" as P iff deduction 14310b57cec5SDimitry Andric // succeeds, so we model this as a deduction failure. Note that 14320b57cec5SDimitry Andric // [the first type] is P and [the other type] is A here; the standard 14330b57cec5SDimitry Andric // gets this backwards. 1434349cc55cSDimitry Andric Qualifiers PQuals = P.getQualifiers(), AQuals = A.getQualifiers(); 1435349cc55cSDimitry Andric if ((PRef->isLValueReferenceType() && !ARef->isLValueReferenceType()) || 1436349cc55cSDimitry Andric PQuals.isStrictSupersetOf(AQuals) || 1437349cc55cSDimitry Andric (PQuals.hasNonTrivialObjCLifetime() && 1438349cc55cSDimitry Andric AQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone && 1439349cc55cSDimitry Andric PQuals.withoutObjCLifetime() == AQuals.withoutObjCLifetime())) { 1440349cc55cSDimitry Andric Info.FirstArg = TemplateArgument(P); 1441349cc55cSDimitry Andric Info.SecondArg = TemplateArgument(A); 14420b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 14430b57cec5SDimitry Andric } 14440b57cec5SDimitry Andric } 1445349cc55cSDimitry Andric Qualifiers DiscardedQuals; 14460b57cec5SDimitry Andric // C++11 [temp.deduct.partial]p7: 14470b57cec5SDimitry Andric // Remove any top-level cv-qualifiers: 14480b57cec5SDimitry Andric // - If P is a cv-qualified type, P is replaced by the cv-unqualified 14490b57cec5SDimitry Andric // version of P. 1450349cc55cSDimitry Andric P = S.Context.getUnqualifiedArrayType(P, DiscardedQuals); 14510b57cec5SDimitry Andric // - If A is a cv-qualified type, A is replaced by the cv-unqualified 14520b57cec5SDimitry Andric // version of A. 1453349cc55cSDimitry Andric A = S.Context.getUnqualifiedArrayType(A, DiscardedQuals); 14540b57cec5SDimitry Andric } else { 14550b57cec5SDimitry Andric // C++0x [temp.deduct.call]p4 bullet 1: 14560b57cec5SDimitry Andric // - If the original P is a reference type, the deduced A (i.e., the type 14570b57cec5SDimitry Andric // referred to by the reference) can be more cv-qualified than the 14580b57cec5SDimitry Andric // transformed A. 14590b57cec5SDimitry Andric if (TDF & TDF_ParamWithReferenceType) { 14600b57cec5SDimitry Andric Qualifiers Quals; 1461349cc55cSDimitry Andric QualType UnqualP = S.Context.getUnqualifiedArrayType(P, Quals); 1462349cc55cSDimitry Andric Quals.setCVRQualifiers(Quals.getCVRQualifiers() & A.getCVRQualifiers()); 1463349cc55cSDimitry Andric P = S.Context.getQualifiedType(UnqualP, Quals); 14640b57cec5SDimitry Andric } 14650b57cec5SDimitry Andric 1466349cc55cSDimitry Andric if ((TDF & TDF_TopLevelParameterTypeList) && !P->isFunctionType()) { 14670b57cec5SDimitry Andric // C++0x [temp.deduct.type]p10: 14680b57cec5SDimitry Andric // If P and A are function types that originated from deduction when 14690b57cec5SDimitry Andric // taking the address of a function template (14.8.2.2) or when deducing 14700b57cec5SDimitry Andric // template arguments from a function declaration (14.8.2.6) and Pi and 14710b57cec5SDimitry Andric // Ai are parameters of the top-level parameter-type-list of P and A, 14720b57cec5SDimitry Andric // respectively, Pi is adjusted if it is a forwarding reference and Ai 14730b57cec5SDimitry Andric // is an lvalue reference, in 14740b57cec5SDimitry Andric // which case the type of Pi is changed to be the template parameter 14750b57cec5SDimitry Andric // type (i.e., T&& is changed to simply T). [ Note: As a result, when 14760b57cec5SDimitry Andric // Pi is T&& and Ai is X&, the adjusted Pi will be T, causing T to be 14770b57cec5SDimitry Andric // deduced as X&. - end note ] 14780b57cec5SDimitry Andric TDF &= ~TDF_TopLevelParameterTypeList; 1479349cc55cSDimitry Andric if (isForwardingReference(P, /*FirstInnerIndex=*/0) && 1480349cc55cSDimitry Andric A->isLValueReferenceType()) 1481349cc55cSDimitry Andric P = P->getPointeeType(); 14820b57cec5SDimitry Andric } 14830b57cec5SDimitry Andric } 14840b57cec5SDimitry Andric 14850b57cec5SDimitry Andric // C++ [temp.deduct.type]p9: 14860b57cec5SDimitry Andric // A template type argument T, a template template argument TT or a 14870b57cec5SDimitry Andric // template non-type argument i can be deduced if P and A have one of 14880b57cec5SDimitry Andric // the following forms: 14890b57cec5SDimitry Andric // 14900b57cec5SDimitry Andric // T 14910b57cec5SDimitry Andric // cv-list T 1492349cc55cSDimitry Andric if (const auto *TTP = P->getAs<TemplateTypeParmType>()) { 14930b57cec5SDimitry Andric // Just skip any attempts to deduce from a placeholder type or a parameter 14940b57cec5SDimitry Andric // at a different depth. 1495349cc55cSDimitry Andric if (A->isPlaceholderType() || Info.getDeducedDepth() != TTP->getDepth()) 14960b57cec5SDimitry Andric return Sema::TDK_Success; 14970b57cec5SDimitry Andric 1498349cc55cSDimitry Andric unsigned Index = TTP->getIndex(); 14990b57cec5SDimitry Andric 15000b57cec5SDimitry Andric // If the argument type is an array type, move the qualifiers up to the 15010b57cec5SDimitry Andric // top level, so they can be matched with the qualifiers on the parameter. 1502349cc55cSDimitry Andric if (A->isArrayType()) { 15030b57cec5SDimitry Andric Qualifiers Quals; 1504349cc55cSDimitry Andric A = S.Context.getUnqualifiedArrayType(A, Quals); 1505349cc55cSDimitry Andric if (Quals) 1506349cc55cSDimitry Andric A = S.Context.getQualifiedType(A, Quals); 15070b57cec5SDimitry Andric } 15080b57cec5SDimitry Andric 15090b57cec5SDimitry Andric // The argument type can not be less qualified than the parameter 15100b57cec5SDimitry Andric // type. 15110b57cec5SDimitry Andric if (!(TDF & TDF_IgnoreQualifiers) && 1512349cc55cSDimitry Andric hasInconsistentOrSupersetQualifiersOf(P, A)) { 15130b57cec5SDimitry Andric Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); 1514349cc55cSDimitry Andric Info.FirstArg = TemplateArgument(P); 1515349cc55cSDimitry Andric Info.SecondArg = TemplateArgument(A); 15160b57cec5SDimitry Andric return Sema::TDK_Underqualified; 15170b57cec5SDimitry Andric } 15180b57cec5SDimitry Andric 15190b57cec5SDimitry Andric // Do not match a function type with a cv-qualified type. 15200b57cec5SDimitry Andric // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1584 1521349cc55cSDimitry Andric if (A->isFunctionType() && P.hasQualifiers()) 15220b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 15230b57cec5SDimitry Andric 1524349cc55cSDimitry Andric assert(TTP->getDepth() == Info.getDeducedDepth() && 15250b57cec5SDimitry Andric "saw template type parameter with wrong depth"); 1526349cc55cSDimitry Andric assert(A->getCanonicalTypeInternal() != S.Context.OverloadTy && 1527349cc55cSDimitry Andric "Unresolved overloaded function"); 1528349cc55cSDimitry Andric QualType DeducedType = A; 15290b57cec5SDimitry Andric 15300b57cec5SDimitry Andric // Remove any qualifiers on the parameter from the deduced type. 15310b57cec5SDimitry Andric // We checked the qualifiers for consistency above. 15320b57cec5SDimitry Andric Qualifiers DeducedQs = DeducedType.getQualifiers(); 1533349cc55cSDimitry Andric Qualifiers ParamQs = P.getQualifiers(); 15340b57cec5SDimitry Andric DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers()); 15350b57cec5SDimitry Andric if (ParamQs.hasObjCGCAttr()) 15360b57cec5SDimitry Andric DeducedQs.removeObjCGCAttr(); 15370b57cec5SDimitry Andric if (ParamQs.hasAddressSpace()) 15380b57cec5SDimitry Andric DeducedQs.removeAddressSpace(); 15390b57cec5SDimitry Andric if (ParamQs.hasObjCLifetime()) 15400b57cec5SDimitry Andric DeducedQs.removeObjCLifetime(); 15410b57cec5SDimitry Andric 15420b57cec5SDimitry Andric // Objective-C ARC: 15430b57cec5SDimitry Andric // If template deduction would produce a lifetime qualifier on a type 15440b57cec5SDimitry Andric // that is not a lifetime type, template argument deduction fails. 15450b57cec5SDimitry Andric if (ParamQs.hasObjCLifetime() && !DeducedType->isObjCLifetimeType() && 15460b57cec5SDimitry Andric !DeducedType->isDependentType()) { 15470b57cec5SDimitry Andric Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); 1548349cc55cSDimitry Andric Info.FirstArg = TemplateArgument(P); 1549349cc55cSDimitry Andric Info.SecondArg = TemplateArgument(A); 15500b57cec5SDimitry Andric return Sema::TDK_Underqualified; 15510b57cec5SDimitry Andric } 15520b57cec5SDimitry Andric 15530b57cec5SDimitry Andric // Objective-C ARC: 15540b57cec5SDimitry Andric // If template deduction would produce an argument type with lifetime type 15550b57cec5SDimitry Andric // but no lifetime qualifier, the __strong lifetime qualifier is inferred. 1556349cc55cSDimitry Andric if (S.getLangOpts().ObjCAutoRefCount && DeducedType->isObjCLifetimeType() && 15570b57cec5SDimitry Andric !DeducedQs.hasObjCLifetime()) 15580b57cec5SDimitry Andric DeducedQs.setObjCLifetime(Qualifiers::OCL_Strong); 15590b57cec5SDimitry Andric 1560349cc55cSDimitry Andric DeducedType = 1561349cc55cSDimitry Andric S.Context.getQualifiedType(DeducedType.getUnqualifiedType(), DeducedQs); 15620b57cec5SDimitry Andric 15630b57cec5SDimitry Andric DeducedTemplateArgument NewDeduced(DeducedType, DeducedFromArrayBound); 1564349cc55cSDimitry Andric DeducedTemplateArgument Result = 1565349cc55cSDimitry Andric checkDeducedTemplateArguments(S.Context, Deduced[Index], NewDeduced); 15660b57cec5SDimitry Andric if (Result.isNull()) { 15670b57cec5SDimitry Andric Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); 15680b57cec5SDimitry Andric Info.FirstArg = Deduced[Index]; 15690b57cec5SDimitry Andric Info.SecondArg = NewDeduced; 15700b57cec5SDimitry Andric return Sema::TDK_Inconsistent; 15710b57cec5SDimitry Andric } 15720b57cec5SDimitry Andric 15730b57cec5SDimitry Andric Deduced[Index] = Result; 15740b57cec5SDimitry Andric return Sema::TDK_Success; 15750b57cec5SDimitry Andric } 15760b57cec5SDimitry Andric 15770b57cec5SDimitry Andric // Set up the template argument deduction information for a failure. 1578349cc55cSDimitry Andric Info.FirstArg = TemplateArgument(P); 1579349cc55cSDimitry Andric Info.SecondArg = TemplateArgument(A); 15800b57cec5SDimitry Andric 15810b57cec5SDimitry Andric // If the parameter is an already-substituted template parameter 15820b57cec5SDimitry Andric // pack, do nothing: we don't know which of its arguments to look 15830b57cec5SDimitry Andric // at, so we have to wait until all of the parameter packs in this 15840b57cec5SDimitry Andric // expansion have arguments. 1585349cc55cSDimitry Andric if (P->getAs<SubstTemplateTypeParmPackType>()) 15860b57cec5SDimitry Andric return Sema::TDK_Success; 15870b57cec5SDimitry Andric 15880b57cec5SDimitry Andric // Check the cv-qualifiers on the parameter and argument types. 15890b57cec5SDimitry Andric if (!(TDF & TDF_IgnoreQualifiers)) { 15900b57cec5SDimitry Andric if (TDF & TDF_ParamWithReferenceType) { 1591349cc55cSDimitry Andric if (hasInconsistentOrSupersetQualifiersOf(P, A)) 15920b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 15930b57cec5SDimitry Andric } else if (TDF & TDF_ArgWithReferenceType) { 15940b57cec5SDimitry Andric // C++ [temp.deduct.conv]p4: 15950b57cec5SDimitry Andric // If the original A is a reference type, A can be more cv-qualified 15960b57cec5SDimitry Andric // than the deduced A 1597349cc55cSDimitry Andric if (!A.getQualifiers().compatiblyIncludes(P.getQualifiers())) 15980b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 15990b57cec5SDimitry Andric 16000b57cec5SDimitry Andric // Strip out all extra qualifiers from the argument to figure out the 16010b57cec5SDimitry Andric // type we're converting to, prior to the qualification conversion. 16020b57cec5SDimitry Andric Qualifiers Quals; 1603349cc55cSDimitry Andric A = S.Context.getUnqualifiedArrayType(A, Quals); 1604349cc55cSDimitry Andric A = S.Context.getQualifiedType(A, P.getQualifiers()); 1605349cc55cSDimitry Andric } else if (!IsPossiblyOpaquelyQualifiedType(P)) { 1606349cc55cSDimitry Andric if (P.getCVRQualifiers() != A.getCVRQualifiers()) 16070b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 16080b57cec5SDimitry Andric } 1609349cc55cSDimitry Andric } 16100b57cec5SDimitry Andric 16110b57cec5SDimitry Andric // If the parameter type is not dependent, there is nothing to deduce. 1612349cc55cSDimitry Andric if (!P->isDependentType()) { 1613349cc55cSDimitry Andric if (TDF & TDF_SkipNonDependent) 1614349cc55cSDimitry Andric return Sema::TDK_Success; 1615349cc55cSDimitry Andric if ((TDF & TDF_IgnoreQualifiers) ? S.Context.hasSameUnqualifiedType(P, A) 1616349cc55cSDimitry Andric : S.Context.hasSameType(P, A)) 1617349cc55cSDimitry Andric return Sema::TDK_Success; 1618349cc55cSDimitry Andric if (TDF & TDF_AllowCompatibleFunctionType && 1619349cc55cSDimitry Andric S.isSameOrCompatibleFunctionType(P, A)) 1620349cc55cSDimitry Andric return Sema::TDK_Success; 1621349cc55cSDimitry Andric if (!(TDF & TDF_IgnoreQualifiers)) 16220b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1623349cc55cSDimitry Andric // Otherwise, when ignoring qualifiers, the types not having the same 1624349cc55cSDimitry Andric // unqualified type does not mean they do not match, so in this case we 1625349cc55cSDimitry Andric // must keep going and analyze with a non-dependent parameter type. 16260b57cec5SDimitry Andric } 16270b57cec5SDimitry Andric 1628349cc55cSDimitry Andric switch (P.getCanonicalType()->getTypeClass()) { 16290b57cec5SDimitry Andric // Non-canonical types cannot appear here. 16300b57cec5SDimitry Andric #define NON_CANONICAL_TYPE(Class, Base) \ 16310b57cec5SDimitry Andric case Type::Class: llvm_unreachable("deducing non-canonical type: " #Class); 16320b57cec5SDimitry Andric #define TYPE(Class, Base) 1633a7dea167SDimitry Andric #include "clang/AST/TypeNodes.inc" 16340b57cec5SDimitry Andric 16350b57cec5SDimitry Andric case Type::TemplateTypeParm: 16360b57cec5SDimitry Andric case Type::SubstTemplateTypeParmPack: 16370b57cec5SDimitry Andric llvm_unreachable("Type nodes handled above"); 16380b57cec5SDimitry Andric 1639349cc55cSDimitry Andric case Type::Auto: 164006c3fb27SDimitry Andric // C++23 [temp.deduct.funcaddr]/3: 164106c3fb27SDimitry Andric // A placeholder type in the return type of a function template is a 164206c3fb27SDimitry Andric // non-deduced context. 164306c3fb27SDimitry Andric // There's no corresponding wording for [temp.deduct.decl], but we treat 164406c3fb27SDimitry Andric // it the same to match other compilers. 1645349cc55cSDimitry Andric if (P->isDependentType()) 1646349cc55cSDimitry Andric return Sema::TDK_Success; 1647bdd1243dSDimitry Andric [[fallthrough]]; 16480b57cec5SDimitry Andric case Type::Builtin: 16490b57cec5SDimitry Andric case Type::VariableArray: 16500b57cec5SDimitry Andric case Type::Vector: 16510b57cec5SDimitry Andric case Type::FunctionNoProto: 16520b57cec5SDimitry Andric case Type::Record: 16530b57cec5SDimitry Andric case Type::Enum: 16540b57cec5SDimitry Andric case Type::ObjCObject: 16550b57cec5SDimitry Andric case Type::ObjCInterface: 16560b57cec5SDimitry Andric case Type::ObjCObjectPointer: 16570eae32dcSDimitry Andric case Type::BitInt: 1658349cc55cSDimitry Andric return (TDF & TDF_SkipNonDependent) || 1659349cc55cSDimitry Andric ((TDF & TDF_IgnoreQualifiers) 1660349cc55cSDimitry Andric ? S.Context.hasSameUnqualifiedType(P, A) 1661349cc55cSDimitry Andric : S.Context.hasSameType(P, A)) 1662349cc55cSDimitry Andric ? Sema::TDK_Success 1663349cc55cSDimitry Andric : Sema::TDK_NonDeducedMismatch; 16640b57cec5SDimitry Andric 16650b57cec5SDimitry Andric // _Complex T [placeholder extension] 1666349cc55cSDimitry Andric case Type::Complex: { 1667349cc55cSDimitry Andric const auto *CP = P->castAs<ComplexType>(), *CA = A->getAs<ComplexType>(); 1668349cc55cSDimitry Andric if (!CA) 16690b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1670349cc55cSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 1671349cc55cSDimitry Andric S, TemplateParams, CP->getElementType(), CA->getElementType(), Info, 1672349cc55cSDimitry Andric Deduced, TDF); 1673349cc55cSDimitry Andric } 16740b57cec5SDimitry Andric 16750b57cec5SDimitry Andric // _Atomic T [extension] 1676349cc55cSDimitry Andric case Type::Atomic: { 1677349cc55cSDimitry Andric const auto *PA = P->castAs<AtomicType>(), *AA = A->getAs<AtomicType>(); 1678349cc55cSDimitry Andric if (!AA) 16790b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1680349cc55cSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 1681349cc55cSDimitry Andric S, TemplateParams, PA->getValueType(), AA->getValueType(), Info, 1682349cc55cSDimitry Andric Deduced, TDF); 1683349cc55cSDimitry Andric } 16840b57cec5SDimitry Andric 16850b57cec5SDimitry Andric // T * 16860b57cec5SDimitry Andric case Type::Pointer: { 16870b57cec5SDimitry Andric QualType PointeeType; 1688349cc55cSDimitry Andric if (const auto *PA = A->getAs<PointerType>()) { 1689349cc55cSDimitry Andric PointeeType = PA->getPointeeType(); 1690349cc55cSDimitry Andric } else if (const auto *PA = A->getAs<ObjCObjectPointerType>()) { 1691349cc55cSDimitry Andric PointeeType = PA->getPointeeType(); 16920b57cec5SDimitry Andric } else { 16930b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 16940b57cec5SDimitry Andric } 1695349cc55cSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 1696349cc55cSDimitry Andric S, TemplateParams, P->castAs<PointerType>()->getPointeeType(), 1697349cc55cSDimitry Andric PointeeType, Info, Deduced, 1698349cc55cSDimitry Andric TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass)); 16990b57cec5SDimitry Andric } 17000b57cec5SDimitry Andric 17010b57cec5SDimitry Andric // T & 17020b57cec5SDimitry Andric case Type::LValueReference: { 1703349cc55cSDimitry Andric const auto *RP = P->castAs<LValueReferenceType>(), 1704349cc55cSDimitry Andric *RA = A->getAs<LValueReferenceType>(); 1705349cc55cSDimitry Andric if (!RA) 17060b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 17070b57cec5SDimitry Andric 1708349cc55cSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 1709349cc55cSDimitry Andric S, TemplateParams, RP->getPointeeType(), RA->getPointeeType(), Info, 1710349cc55cSDimitry Andric Deduced, 0); 17110b57cec5SDimitry Andric } 17120b57cec5SDimitry Andric 17130b57cec5SDimitry Andric // T && [C++0x] 17140b57cec5SDimitry Andric case Type::RValueReference: { 1715349cc55cSDimitry Andric const auto *RP = P->castAs<RValueReferenceType>(), 1716349cc55cSDimitry Andric *RA = A->getAs<RValueReferenceType>(); 1717349cc55cSDimitry Andric if (!RA) 17180b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 17190b57cec5SDimitry Andric 1720349cc55cSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 1721349cc55cSDimitry Andric S, TemplateParams, RP->getPointeeType(), RA->getPointeeType(), Info, 1722349cc55cSDimitry Andric Deduced, 0); 17230b57cec5SDimitry Andric } 17240b57cec5SDimitry Andric 17250b57cec5SDimitry Andric // T [] (implied, but not stated explicitly) 17260b57cec5SDimitry Andric case Type::IncompleteArray: { 1727349cc55cSDimitry Andric const auto *IAA = S.Context.getAsIncompleteArrayType(A); 1728349cc55cSDimitry Andric if (!IAA) 17290b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 17300b57cec5SDimitry Andric 173106c3fb27SDimitry Andric const auto *IAP = S.Context.getAsIncompleteArrayType(P); 173206c3fb27SDimitry Andric assert(IAP && "Template parameter not of incomplete array type"); 173306c3fb27SDimitry Andric 1734349cc55cSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 173506c3fb27SDimitry Andric S, TemplateParams, IAP->getElementType(), IAA->getElementType(), Info, 173606c3fb27SDimitry Andric Deduced, TDF & TDF_IgnoreQualifiers); 17370b57cec5SDimitry Andric } 17380b57cec5SDimitry Andric 17390b57cec5SDimitry Andric // T [integer-constant] 17400b57cec5SDimitry Andric case Type::ConstantArray: { 1741349cc55cSDimitry Andric const auto *CAA = S.Context.getAsConstantArrayType(A), 1742349cc55cSDimitry Andric *CAP = S.Context.getAsConstantArrayType(P); 1743349cc55cSDimitry Andric assert(CAP); 1744349cc55cSDimitry Andric if (!CAA || CAA->getSize() != CAP->getSize()) 17450b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 17460b57cec5SDimitry Andric 1747349cc55cSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 1748349cc55cSDimitry Andric S, TemplateParams, CAP->getElementType(), CAA->getElementType(), Info, 1749349cc55cSDimitry Andric Deduced, TDF & TDF_IgnoreQualifiers); 17500b57cec5SDimitry Andric } 17510b57cec5SDimitry Andric 17520b57cec5SDimitry Andric // type [i] 17530b57cec5SDimitry Andric case Type::DependentSizedArray: { 1754349cc55cSDimitry Andric const auto *AA = S.Context.getAsArrayType(A); 1755349cc55cSDimitry Andric if (!AA) 17560b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 17570b57cec5SDimitry Andric 17580b57cec5SDimitry Andric // Check the element type of the arrays 1759349cc55cSDimitry Andric const auto *DAP = S.Context.getAsDependentSizedArrayType(P); 1760349cc55cSDimitry Andric assert(DAP); 1761349cc55cSDimitry Andric if (auto Result = DeduceTemplateArgumentsByTypeMatch( 1762349cc55cSDimitry Andric S, TemplateParams, DAP->getElementType(), AA->getElementType(), 1763349cc55cSDimitry Andric Info, Deduced, TDF & TDF_IgnoreQualifiers)) 17640b57cec5SDimitry Andric return Result; 17650b57cec5SDimitry Andric 17660b57cec5SDimitry Andric // Determine the array bound is something we can deduce. 1767349cc55cSDimitry Andric const NonTypeTemplateParmDecl *NTTP = 1768349cc55cSDimitry Andric getDeducedParameterFromExpr(Info, DAP->getSizeExpr()); 17690b57cec5SDimitry Andric if (!NTTP) 17700b57cec5SDimitry Andric return Sema::TDK_Success; 17710b57cec5SDimitry Andric 17720b57cec5SDimitry Andric // We can perform template argument deduction for the given non-type 17730b57cec5SDimitry Andric // template parameter. 17740b57cec5SDimitry Andric assert(NTTP->getDepth() == Info.getDeducedDepth() && 17750b57cec5SDimitry Andric "saw non-type template parameter with wrong depth"); 1776349cc55cSDimitry Andric if (const auto *CAA = dyn_cast<ConstantArrayType>(AA)) { 1777349cc55cSDimitry Andric llvm::APSInt Size(CAA->getSize()); 1778349cc55cSDimitry Andric return DeduceNonTypeTemplateArgument( 1779349cc55cSDimitry Andric S, TemplateParams, NTTP, Size, S.Context.getSizeType(), 1780349cc55cSDimitry Andric /*ArrayBound=*/true, Info, Deduced); 17810b57cec5SDimitry Andric } 1782349cc55cSDimitry Andric if (const auto *DAA = dyn_cast<DependentSizedArrayType>(AA)) 1783349cc55cSDimitry Andric if (DAA->getSizeExpr()) 1784349cc55cSDimitry Andric return DeduceNonTypeTemplateArgument( 1785349cc55cSDimitry Andric S, TemplateParams, NTTP, DAA->getSizeExpr(), Info, Deduced); 17860b57cec5SDimitry Andric 17870b57cec5SDimitry Andric // Incomplete type does not match a dependently-sized array type 17880b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 17890b57cec5SDimitry Andric } 17900b57cec5SDimitry Andric 17910b57cec5SDimitry Andric // type(*)(T) 17920b57cec5SDimitry Andric // T(*)() 17930b57cec5SDimitry Andric // T(*)(T) 17940b57cec5SDimitry Andric case Type::FunctionProto: { 1795349cc55cSDimitry Andric const auto *FPP = P->castAs<FunctionProtoType>(), 1796349cc55cSDimitry Andric *FPA = A->getAs<FunctionProtoType>(); 1797349cc55cSDimitry Andric if (!FPA) 17980b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 17990b57cec5SDimitry Andric 1800349cc55cSDimitry Andric if (FPP->getMethodQuals() != FPA->getMethodQuals() || 1801349cc55cSDimitry Andric FPP->getRefQualifier() != FPA->getRefQualifier() || 1802349cc55cSDimitry Andric FPP->isVariadic() != FPA->isVariadic()) 18030b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 18040b57cec5SDimitry Andric 18050b57cec5SDimitry Andric // Check return types. 18060b57cec5SDimitry Andric if (auto Result = DeduceTemplateArgumentsByTypeMatch( 1807349cc55cSDimitry Andric S, TemplateParams, FPP->getReturnType(), FPA->getReturnType(), 1808349cc55cSDimitry Andric Info, Deduced, 0, 1809349cc55cSDimitry Andric /*PartialOrdering=*/false, 1810349cc55cSDimitry Andric /*DeducedFromArrayBound=*/false)) 18110b57cec5SDimitry Andric return Result; 18120b57cec5SDimitry Andric 18130b57cec5SDimitry Andric // Check parameter types. 18140b57cec5SDimitry Andric if (auto Result = DeduceTemplateArguments( 1815349cc55cSDimitry Andric S, TemplateParams, FPP->param_type_begin(), FPP->getNumParams(), 1816349cc55cSDimitry Andric FPA->param_type_begin(), FPA->getNumParams(), Info, Deduced, 1817bdd1243dSDimitry Andric TDF & TDF_TopLevelParameterTypeList, PartialOrdering)) 18180b57cec5SDimitry Andric return Result; 18190b57cec5SDimitry Andric 18200b57cec5SDimitry Andric if (TDF & TDF_AllowCompatibleFunctionType) 18210b57cec5SDimitry Andric return Sema::TDK_Success; 18220b57cec5SDimitry Andric 18230b57cec5SDimitry Andric // FIXME: Per core-2016/10/1019 (no corresponding core issue yet), permit 18240b57cec5SDimitry Andric // deducing through the noexcept-specifier if it's part of the canonical 18250b57cec5SDimitry Andric // type. libstdc++ relies on this. 1826349cc55cSDimitry Andric Expr *NoexceptExpr = FPP->getNoexceptExpr(); 1827e8d8bef9SDimitry Andric if (const NonTypeTemplateParmDecl *NTTP = 18280b57cec5SDimitry Andric NoexceptExpr ? getDeducedParameterFromExpr(Info, NoexceptExpr) 18290b57cec5SDimitry Andric : nullptr) { 18300b57cec5SDimitry Andric assert(NTTP->getDepth() == Info.getDeducedDepth() && 18310b57cec5SDimitry Andric "saw non-type template parameter with wrong depth"); 18320b57cec5SDimitry Andric 18330b57cec5SDimitry Andric llvm::APSInt Noexcept(1); 1834349cc55cSDimitry Andric switch (FPA->canThrow()) { 18350b57cec5SDimitry Andric case CT_Cannot: 18360b57cec5SDimitry Andric Noexcept = 1; 1837bdd1243dSDimitry Andric [[fallthrough]]; 18380b57cec5SDimitry Andric 18390b57cec5SDimitry Andric case CT_Can: 18400b57cec5SDimitry Andric // We give E in noexcept(E) the "deduced from array bound" treatment. 18410b57cec5SDimitry Andric // FIXME: Should we? 18420b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument( 18430b57cec5SDimitry Andric S, TemplateParams, NTTP, Noexcept, S.Context.BoolTy, 1844349cc55cSDimitry Andric /*DeducedFromArrayBound=*/true, Info, Deduced); 18450b57cec5SDimitry Andric 18460b57cec5SDimitry Andric case CT_Dependent: 1847349cc55cSDimitry Andric if (Expr *ArgNoexceptExpr = FPA->getNoexceptExpr()) 18480b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument( 18490b57cec5SDimitry Andric S, TemplateParams, NTTP, ArgNoexceptExpr, Info, Deduced); 18500b57cec5SDimitry Andric // Can't deduce anything from throw(T...). 18510b57cec5SDimitry Andric break; 18520b57cec5SDimitry Andric } 18530b57cec5SDimitry Andric } 18540b57cec5SDimitry Andric // FIXME: Detect non-deduced exception specification mismatches? 18550b57cec5SDimitry Andric // 18560b57cec5SDimitry Andric // Careful about [temp.deduct.call] and [temp.deduct.conv], which allow 18570b57cec5SDimitry Andric // top-level differences in noexcept-specifications. 18580b57cec5SDimitry Andric 18590b57cec5SDimitry Andric return Sema::TDK_Success; 18600b57cec5SDimitry Andric } 18610b57cec5SDimitry Andric 18620b57cec5SDimitry Andric case Type::InjectedClassName: 18630b57cec5SDimitry Andric // Treat a template's injected-class-name as if the template 18640b57cec5SDimitry Andric // specialization type had been used. 18650b57cec5SDimitry Andric 18660b57cec5SDimitry Andric // template-name<T> (where template-name refers to a class template) 18670b57cec5SDimitry Andric // template-name<i> 18680b57cec5SDimitry Andric // TT<T> 18690b57cec5SDimitry Andric // TT<i> 18700b57cec5SDimitry Andric // TT<> 18710b57cec5SDimitry Andric case Type::TemplateSpecialization: { 18720b57cec5SDimitry Andric // When Arg cannot be a derived class, we can just try to deduce template 18730b57cec5SDimitry Andric // arguments from the template-id. 1874349cc55cSDimitry Andric if (!(TDF & TDF_DerivedClass) || !A->isRecordType()) 1875349cc55cSDimitry Andric return DeduceTemplateSpecArguments(S, TemplateParams, P, A, Info, 18760b57cec5SDimitry Andric Deduced); 18770b57cec5SDimitry Andric 18780b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 8> DeducedOrig(Deduced.begin(), 18790b57cec5SDimitry Andric Deduced.end()); 18800b57cec5SDimitry Andric 1881349cc55cSDimitry Andric auto Result = 1882349cc55cSDimitry Andric DeduceTemplateSpecArguments(S, TemplateParams, P, A, Info, Deduced); 18830b57cec5SDimitry Andric if (Result == Sema::TDK_Success) 18840b57cec5SDimitry Andric return Result; 18850b57cec5SDimitry Andric 18860b57cec5SDimitry Andric // We cannot inspect base classes as part of deduction when the type 18870b57cec5SDimitry Andric // is incomplete, so either instantiate any templates necessary to 18880b57cec5SDimitry Andric // complete the type, or skip over it if it cannot be completed. 1889349cc55cSDimitry Andric if (!S.isCompleteType(Info.getLocation(), A)) 18900b57cec5SDimitry Andric return Result; 18910b57cec5SDimitry Andric 18920b57cec5SDimitry Andric // Reset the incorrectly deduced argument from above. 18930b57cec5SDimitry Andric Deduced = DeducedOrig; 18940b57cec5SDimitry Andric 1895e8d8bef9SDimitry Andric // Check bases according to C++14 [temp.deduct.call] p4b3: 1896349cc55cSDimitry Andric auto BaseResult = DeduceTemplateBases(S, getCanonicalRD(A), 1897349cc55cSDimitry Andric TemplateParams, P, Info, Deduced); 1898349cc55cSDimitry Andric return BaseResult != Sema::TDK_Invalid ? BaseResult : Result; 18990b57cec5SDimitry Andric } 19000b57cec5SDimitry Andric 19010b57cec5SDimitry Andric // T type::* 19020b57cec5SDimitry Andric // T T::* 19030b57cec5SDimitry Andric // T (type::*)() 19040b57cec5SDimitry Andric // type (T::*)() 19050b57cec5SDimitry Andric // type (type::*)(T) 19060b57cec5SDimitry Andric // type (T::*)(T) 19070b57cec5SDimitry Andric // T (type::*)(T) 19080b57cec5SDimitry Andric // T (T::*)() 19090b57cec5SDimitry Andric // T (T::*)(T) 19100b57cec5SDimitry Andric case Type::MemberPointer: { 1911349cc55cSDimitry Andric const auto *MPP = P->castAs<MemberPointerType>(), 1912349cc55cSDimitry Andric *MPA = A->getAs<MemberPointerType>(); 1913349cc55cSDimitry Andric if (!MPA) 19140b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 19150b57cec5SDimitry Andric 1916349cc55cSDimitry Andric QualType PPT = MPP->getPointeeType(); 1917349cc55cSDimitry Andric if (PPT->isFunctionType()) 19185f757f3fSDimitry Andric S.adjustMemberFunctionCC(PPT, /*HasThisPointer=*/false, 19190b57cec5SDimitry Andric /*IsCtorOrDtor=*/false, Info.getLocation()); 1920349cc55cSDimitry Andric QualType APT = MPA->getPointeeType(); 1921349cc55cSDimitry Andric if (APT->isFunctionType()) 19225f757f3fSDimitry Andric S.adjustMemberFunctionCC(APT, /*HasThisPointer=*/false, 19230b57cec5SDimitry Andric /*IsCtorOrDtor=*/false, Info.getLocation()); 19240b57cec5SDimitry Andric 1925349cc55cSDimitry Andric unsigned SubTDF = TDF & TDF_IgnoreQualifiers; 1926349cc55cSDimitry Andric if (auto Result = DeduceTemplateArgumentsByTypeMatch( 1927349cc55cSDimitry Andric S, TemplateParams, PPT, APT, Info, Deduced, SubTDF)) 19280b57cec5SDimitry Andric return Result; 1929349cc55cSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 1930349cc55cSDimitry Andric S, TemplateParams, QualType(MPP->getClass(), 0), 1931349cc55cSDimitry Andric QualType(MPA->getClass(), 0), Info, Deduced, SubTDF); 19320b57cec5SDimitry Andric } 19330b57cec5SDimitry Andric 19340b57cec5SDimitry Andric // (clang extension) 19350b57cec5SDimitry Andric // 19360b57cec5SDimitry Andric // type(^)(T) 19370b57cec5SDimitry Andric // T(^)() 19380b57cec5SDimitry Andric // T(^)(T) 19390b57cec5SDimitry Andric case Type::BlockPointer: { 1940349cc55cSDimitry Andric const auto *BPP = P->castAs<BlockPointerType>(), 1941349cc55cSDimitry Andric *BPA = A->getAs<BlockPointerType>(); 1942349cc55cSDimitry Andric if (!BPA) 19430b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1944349cc55cSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 1945349cc55cSDimitry Andric S, TemplateParams, BPP->getPointeeType(), BPA->getPointeeType(), Info, 1946349cc55cSDimitry Andric Deduced, 0); 19470b57cec5SDimitry Andric } 19480b57cec5SDimitry Andric 19490b57cec5SDimitry Andric // (clang extension) 19500b57cec5SDimitry Andric // 19510b57cec5SDimitry Andric // T __attribute__(((ext_vector_type(<integral constant>)))) 19520b57cec5SDimitry Andric case Type::ExtVector: { 1953349cc55cSDimitry Andric const auto *VP = P->castAs<ExtVectorType>(); 1954349cc55cSDimitry Andric QualType ElementType; 1955349cc55cSDimitry Andric if (const auto *VA = A->getAs<ExtVectorType>()) { 19560b57cec5SDimitry Andric // Make sure that the vectors have the same number of elements. 1957349cc55cSDimitry Andric if (VP->getNumElements() != VA->getNumElements()) 19580b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1959349cc55cSDimitry Andric ElementType = VA->getElementType(); 1960349cc55cSDimitry Andric } else if (const auto *VA = A->getAs<DependentSizedExtVectorType>()) { 19610b57cec5SDimitry Andric // We can't check the number of elements, since the argument has a 19620b57cec5SDimitry Andric // dependent number of elements. This can only occur during partial 19630b57cec5SDimitry Andric // ordering. 1964349cc55cSDimitry Andric ElementType = VA->getElementType(); 1965349cc55cSDimitry Andric } else { 19660b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 19670b57cec5SDimitry Andric } 1968349cc55cSDimitry Andric // Perform deduction on the element types. 1969349cc55cSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 1970349cc55cSDimitry Andric S, TemplateParams, VP->getElementType(), ElementType, Info, Deduced, 1971349cc55cSDimitry Andric TDF); 1972349cc55cSDimitry Andric } 19730b57cec5SDimitry Andric 19740b57cec5SDimitry Andric case Type::DependentVector: { 1975349cc55cSDimitry Andric const auto *VP = P->castAs<DependentVectorType>(); 19760b57cec5SDimitry Andric 1977349cc55cSDimitry Andric if (const auto *VA = A->getAs<VectorType>()) { 19780b57cec5SDimitry Andric // Perform deduction on the element types. 1979349cc55cSDimitry Andric if (auto Result = DeduceTemplateArgumentsByTypeMatch( 1980349cc55cSDimitry Andric S, TemplateParams, VP->getElementType(), VA->getElementType(), 1981349cc55cSDimitry Andric Info, Deduced, TDF)) 19820b57cec5SDimitry Andric return Result; 19830b57cec5SDimitry Andric 19840b57cec5SDimitry Andric // Perform deduction on the vector size, if we can. 1985e8d8bef9SDimitry Andric const NonTypeTemplateParmDecl *NTTP = 1986349cc55cSDimitry Andric getDeducedParameterFromExpr(Info, VP->getSizeExpr()); 19870b57cec5SDimitry Andric if (!NTTP) 19880b57cec5SDimitry Andric return Sema::TDK_Success; 19890b57cec5SDimitry Andric 19900b57cec5SDimitry Andric llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); 1991349cc55cSDimitry Andric ArgSize = VA->getNumElements(); 19920b57cec5SDimitry Andric // Note that we use the "array bound" rules here; just like in that 19930b57cec5SDimitry Andric // case, we don't have any particular type for the vector size, but 19940b57cec5SDimitry Andric // we can provide one if necessary. 19950b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, 19960b57cec5SDimitry Andric S.Context.UnsignedIntTy, true, 19970b57cec5SDimitry Andric Info, Deduced); 19980b57cec5SDimitry Andric } 19990b57cec5SDimitry Andric 2000349cc55cSDimitry Andric if (const auto *VA = A->getAs<DependentVectorType>()) { 20010b57cec5SDimitry Andric // Perform deduction on the element types. 2002349cc55cSDimitry Andric if (auto Result = DeduceTemplateArgumentsByTypeMatch( 2003349cc55cSDimitry Andric S, TemplateParams, VP->getElementType(), VA->getElementType(), 2004349cc55cSDimitry Andric Info, Deduced, TDF)) 20050b57cec5SDimitry Andric return Result; 20060b57cec5SDimitry Andric 20070b57cec5SDimitry Andric // Perform deduction on the vector size, if we can. 2008349cc55cSDimitry Andric const NonTypeTemplateParmDecl *NTTP = 2009349cc55cSDimitry Andric getDeducedParameterFromExpr(Info, VP->getSizeExpr()); 20100b57cec5SDimitry Andric if (!NTTP) 20110b57cec5SDimitry Andric return Sema::TDK_Success; 20120b57cec5SDimitry Andric 2013349cc55cSDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, 2014349cc55cSDimitry Andric VA->getSizeExpr(), Info, Deduced); 20150b57cec5SDimitry Andric } 20160b57cec5SDimitry Andric 20170b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 20180b57cec5SDimitry Andric } 20190b57cec5SDimitry Andric 20200b57cec5SDimitry Andric // (clang extension) 20210b57cec5SDimitry Andric // 20220b57cec5SDimitry Andric // T __attribute__(((ext_vector_type(N)))) 20230b57cec5SDimitry Andric case Type::DependentSizedExtVector: { 2024349cc55cSDimitry Andric const auto *VP = P->castAs<DependentSizedExtVectorType>(); 20250b57cec5SDimitry Andric 2026349cc55cSDimitry Andric if (const auto *VA = A->getAs<ExtVectorType>()) { 20270b57cec5SDimitry Andric // Perform deduction on the element types. 2028349cc55cSDimitry Andric if (auto Result = DeduceTemplateArgumentsByTypeMatch( 2029349cc55cSDimitry Andric S, TemplateParams, VP->getElementType(), VA->getElementType(), 20300b57cec5SDimitry Andric Info, Deduced, TDF)) 20310b57cec5SDimitry Andric return Result; 20320b57cec5SDimitry Andric 20330b57cec5SDimitry Andric // Perform deduction on the vector size, if we can. 2034e8d8bef9SDimitry Andric const NonTypeTemplateParmDecl *NTTP = 2035349cc55cSDimitry Andric getDeducedParameterFromExpr(Info, VP->getSizeExpr()); 20360b57cec5SDimitry Andric if (!NTTP) 20370b57cec5SDimitry Andric return Sema::TDK_Success; 20380b57cec5SDimitry Andric 20390b57cec5SDimitry Andric llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); 2040349cc55cSDimitry Andric ArgSize = VA->getNumElements(); 20410b57cec5SDimitry Andric // Note that we use the "array bound" rules here; just like in that 20420b57cec5SDimitry Andric // case, we don't have any particular type for the vector size, but 20430b57cec5SDimitry Andric // we can provide one if necessary. 20440b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, 20450b57cec5SDimitry Andric S.Context.IntTy, true, Info, 20460b57cec5SDimitry Andric Deduced); 20470b57cec5SDimitry Andric } 20480b57cec5SDimitry Andric 2049349cc55cSDimitry Andric if (const auto *VA = A->getAs<DependentSizedExtVectorType>()) { 20500b57cec5SDimitry Andric // Perform deduction on the element types. 2051349cc55cSDimitry Andric if (auto Result = DeduceTemplateArgumentsByTypeMatch( 2052349cc55cSDimitry Andric S, TemplateParams, VP->getElementType(), VA->getElementType(), 20530b57cec5SDimitry Andric Info, Deduced, TDF)) 20540b57cec5SDimitry Andric return Result; 20550b57cec5SDimitry Andric 20560b57cec5SDimitry Andric // Perform deduction on the vector size, if we can. 2057e8d8bef9SDimitry Andric const NonTypeTemplateParmDecl *NTTP = 2058349cc55cSDimitry Andric getDeducedParameterFromExpr(Info, VP->getSizeExpr()); 20590b57cec5SDimitry Andric if (!NTTP) 20600b57cec5SDimitry Andric return Sema::TDK_Success; 20610b57cec5SDimitry Andric 20620b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, 2063349cc55cSDimitry Andric VA->getSizeExpr(), Info, Deduced); 20640b57cec5SDimitry Andric } 20650b57cec5SDimitry Andric 20660b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 20670b57cec5SDimitry Andric } 20680b57cec5SDimitry Andric 20690b57cec5SDimitry Andric // (clang extension) 20700b57cec5SDimitry Andric // 20715ffd83dbSDimitry Andric // T __attribute__((matrix_type(<integral constant>, 20725ffd83dbSDimitry Andric // <integral constant>))) 20735ffd83dbSDimitry Andric case Type::ConstantMatrix: { 2074349cc55cSDimitry Andric const auto *MP = P->castAs<ConstantMatrixType>(), 2075349cc55cSDimitry Andric *MA = A->getAs<ConstantMatrixType>(); 2076349cc55cSDimitry Andric if (!MA) 20775ffd83dbSDimitry Andric return Sema::TDK_NonDeducedMismatch; 20785ffd83dbSDimitry Andric 20795ffd83dbSDimitry Andric // Check that the dimensions are the same 2080349cc55cSDimitry Andric if (MP->getNumRows() != MA->getNumRows() || 2081349cc55cSDimitry Andric MP->getNumColumns() != MA->getNumColumns()) { 20825ffd83dbSDimitry Andric return Sema::TDK_NonDeducedMismatch; 20835ffd83dbSDimitry Andric } 20845ffd83dbSDimitry Andric // Perform deduction on element types. 20855ffd83dbSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 2086349cc55cSDimitry Andric S, TemplateParams, MP->getElementType(), MA->getElementType(), Info, 2087349cc55cSDimitry Andric Deduced, TDF); 20885ffd83dbSDimitry Andric } 20895ffd83dbSDimitry Andric 20905ffd83dbSDimitry Andric case Type::DependentSizedMatrix: { 2091349cc55cSDimitry Andric const auto *MP = P->castAs<DependentSizedMatrixType>(); 2092349cc55cSDimitry Andric const auto *MA = A->getAs<MatrixType>(); 2093349cc55cSDimitry Andric if (!MA) 20945ffd83dbSDimitry Andric return Sema::TDK_NonDeducedMismatch; 20955ffd83dbSDimitry Andric 20965ffd83dbSDimitry Andric // Check the element type of the matrixes. 2097349cc55cSDimitry Andric if (auto Result = DeduceTemplateArgumentsByTypeMatch( 2098349cc55cSDimitry Andric S, TemplateParams, MP->getElementType(), MA->getElementType(), 2099349cc55cSDimitry Andric Info, Deduced, TDF)) 21005ffd83dbSDimitry Andric return Result; 21015ffd83dbSDimitry Andric 21025ffd83dbSDimitry Andric // Try to deduce a matrix dimension. 21035ffd83dbSDimitry Andric auto DeduceMatrixArg = 21045ffd83dbSDimitry Andric [&S, &Info, &Deduced, &TemplateParams]( 2105349cc55cSDimitry Andric Expr *ParamExpr, const MatrixType *A, 21065ffd83dbSDimitry Andric unsigned (ConstantMatrixType::*GetArgDimension)() const, 21075ffd83dbSDimitry Andric Expr *(DependentSizedMatrixType::*GetArgDimensionExpr)() const) { 2108349cc55cSDimitry Andric const auto *ACM = dyn_cast<ConstantMatrixType>(A); 2109349cc55cSDimitry Andric const auto *ADM = dyn_cast<DependentSizedMatrixType>(A); 21105ffd83dbSDimitry Andric if (!ParamExpr->isValueDependent()) { 2111bdd1243dSDimitry Andric std::optional<llvm::APSInt> ParamConst = 2112e8d8bef9SDimitry Andric ParamExpr->getIntegerConstantExpr(S.Context); 2113e8d8bef9SDimitry Andric if (!ParamConst) 21145ffd83dbSDimitry Andric return Sema::TDK_NonDeducedMismatch; 21155ffd83dbSDimitry Andric 2116349cc55cSDimitry Andric if (ACM) { 2117349cc55cSDimitry Andric if ((ACM->*GetArgDimension)() == *ParamConst) 21185ffd83dbSDimitry Andric return Sema::TDK_Success; 21195ffd83dbSDimitry Andric return Sema::TDK_NonDeducedMismatch; 21205ffd83dbSDimitry Andric } 21215ffd83dbSDimitry Andric 2122349cc55cSDimitry Andric Expr *ArgExpr = (ADM->*GetArgDimensionExpr)(); 2123bdd1243dSDimitry Andric if (std::optional<llvm::APSInt> ArgConst = 2124e8d8bef9SDimitry Andric ArgExpr->getIntegerConstantExpr(S.Context)) 2125e8d8bef9SDimitry Andric if (*ArgConst == *ParamConst) 21265ffd83dbSDimitry Andric return Sema::TDK_Success; 21275ffd83dbSDimitry Andric return Sema::TDK_NonDeducedMismatch; 21285ffd83dbSDimitry Andric } 21295ffd83dbSDimitry Andric 2130e8d8bef9SDimitry Andric const NonTypeTemplateParmDecl *NTTP = 21315ffd83dbSDimitry Andric getDeducedParameterFromExpr(Info, ParamExpr); 21325ffd83dbSDimitry Andric if (!NTTP) 21335ffd83dbSDimitry Andric return Sema::TDK_Success; 21345ffd83dbSDimitry Andric 2135349cc55cSDimitry Andric if (ACM) { 21365ffd83dbSDimitry Andric llvm::APSInt ArgConst( 21375ffd83dbSDimitry Andric S.Context.getTypeSize(S.Context.getSizeType())); 2138349cc55cSDimitry Andric ArgConst = (ACM->*GetArgDimension)(); 21395ffd83dbSDimitry Andric return DeduceNonTypeTemplateArgument( 21405ffd83dbSDimitry Andric S, TemplateParams, NTTP, ArgConst, S.Context.getSizeType(), 21415ffd83dbSDimitry Andric /*ArrayBound=*/true, Info, Deduced); 21425ffd83dbSDimitry Andric } 21435ffd83dbSDimitry Andric 2144349cc55cSDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, 2145349cc55cSDimitry Andric (ADM->*GetArgDimensionExpr)(), 21465ffd83dbSDimitry Andric Info, Deduced); 21475ffd83dbSDimitry Andric }; 21485ffd83dbSDimitry Andric 2149349cc55cSDimitry Andric if (auto Result = DeduceMatrixArg(MP->getRowExpr(), MA, 21505ffd83dbSDimitry Andric &ConstantMatrixType::getNumRows, 2151349cc55cSDimitry Andric &DependentSizedMatrixType::getRowExpr)) 21525ffd83dbSDimitry Andric return Result; 21535ffd83dbSDimitry Andric 2154349cc55cSDimitry Andric return DeduceMatrixArg(MP->getColumnExpr(), MA, 21555ffd83dbSDimitry Andric &ConstantMatrixType::getNumColumns, 21565ffd83dbSDimitry Andric &DependentSizedMatrixType::getColumnExpr); 21575ffd83dbSDimitry Andric } 21585ffd83dbSDimitry Andric 21595ffd83dbSDimitry Andric // (clang extension) 21605ffd83dbSDimitry Andric // 21610b57cec5SDimitry Andric // T __attribute__(((address_space(N)))) 21620b57cec5SDimitry Andric case Type::DependentAddressSpace: { 2163349cc55cSDimitry Andric const auto *ASP = P->castAs<DependentAddressSpaceType>(); 21640b57cec5SDimitry Andric 2165349cc55cSDimitry Andric if (const auto *ASA = A->getAs<DependentAddressSpaceType>()) { 21660b57cec5SDimitry Andric // Perform deduction on the pointer type. 2167349cc55cSDimitry Andric if (auto Result = DeduceTemplateArgumentsByTypeMatch( 2168349cc55cSDimitry Andric S, TemplateParams, ASP->getPointeeType(), ASA->getPointeeType(), 2169349cc55cSDimitry Andric Info, Deduced, TDF)) 21700b57cec5SDimitry Andric return Result; 21710b57cec5SDimitry Andric 21720b57cec5SDimitry Andric // Perform deduction on the address space, if we can. 2173349cc55cSDimitry Andric const NonTypeTemplateParmDecl *NTTP = 2174349cc55cSDimitry Andric getDeducedParameterFromExpr(Info, ASP->getAddrSpaceExpr()); 21750b57cec5SDimitry Andric if (!NTTP) 21760b57cec5SDimitry Andric return Sema::TDK_Success; 21770b57cec5SDimitry Andric 21780b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument( 2179349cc55cSDimitry Andric S, TemplateParams, NTTP, ASA->getAddrSpaceExpr(), Info, Deduced); 21800b57cec5SDimitry Andric } 21810b57cec5SDimitry Andric 2182349cc55cSDimitry Andric if (isTargetAddressSpace(A.getAddressSpace())) { 21830b57cec5SDimitry Andric llvm::APSInt ArgAddressSpace(S.Context.getTypeSize(S.Context.IntTy), 21840b57cec5SDimitry Andric false); 2185349cc55cSDimitry Andric ArgAddressSpace = toTargetAddressSpace(A.getAddressSpace()); 21860b57cec5SDimitry Andric 21870b57cec5SDimitry Andric // Perform deduction on the pointer types. 2188349cc55cSDimitry Andric if (auto Result = DeduceTemplateArgumentsByTypeMatch( 2189349cc55cSDimitry Andric S, TemplateParams, ASP->getPointeeType(), 2190349cc55cSDimitry Andric S.Context.removeAddrSpaceQualType(A), Info, Deduced, TDF)) 21910b57cec5SDimitry Andric return Result; 21920b57cec5SDimitry Andric 21930b57cec5SDimitry Andric // Perform deduction on the address space, if we can. 2194349cc55cSDimitry Andric const NonTypeTemplateParmDecl *NTTP = 2195349cc55cSDimitry Andric getDeducedParameterFromExpr(Info, ASP->getAddrSpaceExpr()); 21960b57cec5SDimitry Andric if (!NTTP) 21970b57cec5SDimitry Andric return Sema::TDK_Success; 21980b57cec5SDimitry Andric 21990b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, 22000b57cec5SDimitry Andric ArgAddressSpace, S.Context.IntTy, 22010b57cec5SDimitry Andric true, Info, Deduced); 22020b57cec5SDimitry Andric } 22030b57cec5SDimitry Andric 22040b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 22050b57cec5SDimitry Andric } 22060eae32dcSDimitry Andric case Type::DependentBitInt: { 22070eae32dcSDimitry Andric const auto *IP = P->castAs<DependentBitIntType>(); 22085ffd83dbSDimitry Andric 22090eae32dcSDimitry Andric if (const auto *IA = A->getAs<BitIntType>()) { 2210349cc55cSDimitry Andric if (IP->isUnsigned() != IA->isUnsigned()) 22115ffd83dbSDimitry Andric return Sema::TDK_NonDeducedMismatch; 22125ffd83dbSDimitry Andric 2213e8d8bef9SDimitry Andric const NonTypeTemplateParmDecl *NTTP = 2214349cc55cSDimitry Andric getDeducedParameterFromExpr(Info, IP->getNumBitsExpr()); 22155ffd83dbSDimitry Andric if (!NTTP) 22165ffd83dbSDimitry Andric return Sema::TDK_Success; 22175ffd83dbSDimitry Andric 22185ffd83dbSDimitry Andric llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); 2219349cc55cSDimitry Andric ArgSize = IA->getNumBits(); 22205ffd83dbSDimitry Andric 22215ffd83dbSDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, 22225ffd83dbSDimitry Andric S.Context.IntTy, true, Info, 22235ffd83dbSDimitry Andric Deduced); 22245ffd83dbSDimitry Andric } 22255ffd83dbSDimitry Andric 22260eae32dcSDimitry Andric if (const auto *IA = A->getAs<DependentBitIntType>()) { 2227349cc55cSDimitry Andric if (IP->isUnsigned() != IA->isUnsigned()) 22285ffd83dbSDimitry Andric return Sema::TDK_NonDeducedMismatch; 22295ffd83dbSDimitry Andric return Sema::TDK_Success; 22305ffd83dbSDimitry Andric } 2231349cc55cSDimitry Andric 22325ffd83dbSDimitry Andric return Sema::TDK_NonDeducedMismatch; 22335ffd83dbSDimitry Andric } 22340b57cec5SDimitry Andric 22350b57cec5SDimitry Andric case Type::TypeOfExpr: 22360b57cec5SDimitry Andric case Type::TypeOf: 22370b57cec5SDimitry Andric case Type::DependentName: 22380b57cec5SDimitry Andric case Type::UnresolvedUsing: 22390b57cec5SDimitry Andric case Type::Decltype: 22400b57cec5SDimitry Andric case Type::UnaryTransform: 22410b57cec5SDimitry Andric case Type::DeducedTemplateSpecialization: 22420b57cec5SDimitry Andric case Type::DependentTemplateSpecialization: 22430b57cec5SDimitry Andric case Type::PackExpansion: 22440b57cec5SDimitry Andric case Type::Pipe: 22450b57cec5SDimitry Andric // No template argument deduction for these types 22460b57cec5SDimitry Andric return Sema::TDK_Success; 22470b57cec5SDimitry Andric } 22480b57cec5SDimitry Andric 22490b57cec5SDimitry Andric llvm_unreachable("Invalid Type Class!"); 22500b57cec5SDimitry Andric } 22510b57cec5SDimitry Andric 22520b57cec5SDimitry Andric static Sema::TemplateDeductionResult 2253349cc55cSDimitry Andric DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, 2254349cc55cSDimitry Andric const TemplateArgument &P, TemplateArgument A, 22550b57cec5SDimitry Andric TemplateDeductionInfo &Info, 22560b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 22570b57cec5SDimitry Andric // If the template argument is a pack expansion, perform template argument 22580b57cec5SDimitry Andric // deduction against the pattern of that expansion. This only occurs during 22590b57cec5SDimitry Andric // partial ordering. 2260349cc55cSDimitry Andric if (A.isPackExpansion()) 2261349cc55cSDimitry Andric A = A.getPackExpansionPattern(); 22620b57cec5SDimitry Andric 2263349cc55cSDimitry Andric switch (P.getKind()) { 22640b57cec5SDimitry Andric case TemplateArgument::Null: 22650b57cec5SDimitry Andric llvm_unreachable("Null template argument in parameter list"); 22660b57cec5SDimitry Andric 22670b57cec5SDimitry Andric case TemplateArgument::Type: 2268349cc55cSDimitry Andric if (A.getKind() == TemplateArgument::Type) 2269349cc55cSDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 2270349cc55cSDimitry Andric S, TemplateParams, P.getAsType(), A.getAsType(), Info, Deduced, 0); 2271349cc55cSDimitry Andric Info.FirstArg = P; 2272349cc55cSDimitry Andric Info.SecondArg = A; 22730b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 22740b57cec5SDimitry Andric 22750b57cec5SDimitry Andric case TemplateArgument::Template: 2276349cc55cSDimitry Andric if (A.getKind() == TemplateArgument::Template) 2277349cc55cSDimitry Andric return DeduceTemplateArguments(S, TemplateParams, P.getAsTemplate(), 2278349cc55cSDimitry Andric A.getAsTemplate(), Info, Deduced); 2279349cc55cSDimitry Andric Info.FirstArg = P; 2280349cc55cSDimitry Andric Info.SecondArg = A; 22810b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 22820b57cec5SDimitry Andric 22830b57cec5SDimitry Andric case TemplateArgument::TemplateExpansion: 22840b57cec5SDimitry Andric llvm_unreachable("caller should handle pack expansions"); 22850b57cec5SDimitry Andric 22860b57cec5SDimitry Andric case TemplateArgument::Declaration: 2287349cc55cSDimitry Andric if (A.getKind() == TemplateArgument::Declaration && 2288349cc55cSDimitry Andric isSameDeclaration(P.getAsDecl(), A.getAsDecl())) 22890b57cec5SDimitry Andric return Sema::TDK_Success; 22900b57cec5SDimitry Andric 2291349cc55cSDimitry Andric Info.FirstArg = P; 2292349cc55cSDimitry Andric Info.SecondArg = A; 22930b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 22940b57cec5SDimitry Andric 22950b57cec5SDimitry Andric case TemplateArgument::NullPtr: 2296349cc55cSDimitry Andric if (A.getKind() == TemplateArgument::NullPtr && 2297349cc55cSDimitry Andric S.Context.hasSameType(P.getNullPtrType(), A.getNullPtrType())) 22980b57cec5SDimitry Andric return Sema::TDK_Success; 22990b57cec5SDimitry Andric 2300349cc55cSDimitry Andric Info.FirstArg = P; 2301349cc55cSDimitry Andric Info.SecondArg = A; 23020b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 23030b57cec5SDimitry Andric 23040b57cec5SDimitry Andric case TemplateArgument::Integral: 2305349cc55cSDimitry Andric if (A.getKind() == TemplateArgument::Integral) { 2306349cc55cSDimitry Andric if (hasSameExtendedValue(P.getAsIntegral(), A.getAsIntegral())) 23070b57cec5SDimitry Andric return Sema::TDK_Success; 23080b57cec5SDimitry Andric } 2309349cc55cSDimitry Andric Info.FirstArg = P; 2310349cc55cSDimitry Andric Info.SecondArg = A; 23110b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 23120b57cec5SDimitry Andric 2313*7a6dacacSDimitry Andric case TemplateArgument::StructuralValue: 2314*7a6dacacSDimitry Andric if (A.getKind() == TemplateArgument::StructuralValue && 2315*7a6dacacSDimitry Andric A.structurallyEquals(P)) 2316*7a6dacacSDimitry Andric return Sema::TDK_Success; 23170b57cec5SDimitry Andric 2318349cc55cSDimitry Andric Info.FirstArg = P; 2319349cc55cSDimitry Andric Info.SecondArg = A; 23200b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 2321*7a6dacacSDimitry Andric 2322*7a6dacacSDimitry Andric case TemplateArgument::Expression: 2323*7a6dacacSDimitry Andric if (const NonTypeTemplateParmDecl *NTTP = 2324*7a6dacacSDimitry Andric getDeducedParameterFromExpr(Info, P.getAsExpr())) { 2325*7a6dacacSDimitry Andric switch (A.getKind()) { 2326*7a6dacacSDimitry Andric case TemplateArgument::Integral: 2327*7a6dacacSDimitry Andric case TemplateArgument::Expression: 2328*7a6dacacSDimitry Andric case TemplateArgument::StructuralValue: 2329*7a6dacacSDimitry Andric return DeduceNonTypeTemplateArgument( 2330*7a6dacacSDimitry Andric S, TemplateParams, NTTP, DeducedTemplateArgument(A), 2331*7a6dacacSDimitry Andric A.getNonTypeTemplateArgumentType(), Info, Deduced); 2332*7a6dacacSDimitry Andric 2333*7a6dacacSDimitry Andric case TemplateArgument::NullPtr: 2334*7a6dacacSDimitry Andric return DeduceNullPtrTemplateArgument(S, TemplateParams, NTTP, 2335*7a6dacacSDimitry Andric A.getNullPtrType(), Info, Deduced); 2336*7a6dacacSDimitry Andric 2337*7a6dacacSDimitry Andric case TemplateArgument::Declaration: 2338*7a6dacacSDimitry Andric return DeduceNonTypeTemplateArgument( 2339*7a6dacacSDimitry Andric S, TemplateParams, NTTP, A.getAsDecl(), A.getParamTypeForDecl(), 2340*7a6dacacSDimitry Andric Info, Deduced); 2341*7a6dacacSDimitry Andric 2342*7a6dacacSDimitry Andric case TemplateArgument::Null: 2343*7a6dacacSDimitry Andric case TemplateArgument::Type: 2344*7a6dacacSDimitry Andric case TemplateArgument::Template: 2345*7a6dacacSDimitry Andric case TemplateArgument::TemplateExpansion: 2346*7a6dacacSDimitry Andric case TemplateArgument::Pack: 2347*7a6dacacSDimitry Andric Info.FirstArg = P; 2348*7a6dacacSDimitry Andric Info.SecondArg = A; 2349*7a6dacacSDimitry Andric return Sema::TDK_NonDeducedMismatch; 2350*7a6dacacSDimitry Andric } 2351*7a6dacacSDimitry Andric llvm_unreachable("Unknown template argument kind"); 23520b57cec5SDimitry Andric } 23530b57cec5SDimitry Andric 23540b57cec5SDimitry Andric // Can't deduce anything, but that's okay. 23550b57cec5SDimitry Andric return Sema::TDK_Success; 23560b57cec5SDimitry Andric case TemplateArgument::Pack: 23570b57cec5SDimitry Andric llvm_unreachable("Argument packs should be expanded by the caller!"); 23580b57cec5SDimitry Andric } 23590b57cec5SDimitry Andric 23600b57cec5SDimitry Andric llvm_unreachable("Invalid TemplateArgument Kind!"); 23610b57cec5SDimitry Andric } 23620b57cec5SDimitry Andric 23630b57cec5SDimitry Andric /// Determine whether there is a template argument to be used for 23640b57cec5SDimitry Andric /// deduction. 23650b57cec5SDimitry Andric /// 23660b57cec5SDimitry Andric /// This routine "expands" argument packs in-place, overriding its input 23670b57cec5SDimitry Andric /// parameters so that \c Args[ArgIdx] will be the available template argument. 23680b57cec5SDimitry Andric /// 23690b57cec5SDimitry Andric /// \returns true if there is another template argument (which will be at 23700b57cec5SDimitry Andric /// \c Args[ArgIdx]), false otherwise. 23710b57cec5SDimitry Andric static bool hasTemplateArgumentForDeduction(ArrayRef<TemplateArgument> &Args, 23720b57cec5SDimitry Andric unsigned &ArgIdx) { 23730b57cec5SDimitry Andric if (ArgIdx == Args.size()) 23740b57cec5SDimitry Andric return false; 23750b57cec5SDimitry Andric 23760b57cec5SDimitry Andric const TemplateArgument &Arg = Args[ArgIdx]; 23770b57cec5SDimitry Andric if (Arg.getKind() != TemplateArgument::Pack) 23780b57cec5SDimitry Andric return true; 23790b57cec5SDimitry Andric 23800b57cec5SDimitry Andric assert(ArgIdx == Args.size() - 1 && "Pack not at the end of argument list?"); 23810b57cec5SDimitry Andric Args = Arg.pack_elements(); 23820b57cec5SDimitry Andric ArgIdx = 0; 23830b57cec5SDimitry Andric return ArgIdx < Args.size(); 23840b57cec5SDimitry Andric } 23850b57cec5SDimitry Andric 23860b57cec5SDimitry Andric /// Determine whether the given set of template arguments has a pack 23870b57cec5SDimitry Andric /// expansion that is not the last template argument. 23880b57cec5SDimitry Andric static bool hasPackExpansionBeforeEnd(ArrayRef<TemplateArgument> Args) { 23890b57cec5SDimitry Andric bool FoundPackExpansion = false; 23900b57cec5SDimitry Andric for (const auto &A : Args) { 23910b57cec5SDimitry Andric if (FoundPackExpansion) 23920b57cec5SDimitry Andric return true; 23930b57cec5SDimitry Andric 23940b57cec5SDimitry Andric if (A.getKind() == TemplateArgument::Pack) 23950b57cec5SDimitry Andric return hasPackExpansionBeforeEnd(A.pack_elements()); 23960b57cec5SDimitry Andric 23970b57cec5SDimitry Andric // FIXME: If this is a fixed-arity pack expansion from an outer level of 23980b57cec5SDimitry Andric // templates, it should not be treated as a pack expansion. 23990b57cec5SDimitry Andric if (A.isPackExpansion()) 24000b57cec5SDimitry Andric FoundPackExpansion = true; 24010b57cec5SDimitry Andric } 24020b57cec5SDimitry Andric 24030b57cec5SDimitry Andric return false; 24040b57cec5SDimitry Andric } 24050b57cec5SDimitry Andric 24060b57cec5SDimitry Andric static Sema::TemplateDeductionResult 24070b57cec5SDimitry Andric DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, 2408349cc55cSDimitry Andric ArrayRef<TemplateArgument> Ps, 2409349cc55cSDimitry Andric ArrayRef<TemplateArgument> As, 24100b57cec5SDimitry Andric TemplateDeductionInfo &Info, 24110b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 24120b57cec5SDimitry Andric bool NumberOfArgumentsMustMatch) { 24130b57cec5SDimitry Andric // C++0x [temp.deduct.type]p9: 24140b57cec5SDimitry Andric // If the template argument list of P contains a pack expansion that is not 24150b57cec5SDimitry Andric // the last template argument, the entire template argument list is a 24160b57cec5SDimitry Andric // non-deduced context. 2417349cc55cSDimitry Andric if (hasPackExpansionBeforeEnd(Ps)) 24180b57cec5SDimitry Andric return Sema::TDK_Success; 24190b57cec5SDimitry Andric 24200b57cec5SDimitry Andric // C++0x [temp.deduct.type]p9: 24210b57cec5SDimitry Andric // If P has a form that contains <T> or <i>, then each argument Pi of the 24220b57cec5SDimitry Andric // respective template argument list P is compared with the corresponding 24230b57cec5SDimitry Andric // argument Ai of the corresponding template argument list of A. 24240b57cec5SDimitry Andric unsigned ArgIdx = 0, ParamIdx = 0; 2425349cc55cSDimitry Andric for (; hasTemplateArgumentForDeduction(Ps, ParamIdx); ++ParamIdx) { 2426349cc55cSDimitry Andric const TemplateArgument &P = Ps[ParamIdx]; 2427349cc55cSDimitry Andric if (!P.isPackExpansion()) { 24280b57cec5SDimitry Andric // The simple case: deduce template arguments by matching Pi and Ai. 24290b57cec5SDimitry Andric 24300b57cec5SDimitry Andric // Check whether we have enough arguments. 2431349cc55cSDimitry Andric if (!hasTemplateArgumentForDeduction(As, ArgIdx)) 24320b57cec5SDimitry Andric return NumberOfArgumentsMustMatch 24330b57cec5SDimitry Andric ? Sema::TDK_MiscellaneousDeductionFailure 24340b57cec5SDimitry Andric : Sema::TDK_Success; 24350b57cec5SDimitry Andric 24360b57cec5SDimitry Andric // C++1z [temp.deduct.type]p9: 24370b57cec5SDimitry Andric // During partial ordering, if Ai was originally a pack expansion [and] 24380b57cec5SDimitry Andric // Pi is not a pack expansion, template argument deduction fails. 2439349cc55cSDimitry Andric if (As[ArgIdx].isPackExpansion()) 24400b57cec5SDimitry Andric return Sema::TDK_MiscellaneousDeductionFailure; 24410b57cec5SDimitry Andric 24420b57cec5SDimitry Andric // Perform deduction for this Pi/Ai pair. 2443349cc55cSDimitry Andric if (auto Result = DeduceTemplateArguments(S, TemplateParams, P, 2444349cc55cSDimitry Andric As[ArgIdx], Info, Deduced)) 24450b57cec5SDimitry Andric return Result; 24460b57cec5SDimitry Andric 24470b57cec5SDimitry Andric // Move to the next argument. 24480b57cec5SDimitry Andric ++ArgIdx; 24490b57cec5SDimitry Andric continue; 24500b57cec5SDimitry Andric } 24510b57cec5SDimitry Andric 24520b57cec5SDimitry Andric // The parameter is a pack expansion. 24530b57cec5SDimitry Andric 24540b57cec5SDimitry Andric // C++0x [temp.deduct.type]p9: 24550b57cec5SDimitry Andric // If Pi is a pack expansion, then the pattern of Pi is compared with 24560b57cec5SDimitry Andric // each remaining argument in the template argument list of A. Each 24570b57cec5SDimitry Andric // comparison deduces template arguments for subsequent positions in the 24580b57cec5SDimitry Andric // template parameter packs expanded by Pi. 2459349cc55cSDimitry Andric TemplateArgument Pattern = P.getPackExpansionPattern(); 24600b57cec5SDimitry Andric 24610b57cec5SDimitry Andric // Prepare to deduce the packs within the pattern. 24620b57cec5SDimitry Andric PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); 24630b57cec5SDimitry Andric 24640b57cec5SDimitry Andric // Keep track of the deduced template arguments for each parameter pack 24650b57cec5SDimitry Andric // expanded by this pack expansion (the outer index) and for each 24660b57cec5SDimitry Andric // template argument (the inner SmallVectors). 2467349cc55cSDimitry Andric for (; hasTemplateArgumentForDeduction(As, ArgIdx) && 24680b57cec5SDimitry Andric PackScope.hasNextElement(); 24690b57cec5SDimitry Andric ++ArgIdx) { 24700b57cec5SDimitry Andric // Deduce template arguments from the pattern. 2471349cc55cSDimitry Andric if (auto Result = DeduceTemplateArguments(S, TemplateParams, Pattern, 2472349cc55cSDimitry Andric As[ArgIdx], Info, Deduced)) 24730b57cec5SDimitry Andric return Result; 24740b57cec5SDimitry Andric 24750b57cec5SDimitry Andric PackScope.nextPackElement(); 24760b57cec5SDimitry Andric } 24770b57cec5SDimitry Andric 24780b57cec5SDimitry Andric // Build argument packs for each of the parameter packs expanded by this 24790b57cec5SDimitry Andric // pack expansion. 24800b57cec5SDimitry Andric if (auto Result = PackScope.finish()) 24810b57cec5SDimitry Andric return Result; 24820b57cec5SDimitry Andric } 24830b57cec5SDimitry Andric 24840b57cec5SDimitry Andric return Sema::TDK_Success; 24850b57cec5SDimitry Andric } 24860b57cec5SDimitry Andric 24870b57cec5SDimitry Andric static Sema::TemplateDeductionResult 2488349cc55cSDimitry Andric DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, 24890b57cec5SDimitry Andric const TemplateArgumentList &ParamList, 24900b57cec5SDimitry Andric const TemplateArgumentList &ArgList, 24910b57cec5SDimitry Andric TemplateDeductionInfo &Info, 24920b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 24930b57cec5SDimitry Andric return DeduceTemplateArguments(S, TemplateParams, ParamList.asArray(), 24940b57cec5SDimitry Andric ArgList.asArray(), Info, Deduced, 2495349cc55cSDimitry Andric /*NumberOfArgumentsMustMatch=*/false); 24960b57cec5SDimitry Andric } 24970b57cec5SDimitry Andric 24980b57cec5SDimitry Andric /// Determine whether two template arguments are the same. 24990b57cec5SDimitry Andric static bool isSameTemplateArg(ASTContext &Context, 25000b57cec5SDimitry Andric TemplateArgument X, 25010b57cec5SDimitry Andric const TemplateArgument &Y, 2502bdd1243dSDimitry Andric bool PartialOrdering, 25030b57cec5SDimitry Andric bool PackExpansionMatchesPack = false) { 25040b57cec5SDimitry Andric // If we're checking deduced arguments (X) against original arguments (Y), 25050b57cec5SDimitry Andric // we will have flattened packs to non-expansions in X. 25060b57cec5SDimitry Andric if (PackExpansionMatchesPack && X.isPackExpansion() && !Y.isPackExpansion()) 25070b57cec5SDimitry Andric X = X.getPackExpansionPattern(); 25080b57cec5SDimitry Andric 25090b57cec5SDimitry Andric if (X.getKind() != Y.getKind()) 25100b57cec5SDimitry Andric return false; 25110b57cec5SDimitry Andric 25120b57cec5SDimitry Andric switch (X.getKind()) { 25130b57cec5SDimitry Andric case TemplateArgument::Null: 25140b57cec5SDimitry Andric llvm_unreachable("Comparing NULL template argument"); 25150b57cec5SDimitry Andric 25160b57cec5SDimitry Andric case TemplateArgument::Type: 25170b57cec5SDimitry Andric return Context.getCanonicalType(X.getAsType()) == 25180b57cec5SDimitry Andric Context.getCanonicalType(Y.getAsType()); 25190b57cec5SDimitry Andric 25200b57cec5SDimitry Andric case TemplateArgument::Declaration: 25210b57cec5SDimitry Andric return isSameDeclaration(X.getAsDecl(), Y.getAsDecl()); 25220b57cec5SDimitry Andric 25230b57cec5SDimitry Andric case TemplateArgument::NullPtr: 25240b57cec5SDimitry Andric return Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType()); 25250b57cec5SDimitry Andric 25260b57cec5SDimitry Andric case TemplateArgument::Template: 25270b57cec5SDimitry Andric case TemplateArgument::TemplateExpansion: 25280b57cec5SDimitry Andric return Context.getCanonicalTemplateName( 25290b57cec5SDimitry Andric X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() == 25300b57cec5SDimitry Andric Context.getCanonicalTemplateName( 25310b57cec5SDimitry Andric Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer(); 25320b57cec5SDimitry Andric 25330b57cec5SDimitry Andric case TemplateArgument::Integral: 25340b57cec5SDimitry Andric return hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()); 25350b57cec5SDimitry Andric 2536*7a6dacacSDimitry Andric case TemplateArgument::StructuralValue: 2537*7a6dacacSDimitry Andric return X.structurallyEquals(Y); 2538*7a6dacacSDimitry Andric 25390b57cec5SDimitry Andric case TemplateArgument::Expression: { 25400b57cec5SDimitry Andric llvm::FoldingSetNodeID XID, YID; 25410b57cec5SDimitry Andric X.getAsExpr()->Profile(XID, Context, true); 25420b57cec5SDimitry Andric Y.getAsExpr()->Profile(YID, Context, true); 25430b57cec5SDimitry Andric return XID == YID; 25440b57cec5SDimitry Andric } 25450b57cec5SDimitry Andric 2546bdd1243dSDimitry Andric case TemplateArgument::Pack: { 2547bdd1243dSDimitry Andric unsigned PackIterationSize = X.pack_size(); 2548bdd1243dSDimitry Andric if (X.pack_size() != Y.pack_size()) { 2549bdd1243dSDimitry Andric if (!PartialOrdering) 25500b57cec5SDimitry Andric return false; 25510b57cec5SDimitry Andric 2552bdd1243dSDimitry Andric // C++0x [temp.deduct.type]p9: 2553bdd1243dSDimitry Andric // During partial ordering, if Ai was originally a pack expansion: 2554bdd1243dSDimitry Andric // - if P does not contain a template argument corresponding to Ai 2555bdd1243dSDimitry Andric // then Ai is ignored; 2556bdd1243dSDimitry Andric bool XHasMoreArg = X.pack_size() > Y.pack_size(); 2557bdd1243dSDimitry Andric if (!(XHasMoreArg && X.pack_elements().back().isPackExpansion()) && 2558bdd1243dSDimitry Andric !(!XHasMoreArg && Y.pack_elements().back().isPackExpansion())) 25590b57cec5SDimitry Andric return false; 25600b57cec5SDimitry Andric 2561bdd1243dSDimitry Andric if (XHasMoreArg) 2562bdd1243dSDimitry Andric PackIterationSize = Y.pack_size(); 2563bdd1243dSDimitry Andric } 2564bdd1243dSDimitry Andric 2565bdd1243dSDimitry Andric ArrayRef<TemplateArgument> XP = X.pack_elements(); 2566bdd1243dSDimitry Andric ArrayRef<TemplateArgument> YP = Y.pack_elements(); 2567bdd1243dSDimitry Andric for (unsigned i = 0; i < PackIterationSize; ++i) 2568bdd1243dSDimitry Andric if (!isSameTemplateArg(Context, XP[i], YP[i], PartialOrdering, 2569bdd1243dSDimitry Andric PackExpansionMatchesPack)) 2570bdd1243dSDimitry Andric return false; 25710b57cec5SDimitry Andric return true; 25720b57cec5SDimitry Andric } 2573bdd1243dSDimitry Andric } 25740b57cec5SDimitry Andric 25750b57cec5SDimitry Andric llvm_unreachable("Invalid TemplateArgument Kind!"); 25760b57cec5SDimitry Andric } 25770b57cec5SDimitry Andric 25780b57cec5SDimitry Andric /// Allocate a TemplateArgumentLoc where all locations have 25790b57cec5SDimitry Andric /// been initialized to the given location. 25800b57cec5SDimitry Andric /// 25810b57cec5SDimitry Andric /// \param Arg The template argument we are producing template argument 25820b57cec5SDimitry Andric /// location information for. 25830b57cec5SDimitry Andric /// 25840b57cec5SDimitry Andric /// \param NTTPType For a declaration template argument, the type of 25850b57cec5SDimitry Andric /// the non-type template parameter that corresponds to this template 25860b57cec5SDimitry Andric /// argument. Can be null if no type sugar is available to add to the 25870b57cec5SDimitry Andric /// type from the template argument. 25880b57cec5SDimitry Andric /// 25890b57cec5SDimitry Andric /// \param Loc The source location to use for the resulting template 25900b57cec5SDimitry Andric /// argument. 25910b57cec5SDimitry Andric TemplateArgumentLoc 25920b57cec5SDimitry Andric Sema::getTrivialTemplateArgumentLoc(const TemplateArgument &Arg, 25930b57cec5SDimitry Andric QualType NTTPType, SourceLocation Loc) { 25940b57cec5SDimitry Andric switch (Arg.getKind()) { 25950b57cec5SDimitry Andric case TemplateArgument::Null: 25960b57cec5SDimitry Andric llvm_unreachable("Can't get a NULL template argument here"); 25970b57cec5SDimitry Andric 25980b57cec5SDimitry Andric case TemplateArgument::Type: 25990b57cec5SDimitry Andric return TemplateArgumentLoc( 26000b57cec5SDimitry Andric Arg, Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc)); 26010b57cec5SDimitry Andric 26020b57cec5SDimitry Andric case TemplateArgument::Declaration: { 26030b57cec5SDimitry Andric if (NTTPType.isNull()) 26040b57cec5SDimitry Andric NTTPType = Arg.getParamTypeForDecl(); 26050b57cec5SDimitry Andric Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) 26060b57cec5SDimitry Andric .getAs<Expr>(); 26070b57cec5SDimitry Andric return TemplateArgumentLoc(TemplateArgument(E), E); 26080b57cec5SDimitry Andric } 26090b57cec5SDimitry Andric 26100b57cec5SDimitry Andric case TemplateArgument::NullPtr: { 26110b57cec5SDimitry Andric if (NTTPType.isNull()) 26120b57cec5SDimitry Andric NTTPType = Arg.getNullPtrType(); 26130b57cec5SDimitry Andric Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) 26140b57cec5SDimitry Andric .getAs<Expr>(); 26150b57cec5SDimitry Andric return TemplateArgumentLoc(TemplateArgument(NTTPType, /*isNullPtr*/true), 26160b57cec5SDimitry Andric E); 26170b57cec5SDimitry Andric } 26180b57cec5SDimitry Andric 2619*7a6dacacSDimitry Andric case TemplateArgument::Integral: 2620*7a6dacacSDimitry Andric case TemplateArgument::StructuralValue: { 2621*7a6dacacSDimitry Andric Expr *E = BuildExpressionFromNonTypeTemplateArgument(Arg, Loc).get(); 26220b57cec5SDimitry Andric return TemplateArgumentLoc(TemplateArgument(E), E); 26230b57cec5SDimitry Andric } 26240b57cec5SDimitry Andric 26250b57cec5SDimitry Andric case TemplateArgument::Template: 26260b57cec5SDimitry Andric case TemplateArgument::TemplateExpansion: { 26270b57cec5SDimitry Andric NestedNameSpecifierLocBuilder Builder; 262813138422SDimitry Andric TemplateName Template = Arg.getAsTemplateOrTemplatePattern(); 26290b57cec5SDimitry Andric if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) 26300b57cec5SDimitry Andric Builder.MakeTrivial(Context, DTN->getQualifier(), Loc); 26310b57cec5SDimitry Andric else if (QualifiedTemplateName *QTN = 26320b57cec5SDimitry Andric Template.getAsQualifiedTemplateName()) 26330b57cec5SDimitry Andric Builder.MakeTrivial(Context, QTN->getQualifier(), Loc); 26340b57cec5SDimitry Andric 26350b57cec5SDimitry Andric if (Arg.getKind() == TemplateArgument::Template) 2636e8d8bef9SDimitry Andric return TemplateArgumentLoc(Context, Arg, 2637e8d8bef9SDimitry Andric Builder.getWithLocInContext(Context), Loc); 26380b57cec5SDimitry Andric 2639e8d8bef9SDimitry Andric return TemplateArgumentLoc( 2640e8d8bef9SDimitry Andric Context, Arg, Builder.getWithLocInContext(Context), Loc, Loc); 26410b57cec5SDimitry Andric } 26420b57cec5SDimitry Andric 26430b57cec5SDimitry Andric case TemplateArgument::Expression: 26440b57cec5SDimitry Andric return TemplateArgumentLoc(Arg, Arg.getAsExpr()); 26450b57cec5SDimitry Andric 26460b57cec5SDimitry Andric case TemplateArgument::Pack: 26470b57cec5SDimitry Andric return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo()); 26480b57cec5SDimitry Andric } 26490b57cec5SDimitry Andric 26500b57cec5SDimitry Andric llvm_unreachable("Invalid TemplateArgument Kind!"); 26510b57cec5SDimitry Andric } 26520b57cec5SDimitry Andric 2653480093f4SDimitry Andric TemplateArgumentLoc 265413138422SDimitry Andric Sema::getIdentityTemplateArgumentLoc(NamedDecl *TemplateParm, 2655480093f4SDimitry Andric SourceLocation Location) { 2656480093f4SDimitry Andric return getTrivialTemplateArgumentLoc( 265713138422SDimitry Andric Context.getInjectedTemplateArg(TemplateParm), QualType(), Location); 2658480093f4SDimitry Andric } 2659480093f4SDimitry Andric 26600b57cec5SDimitry Andric /// Convert the given deduced template argument and add it to the set of 26610b57cec5SDimitry Andric /// fully-converted template arguments. 2662bdd1243dSDimitry Andric static bool ConvertDeducedTemplateArgument( 2663bdd1243dSDimitry Andric Sema &S, NamedDecl *Param, DeducedTemplateArgument Arg, NamedDecl *Template, 2664bdd1243dSDimitry Andric TemplateDeductionInfo &Info, bool IsDeduced, 2665bdd1243dSDimitry Andric SmallVectorImpl<TemplateArgument> &SugaredOutput, 2666bdd1243dSDimitry Andric SmallVectorImpl<TemplateArgument> &CanonicalOutput) { 26670b57cec5SDimitry Andric auto ConvertArg = [&](DeducedTemplateArgument Arg, 26680b57cec5SDimitry Andric unsigned ArgumentPackIndex) { 26690b57cec5SDimitry Andric // Convert the deduced template argument into a template 26700b57cec5SDimitry Andric // argument that we can check, almost as if the user had written 26710b57cec5SDimitry Andric // the template argument explicitly. 26720b57cec5SDimitry Andric TemplateArgumentLoc ArgLoc = 26730b57cec5SDimitry Andric S.getTrivialTemplateArgumentLoc(Arg, QualType(), Info.getLocation()); 26740b57cec5SDimitry Andric 26750b57cec5SDimitry Andric // Check the template argument, converting it as necessary. 26760b57cec5SDimitry Andric return S.CheckTemplateArgument( 26770b57cec5SDimitry Andric Param, ArgLoc, Template, Template->getLocation(), 2678bdd1243dSDimitry Andric Template->getSourceRange().getEnd(), ArgumentPackIndex, SugaredOutput, 2679bdd1243dSDimitry Andric CanonicalOutput, 26800b57cec5SDimitry Andric IsDeduced 26810b57cec5SDimitry Andric ? (Arg.wasDeducedFromArrayBound() ? Sema::CTAK_DeducedFromArrayBound 26820b57cec5SDimitry Andric : Sema::CTAK_Deduced) 26830b57cec5SDimitry Andric : Sema::CTAK_Specified); 26840b57cec5SDimitry Andric }; 26850b57cec5SDimitry Andric 26860b57cec5SDimitry Andric if (Arg.getKind() == TemplateArgument::Pack) { 26870b57cec5SDimitry Andric // This is a template argument pack, so check each of its arguments against 26880b57cec5SDimitry Andric // the template parameter. 2689bdd1243dSDimitry Andric SmallVector<TemplateArgument, 2> SugaredPackedArgsBuilder, 2690bdd1243dSDimitry Andric CanonicalPackedArgsBuilder; 26910b57cec5SDimitry Andric for (const auto &P : Arg.pack_elements()) { 26920b57cec5SDimitry Andric // When converting the deduced template argument, append it to the 26930b57cec5SDimitry Andric // general output list. We need to do this so that the template argument 26940b57cec5SDimitry Andric // checking logic has all of the prior template arguments available. 26950b57cec5SDimitry Andric DeducedTemplateArgument InnerArg(P); 26960b57cec5SDimitry Andric InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound()); 26970b57cec5SDimitry Andric assert(InnerArg.getKind() != TemplateArgument::Pack && 26980b57cec5SDimitry Andric "deduced nested pack"); 26990b57cec5SDimitry Andric if (P.isNull()) { 27000b57cec5SDimitry Andric // We deduced arguments for some elements of this pack, but not for 27010b57cec5SDimitry Andric // all of them. This happens if we get a conditionally-non-deduced 27020b57cec5SDimitry Andric // context in a pack expansion (such as an overload set in one of the 27030b57cec5SDimitry Andric // arguments). 27040b57cec5SDimitry Andric S.Diag(Param->getLocation(), 27050b57cec5SDimitry Andric diag::err_template_arg_deduced_incomplete_pack) 27060b57cec5SDimitry Andric << Arg << Param; 27070b57cec5SDimitry Andric return true; 27080b57cec5SDimitry Andric } 2709bdd1243dSDimitry Andric if (ConvertArg(InnerArg, SugaredPackedArgsBuilder.size())) 27100b57cec5SDimitry Andric return true; 27110b57cec5SDimitry Andric 27120b57cec5SDimitry Andric // Move the converted template argument into our argument pack. 2713bdd1243dSDimitry Andric SugaredPackedArgsBuilder.push_back(SugaredOutput.pop_back_val()); 2714bdd1243dSDimitry Andric CanonicalPackedArgsBuilder.push_back(CanonicalOutput.pop_back_val()); 27150b57cec5SDimitry Andric } 27160b57cec5SDimitry Andric 27170b57cec5SDimitry Andric // If the pack is empty, we still need to substitute into the parameter 27180b57cec5SDimitry Andric // itself, in case that substitution fails. 2719bdd1243dSDimitry Andric if (SugaredPackedArgsBuilder.empty()) { 27200b57cec5SDimitry Andric LocalInstantiationScope Scope(S); 2721bdd1243dSDimitry Andric MultiLevelTemplateArgumentList Args(Template, SugaredOutput, 2722bdd1243dSDimitry Andric /*Final=*/true); 27230b57cec5SDimitry Andric 27240b57cec5SDimitry Andric if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { 27250b57cec5SDimitry Andric Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, 2726bdd1243dSDimitry Andric NTTP, SugaredOutput, 27270b57cec5SDimitry Andric Template->getSourceRange()); 27280b57cec5SDimitry Andric if (Inst.isInvalid() || 27290b57cec5SDimitry Andric S.SubstType(NTTP->getType(), Args, NTTP->getLocation(), 27300b57cec5SDimitry Andric NTTP->getDeclName()).isNull()) 27310b57cec5SDimitry Andric return true; 27320b57cec5SDimitry Andric } else if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param)) { 27330b57cec5SDimitry Andric Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, 2734bdd1243dSDimitry Andric TTP, SugaredOutput, 27350b57cec5SDimitry Andric Template->getSourceRange()); 27360b57cec5SDimitry Andric if (Inst.isInvalid() || !S.SubstDecl(TTP, S.CurContext, Args)) 27370b57cec5SDimitry Andric return true; 27380b57cec5SDimitry Andric } 27390b57cec5SDimitry Andric // For type parameters, no substitution is ever required. 27400b57cec5SDimitry Andric } 27410b57cec5SDimitry Andric 27420b57cec5SDimitry Andric // Create the resulting argument pack. 2743bdd1243dSDimitry Andric SugaredOutput.push_back( 2744bdd1243dSDimitry Andric TemplateArgument::CreatePackCopy(S.Context, SugaredPackedArgsBuilder)); 2745bdd1243dSDimitry Andric CanonicalOutput.push_back(TemplateArgument::CreatePackCopy( 2746bdd1243dSDimitry Andric S.Context, CanonicalPackedArgsBuilder)); 27470b57cec5SDimitry Andric return false; 27480b57cec5SDimitry Andric } 27490b57cec5SDimitry Andric 27500b57cec5SDimitry Andric return ConvertArg(Arg, 0); 27510b57cec5SDimitry Andric } 27520b57cec5SDimitry Andric 27530b57cec5SDimitry Andric // FIXME: This should not be a template, but 27540b57cec5SDimitry Andric // ClassTemplatePartialSpecializationDecl sadly does not derive from 27550b57cec5SDimitry Andric // TemplateDecl. 27560b57cec5SDimitry Andric template <typename TemplateDeclT> 27570b57cec5SDimitry Andric static Sema::TemplateDeductionResult ConvertDeducedTemplateArguments( 27580b57cec5SDimitry Andric Sema &S, TemplateDeclT *Template, bool IsDeduced, 27590b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 2760bdd1243dSDimitry Andric TemplateDeductionInfo &Info, 2761bdd1243dSDimitry Andric SmallVectorImpl<TemplateArgument> &SugaredBuilder, 2762bdd1243dSDimitry Andric SmallVectorImpl<TemplateArgument> &CanonicalBuilder, 27630b57cec5SDimitry Andric LocalInstantiationScope *CurrentInstantiationScope = nullptr, 27640b57cec5SDimitry Andric unsigned NumAlreadyConverted = 0, bool PartialOverloading = false) { 27650b57cec5SDimitry Andric TemplateParameterList *TemplateParams = Template->getTemplateParameters(); 27660b57cec5SDimitry Andric 27670b57cec5SDimitry Andric for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { 27680b57cec5SDimitry Andric NamedDecl *Param = TemplateParams->getParam(I); 27690b57cec5SDimitry Andric 27700b57cec5SDimitry Andric // C++0x [temp.arg.explicit]p3: 27710b57cec5SDimitry Andric // A trailing template parameter pack (14.5.3) not otherwise deduced will 27720b57cec5SDimitry Andric // be deduced to an empty sequence of template arguments. 27730b57cec5SDimitry Andric // FIXME: Where did the word "trailing" come from? 27740b57cec5SDimitry Andric if (Deduced[I].isNull() && Param->isTemplateParameterPack()) { 2775480093f4SDimitry Andric if (auto Result = 2776480093f4SDimitry Andric PackDeductionScope(S, TemplateParams, Deduced, Info, I).finish()) 27770b57cec5SDimitry Andric return Result; 27780b57cec5SDimitry Andric } 27790b57cec5SDimitry Andric 27800b57cec5SDimitry Andric if (!Deduced[I].isNull()) { 27810b57cec5SDimitry Andric if (I < NumAlreadyConverted) { 27820b57cec5SDimitry Andric // We may have had explicitly-specified template arguments for a 27830b57cec5SDimitry Andric // template parameter pack (that may or may not have been extended 27840b57cec5SDimitry Andric // via additional deduced arguments). 27850b57cec5SDimitry Andric if (Param->isParameterPack() && CurrentInstantiationScope && 27860b57cec5SDimitry Andric CurrentInstantiationScope->getPartiallySubstitutedPack() == Param) { 27870b57cec5SDimitry Andric // Forget the partially-substituted pack; its substitution is now 27880b57cec5SDimitry Andric // complete. 27890b57cec5SDimitry Andric CurrentInstantiationScope->ResetPartiallySubstitutedPack(); 27900b57cec5SDimitry Andric // We still need to check the argument in case it was extended by 27910b57cec5SDimitry Andric // deduction. 27920b57cec5SDimitry Andric } else { 27930b57cec5SDimitry Andric // We have already fully type-checked and converted this 27940b57cec5SDimitry Andric // argument, because it was explicitly-specified. Just record the 27950b57cec5SDimitry Andric // presence of this argument. 2796bdd1243dSDimitry Andric SugaredBuilder.push_back(Deduced[I]); 2797bdd1243dSDimitry Andric CanonicalBuilder.push_back( 2798bdd1243dSDimitry Andric S.Context.getCanonicalTemplateArgument(Deduced[I])); 27990b57cec5SDimitry Andric continue; 28000b57cec5SDimitry Andric } 28010b57cec5SDimitry Andric } 28020b57cec5SDimitry Andric 28030b57cec5SDimitry Andric // We may have deduced this argument, so it still needs to be 28040b57cec5SDimitry Andric // checked and converted. 28050b57cec5SDimitry Andric if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Template, Info, 2806bdd1243dSDimitry Andric IsDeduced, SugaredBuilder, 2807bdd1243dSDimitry Andric CanonicalBuilder)) { 28080b57cec5SDimitry Andric Info.Param = makeTemplateParameter(Param); 28090b57cec5SDimitry Andric // FIXME: These template arguments are temporary. Free them! 2810bdd1243dSDimitry Andric Info.reset( 2811bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(S.Context, SugaredBuilder), 2812bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(S.Context, CanonicalBuilder)); 28130b57cec5SDimitry Andric return Sema::TDK_SubstitutionFailure; 28140b57cec5SDimitry Andric } 28150b57cec5SDimitry Andric 28160b57cec5SDimitry Andric continue; 28170b57cec5SDimitry Andric } 28180b57cec5SDimitry Andric 28190b57cec5SDimitry Andric // Substitute into the default template argument, if available. 28200b57cec5SDimitry Andric bool HasDefaultArg = false; 28210b57cec5SDimitry Andric TemplateDecl *TD = dyn_cast<TemplateDecl>(Template); 28220b57cec5SDimitry Andric if (!TD) { 28230b57cec5SDimitry Andric assert(isa<ClassTemplatePartialSpecializationDecl>(Template) || 28240b57cec5SDimitry Andric isa<VarTemplatePartialSpecializationDecl>(Template)); 28250b57cec5SDimitry Andric return Sema::TDK_Incomplete; 28260b57cec5SDimitry Andric } 28270b57cec5SDimitry Andric 2828349cc55cSDimitry Andric TemplateArgumentLoc DefArg; 2829349cc55cSDimitry Andric { 2830349cc55cSDimitry Andric Qualifiers ThisTypeQuals; 2831349cc55cSDimitry Andric CXXRecordDecl *ThisContext = nullptr; 2832349cc55cSDimitry Andric if (auto *Rec = dyn_cast<CXXRecordDecl>(TD->getDeclContext())) 2833349cc55cSDimitry Andric if (Rec->isLambda()) 2834349cc55cSDimitry Andric if (auto *Method = dyn_cast<CXXMethodDecl>(Rec->getDeclContext())) { 2835349cc55cSDimitry Andric ThisContext = Method->getParent(); 2836349cc55cSDimitry Andric ThisTypeQuals = Method->getMethodQualifiers(); 2837349cc55cSDimitry Andric } 2838349cc55cSDimitry Andric 2839349cc55cSDimitry Andric Sema::CXXThisScopeRAII ThisScope(S, ThisContext, ThisTypeQuals, 2840349cc55cSDimitry Andric S.getLangOpts().CPlusPlus17); 2841349cc55cSDimitry Andric 2842349cc55cSDimitry Andric DefArg = S.SubstDefaultTemplateArgumentIfAvailable( 2843bdd1243dSDimitry Andric TD, TD->getLocation(), TD->getSourceRange().getEnd(), Param, 2844bdd1243dSDimitry Andric SugaredBuilder, CanonicalBuilder, HasDefaultArg); 2845349cc55cSDimitry Andric } 28460b57cec5SDimitry Andric 28470b57cec5SDimitry Andric // If there was no default argument, deduction is incomplete. 28480b57cec5SDimitry Andric if (DefArg.getArgument().isNull()) { 28490b57cec5SDimitry Andric Info.Param = makeTemplateParameter( 28500b57cec5SDimitry Andric const_cast<NamedDecl *>(TemplateParams->getParam(I))); 2851bdd1243dSDimitry Andric Info.reset(TemplateArgumentList::CreateCopy(S.Context, SugaredBuilder), 2852bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(S.Context, CanonicalBuilder)); 28530b57cec5SDimitry Andric if (PartialOverloading) break; 28540b57cec5SDimitry Andric 28550b57cec5SDimitry Andric return HasDefaultArg ? Sema::TDK_SubstitutionFailure 28560b57cec5SDimitry Andric : Sema::TDK_Incomplete; 28570b57cec5SDimitry Andric } 28580b57cec5SDimitry Andric 28590b57cec5SDimitry Andric // Check whether we can actually use the default argument. 2860bdd1243dSDimitry Andric if (S.CheckTemplateArgument( 2861bdd1243dSDimitry Andric Param, DefArg, TD, TD->getLocation(), TD->getSourceRange().getEnd(), 2862bdd1243dSDimitry Andric 0, SugaredBuilder, CanonicalBuilder, Sema::CTAK_Specified)) { 28630b57cec5SDimitry Andric Info.Param = makeTemplateParameter( 28640b57cec5SDimitry Andric const_cast<NamedDecl *>(TemplateParams->getParam(I))); 28650b57cec5SDimitry Andric // FIXME: These template arguments are temporary. Free them! 2866bdd1243dSDimitry Andric Info.reset(TemplateArgumentList::CreateCopy(S.Context, SugaredBuilder), 2867bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(S.Context, CanonicalBuilder)); 28680b57cec5SDimitry Andric return Sema::TDK_SubstitutionFailure; 28690b57cec5SDimitry Andric } 28700b57cec5SDimitry Andric 28710b57cec5SDimitry Andric // If we get here, we successfully used the default template argument. 28720b57cec5SDimitry Andric } 28730b57cec5SDimitry Andric 28740b57cec5SDimitry Andric return Sema::TDK_Success; 28750b57cec5SDimitry Andric } 28760b57cec5SDimitry Andric 28770b57cec5SDimitry Andric static DeclContext *getAsDeclContextOrEnclosing(Decl *D) { 28780b57cec5SDimitry Andric if (auto *DC = dyn_cast<DeclContext>(D)) 28790b57cec5SDimitry Andric return DC; 28800b57cec5SDimitry Andric return D->getDeclContext(); 28810b57cec5SDimitry Andric } 28820b57cec5SDimitry Andric 28830b57cec5SDimitry Andric template<typename T> struct IsPartialSpecialization { 28840b57cec5SDimitry Andric static constexpr bool value = false; 28850b57cec5SDimitry Andric }; 28860b57cec5SDimitry Andric template<> 28870b57cec5SDimitry Andric struct IsPartialSpecialization<ClassTemplatePartialSpecializationDecl> { 28880b57cec5SDimitry Andric static constexpr bool value = true; 28890b57cec5SDimitry Andric }; 28900b57cec5SDimitry Andric template<> 28910b57cec5SDimitry Andric struct IsPartialSpecialization<VarTemplatePartialSpecializationDecl> { 28920b57cec5SDimitry Andric static constexpr bool value = true; 28930b57cec5SDimitry Andric }; 2894bdd1243dSDimitry Andric template <typename TemplateDeclT> 2895bdd1243dSDimitry Andric static bool DeducedArgsNeedReplacement(TemplateDeclT *Template) { 2896bdd1243dSDimitry Andric return false; 2897bdd1243dSDimitry Andric } 2898bdd1243dSDimitry Andric template <> 2899bdd1243dSDimitry Andric bool DeducedArgsNeedReplacement<VarTemplatePartialSpecializationDecl>( 2900bdd1243dSDimitry Andric VarTemplatePartialSpecializationDecl *Spec) { 2901bdd1243dSDimitry Andric return !Spec->isClassScopeExplicitSpecialization(); 2902bdd1243dSDimitry Andric } 2903bdd1243dSDimitry Andric template <> 2904bdd1243dSDimitry Andric bool DeducedArgsNeedReplacement<ClassTemplatePartialSpecializationDecl>( 2905bdd1243dSDimitry Andric ClassTemplatePartialSpecializationDecl *Spec) { 2906bdd1243dSDimitry Andric return !Spec->isClassScopeExplicitSpecialization(); 2907bdd1243dSDimitry Andric } 29080b57cec5SDimitry Andric 2909480093f4SDimitry Andric template <typename TemplateDeclT> 2910480093f4SDimitry Andric static Sema::TemplateDeductionResult 2911480093f4SDimitry Andric CheckDeducedArgumentConstraints(Sema &S, TemplateDeclT *Template, 2912bdd1243dSDimitry Andric ArrayRef<TemplateArgument> SugaredDeducedArgs, 2913bdd1243dSDimitry Andric ArrayRef<TemplateArgument> CanonicalDeducedArgs, 2914480093f4SDimitry Andric TemplateDeductionInfo &Info) { 2915480093f4SDimitry Andric llvm::SmallVector<const Expr *, 3> AssociatedConstraints; 2916480093f4SDimitry Andric Template->getAssociatedConstraints(AssociatedConstraints); 2917bdd1243dSDimitry Andric 2918bdd1243dSDimitry Andric bool NeedsReplacement = DeducedArgsNeedReplacement(Template); 2919bdd1243dSDimitry Andric TemplateArgumentList DeducedTAL{TemplateArgumentList::OnStack, 2920bdd1243dSDimitry Andric CanonicalDeducedArgs}; 2921bdd1243dSDimitry Andric 2922bdd1243dSDimitry Andric MultiLevelTemplateArgumentList MLTAL = S.getTemplateInstantiationArgs( 29235f757f3fSDimitry Andric Template, Template->getDeclContext(), /*Final=*/false, 2924bdd1243dSDimitry Andric /*InnerMost=*/NeedsReplacement ? nullptr : &DeducedTAL, 2925bdd1243dSDimitry Andric /*RelativeToPrimary=*/true, /*Pattern=*/ 2926bdd1243dSDimitry Andric nullptr, /*ForConstraintInstantiation=*/true); 2927bdd1243dSDimitry Andric 2928bdd1243dSDimitry Andric // getTemplateInstantiationArgs picks up the non-deduced version of the 2929bdd1243dSDimitry Andric // template args when this is a variable template partial specialization and 2930bdd1243dSDimitry Andric // not class-scope explicit specialization, so replace with Deduced Args 2931bdd1243dSDimitry Andric // instead of adding to inner-most. 2932bdd1243dSDimitry Andric if (NeedsReplacement) 293306c3fb27SDimitry Andric MLTAL.replaceInnermostTemplateArguments(Template, CanonicalDeducedArgs); 2934bdd1243dSDimitry Andric 2935bdd1243dSDimitry Andric if (S.CheckConstraintSatisfaction(Template, AssociatedConstraints, MLTAL, 2936bdd1243dSDimitry Andric Info.getLocation(), 2937480093f4SDimitry Andric Info.AssociatedConstraintsSatisfaction) || 2938480093f4SDimitry Andric !Info.AssociatedConstraintsSatisfaction.IsSatisfied) { 2939bdd1243dSDimitry Andric Info.reset( 2940bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(S.Context, SugaredDeducedArgs), 2941bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(S.Context, CanonicalDeducedArgs)); 2942480093f4SDimitry Andric return Sema::TDK_ConstraintsNotSatisfied; 2943480093f4SDimitry Andric } 2944480093f4SDimitry Andric return Sema::TDK_Success; 2945480093f4SDimitry Andric } 2946480093f4SDimitry Andric 29470b57cec5SDimitry Andric /// Complete template argument deduction for a partial specialization. 29480b57cec5SDimitry Andric template <typename T> 29495ffd83dbSDimitry Andric static std::enable_if_t<IsPartialSpecialization<T>::value, 29505ffd83dbSDimitry Andric Sema::TemplateDeductionResult> 29510b57cec5SDimitry Andric FinishTemplateArgumentDeduction( 29520b57cec5SDimitry Andric Sema &S, T *Partial, bool IsPartialOrdering, 29530b57cec5SDimitry Andric const TemplateArgumentList &TemplateArgs, 29540b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 29550b57cec5SDimitry Andric TemplateDeductionInfo &Info) { 29560b57cec5SDimitry Andric // Unevaluated SFINAE context. 29570b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 29580b57cec5SDimitry Andric S, Sema::ExpressionEvaluationContext::Unevaluated); 29590b57cec5SDimitry Andric Sema::SFINAETrap Trap(S); 29600b57cec5SDimitry Andric 29610b57cec5SDimitry Andric Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Partial)); 29620b57cec5SDimitry Andric 29630b57cec5SDimitry Andric // C++ [temp.deduct.type]p2: 29640b57cec5SDimitry Andric // [...] or if any template argument remains neither deduced nor 29650b57cec5SDimitry Andric // explicitly specified, template argument deduction fails. 2966bdd1243dSDimitry Andric SmallVector<TemplateArgument, 4> SugaredBuilder, CanonicalBuilder; 29670b57cec5SDimitry Andric if (auto Result = ConvertDeducedTemplateArguments( 2968bdd1243dSDimitry Andric S, Partial, IsPartialOrdering, Deduced, Info, SugaredBuilder, 2969bdd1243dSDimitry Andric CanonicalBuilder)) 29700b57cec5SDimitry Andric return Result; 29710b57cec5SDimitry Andric 29720b57cec5SDimitry Andric // Form the template argument list from the deduced template arguments. 2973bdd1243dSDimitry Andric TemplateArgumentList *SugaredDeducedArgumentList = 2974bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(S.Context, SugaredBuilder); 2975bdd1243dSDimitry Andric TemplateArgumentList *CanonicalDeducedArgumentList = 2976bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(S.Context, CanonicalBuilder); 29770b57cec5SDimitry Andric 2978bdd1243dSDimitry Andric Info.reset(SugaredDeducedArgumentList, CanonicalDeducedArgumentList); 29790b57cec5SDimitry Andric 29800b57cec5SDimitry Andric // Substitute the deduced template arguments into the template 29810b57cec5SDimitry Andric // arguments of the class template partial specialization, and 29820b57cec5SDimitry Andric // verify that the instantiated template arguments are both valid 29830b57cec5SDimitry Andric // and are equivalent to the template arguments originally provided 29840b57cec5SDimitry Andric // to the class template. 29850b57cec5SDimitry Andric LocalInstantiationScope InstScope(S); 29860b57cec5SDimitry Andric auto *Template = Partial->getSpecializedTemplate(); 29870b57cec5SDimitry Andric const ASTTemplateArgumentListInfo *PartialTemplArgInfo = 29880b57cec5SDimitry Andric Partial->getTemplateArgsAsWritten(); 29890b57cec5SDimitry Andric 29900b57cec5SDimitry Andric TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, 29910b57cec5SDimitry Andric PartialTemplArgInfo->RAngleLoc); 29920b57cec5SDimitry Andric 2993bdd1243dSDimitry Andric if (S.SubstTemplateArguments(PartialTemplArgInfo->arguments(), 2994bdd1243dSDimitry Andric MultiLevelTemplateArgumentList(Partial, 2995bdd1243dSDimitry Andric SugaredBuilder, 2996bdd1243dSDimitry Andric /*Final=*/true), 2997bdd1243dSDimitry Andric InstArgs)) { 29980b57cec5SDimitry Andric unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; 29990b57cec5SDimitry Andric if (ParamIdx >= Partial->getTemplateParameters()->size()) 30000b57cec5SDimitry Andric ParamIdx = Partial->getTemplateParameters()->size() - 1; 30010b57cec5SDimitry Andric 30020b57cec5SDimitry Andric Decl *Param = const_cast<NamedDecl *>( 30030b57cec5SDimitry Andric Partial->getTemplateParameters()->getParam(ParamIdx)); 30040b57cec5SDimitry Andric Info.Param = makeTemplateParameter(Param); 3005349cc55cSDimitry Andric Info.FirstArg = (*PartialTemplArgInfo)[ArgIdx].getArgument(); 30060b57cec5SDimitry Andric return Sema::TDK_SubstitutionFailure; 30070b57cec5SDimitry Andric } 30080b57cec5SDimitry Andric 3009480093f4SDimitry Andric bool ConstraintsNotSatisfied; 3010bdd1243dSDimitry Andric SmallVector<TemplateArgument, 4> SugaredConvertedInstArgs, 3011bdd1243dSDimitry Andric CanonicalConvertedInstArgs; 3012bdd1243dSDimitry Andric if (S.CheckTemplateArgumentList( 3013bdd1243dSDimitry Andric Template, Partial->getLocation(), InstArgs, false, 3014bdd1243dSDimitry Andric SugaredConvertedInstArgs, CanonicalConvertedInstArgs, 3015bdd1243dSDimitry Andric /*UpdateArgsWithConversions=*/true, &ConstraintsNotSatisfied)) 3016bdd1243dSDimitry Andric return ConstraintsNotSatisfied ? Sema::TDK_ConstraintsNotSatisfied 3017bdd1243dSDimitry Andric : Sema::TDK_SubstitutionFailure; 30180b57cec5SDimitry Andric 30190b57cec5SDimitry Andric TemplateParameterList *TemplateParams = Template->getTemplateParameters(); 30200b57cec5SDimitry Andric for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { 3021bdd1243dSDimitry Andric TemplateArgument InstArg = SugaredConvertedInstArgs.data()[I]; 3022bdd1243dSDimitry Andric if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg, 3023bdd1243dSDimitry Andric IsPartialOrdering)) { 30240b57cec5SDimitry Andric Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); 30250b57cec5SDimitry Andric Info.FirstArg = TemplateArgs[I]; 30260b57cec5SDimitry Andric Info.SecondArg = InstArg; 30270b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 30280b57cec5SDimitry Andric } 30290b57cec5SDimitry Andric } 30300b57cec5SDimitry Andric 30310b57cec5SDimitry Andric if (Trap.hasErrorOccurred()) 30320b57cec5SDimitry Andric return Sema::TDK_SubstitutionFailure; 30330b57cec5SDimitry Andric 3034bdd1243dSDimitry Andric if (auto Result = CheckDeducedArgumentConstraints(S, Partial, SugaredBuilder, 3035bdd1243dSDimitry Andric CanonicalBuilder, Info)) 3036480093f4SDimitry Andric return Result; 3037480093f4SDimitry Andric 30380b57cec5SDimitry Andric return Sema::TDK_Success; 30390b57cec5SDimitry Andric } 30400b57cec5SDimitry Andric 30410b57cec5SDimitry Andric /// Complete template argument deduction for a class or variable template, 30420b57cec5SDimitry Andric /// when partial ordering against a partial specialization. 30430b57cec5SDimitry Andric // FIXME: Factor out duplication with partial specialization version above. 30440b57cec5SDimitry Andric static Sema::TemplateDeductionResult FinishTemplateArgumentDeduction( 30450b57cec5SDimitry Andric Sema &S, TemplateDecl *Template, bool PartialOrdering, 30460b57cec5SDimitry Andric const TemplateArgumentList &TemplateArgs, 30470b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 30480b57cec5SDimitry Andric TemplateDeductionInfo &Info) { 30490b57cec5SDimitry Andric // Unevaluated SFINAE context. 30500b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 30510b57cec5SDimitry Andric S, Sema::ExpressionEvaluationContext::Unevaluated); 30520b57cec5SDimitry Andric Sema::SFINAETrap Trap(S); 30530b57cec5SDimitry Andric 30540b57cec5SDimitry Andric Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Template)); 30550b57cec5SDimitry Andric 30560b57cec5SDimitry Andric // C++ [temp.deduct.type]p2: 30570b57cec5SDimitry Andric // [...] or if any template argument remains neither deduced nor 30580b57cec5SDimitry Andric // explicitly specified, template argument deduction fails. 3059bdd1243dSDimitry Andric SmallVector<TemplateArgument, 4> SugaredBuilder, CanonicalBuilder; 30600b57cec5SDimitry Andric if (auto Result = ConvertDeducedTemplateArguments( 3061bdd1243dSDimitry Andric S, Template, /*IsDeduced*/ PartialOrdering, Deduced, Info, 3062bdd1243dSDimitry Andric SugaredBuilder, CanonicalBuilder, 3063bdd1243dSDimitry Andric /*CurrentInstantiationScope=*/nullptr, 3064bdd1243dSDimitry Andric /*NumAlreadyConverted=*/0U, /*PartialOverloading=*/false)) 30650b57cec5SDimitry Andric return Result; 30660b57cec5SDimitry Andric 30670b57cec5SDimitry Andric // Check that we produced the correct argument list. 30680b57cec5SDimitry Andric TemplateParameterList *TemplateParams = Template->getTemplateParameters(); 30690b57cec5SDimitry Andric for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { 3070bdd1243dSDimitry Andric TemplateArgument InstArg = CanonicalBuilder[I]; 3071bdd1243dSDimitry Andric if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg, PartialOrdering, 3072bdd1243dSDimitry Andric /*PackExpansionMatchesPack=*/true)) { 30730b57cec5SDimitry Andric Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); 30740b57cec5SDimitry Andric Info.FirstArg = TemplateArgs[I]; 30750b57cec5SDimitry Andric Info.SecondArg = InstArg; 30760b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 30770b57cec5SDimitry Andric } 30780b57cec5SDimitry Andric } 30790b57cec5SDimitry Andric 30800b57cec5SDimitry Andric if (Trap.hasErrorOccurred()) 30810b57cec5SDimitry Andric return Sema::TDK_SubstitutionFailure; 30820b57cec5SDimitry Andric 3083bdd1243dSDimitry Andric if (auto Result = CheckDeducedArgumentConstraints(S, Template, SugaredBuilder, 3084bdd1243dSDimitry Andric CanonicalBuilder, Info)) 3085480093f4SDimitry Andric return Result; 3086480093f4SDimitry Andric 30870b57cec5SDimitry Andric return Sema::TDK_Success; 30880b57cec5SDimitry Andric } 30890b57cec5SDimitry Andric 30900b57cec5SDimitry Andric /// Perform template argument deduction to determine whether 30910b57cec5SDimitry Andric /// the given template arguments match the given class template 30920b57cec5SDimitry Andric /// partial specialization per C++ [temp.class.spec.match]. 30930b57cec5SDimitry Andric Sema::TemplateDeductionResult 30940b57cec5SDimitry Andric Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, 30950b57cec5SDimitry Andric const TemplateArgumentList &TemplateArgs, 30960b57cec5SDimitry Andric TemplateDeductionInfo &Info) { 30970b57cec5SDimitry Andric if (Partial->isInvalidDecl()) 30980b57cec5SDimitry Andric return TDK_Invalid; 30990b57cec5SDimitry Andric 31000b57cec5SDimitry Andric // C++ [temp.class.spec.match]p2: 31010b57cec5SDimitry Andric // A partial specialization matches a given actual template 31020b57cec5SDimitry Andric // argument list if the template arguments of the partial 31030b57cec5SDimitry Andric // specialization can be deduced from the actual template argument 31040b57cec5SDimitry Andric // list (14.8.2). 31050b57cec5SDimitry Andric 31060b57cec5SDimitry Andric // Unevaluated SFINAE context. 31070b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 31080b57cec5SDimitry Andric *this, Sema::ExpressionEvaluationContext::Unevaluated); 31090b57cec5SDimitry Andric SFINAETrap Trap(*this); 31100b57cec5SDimitry Andric 3111fe6060f1SDimitry Andric // This deduction has no relation to any outer instantiation we might be 3112fe6060f1SDimitry Andric // performing. 3113fe6060f1SDimitry Andric LocalInstantiationScope InstantiationScope(*this); 3114fe6060f1SDimitry Andric 31150b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> Deduced; 31160b57cec5SDimitry Andric Deduced.resize(Partial->getTemplateParameters()->size()); 31170b57cec5SDimitry Andric if (TemplateDeductionResult Result 31180b57cec5SDimitry Andric = ::DeduceTemplateArguments(*this, 31190b57cec5SDimitry Andric Partial->getTemplateParameters(), 31200b57cec5SDimitry Andric Partial->getTemplateArgs(), 31210b57cec5SDimitry Andric TemplateArgs, Info, Deduced)) 31220b57cec5SDimitry Andric return Result; 31230b57cec5SDimitry Andric 31240b57cec5SDimitry Andric SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); 31250b57cec5SDimitry Andric InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, 31260b57cec5SDimitry Andric Info); 31270b57cec5SDimitry Andric if (Inst.isInvalid()) 31280b57cec5SDimitry Andric return TDK_InstantiationDepth; 31290b57cec5SDimitry Andric 31300b57cec5SDimitry Andric if (Trap.hasErrorOccurred()) 31310b57cec5SDimitry Andric return Sema::TDK_SubstitutionFailure; 31320b57cec5SDimitry Andric 31335ffd83dbSDimitry Andric TemplateDeductionResult Result; 31345ffd83dbSDimitry Andric runWithSufficientStackSpace(Info.getLocation(), [&] { 31355ffd83dbSDimitry Andric Result = ::FinishTemplateArgumentDeduction(*this, Partial, 31365ffd83dbSDimitry Andric /*IsPartialOrdering=*/false, 31375ffd83dbSDimitry Andric TemplateArgs, Deduced, Info); 31385ffd83dbSDimitry Andric }); 31395ffd83dbSDimitry Andric return Result; 31400b57cec5SDimitry Andric } 31410b57cec5SDimitry Andric 31420b57cec5SDimitry Andric /// Perform template argument deduction to determine whether 31430b57cec5SDimitry Andric /// the given template arguments match the given variable template 31440b57cec5SDimitry Andric /// partial specialization per C++ [temp.class.spec.match]. 31450b57cec5SDimitry Andric Sema::TemplateDeductionResult 31460b57cec5SDimitry Andric Sema::DeduceTemplateArguments(VarTemplatePartialSpecializationDecl *Partial, 31470b57cec5SDimitry Andric const TemplateArgumentList &TemplateArgs, 31480b57cec5SDimitry Andric TemplateDeductionInfo &Info) { 31490b57cec5SDimitry Andric if (Partial->isInvalidDecl()) 31500b57cec5SDimitry Andric return TDK_Invalid; 31510b57cec5SDimitry Andric 31520b57cec5SDimitry Andric // C++ [temp.class.spec.match]p2: 31530b57cec5SDimitry Andric // A partial specialization matches a given actual template 31540b57cec5SDimitry Andric // argument list if the template arguments of the partial 31550b57cec5SDimitry Andric // specialization can be deduced from the actual template argument 31560b57cec5SDimitry Andric // list (14.8.2). 31570b57cec5SDimitry Andric 31580b57cec5SDimitry Andric // Unevaluated SFINAE context. 31590b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 31600b57cec5SDimitry Andric *this, Sema::ExpressionEvaluationContext::Unevaluated); 31610b57cec5SDimitry Andric SFINAETrap Trap(*this); 31620b57cec5SDimitry Andric 3163fe6060f1SDimitry Andric // This deduction has no relation to any outer instantiation we might be 3164fe6060f1SDimitry Andric // performing. 3165fe6060f1SDimitry Andric LocalInstantiationScope InstantiationScope(*this); 3166fe6060f1SDimitry Andric 31670b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> Deduced; 31680b57cec5SDimitry Andric Deduced.resize(Partial->getTemplateParameters()->size()); 31690b57cec5SDimitry Andric if (TemplateDeductionResult Result = ::DeduceTemplateArguments( 31700b57cec5SDimitry Andric *this, Partial->getTemplateParameters(), Partial->getTemplateArgs(), 31710b57cec5SDimitry Andric TemplateArgs, Info, Deduced)) 31720b57cec5SDimitry Andric return Result; 31730b57cec5SDimitry Andric 31740b57cec5SDimitry Andric SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); 31750b57cec5SDimitry Andric InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, 31760b57cec5SDimitry Andric Info); 31770b57cec5SDimitry Andric if (Inst.isInvalid()) 31780b57cec5SDimitry Andric return TDK_InstantiationDepth; 31790b57cec5SDimitry Andric 31800b57cec5SDimitry Andric if (Trap.hasErrorOccurred()) 31810b57cec5SDimitry Andric return Sema::TDK_SubstitutionFailure; 31820b57cec5SDimitry Andric 31835ffd83dbSDimitry Andric TemplateDeductionResult Result; 31845ffd83dbSDimitry Andric runWithSufficientStackSpace(Info.getLocation(), [&] { 31855ffd83dbSDimitry Andric Result = ::FinishTemplateArgumentDeduction(*this, Partial, 31865ffd83dbSDimitry Andric /*IsPartialOrdering=*/false, 31875ffd83dbSDimitry Andric TemplateArgs, Deduced, Info); 31885ffd83dbSDimitry Andric }); 31895ffd83dbSDimitry Andric return Result; 31900b57cec5SDimitry Andric } 31910b57cec5SDimitry Andric 31920b57cec5SDimitry Andric /// Determine whether the given type T is a simple-template-id type. 31930b57cec5SDimitry Andric static bool isSimpleTemplateIdType(QualType T) { 31940b57cec5SDimitry Andric if (const TemplateSpecializationType *Spec 31950b57cec5SDimitry Andric = T->getAs<TemplateSpecializationType>()) 31960b57cec5SDimitry Andric return Spec->getTemplateName().getAsTemplateDecl() != nullptr; 31970b57cec5SDimitry Andric 31980b57cec5SDimitry Andric // C++17 [temp.local]p2: 31990b57cec5SDimitry Andric // the injected-class-name [...] is equivalent to the template-name followed 32000b57cec5SDimitry Andric // by the template-arguments of the class template specialization or partial 32010b57cec5SDimitry Andric // specialization enclosed in <> 32020b57cec5SDimitry Andric // ... which means it's equivalent to a simple-template-id. 32030b57cec5SDimitry Andric // 32040b57cec5SDimitry Andric // This only arises during class template argument deduction for a copy 32050b57cec5SDimitry Andric // deduction candidate, where it permits slicing. 32060b57cec5SDimitry Andric if (T->getAs<InjectedClassNameType>()) 32070b57cec5SDimitry Andric return true; 32080b57cec5SDimitry Andric 32090b57cec5SDimitry Andric return false; 32100b57cec5SDimitry Andric } 32110b57cec5SDimitry Andric 32120b57cec5SDimitry Andric /// Substitute the explicitly-provided template arguments into the 32130b57cec5SDimitry Andric /// given function template according to C++ [temp.arg.explicit]. 32140b57cec5SDimitry Andric /// 32150b57cec5SDimitry Andric /// \param FunctionTemplate the function template into which the explicit 32160b57cec5SDimitry Andric /// template arguments will be substituted. 32170b57cec5SDimitry Andric /// 32180b57cec5SDimitry Andric /// \param ExplicitTemplateArgs the explicitly-specified template 32190b57cec5SDimitry Andric /// arguments. 32200b57cec5SDimitry Andric /// 32210b57cec5SDimitry Andric /// \param Deduced the deduced template arguments, which will be populated 32220b57cec5SDimitry Andric /// with the converted and checked explicit template arguments. 32230b57cec5SDimitry Andric /// 32240b57cec5SDimitry Andric /// \param ParamTypes will be populated with the instantiated function 32250b57cec5SDimitry Andric /// parameters. 32260b57cec5SDimitry Andric /// 32270b57cec5SDimitry Andric /// \param FunctionType if non-NULL, the result type of the function template 32280b57cec5SDimitry Andric /// will also be instantiated and the pointed-to value will be updated with 32290b57cec5SDimitry Andric /// the instantiated function type. 32300b57cec5SDimitry Andric /// 32310b57cec5SDimitry Andric /// \param Info if substitution fails for any reason, this object will be 32320b57cec5SDimitry Andric /// populated with more information about the failure. 32330b57cec5SDimitry Andric /// 32340b57cec5SDimitry Andric /// \returns TDK_Success if substitution was successful, or some failure 32350b57cec5SDimitry Andric /// condition. 3236bdd1243dSDimitry Andric Sema::TemplateDeductionResult Sema::SubstituteExplicitTemplateArguments( 32370b57cec5SDimitry Andric FunctionTemplateDecl *FunctionTemplate, 32380b57cec5SDimitry Andric TemplateArgumentListInfo &ExplicitTemplateArgs, 32390b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 3240bdd1243dSDimitry Andric SmallVectorImpl<QualType> &ParamTypes, QualType *FunctionType, 32410b57cec5SDimitry Andric TemplateDeductionInfo &Info) { 32420b57cec5SDimitry Andric FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 32430b57cec5SDimitry Andric TemplateParameterList *TemplateParams 32440b57cec5SDimitry Andric = FunctionTemplate->getTemplateParameters(); 32450b57cec5SDimitry Andric 32460b57cec5SDimitry Andric if (ExplicitTemplateArgs.size() == 0) { 32470b57cec5SDimitry Andric // No arguments to substitute; just copy over the parameter types and 32480b57cec5SDimitry Andric // fill in the function type. 3249bdd1243dSDimitry Andric for (auto *P : Function->parameters()) 32500b57cec5SDimitry Andric ParamTypes.push_back(P->getType()); 32510b57cec5SDimitry Andric 32520b57cec5SDimitry Andric if (FunctionType) 32530b57cec5SDimitry Andric *FunctionType = Function->getType(); 32540b57cec5SDimitry Andric return TDK_Success; 32550b57cec5SDimitry Andric } 32560b57cec5SDimitry Andric 32570b57cec5SDimitry Andric // Unevaluated SFINAE context. 32580b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 32590b57cec5SDimitry Andric *this, Sema::ExpressionEvaluationContext::Unevaluated); 32600b57cec5SDimitry Andric SFINAETrap Trap(*this); 32610b57cec5SDimitry Andric 32620b57cec5SDimitry Andric // C++ [temp.arg.explicit]p3: 32630b57cec5SDimitry Andric // Template arguments that are present shall be specified in the 32640b57cec5SDimitry Andric // declaration order of their corresponding template-parameters. The 32650b57cec5SDimitry Andric // template argument list shall not specify more template-arguments than 32660b57cec5SDimitry Andric // there are corresponding template-parameters. 3267bdd1243dSDimitry Andric SmallVector<TemplateArgument, 4> SugaredBuilder, CanonicalBuilder; 32680b57cec5SDimitry Andric 32690b57cec5SDimitry Andric // Enter a new template instantiation context where we check the 32700b57cec5SDimitry Andric // explicitly-specified template arguments against this function template, 32710b57cec5SDimitry Andric // and then substitute them into the function parameter types. 32720b57cec5SDimitry Andric SmallVector<TemplateArgument, 4> DeducedArgs; 32730b57cec5SDimitry Andric InstantiatingTemplate Inst( 32740b57cec5SDimitry Andric *this, Info.getLocation(), FunctionTemplate, DeducedArgs, 32750b57cec5SDimitry Andric CodeSynthesisContext::ExplicitTemplateArgumentSubstitution, Info); 32760b57cec5SDimitry Andric if (Inst.isInvalid()) 32770b57cec5SDimitry Andric return TDK_InstantiationDepth; 32780b57cec5SDimitry Andric 32790b57cec5SDimitry Andric if (CheckTemplateArgumentList(FunctionTemplate, SourceLocation(), 3280bdd1243dSDimitry Andric ExplicitTemplateArgs, true, SugaredBuilder, 3281bdd1243dSDimitry Andric CanonicalBuilder, 3282bdd1243dSDimitry Andric /*UpdateArgsWithConversions=*/false) || 32830b57cec5SDimitry Andric Trap.hasErrorOccurred()) { 3284bdd1243dSDimitry Andric unsigned Index = SugaredBuilder.size(); 32850b57cec5SDimitry Andric if (Index >= TemplateParams->size()) 32860b57cec5SDimitry Andric return TDK_SubstitutionFailure; 32870b57cec5SDimitry Andric Info.Param = makeTemplateParameter(TemplateParams->getParam(Index)); 32880b57cec5SDimitry Andric return TDK_InvalidExplicitArguments; 32890b57cec5SDimitry Andric } 32900b57cec5SDimitry Andric 32910b57cec5SDimitry Andric // Form the template argument list from the explicitly-specified 32920b57cec5SDimitry Andric // template arguments. 3293bdd1243dSDimitry Andric TemplateArgumentList *SugaredExplicitArgumentList = 3294bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(Context, SugaredBuilder); 3295bdd1243dSDimitry Andric TemplateArgumentList *CanonicalExplicitArgumentList = 3296bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(Context, CanonicalBuilder); 3297bdd1243dSDimitry Andric Info.setExplicitArgs(SugaredExplicitArgumentList, 3298bdd1243dSDimitry Andric CanonicalExplicitArgumentList); 32990b57cec5SDimitry Andric 33000b57cec5SDimitry Andric // Template argument deduction and the final substitution should be 33010b57cec5SDimitry Andric // done in the context of the templated declaration. Explicit 33020b57cec5SDimitry Andric // argument substitution, on the other hand, needs to happen in the 33030b57cec5SDimitry Andric // calling context. 33040b57cec5SDimitry Andric ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); 33050b57cec5SDimitry Andric 33060b57cec5SDimitry Andric // If we deduced template arguments for a template parameter pack, 33070b57cec5SDimitry Andric // note that the template argument pack is partially substituted and record 33080b57cec5SDimitry Andric // the explicit template arguments. They'll be used as part of deduction 33090b57cec5SDimitry Andric // for this template parameter pack. 33100b57cec5SDimitry Andric unsigned PartiallySubstitutedPackIndex = -1u; 3311bdd1243dSDimitry Andric if (!CanonicalBuilder.empty()) { 3312bdd1243dSDimitry Andric const TemplateArgument &Arg = CanonicalBuilder.back(); 33130b57cec5SDimitry Andric if (Arg.getKind() == TemplateArgument::Pack) { 3314bdd1243dSDimitry Andric auto *Param = TemplateParams->getParam(CanonicalBuilder.size() - 1); 33150b57cec5SDimitry Andric // If this is a fully-saturated fixed-size pack, it should be 33160b57cec5SDimitry Andric // fully-substituted, not partially-substituted. 3317bdd1243dSDimitry Andric std::optional<unsigned> Expansions = getExpandedPackSize(Param); 33180b57cec5SDimitry Andric if (!Expansions || Arg.pack_size() < *Expansions) { 3319bdd1243dSDimitry Andric PartiallySubstitutedPackIndex = CanonicalBuilder.size() - 1; 33200b57cec5SDimitry Andric CurrentInstantiationScope->SetPartiallySubstitutedPack( 33210b57cec5SDimitry Andric Param, Arg.pack_begin(), Arg.pack_size()); 33220b57cec5SDimitry Andric } 33230b57cec5SDimitry Andric } 33240b57cec5SDimitry Andric } 33250b57cec5SDimitry Andric 33260b57cec5SDimitry Andric const FunctionProtoType *Proto 33270b57cec5SDimitry Andric = Function->getType()->getAs<FunctionProtoType>(); 33280b57cec5SDimitry Andric assert(Proto && "Function template does not have a prototype?"); 33290b57cec5SDimitry Andric 33300b57cec5SDimitry Andric // Isolate our substituted parameters from our caller. 33310b57cec5SDimitry Andric LocalInstantiationScope InstScope(*this, /*MergeWithOuterScope*/true); 33320b57cec5SDimitry Andric 33330b57cec5SDimitry Andric ExtParameterInfoBuilder ExtParamInfos; 33340b57cec5SDimitry Andric 3335bdd1243dSDimitry Andric MultiLevelTemplateArgumentList MLTAL(FunctionTemplate, 3336bdd1243dSDimitry Andric SugaredExplicitArgumentList->asArray(), 3337bdd1243dSDimitry Andric /*Final=*/true); 3338bdd1243dSDimitry Andric 33390b57cec5SDimitry Andric // Instantiate the types of each of the function parameters given the 33400b57cec5SDimitry Andric // explicitly-specified template arguments. If the function has a trailing 33410b57cec5SDimitry Andric // return type, substitute it after the arguments to ensure we substitute 33420b57cec5SDimitry Andric // in lexical order. 33430b57cec5SDimitry Andric if (Proto->hasTrailingReturn()) { 33440b57cec5SDimitry Andric if (SubstParmTypes(Function->getLocation(), Function->parameters(), 3345bdd1243dSDimitry Andric Proto->getExtParameterInfosOrNull(), MLTAL, ParamTypes, 3346bdd1243dSDimitry Andric /*params=*/nullptr, ExtParamInfos)) 33470b57cec5SDimitry Andric return TDK_SubstitutionFailure; 33480b57cec5SDimitry Andric } 33490b57cec5SDimitry Andric 33500b57cec5SDimitry Andric // Instantiate the return type. 33510b57cec5SDimitry Andric QualType ResultType; 33520b57cec5SDimitry Andric { 33530b57cec5SDimitry Andric // C++11 [expr.prim.general]p3: 33540b57cec5SDimitry Andric // If a declaration declares a member function or member function 33550b57cec5SDimitry Andric // template of a class X, the expression this is a prvalue of type 33560b57cec5SDimitry Andric // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq 33570b57cec5SDimitry Andric // and the end of the function-definition, member-declarator, or 33580b57cec5SDimitry Andric // declarator. 33590b57cec5SDimitry Andric Qualifiers ThisTypeQuals; 33600b57cec5SDimitry Andric CXXRecordDecl *ThisContext = nullptr; 33610b57cec5SDimitry Andric if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { 33620b57cec5SDimitry Andric ThisContext = Method->getParent(); 33630b57cec5SDimitry Andric ThisTypeQuals = Method->getMethodQualifiers(); 33640b57cec5SDimitry Andric } 33650b57cec5SDimitry Andric 33660b57cec5SDimitry Andric CXXThisScopeRAII ThisScope(*this, ThisContext, ThisTypeQuals, 33670b57cec5SDimitry Andric getLangOpts().CPlusPlus11); 33680b57cec5SDimitry Andric 33690b57cec5SDimitry Andric ResultType = 3370bdd1243dSDimitry Andric SubstType(Proto->getReturnType(), MLTAL, 33710b57cec5SDimitry Andric Function->getTypeSpecStartLoc(), Function->getDeclName()); 33720b57cec5SDimitry Andric if (ResultType.isNull() || Trap.hasErrorOccurred()) 33730b57cec5SDimitry Andric return TDK_SubstitutionFailure; 3374a7dea167SDimitry Andric // CUDA: Kernel function must have 'void' return type. 3375a7dea167SDimitry Andric if (getLangOpts().CUDA) 3376a7dea167SDimitry Andric if (Function->hasAttr<CUDAGlobalAttr>() && !ResultType->isVoidType()) { 3377a7dea167SDimitry Andric Diag(Function->getLocation(), diag::err_kern_type_not_void_return) 3378a7dea167SDimitry Andric << Function->getType() << Function->getSourceRange(); 3379a7dea167SDimitry Andric return TDK_SubstitutionFailure; 3380a7dea167SDimitry Andric } 33810b57cec5SDimitry Andric } 33820b57cec5SDimitry Andric 33830b57cec5SDimitry Andric // Instantiate the types of each of the function parameters given the 33840b57cec5SDimitry Andric // explicitly-specified template arguments if we didn't do so earlier. 33850b57cec5SDimitry Andric if (!Proto->hasTrailingReturn() && 33860b57cec5SDimitry Andric SubstParmTypes(Function->getLocation(), Function->parameters(), 3387bdd1243dSDimitry Andric Proto->getExtParameterInfosOrNull(), MLTAL, ParamTypes, 3388bdd1243dSDimitry Andric /*params*/ nullptr, ExtParamInfos)) 33890b57cec5SDimitry Andric return TDK_SubstitutionFailure; 33900b57cec5SDimitry Andric 33910b57cec5SDimitry Andric if (FunctionType) { 33920b57cec5SDimitry Andric auto EPI = Proto->getExtProtoInfo(); 33930b57cec5SDimitry Andric EPI.ExtParameterInfos = ExtParamInfos.getPointerOrNull(ParamTypes.size()); 33940b57cec5SDimitry Andric 33950b57cec5SDimitry Andric // In C++1z onwards, exception specifications are part of the function type, 33960b57cec5SDimitry Andric // so substitution into the type must also substitute into the exception 33970b57cec5SDimitry Andric // specification. 33980b57cec5SDimitry Andric SmallVector<QualType, 4> ExceptionStorage; 33990b57cec5SDimitry Andric if (getLangOpts().CPlusPlus17 && 3400bdd1243dSDimitry Andric SubstExceptionSpec(Function->getLocation(), EPI.ExceptionSpec, 34015f757f3fSDimitry Andric ExceptionStorage, 34025f757f3fSDimitry Andric getTemplateInstantiationArgs( 34035f757f3fSDimitry Andric FunctionTemplate, nullptr, /*Final=*/true, 34045f757f3fSDimitry Andric /*Innermost=*/SugaredExplicitArgumentList, 34055f757f3fSDimitry Andric /*RelativeToPrimary=*/false, 34065f757f3fSDimitry Andric /*Pattern=*/nullptr, 34075f757f3fSDimitry Andric /*ForConstraintInstantiation=*/false, 34085f757f3fSDimitry Andric /*SkipForSpecialization=*/true))) 34090b57cec5SDimitry Andric return TDK_SubstitutionFailure; 34100b57cec5SDimitry Andric 34110b57cec5SDimitry Andric *FunctionType = BuildFunctionType(ResultType, ParamTypes, 34120b57cec5SDimitry Andric Function->getLocation(), 34130b57cec5SDimitry Andric Function->getDeclName(), 34140b57cec5SDimitry Andric EPI); 34150b57cec5SDimitry Andric if (FunctionType->isNull() || Trap.hasErrorOccurred()) 34160b57cec5SDimitry Andric return TDK_SubstitutionFailure; 34170b57cec5SDimitry Andric } 34180b57cec5SDimitry Andric 34190b57cec5SDimitry Andric // C++ [temp.arg.explicit]p2: 34200b57cec5SDimitry Andric // Trailing template arguments that can be deduced (14.8.2) may be 34210b57cec5SDimitry Andric // omitted from the list of explicit template-arguments. If all of the 34220b57cec5SDimitry Andric // template arguments can be deduced, they may all be omitted; in this 34230b57cec5SDimitry Andric // case, the empty template argument list <> itself may also be omitted. 34240b57cec5SDimitry Andric // 34250b57cec5SDimitry Andric // Take all of the explicitly-specified arguments and put them into 34260b57cec5SDimitry Andric // the set of deduced template arguments. The partially-substituted 34270b57cec5SDimitry Andric // parameter pack, however, will be set to NULL since the deduction 34280b57cec5SDimitry Andric // mechanism handles the partially-substituted argument pack directly. 34290b57cec5SDimitry Andric Deduced.reserve(TemplateParams->size()); 3430bdd1243dSDimitry Andric for (unsigned I = 0, N = SugaredExplicitArgumentList->size(); I != N; ++I) { 3431bdd1243dSDimitry Andric const TemplateArgument &Arg = SugaredExplicitArgumentList->get(I); 34320b57cec5SDimitry Andric if (I == PartiallySubstitutedPackIndex) 34330b57cec5SDimitry Andric Deduced.push_back(DeducedTemplateArgument()); 34340b57cec5SDimitry Andric else 34350b57cec5SDimitry Andric Deduced.push_back(Arg); 34360b57cec5SDimitry Andric } 34370b57cec5SDimitry Andric 34380b57cec5SDimitry Andric return TDK_Success; 34390b57cec5SDimitry Andric } 34400b57cec5SDimitry Andric 34410b57cec5SDimitry Andric /// Check whether the deduced argument type for a call to a function 34420b57cec5SDimitry Andric /// template matches the actual argument type per C++ [temp.deduct.call]p4. 34430b57cec5SDimitry Andric static Sema::TemplateDeductionResult 34440b57cec5SDimitry Andric CheckOriginalCallArgDeduction(Sema &S, TemplateDeductionInfo &Info, 34450b57cec5SDimitry Andric Sema::OriginalCallArg OriginalArg, 34460b57cec5SDimitry Andric QualType DeducedA) { 34470b57cec5SDimitry Andric ASTContext &Context = S.Context; 34480b57cec5SDimitry Andric 34490b57cec5SDimitry Andric auto Failed = [&]() -> Sema::TemplateDeductionResult { 34500b57cec5SDimitry Andric Info.FirstArg = TemplateArgument(DeducedA); 34510b57cec5SDimitry Andric Info.SecondArg = TemplateArgument(OriginalArg.OriginalArgType); 34520b57cec5SDimitry Andric Info.CallArgIndex = OriginalArg.ArgIdx; 34530b57cec5SDimitry Andric return OriginalArg.DecomposedParam ? Sema::TDK_DeducedMismatchNested 34540b57cec5SDimitry Andric : Sema::TDK_DeducedMismatch; 34550b57cec5SDimitry Andric }; 34560b57cec5SDimitry Andric 34570b57cec5SDimitry Andric QualType A = OriginalArg.OriginalArgType; 34580b57cec5SDimitry Andric QualType OriginalParamType = OriginalArg.OriginalParamType; 34590b57cec5SDimitry Andric 34600b57cec5SDimitry Andric // Check for type equality (top-level cv-qualifiers are ignored). 34610b57cec5SDimitry Andric if (Context.hasSameUnqualifiedType(A, DeducedA)) 34620b57cec5SDimitry Andric return Sema::TDK_Success; 34630b57cec5SDimitry Andric 34640b57cec5SDimitry Andric // Strip off references on the argument types; they aren't needed for 34650b57cec5SDimitry Andric // the following checks. 34660b57cec5SDimitry Andric if (const ReferenceType *DeducedARef = DeducedA->getAs<ReferenceType>()) 34670b57cec5SDimitry Andric DeducedA = DeducedARef->getPointeeType(); 34680b57cec5SDimitry Andric if (const ReferenceType *ARef = A->getAs<ReferenceType>()) 34690b57cec5SDimitry Andric A = ARef->getPointeeType(); 34700b57cec5SDimitry Andric 34710b57cec5SDimitry Andric // C++ [temp.deduct.call]p4: 34720b57cec5SDimitry Andric // [...] However, there are three cases that allow a difference: 34730b57cec5SDimitry Andric // - If the original P is a reference type, the deduced A (i.e., the 34740b57cec5SDimitry Andric // type referred to by the reference) can be more cv-qualified than 34750b57cec5SDimitry Andric // the transformed A. 34760b57cec5SDimitry Andric if (const ReferenceType *OriginalParamRef 34770b57cec5SDimitry Andric = OriginalParamType->getAs<ReferenceType>()) { 34780b57cec5SDimitry Andric // We don't want to keep the reference around any more. 34790b57cec5SDimitry Andric OriginalParamType = OriginalParamRef->getPointeeType(); 34800b57cec5SDimitry Andric 34810b57cec5SDimitry Andric // FIXME: Resolve core issue (no number yet): if the original P is a 34820b57cec5SDimitry Andric // reference type and the transformed A is function type "noexcept F", 34830b57cec5SDimitry Andric // the deduced A can be F. 34840b57cec5SDimitry Andric QualType Tmp; 34850b57cec5SDimitry Andric if (A->isFunctionType() && S.IsFunctionConversion(A, DeducedA, Tmp)) 34860b57cec5SDimitry Andric return Sema::TDK_Success; 34870b57cec5SDimitry Andric 34880b57cec5SDimitry Andric Qualifiers AQuals = A.getQualifiers(); 34890b57cec5SDimitry Andric Qualifiers DeducedAQuals = DeducedA.getQualifiers(); 34900b57cec5SDimitry Andric 34910b57cec5SDimitry Andric // Under Objective-C++ ARC, the deduced type may have implicitly 34920b57cec5SDimitry Andric // been given strong or (when dealing with a const reference) 34930b57cec5SDimitry Andric // unsafe_unretained lifetime. If so, update the original 34940b57cec5SDimitry Andric // qualifiers to include this lifetime. 34950b57cec5SDimitry Andric if (S.getLangOpts().ObjCAutoRefCount && 34960b57cec5SDimitry Andric ((DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_Strong && 34970b57cec5SDimitry Andric AQuals.getObjCLifetime() == Qualifiers::OCL_None) || 34980b57cec5SDimitry Andric (DeducedAQuals.hasConst() && 34990b57cec5SDimitry Andric DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone))) { 35000b57cec5SDimitry Andric AQuals.setObjCLifetime(DeducedAQuals.getObjCLifetime()); 35010b57cec5SDimitry Andric } 35020b57cec5SDimitry Andric 35030b57cec5SDimitry Andric if (AQuals == DeducedAQuals) { 35040b57cec5SDimitry Andric // Qualifiers match; there's nothing to do. 35050b57cec5SDimitry Andric } else if (!DeducedAQuals.compatiblyIncludes(AQuals)) { 35060b57cec5SDimitry Andric return Failed(); 35070b57cec5SDimitry Andric } else { 35080b57cec5SDimitry Andric // Qualifiers are compatible, so have the argument type adopt the 35090b57cec5SDimitry Andric // deduced argument type's qualifiers as if we had performed the 35100b57cec5SDimitry Andric // qualification conversion. 35110b57cec5SDimitry Andric A = Context.getQualifiedType(A.getUnqualifiedType(), DeducedAQuals); 35120b57cec5SDimitry Andric } 35130b57cec5SDimitry Andric } 35140b57cec5SDimitry Andric 35150b57cec5SDimitry Andric // - The transformed A can be another pointer or pointer to member 35160b57cec5SDimitry Andric // type that can be converted to the deduced A via a function pointer 35170b57cec5SDimitry Andric // conversion and/or a qualification conversion. 35180b57cec5SDimitry Andric // 35190b57cec5SDimitry Andric // Also allow conversions which merely strip __attribute__((noreturn)) from 35200b57cec5SDimitry Andric // function types (recursively). 35210b57cec5SDimitry Andric bool ObjCLifetimeConversion = false; 35220b57cec5SDimitry Andric QualType ResultTy; 35230b57cec5SDimitry Andric if ((A->isAnyPointerType() || A->isMemberPointerType()) && 35240b57cec5SDimitry Andric (S.IsQualificationConversion(A, DeducedA, false, 35250b57cec5SDimitry Andric ObjCLifetimeConversion) || 35260b57cec5SDimitry Andric S.IsFunctionConversion(A, DeducedA, ResultTy))) 35270b57cec5SDimitry Andric return Sema::TDK_Success; 35280b57cec5SDimitry Andric 35290b57cec5SDimitry Andric // - If P is a class and P has the form simple-template-id, then the 35300b57cec5SDimitry Andric // transformed A can be a derived class of the deduced A. [...] 35310b57cec5SDimitry Andric // [...] Likewise, if P is a pointer to a class of the form 35320b57cec5SDimitry Andric // simple-template-id, the transformed A can be a pointer to a 35330b57cec5SDimitry Andric // derived class pointed to by the deduced A. 35340b57cec5SDimitry Andric if (const PointerType *OriginalParamPtr 35350b57cec5SDimitry Andric = OriginalParamType->getAs<PointerType>()) { 35360b57cec5SDimitry Andric if (const PointerType *DeducedAPtr = DeducedA->getAs<PointerType>()) { 35370b57cec5SDimitry Andric if (const PointerType *APtr = A->getAs<PointerType>()) { 35380b57cec5SDimitry Andric if (A->getPointeeType()->isRecordType()) { 35390b57cec5SDimitry Andric OriginalParamType = OriginalParamPtr->getPointeeType(); 35400b57cec5SDimitry Andric DeducedA = DeducedAPtr->getPointeeType(); 35410b57cec5SDimitry Andric A = APtr->getPointeeType(); 35420b57cec5SDimitry Andric } 35430b57cec5SDimitry Andric } 35440b57cec5SDimitry Andric } 35450b57cec5SDimitry Andric } 35460b57cec5SDimitry Andric 35470b57cec5SDimitry Andric if (Context.hasSameUnqualifiedType(A, DeducedA)) 35480b57cec5SDimitry Andric return Sema::TDK_Success; 35490b57cec5SDimitry Andric 35500b57cec5SDimitry Andric if (A->isRecordType() && isSimpleTemplateIdType(OriginalParamType) && 35510b57cec5SDimitry Andric S.IsDerivedFrom(Info.getLocation(), A, DeducedA)) 35520b57cec5SDimitry Andric return Sema::TDK_Success; 35530b57cec5SDimitry Andric 35540b57cec5SDimitry Andric return Failed(); 35550b57cec5SDimitry Andric } 35560b57cec5SDimitry Andric 35570b57cec5SDimitry Andric /// Find the pack index for a particular parameter index in an instantiation of 35580b57cec5SDimitry Andric /// a function template with specific arguments. 35590b57cec5SDimitry Andric /// 35600b57cec5SDimitry Andric /// \return The pack index for whichever pack produced this parameter, or -1 35610b57cec5SDimitry Andric /// if this was not produced by a parameter. Intended to be used as the 35620b57cec5SDimitry Andric /// ArgumentPackSubstitutionIndex for further substitutions. 35630b57cec5SDimitry Andric // FIXME: We should track this in OriginalCallArgs so we don't need to 35640b57cec5SDimitry Andric // reconstruct it here. 35650b57cec5SDimitry Andric static unsigned getPackIndexForParam(Sema &S, 35660b57cec5SDimitry Andric FunctionTemplateDecl *FunctionTemplate, 35670b57cec5SDimitry Andric const MultiLevelTemplateArgumentList &Args, 35680b57cec5SDimitry Andric unsigned ParamIdx) { 35690b57cec5SDimitry Andric unsigned Idx = 0; 35700b57cec5SDimitry Andric for (auto *PD : FunctionTemplate->getTemplatedDecl()->parameters()) { 35710b57cec5SDimitry Andric if (PD->isParameterPack()) { 35720b57cec5SDimitry Andric unsigned NumExpansions = 357381ad6265SDimitry Andric S.getNumArgumentsInExpansion(PD->getType(), Args).value_or(1); 35740b57cec5SDimitry Andric if (Idx + NumExpansions > ParamIdx) 35750b57cec5SDimitry Andric return ParamIdx - Idx; 35760b57cec5SDimitry Andric Idx += NumExpansions; 35770b57cec5SDimitry Andric } else { 35780b57cec5SDimitry Andric if (Idx == ParamIdx) 35790b57cec5SDimitry Andric return -1; // Not a pack expansion 35800b57cec5SDimitry Andric ++Idx; 35810b57cec5SDimitry Andric } 35820b57cec5SDimitry Andric } 35830b57cec5SDimitry Andric 35840b57cec5SDimitry Andric llvm_unreachable("parameter index would not be produced from template"); 35850b57cec5SDimitry Andric } 35860b57cec5SDimitry Andric 35875f757f3fSDimitry Andric // if `Specialization` is a `CXXConstructorDecl` or `CXXConversionDecl`, 35885f757f3fSDimitry Andric // we'll try to instantiate and update its explicit specifier after constraint 35895f757f3fSDimitry Andric // checking. 35905f757f3fSDimitry Andric static Sema::TemplateDeductionResult instantiateExplicitSpecifierDeferred( 35915f757f3fSDimitry Andric Sema &S, FunctionDecl *Specialization, 35925f757f3fSDimitry Andric const MultiLevelTemplateArgumentList &SubstArgs, 35935f757f3fSDimitry Andric TemplateDeductionInfo &Info, FunctionTemplateDecl *FunctionTemplate, 35945f757f3fSDimitry Andric ArrayRef<TemplateArgument> DeducedArgs) { 35955f757f3fSDimitry Andric auto GetExplicitSpecifier = [](FunctionDecl *D) { 35965f757f3fSDimitry Andric return isa<CXXConstructorDecl>(D) 35975f757f3fSDimitry Andric ? cast<CXXConstructorDecl>(D)->getExplicitSpecifier() 35985f757f3fSDimitry Andric : cast<CXXConversionDecl>(D)->getExplicitSpecifier(); 35995f757f3fSDimitry Andric }; 36005f757f3fSDimitry Andric auto SetExplicitSpecifier = [](FunctionDecl *D, ExplicitSpecifier ES) { 36015f757f3fSDimitry Andric isa<CXXConstructorDecl>(D) 36025f757f3fSDimitry Andric ? cast<CXXConstructorDecl>(D)->setExplicitSpecifier(ES) 36035f757f3fSDimitry Andric : cast<CXXConversionDecl>(D)->setExplicitSpecifier(ES); 36045f757f3fSDimitry Andric }; 36055f757f3fSDimitry Andric 36065f757f3fSDimitry Andric ExplicitSpecifier ES = GetExplicitSpecifier(Specialization); 36075f757f3fSDimitry Andric Expr *ExplicitExpr = ES.getExpr(); 36085f757f3fSDimitry Andric if (!ExplicitExpr) 36095f757f3fSDimitry Andric return Sema::TDK_Success; 36105f757f3fSDimitry Andric if (!ExplicitExpr->isValueDependent()) 36115f757f3fSDimitry Andric return Sema::TDK_Success; 36125f757f3fSDimitry Andric 36135f757f3fSDimitry Andric Sema::InstantiatingTemplate Inst( 36145f757f3fSDimitry Andric S, Info.getLocation(), FunctionTemplate, DeducedArgs, 36155f757f3fSDimitry Andric Sema::CodeSynthesisContext::DeducedTemplateArgumentSubstitution, Info); 36165f757f3fSDimitry Andric if (Inst.isInvalid()) 36175f757f3fSDimitry Andric return Sema::TDK_InstantiationDepth; 36185f757f3fSDimitry Andric Sema::SFINAETrap Trap(S); 36195f757f3fSDimitry Andric const ExplicitSpecifier InstantiatedES = 36205f757f3fSDimitry Andric S.instantiateExplicitSpecifier(SubstArgs, ES); 36215f757f3fSDimitry Andric if (InstantiatedES.isInvalid() || Trap.hasErrorOccurred()) { 36225f757f3fSDimitry Andric Specialization->setInvalidDecl(true); 36235f757f3fSDimitry Andric return Sema::TDK_SubstitutionFailure; 36245f757f3fSDimitry Andric } 36255f757f3fSDimitry Andric SetExplicitSpecifier(Specialization, InstantiatedES); 36265f757f3fSDimitry Andric return Sema::TDK_Success; 36275f757f3fSDimitry Andric } 36285f757f3fSDimitry Andric 36290b57cec5SDimitry Andric /// Finish template argument deduction for a function template, 36300b57cec5SDimitry Andric /// checking the deduced template arguments for completeness and forming 36310b57cec5SDimitry Andric /// the function template specialization. 36320b57cec5SDimitry Andric /// 36330b57cec5SDimitry Andric /// \param OriginalCallArgs If non-NULL, the original call arguments against 36340b57cec5SDimitry Andric /// which the deduced argument types should be compared. 36350b57cec5SDimitry Andric Sema::TemplateDeductionResult Sema::FinishTemplateArgumentDeduction( 36360b57cec5SDimitry Andric FunctionTemplateDecl *FunctionTemplate, 36370b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 36380b57cec5SDimitry Andric unsigned NumExplicitlySpecified, FunctionDecl *&Specialization, 36390b57cec5SDimitry Andric TemplateDeductionInfo &Info, 36400b57cec5SDimitry Andric SmallVectorImpl<OriginalCallArg> const *OriginalCallArgs, 36410b57cec5SDimitry Andric bool PartialOverloading, llvm::function_ref<bool()> CheckNonDependent) { 36420b57cec5SDimitry Andric // Unevaluated SFINAE context. 36430b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 36440b57cec5SDimitry Andric *this, Sema::ExpressionEvaluationContext::Unevaluated); 36450b57cec5SDimitry Andric SFINAETrap Trap(*this); 36460b57cec5SDimitry Andric 36470b57cec5SDimitry Andric // Enter a new template instantiation context while we instantiate the 36480b57cec5SDimitry Andric // actual function declaration. 36490b57cec5SDimitry Andric SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); 36500b57cec5SDimitry Andric InstantiatingTemplate Inst( 36510b57cec5SDimitry Andric *this, Info.getLocation(), FunctionTemplate, DeducedArgs, 36520b57cec5SDimitry Andric CodeSynthesisContext::DeducedTemplateArgumentSubstitution, Info); 36530b57cec5SDimitry Andric if (Inst.isInvalid()) 36540b57cec5SDimitry Andric return TDK_InstantiationDepth; 36550b57cec5SDimitry Andric 36560b57cec5SDimitry Andric ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); 36570b57cec5SDimitry Andric 36580b57cec5SDimitry Andric // C++ [temp.deduct.type]p2: 36590b57cec5SDimitry Andric // [...] or if any template argument remains neither deduced nor 36600b57cec5SDimitry Andric // explicitly specified, template argument deduction fails. 3661bdd1243dSDimitry Andric SmallVector<TemplateArgument, 4> SugaredBuilder, CanonicalBuilder; 36620b57cec5SDimitry Andric if (auto Result = ConvertDeducedTemplateArguments( 3663bdd1243dSDimitry Andric *this, FunctionTemplate, /*IsDeduced*/ true, Deduced, Info, 3664bdd1243dSDimitry Andric SugaredBuilder, CanonicalBuilder, CurrentInstantiationScope, 3665bdd1243dSDimitry Andric NumExplicitlySpecified, PartialOverloading)) 36660b57cec5SDimitry Andric return Result; 36670b57cec5SDimitry Andric 36680b57cec5SDimitry Andric // C++ [temp.deduct.call]p10: [DR1391] 36690b57cec5SDimitry Andric // If deduction succeeds for all parameters that contain 36700b57cec5SDimitry Andric // template-parameters that participate in template argument deduction, 36710b57cec5SDimitry Andric // and all template arguments are explicitly specified, deduced, or 36720b57cec5SDimitry Andric // obtained from default template arguments, remaining parameters are then 36730b57cec5SDimitry Andric // compared with the corresponding arguments. For each remaining parameter 36740b57cec5SDimitry Andric // P with a type that was non-dependent before substitution of any 36750b57cec5SDimitry Andric // explicitly-specified template arguments, if the corresponding argument 36760b57cec5SDimitry Andric // A cannot be implicitly converted to P, deduction fails. 36770b57cec5SDimitry Andric if (CheckNonDependent()) 36780b57cec5SDimitry Andric return TDK_NonDependentConversionFailure; 36790b57cec5SDimitry Andric 36800b57cec5SDimitry Andric // Form the template argument list from the deduced template arguments. 3681bdd1243dSDimitry Andric TemplateArgumentList *SugaredDeducedArgumentList = 3682bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(Context, SugaredBuilder); 3683bdd1243dSDimitry Andric TemplateArgumentList *CanonicalDeducedArgumentList = 3684bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(Context, CanonicalBuilder); 3685bdd1243dSDimitry Andric Info.reset(SugaredDeducedArgumentList, CanonicalDeducedArgumentList); 36860b57cec5SDimitry Andric 36870b57cec5SDimitry Andric // Substitute the deduced template arguments into the function template 36880b57cec5SDimitry Andric // declaration to produce the function template specialization. 36890b57cec5SDimitry Andric DeclContext *Owner = FunctionTemplate->getDeclContext(); 36900b57cec5SDimitry Andric if (FunctionTemplate->getFriendObjectKind()) 36910b57cec5SDimitry Andric Owner = FunctionTemplate->getLexicalDeclContext(); 369206c3fb27SDimitry Andric FunctionDecl *FD = FunctionTemplate->getTemplatedDecl(); 369306c3fb27SDimitry Andric // additional check for inline friend, 369406c3fb27SDimitry Andric // ``` 369506c3fb27SDimitry Andric // template <class F1> int foo(F1 X); 369606c3fb27SDimitry Andric // template <int A1> struct A { 369706c3fb27SDimitry Andric // template <class F1> friend int foo(F1 X) { return A1; } 369806c3fb27SDimitry Andric // }; 369906c3fb27SDimitry Andric // template struct A<1>; 370006c3fb27SDimitry Andric // int a = foo(1.0); 370106c3fb27SDimitry Andric // ``` 370206c3fb27SDimitry Andric const FunctionDecl *FDFriend; 370306c3fb27SDimitry Andric if (FD->getFriendObjectKind() == Decl::FriendObjectKind::FOK_None && 370406c3fb27SDimitry Andric FD->isDefined(FDFriend, /*CheckForPendingFriendDefinition*/ true) && 370506c3fb27SDimitry Andric FDFriend->getFriendObjectKind() != Decl::FriendObjectKind::FOK_None) { 370606c3fb27SDimitry Andric FD = const_cast<FunctionDecl *>(FDFriend); 370706c3fb27SDimitry Andric Owner = FD->getLexicalDeclContext(); 370806c3fb27SDimitry Andric } 3709bdd1243dSDimitry Andric MultiLevelTemplateArgumentList SubstArgs( 3710bdd1243dSDimitry Andric FunctionTemplate, CanonicalDeducedArgumentList->asArray(), 3711bdd1243dSDimitry Andric /*Final=*/false); 37120b57cec5SDimitry Andric Specialization = cast_or_null<FunctionDecl>( 371306c3fb27SDimitry Andric SubstDecl(FD, Owner, SubstArgs)); 37140b57cec5SDimitry Andric if (!Specialization || Specialization->isInvalidDecl()) 37150b57cec5SDimitry Andric return TDK_SubstitutionFailure; 37160b57cec5SDimitry Andric 37170b57cec5SDimitry Andric assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == 37180b57cec5SDimitry Andric FunctionTemplate->getCanonicalDecl()); 37190b57cec5SDimitry Andric 37200b57cec5SDimitry Andric // If the template argument list is owned by the function template 37210b57cec5SDimitry Andric // specialization, release it. 3722bdd1243dSDimitry Andric if (Specialization->getTemplateSpecializationArgs() == 3723bdd1243dSDimitry Andric CanonicalDeducedArgumentList && 37240b57cec5SDimitry Andric !Trap.hasErrorOccurred()) 3725bdd1243dSDimitry Andric Info.takeCanonical(); 37260b57cec5SDimitry Andric 37270b57cec5SDimitry Andric // There may have been an error that did not prevent us from constructing a 37280b57cec5SDimitry Andric // declaration. Mark the declaration invalid and return with a substitution 37290b57cec5SDimitry Andric // failure. 37300b57cec5SDimitry Andric if (Trap.hasErrorOccurred()) { 37310b57cec5SDimitry Andric Specialization->setInvalidDecl(true); 37320b57cec5SDimitry Andric return TDK_SubstitutionFailure; 37330b57cec5SDimitry Andric } 37340b57cec5SDimitry Andric 3735480093f4SDimitry Andric // C++2a [temp.deduct]p5 3736480093f4SDimitry Andric // [...] When all template arguments have been deduced [...] all uses of 3737480093f4SDimitry Andric // template parameters [...] are replaced with the corresponding deduced 3738480093f4SDimitry Andric // or default argument values. 3739480093f4SDimitry Andric // [...] If the function template has associated constraints 3740480093f4SDimitry Andric // ([temp.constr.decl]), those constraints are checked for satisfaction 3741480093f4SDimitry Andric // ([temp.constr.constr]). If the constraints are not satisfied, type 3742480093f4SDimitry Andric // deduction fails. 374313138422SDimitry Andric if (!PartialOverloading || 3744bdd1243dSDimitry Andric (CanonicalBuilder.size() == 3745bdd1243dSDimitry Andric FunctionTemplate->getTemplateParameters()->size())) { 3746bdd1243dSDimitry Andric if (CheckInstantiatedFunctionTemplateConstraints( 3747bdd1243dSDimitry Andric Info.getLocation(), Specialization, CanonicalBuilder, 3748bdd1243dSDimitry Andric Info.AssociatedConstraintsSatisfaction)) 3749480093f4SDimitry Andric return TDK_MiscellaneousDeductionFailure; 3750480093f4SDimitry Andric 3751480093f4SDimitry Andric if (!Info.AssociatedConstraintsSatisfaction.IsSatisfied) { 3752bdd1243dSDimitry Andric Info.reset(Info.takeSugared(), 3753bdd1243dSDimitry Andric TemplateArgumentList::CreateCopy(Context, CanonicalBuilder)); 3754480093f4SDimitry Andric return TDK_ConstraintsNotSatisfied; 3755480093f4SDimitry Andric } 375613138422SDimitry Andric } 3757480093f4SDimitry Andric 37585f757f3fSDimitry Andric // We skipped the instantiation of the explicit-specifier during the 37595f757f3fSDimitry Andric // substitution of `FD` before. So, we try to instantiate it back if 37605f757f3fSDimitry Andric // `Specialization` is either a constructor or a conversion function. 37615f757f3fSDimitry Andric if (isa<CXXConstructorDecl, CXXConversionDecl>(Specialization)) { 37625f757f3fSDimitry Andric if (TDK_Success != instantiateExplicitSpecifierDeferred( 37635f757f3fSDimitry Andric *this, Specialization, SubstArgs, Info, 37645f757f3fSDimitry Andric FunctionTemplate, DeducedArgs)) { 37655f757f3fSDimitry Andric return TDK_SubstitutionFailure; 37665f757f3fSDimitry Andric } 37675f757f3fSDimitry Andric } 37685f757f3fSDimitry Andric 37690b57cec5SDimitry Andric if (OriginalCallArgs) { 37700b57cec5SDimitry Andric // C++ [temp.deduct.call]p4: 37710b57cec5SDimitry Andric // In general, the deduction process attempts to find template argument 37720b57cec5SDimitry Andric // values that will make the deduced A identical to A (after the type A 37730b57cec5SDimitry Andric // is transformed as described above). [...] 37740b57cec5SDimitry Andric llvm::SmallDenseMap<std::pair<unsigned, QualType>, QualType> DeducedATypes; 37750b57cec5SDimitry Andric for (unsigned I = 0, N = OriginalCallArgs->size(); I != N; ++I) { 37760b57cec5SDimitry Andric OriginalCallArg OriginalArg = (*OriginalCallArgs)[I]; 37770b57cec5SDimitry Andric 37780b57cec5SDimitry Andric auto ParamIdx = OriginalArg.ArgIdx; 37795f757f3fSDimitry Andric unsigned ExplicitOffset = 37805f757f3fSDimitry Andric Specialization->hasCXXExplicitFunctionObjectParameter() ? 1 : 0; 37815f757f3fSDimitry Andric if (ParamIdx >= Specialization->getNumParams() - ExplicitOffset) 37820b57cec5SDimitry Andric // FIXME: This presumably means a pack ended up smaller than we 37830b57cec5SDimitry Andric // expected while deducing. Should this not result in deduction 37840b57cec5SDimitry Andric // failure? Can it even happen? 37850b57cec5SDimitry Andric continue; 37860b57cec5SDimitry Andric 37870b57cec5SDimitry Andric QualType DeducedA; 37880b57cec5SDimitry Andric if (!OriginalArg.DecomposedParam) { 37890b57cec5SDimitry Andric // P is one of the function parameters, just look up its substituted 37900b57cec5SDimitry Andric // type. 37915f757f3fSDimitry Andric DeducedA = 37925f757f3fSDimitry Andric Specialization->getParamDecl(ParamIdx + ExplicitOffset)->getType(); 37930b57cec5SDimitry Andric } else { 37940b57cec5SDimitry Andric // P is a decomposed element of a parameter corresponding to a 37950b57cec5SDimitry Andric // braced-init-list argument. Substitute back into P to find the 37960b57cec5SDimitry Andric // deduced A. 37970b57cec5SDimitry Andric QualType &CacheEntry = 37980b57cec5SDimitry Andric DeducedATypes[{ParamIdx, OriginalArg.OriginalParamType}]; 37990b57cec5SDimitry Andric if (CacheEntry.isNull()) { 38000b57cec5SDimitry Andric ArgumentPackSubstitutionIndexRAII PackIndex( 38010b57cec5SDimitry Andric *this, getPackIndexForParam(*this, FunctionTemplate, SubstArgs, 38020b57cec5SDimitry Andric ParamIdx)); 38030b57cec5SDimitry Andric CacheEntry = 38040b57cec5SDimitry Andric SubstType(OriginalArg.OriginalParamType, SubstArgs, 38050b57cec5SDimitry Andric Specialization->getTypeSpecStartLoc(), 38060b57cec5SDimitry Andric Specialization->getDeclName()); 38070b57cec5SDimitry Andric } 38080b57cec5SDimitry Andric DeducedA = CacheEntry; 38090b57cec5SDimitry Andric } 38100b57cec5SDimitry Andric 38110b57cec5SDimitry Andric if (auto TDK = 38120b57cec5SDimitry Andric CheckOriginalCallArgDeduction(*this, Info, OriginalArg, DeducedA)) 38130b57cec5SDimitry Andric return TDK; 38140b57cec5SDimitry Andric } 38150b57cec5SDimitry Andric } 38160b57cec5SDimitry Andric 38170b57cec5SDimitry Andric // If we suppressed any diagnostics while performing template argument 38180b57cec5SDimitry Andric // deduction, and if we haven't already instantiated this declaration, 38190b57cec5SDimitry Andric // keep track of these diagnostics. They'll be emitted if this specialization 38200b57cec5SDimitry Andric // is actually used. 38210b57cec5SDimitry Andric if (Info.diag_begin() != Info.diag_end()) { 38220b57cec5SDimitry Andric SuppressedDiagnosticsMap::iterator 38230b57cec5SDimitry Andric Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl()); 38240b57cec5SDimitry Andric if (Pos == SuppressedDiagnostics.end()) 38250b57cec5SDimitry Andric SuppressedDiagnostics[Specialization->getCanonicalDecl()] 38260b57cec5SDimitry Andric .append(Info.diag_begin(), Info.diag_end()); 38270b57cec5SDimitry Andric } 38280b57cec5SDimitry Andric 38290b57cec5SDimitry Andric return TDK_Success; 38300b57cec5SDimitry Andric } 38310b57cec5SDimitry Andric 38320b57cec5SDimitry Andric /// Gets the type of a function for template-argument-deducton 38330b57cec5SDimitry Andric /// purposes when it's considered as part of an overload set. 38340b57cec5SDimitry Andric static QualType GetTypeOfFunction(Sema &S, const OverloadExpr::FindResult &R, 38350b57cec5SDimitry Andric FunctionDecl *Fn) { 38360b57cec5SDimitry Andric // We may need to deduce the return type of the function now. 38370b57cec5SDimitry Andric if (S.getLangOpts().CPlusPlus14 && Fn->getReturnType()->isUndeducedType() && 38380b57cec5SDimitry Andric S.DeduceReturnType(Fn, R.Expression->getExprLoc(), /*Diagnose*/ false)) 38390b57cec5SDimitry Andric return {}; 38400b57cec5SDimitry Andric 38410b57cec5SDimitry Andric if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) 38425f757f3fSDimitry Andric if (Method->isImplicitObjectMemberFunction()) { 38430b57cec5SDimitry Andric // An instance method that's referenced in a form that doesn't 38440b57cec5SDimitry Andric // look like a member pointer is just invalid. 38450b57cec5SDimitry Andric if (!R.HasFormOfMemberPointer) 38460b57cec5SDimitry Andric return {}; 38470b57cec5SDimitry Andric 38480b57cec5SDimitry Andric return S.Context.getMemberPointerType(Fn->getType(), 38490b57cec5SDimitry Andric S.Context.getTypeDeclType(Method->getParent()).getTypePtr()); 38500b57cec5SDimitry Andric } 38510b57cec5SDimitry Andric 38520b57cec5SDimitry Andric if (!R.IsAddressOfOperand) return Fn->getType(); 38530b57cec5SDimitry Andric return S.Context.getPointerType(Fn->getType()); 38540b57cec5SDimitry Andric } 38550b57cec5SDimitry Andric 38560b57cec5SDimitry Andric /// Apply the deduction rules for overload sets. 38570b57cec5SDimitry Andric /// 38580b57cec5SDimitry Andric /// \return the null type if this argument should be treated as an 38590b57cec5SDimitry Andric /// undeduced context 38600b57cec5SDimitry Andric static QualType 38610b57cec5SDimitry Andric ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams, 38620b57cec5SDimitry Andric Expr *Arg, QualType ParamType, 386306c3fb27SDimitry Andric bool ParamWasReference, 386406c3fb27SDimitry Andric TemplateSpecCandidateSet *FailedTSC = nullptr) { 38650b57cec5SDimitry Andric 38660b57cec5SDimitry Andric OverloadExpr::FindResult R = OverloadExpr::find(Arg); 38670b57cec5SDimitry Andric 38680b57cec5SDimitry Andric OverloadExpr *Ovl = R.Expression; 38690b57cec5SDimitry Andric 38700b57cec5SDimitry Andric // C++0x [temp.deduct.call]p4 38710b57cec5SDimitry Andric unsigned TDF = 0; 38720b57cec5SDimitry Andric if (ParamWasReference) 38730b57cec5SDimitry Andric TDF |= TDF_ParamWithReferenceType; 38740b57cec5SDimitry Andric if (R.IsAddressOfOperand) 38750b57cec5SDimitry Andric TDF |= TDF_IgnoreQualifiers; 38760b57cec5SDimitry Andric 38770b57cec5SDimitry Andric // C++0x [temp.deduct.call]p6: 38780b57cec5SDimitry Andric // When P is a function type, pointer to function type, or pointer 38790b57cec5SDimitry Andric // to member function type: 38800b57cec5SDimitry Andric 38810b57cec5SDimitry Andric if (!ParamType->isFunctionType() && 38820b57cec5SDimitry Andric !ParamType->isFunctionPointerType() && 38830b57cec5SDimitry Andric !ParamType->isMemberFunctionPointerType()) { 38840b57cec5SDimitry Andric if (Ovl->hasExplicitTemplateArgs()) { 38850b57cec5SDimitry Andric // But we can still look for an explicit specialization. 388606c3fb27SDimitry Andric if (FunctionDecl *ExplicitSpec = 388706c3fb27SDimitry Andric S.ResolveSingleFunctionTemplateSpecialization( 388806c3fb27SDimitry Andric Ovl, /*Complain=*/false, 388906c3fb27SDimitry Andric /*FoundDeclAccessPair=*/nullptr, FailedTSC)) 38900b57cec5SDimitry Andric return GetTypeOfFunction(S, R, ExplicitSpec); 38910b57cec5SDimitry Andric } 38920b57cec5SDimitry Andric 38930b57cec5SDimitry Andric DeclAccessPair DAP; 38940b57cec5SDimitry Andric if (FunctionDecl *Viable = 3895480093f4SDimitry Andric S.resolveAddressOfSingleOverloadCandidate(Arg, DAP)) 38960b57cec5SDimitry Andric return GetTypeOfFunction(S, R, Viable); 38970b57cec5SDimitry Andric 38980b57cec5SDimitry Andric return {}; 38990b57cec5SDimitry Andric } 39000b57cec5SDimitry Andric 39010b57cec5SDimitry Andric // Gather the explicit template arguments, if any. 39020b57cec5SDimitry Andric TemplateArgumentListInfo ExplicitTemplateArgs; 39030b57cec5SDimitry Andric if (Ovl->hasExplicitTemplateArgs()) 39040b57cec5SDimitry Andric Ovl->copyTemplateArgumentsInto(ExplicitTemplateArgs); 39050b57cec5SDimitry Andric QualType Match; 39060b57cec5SDimitry Andric for (UnresolvedSetIterator I = Ovl->decls_begin(), 39070b57cec5SDimitry Andric E = Ovl->decls_end(); I != E; ++I) { 39080b57cec5SDimitry Andric NamedDecl *D = (*I)->getUnderlyingDecl(); 39090b57cec5SDimitry Andric 39100b57cec5SDimitry Andric if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) { 39110b57cec5SDimitry Andric // - If the argument is an overload set containing one or more 39120b57cec5SDimitry Andric // function templates, the parameter is treated as a 39130b57cec5SDimitry Andric // non-deduced context. 39140b57cec5SDimitry Andric if (!Ovl->hasExplicitTemplateArgs()) 39150b57cec5SDimitry Andric return {}; 39160b57cec5SDimitry Andric 39170b57cec5SDimitry Andric // Otherwise, see if we can resolve a function type 39180b57cec5SDimitry Andric FunctionDecl *Specialization = nullptr; 39190b57cec5SDimitry Andric TemplateDeductionInfo Info(Ovl->getNameLoc()); 39200b57cec5SDimitry Andric if (S.DeduceTemplateArguments(FunTmpl, &ExplicitTemplateArgs, 39210b57cec5SDimitry Andric Specialization, Info)) 39220b57cec5SDimitry Andric continue; 39230b57cec5SDimitry Andric 39240b57cec5SDimitry Andric D = Specialization; 39250b57cec5SDimitry Andric } 39260b57cec5SDimitry Andric 39270b57cec5SDimitry Andric FunctionDecl *Fn = cast<FunctionDecl>(D); 39280b57cec5SDimitry Andric QualType ArgType = GetTypeOfFunction(S, R, Fn); 39290b57cec5SDimitry Andric if (ArgType.isNull()) continue; 39300b57cec5SDimitry Andric 39310b57cec5SDimitry Andric // Function-to-pointer conversion. 39320b57cec5SDimitry Andric if (!ParamWasReference && ParamType->isPointerType() && 39330b57cec5SDimitry Andric ArgType->isFunctionType()) 39340b57cec5SDimitry Andric ArgType = S.Context.getPointerType(ArgType); 39350b57cec5SDimitry Andric 39360b57cec5SDimitry Andric // - If the argument is an overload set (not containing function 39370b57cec5SDimitry Andric // templates), trial argument deduction is attempted using each 39380b57cec5SDimitry Andric // of the members of the set. If deduction succeeds for only one 39390b57cec5SDimitry Andric // of the overload set members, that member is used as the 39400b57cec5SDimitry Andric // argument value for the deduction. If deduction succeeds for 39410b57cec5SDimitry Andric // more than one member of the overload set the parameter is 39420b57cec5SDimitry Andric // treated as a non-deduced context. 39430b57cec5SDimitry Andric 39440b57cec5SDimitry Andric // We do all of this in a fresh context per C++0x [temp.deduct.type]p2: 39450b57cec5SDimitry Andric // Type deduction is done independently for each P/A pair, and 39460b57cec5SDimitry Andric // the deduced template argument values are then combined. 39470b57cec5SDimitry Andric // So we do not reject deductions which were made elsewhere. 39480b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 8> 39490b57cec5SDimitry Andric Deduced(TemplateParams->size()); 39500b57cec5SDimitry Andric TemplateDeductionInfo Info(Ovl->getNameLoc()); 39510b57cec5SDimitry Andric Sema::TemplateDeductionResult Result 39520b57cec5SDimitry Andric = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, 39530b57cec5SDimitry Andric ArgType, Info, Deduced, TDF); 39540b57cec5SDimitry Andric if (Result) continue; 39550b57cec5SDimitry Andric if (!Match.isNull()) 39560b57cec5SDimitry Andric return {}; 39570b57cec5SDimitry Andric Match = ArgType; 39580b57cec5SDimitry Andric } 39590b57cec5SDimitry Andric 39600b57cec5SDimitry Andric return Match; 39610b57cec5SDimitry Andric } 39620b57cec5SDimitry Andric 39630b57cec5SDimitry Andric /// Perform the adjustments to the parameter and argument types 39640b57cec5SDimitry Andric /// described in C++ [temp.deduct.call]. 39650b57cec5SDimitry Andric /// 39660b57cec5SDimitry Andric /// \returns true if the caller should not attempt to perform any template 39670b57cec5SDimitry Andric /// argument deduction based on this P/A pair because the argument is an 39680b57cec5SDimitry Andric /// overloaded function set that could not be resolved. 39690b57cec5SDimitry Andric static bool AdjustFunctionParmAndArgTypesForDeduction( 39700b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, 39715f757f3fSDimitry Andric QualType &ParamType, QualType &ArgType, 39725f757f3fSDimitry Andric Expr::Classification ArgClassification, Expr *Arg, unsigned &TDF, 397306c3fb27SDimitry Andric TemplateSpecCandidateSet *FailedTSC = nullptr) { 39740b57cec5SDimitry Andric // C++0x [temp.deduct.call]p3: 39750b57cec5SDimitry Andric // If P is a cv-qualified type, the top level cv-qualifiers of P's type 39760b57cec5SDimitry Andric // are ignored for type deduction. 39770b57cec5SDimitry Andric if (ParamType.hasQualifiers()) 39780b57cec5SDimitry Andric ParamType = ParamType.getUnqualifiedType(); 39790b57cec5SDimitry Andric 39800b57cec5SDimitry Andric // [...] If P is a reference type, the type referred to by P is 39810b57cec5SDimitry Andric // used for type deduction. 39820b57cec5SDimitry Andric const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>(); 39830b57cec5SDimitry Andric if (ParamRefType) 39840b57cec5SDimitry Andric ParamType = ParamRefType->getPointeeType(); 39850b57cec5SDimitry Andric 39860b57cec5SDimitry Andric // Overload sets usually make this parameter an undeduced context, 39870b57cec5SDimitry Andric // but there are sometimes special circumstances. Typically 39880b57cec5SDimitry Andric // involving a template-id-expr. 39890b57cec5SDimitry Andric if (ArgType == S.Context.OverloadTy) { 39905f757f3fSDimitry Andric assert(Arg && "expected a non-null arg expression"); 399106c3fb27SDimitry Andric ArgType = ResolveOverloadForDeduction(S, TemplateParams, Arg, ParamType, 399206c3fb27SDimitry Andric ParamRefType != nullptr, FailedTSC); 39930b57cec5SDimitry Andric if (ArgType.isNull()) 39940b57cec5SDimitry Andric return true; 39950b57cec5SDimitry Andric } 39960b57cec5SDimitry Andric 39970b57cec5SDimitry Andric if (ParamRefType) { 39980b57cec5SDimitry Andric // If the argument has incomplete array type, try to complete its type. 39995f757f3fSDimitry Andric if (ArgType->isIncompleteArrayType()) { 40005f757f3fSDimitry Andric assert(Arg && "expected a non-null arg expression"); 4001e8d8bef9SDimitry Andric ArgType = S.getCompletedType(Arg); 40025f757f3fSDimitry Andric } 40030b57cec5SDimitry Andric 40040b57cec5SDimitry Andric // C++1z [temp.deduct.call]p3: 40050b57cec5SDimitry Andric // If P is a forwarding reference and the argument is an lvalue, the type 40060b57cec5SDimitry Andric // "lvalue reference to A" is used in place of A for type deduction. 40070b57cec5SDimitry Andric if (isForwardingReference(QualType(ParamRefType, 0), FirstInnerIndex) && 40085f757f3fSDimitry Andric ArgClassification.isLValue()) { 4009fe6060f1SDimitry Andric if (S.getLangOpts().OpenCL && !ArgType.hasAddressSpace()) 4010349cc55cSDimitry Andric ArgType = S.Context.getAddrSpaceQualType( 4011349cc55cSDimitry Andric ArgType, S.Context.getDefaultOpenCLPointeeAddrSpace()); 40120b57cec5SDimitry Andric ArgType = S.Context.getLValueReferenceType(ArgType); 4013e8d8bef9SDimitry Andric } 40140b57cec5SDimitry Andric } else { 40150b57cec5SDimitry Andric // C++ [temp.deduct.call]p2: 40160b57cec5SDimitry Andric // If P is not a reference type: 40170b57cec5SDimitry Andric // - If A is an array type, the pointer type produced by the 40180b57cec5SDimitry Andric // array-to-pointer standard conversion (4.2) is used in place of 40190b57cec5SDimitry Andric // A for type deduction; otherwise, 40200b57cec5SDimitry Andric // - If A is a function type, the pointer type produced by the 40210b57cec5SDimitry Andric // function-to-pointer standard conversion (4.3) is used in place 40220b57cec5SDimitry Andric // of A for type deduction; otherwise, 4023bdd1243dSDimitry Andric if (ArgType->canDecayToPointerType()) 4024bdd1243dSDimitry Andric ArgType = S.Context.getDecayedType(ArgType); 40250b57cec5SDimitry Andric else { 40260b57cec5SDimitry Andric // - If A is a cv-qualified type, the top level cv-qualifiers of A's 40270b57cec5SDimitry Andric // type are ignored for type deduction. 40280b57cec5SDimitry Andric ArgType = ArgType.getUnqualifiedType(); 40290b57cec5SDimitry Andric } 40300b57cec5SDimitry Andric } 40310b57cec5SDimitry Andric 40320b57cec5SDimitry Andric // C++0x [temp.deduct.call]p4: 40330b57cec5SDimitry Andric // In general, the deduction process attempts to find template argument 40340b57cec5SDimitry Andric // values that will make the deduced A identical to A (after the type A 40350b57cec5SDimitry Andric // is transformed as described above). [...] 40360b57cec5SDimitry Andric TDF = TDF_SkipNonDependent; 40370b57cec5SDimitry Andric 40380b57cec5SDimitry Andric // - If the original P is a reference type, the deduced A (i.e., the 40390b57cec5SDimitry Andric // type referred to by the reference) can be more cv-qualified than 40400b57cec5SDimitry Andric // the transformed A. 40410b57cec5SDimitry Andric if (ParamRefType) 40420b57cec5SDimitry Andric TDF |= TDF_ParamWithReferenceType; 40430b57cec5SDimitry Andric // - The transformed A can be another pointer or pointer to member 40440b57cec5SDimitry Andric // type that can be converted to the deduced A via a qualification 40450b57cec5SDimitry Andric // conversion (4.4). 40460b57cec5SDimitry Andric if (ArgType->isPointerType() || ArgType->isMemberPointerType() || 40470b57cec5SDimitry Andric ArgType->isObjCObjectPointerType()) 40480b57cec5SDimitry Andric TDF |= TDF_IgnoreQualifiers; 40490b57cec5SDimitry Andric // - If P is a class and P has the form simple-template-id, then the 40500b57cec5SDimitry Andric // transformed A can be a derived class of the deduced A. Likewise, 40510b57cec5SDimitry Andric // if P is a pointer to a class of the form simple-template-id, the 40520b57cec5SDimitry Andric // transformed A can be a pointer to a derived class pointed to by 40530b57cec5SDimitry Andric // the deduced A. 40540b57cec5SDimitry Andric if (isSimpleTemplateIdType(ParamType) || 40550b57cec5SDimitry Andric (isa<PointerType>(ParamType) && 40560b57cec5SDimitry Andric isSimpleTemplateIdType( 4057fe6060f1SDimitry Andric ParamType->castAs<PointerType>()->getPointeeType()))) 40580b57cec5SDimitry Andric TDF |= TDF_DerivedClass; 40590b57cec5SDimitry Andric 40600b57cec5SDimitry Andric return false; 40610b57cec5SDimitry Andric } 40620b57cec5SDimitry Andric 40630b57cec5SDimitry Andric static bool 40640b57cec5SDimitry Andric hasDeducibleTemplateParameters(Sema &S, FunctionTemplateDecl *FunctionTemplate, 40650b57cec5SDimitry Andric QualType T); 40660b57cec5SDimitry Andric 40670b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceTemplateArgumentsFromCallArgument( 40680b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, 40695f757f3fSDimitry Andric QualType ParamType, QualType ArgType, 40705f757f3fSDimitry Andric Expr::Classification ArgClassification, Expr *Arg, 40715f757f3fSDimitry Andric TemplateDeductionInfo &Info, 40720b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 40730b57cec5SDimitry Andric SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, 407406c3fb27SDimitry Andric bool DecomposedParam, unsigned ArgIdx, unsigned TDF, 407506c3fb27SDimitry Andric TemplateSpecCandidateSet *FailedTSC = nullptr); 40760b57cec5SDimitry Andric 40770b57cec5SDimitry Andric /// Attempt template argument deduction from an initializer list 40780b57cec5SDimitry Andric /// deemed to be an argument in a function call. 40790b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceFromInitializerList( 40800b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, QualType AdjustedParamType, 40810b57cec5SDimitry Andric InitListExpr *ILE, TemplateDeductionInfo &Info, 40820b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 40830b57cec5SDimitry Andric SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, unsigned ArgIdx, 40840b57cec5SDimitry Andric unsigned TDF) { 40850b57cec5SDimitry Andric // C++ [temp.deduct.call]p1: (CWG 1591) 40860b57cec5SDimitry Andric // If removing references and cv-qualifiers from P gives 40870b57cec5SDimitry Andric // std::initializer_list<P0> or P0[N] for some P0 and N and the argument is 40880b57cec5SDimitry Andric // a non-empty initializer list, then deduction is performed instead for 40890b57cec5SDimitry Andric // each element of the initializer list, taking P0 as a function template 40900b57cec5SDimitry Andric // parameter type and the initializer element as its argument 40910b57cec5SDimitry Andric // 40920b57cec5SDimitry Andric // We've already removed references and cv-qualifiers here. 40930b57cec5SDimitry Andric if (!ILE->getNumInits()) 40940b57cec5SDimitry Andric return Sema::TDK_Success; 40950b57cec5SDimitry Andric 40960b57cec5SDimitry Andric QualType ElTy; 40970b57cec5SDimitry Andric auto *ArrTy = S.Context.getAsArrayType(AdjustedParamType); 40980b57cec5SDimitry Andric if (ArrTy) 40990b57cec5SDimitry Andric ElTy = ArrTy->getElementType(); 41000b57cec5SDimitry Andric else if (!S.isStdInitializerList(AdjustedParamType, &ElTy)) { 41010b57cec5SDimitry Andric // Otherwise, an initializer list argument causes the parameter to be 41020b57cec5SDimitry Andric // considered a non-deduced context 41030b57cec5SDimitry Andric return Sema::TDK_Success; 41040b57cec5SDimitry Andric } 41050b57cec5SDimitry Andric 4106a7dea167SDimitry Andric // Resolving a core issue: a braced-init-list containing any designators is 4107a7dea167SDimitry Andric // a non-deduced context. 4108a7dea167SDimitry Andric for (Expr *E : ILE->inits()) 4109a7dea167SDimitry Andric if (isa<DesignatedInitExpr>(E)) 4110a7dea167SDimitry Andric return Sema::TDK_Success; 4111a7dea167SDimitry Andric 41120b57cec5SDimitry Andric // Deduction only needs to be done for dependent types. 41130b57cec5SDimitry Andric if (ElTy->isDependentType()) { 41140b57cec5SDimitry Andric for (Expr *E : ILE->inits()) { 41150b57cec5SDimitry Andric if (auto Result = DeduceTemplateArgumentsFromCallArgument( 41165f757f3fSDimitry Andric S, TemplateParams, 0, ElTy, E->getType(), 41175f757f3fSDimitry Andric E->Classify(S.getASTContext()), E, Info, Deduced, 41185f757f3fSDimitry Andric OriginalCallArgs, true, ArgIdx, TDF)) 41190b57cec5SDimitry Andric return Result; 41200b57cec5SDimitry Andric } 41210b57cec5SDimitry Andric } 41220b57cec5SDimitry Andric 41230b57cec5SDimitry Andric // in the P0[N] case, if N is a non-type template parameter, N is deduced 41240b57cec5SDimitry Andric // from the length of the initializer list. 41250b57cec5SDimitry Andric if (auto *DependentArrTy = dyn_cast_or_null<DependentSizedArrayType>(ArrTy)) { 41260b57cec5SDimitry Andric // Determine the array bound is something we can deduce. 4127e8d8bef9SDimitry Andric if (const NonTypeTemplateParmDecl *NTTP = 41280b57cec5SDimitry Andric getDeducedParameterFromExpr(Info, DependentArrTy->getSizeExpr())) { 41290b57cec5SDimitry Andric // We can perform template argument deduction for the given non-type 41300b57cec5SDimitry Andric // template parameter. 41310b57cec5SDimitry Andric // C++ [temp.deduct.type]p13: 41320b57cec5SDimitry Andric // The type of N in the type T[N] is std::size_t. 41330b57cec5SDimitry Andric QualType T = S.Context.getSizeType(); 41340b57cec5SDimitry Andric llvm::APInt Size(S.Context.getIntWidth(T), ILE->getNumInits()); 41350b57cec5SDimitry Andric if (auto Result = DeduceNonTypeTemplateArgument( 41360b57cec5SDimitry Andric S, TemplateParams, NTTP, llvm::APSInt(Size), T, 41370b57cec5SDimitry Andric /*ArrayBound=*/true, Info, Deduced)) 41380b57cec5SDimitry Andric return Result; 41390b57cec5SDimitry Andric } 41400b57cec5SDimitry Andric } 41410b57cec5SDimitry Andric 41420b57cec5SDimitry Andric return Sema::TDK_Success; 41430b57cec5SDimitry Andric } 41440b57cec5SDimitry Andric 41450b57cec5SDimitry Andric /// Perform template argument deduction per [temp.deduct.call] for a 41460b57cec5SDimitry Andric /// single parameter / argument pair. 41470b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceTemplateArgumentsFromCallArgument( 41480b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, 41495f757f3fSDimitry Andric QualType ParamType, QualType ArgType, 41505f757f3fSDimitry Andric Expr::Classification ArgClassification, Expr *Arg, 41515f757f3fSDimitry Andric TemplateDeductionInfo &Info, 41520b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 41530b57cec5SDimitry Andric SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, 415406c3fb27SDimitry Andric bool DecomposedParam, unsigned ArgIdx, unsigned TDF, 415506c3fb27SDimitry Andric TemplateSpecCandidateSet *FailedTSC) { 41565f757f3fSDimitry Andric 41570b57cec5SDimitry Andric QualType OrigParamType = ParamType; 41580b57cec5SDimitry Andric 41590b57cec5SDimitry Andric // If P is a reference type [...] 41600b57cec5SDimitry Andric // If P is a cv-qualified type [...] 41615f757f3fSDimitry Andric if (AdjustFunctionParmAndArgTypesForDeduction( 41625f757f3fSDimitry Andric S, TemplateParams, FirstInnerIndex, ParamType, ArgType, 41635f757f3fSDimitry Andric ArgClassification, Arg, TDF, FailedTSC)) 41640b57cec5SDimitry Andric return Sema::TDK_Success; 41650b57cec5SDimitry Andric 41660b57cec5SDimitry Andric // If [...] the argument is a non-empty initializer list [...] 41675f757f3fSDimitry Andric if (InitListExpr *ILE = dyn_cast_if_present<InitListExpr>(Arg)) 41680b57cec5SDimitry Andric return DeduceFromInitializerList(S, TemplateParams, ParamType, ILE, Info, 41690b57cec5SDimitry Andric Deduced, OriginalCallArgs, ArgIdx, TDF); 41700b57cec5SDimitry Andric 41710b57cec5SDimitry Andric // [...] the deduction process attempts to find template argument values 41720b57cec5SDimitry Andric // that will make the deduced A identical to A 41730b57cec5SDimitry Andric // 41740b57cec5SDimitry Andric // Keep track of the argument type and corresponding parameter index, 41750b57cec5SDimitry Andric // so we can check for compatibility between the deduced A and A. 41765f757f3fSDimitry Andric if (Arg) 41770b57cec5SDimitry Andric OriginalCallArgs.push_back( 41780b57cec5SDimitry Andric Sema::OriginalCallArg(OrigParamType, DecomposedParam, ArgIdx, ArgType)); 41790b57cec5SDimitry Andric return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, 41800b57cec5SDimitry Andric ArgType, Info, Deduced, TDF); 41810b57cec5SDimitry Andric } 41820b57cec5SDimitry Andric 41830b57cec5SDimitry Andric /// Perform template argument deduction from a function call 41840b57cec5SDimitry Andric /// (C++ [temp.deduct.call]). 41850b57cec5SDimitry Andric /// 41860b57cec5SDimitry Andric /// \param FunctionTemplate the function template for which we are performing 41870b57cec5SDimitry Andric /// template argument deduction. 41880b57cec5SDimitry Andric /// 41890b57cec5SDimitry Andric /// \param ExplicitTemplateArgs the explicit template arguments provided 41900b57cec5SDimitry Andric /// for this call. 41910b57cec5SDimitry Andric /// 41920b57cec5SDimitry Andric /// \param Args the function call arguments 41930b57cec5SDimitry Andric /// 41940b57cec5SDimitry Andric /// \param Specialization if template argument deduction was successful, 41950b57cec5SDimitry Andric /// this will be set to the function template specialization produced by 41960b57cec5SDimitry Andric /// template argument deduction. 41970b57cec5SDimitry Andric /// 41980b57cec5SDimitry Andric /// \param Info the argument will be updated to provide additional information 41990b57cec5SDimitry Andric /// about template argument deduction. 42000b57cec5SDimitry Andric /// 42010b57cec5SDimitry Andric /// \param CheckNonDependent A callback to invoke to check conversions for 42020b57cec5SDimitry Andric /// non-dependent parameters, between deduction and substitution, per DR1391. 42030b57cec5SDimitry Andric /// If this returns true, substitution will be skipped and we return 42040b57cec5SDimitry Andric /// TDK_NonDependentConversionFailure. The callback is passed the parameter 42050b57cec5SDimitry Andric /// types (after substituting explicit template arguments). 42060b57cec5SDimitry Andric /// 42070b57cec5SDimitry Andric /// \returns the result of template argument deduction. 42080b57cec5SDimitry Andric Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( 42090b57cec5SDimitry Andric FunctionTemplateDecl *FunctionTemplate, 42100b57cec5SDimitry Andric TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args, 42110b57cec5SDimitry Andric FunctionDecl *&Specialization, TemplateDeductionInfo &Info, 421206c3fb27SDimitry Andric bool PartialOverloading, bool AggregateDeductionCandidate, 42135f757f3fSDimitry Andric QualType ObjectType, Expr::Classification ObjectClassification, 42140b57cec5SDimitry Andric llvm::function_ref<bool(ArrayRef<QualType>)> CheckNonDependent) { 42150b57cec5SDimitry Andric if (FunctionTemplate->isInvalidDecl()) 42160b57cec5SDimitry Andric return TDK_Invalid; 42170b57cec5SDimitry Andric 42180b57cec5SDimitry Andric FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 42190b57cec5SDimitry Andric unsigned NumParams = Function->getNumParams(); 42205f757f3fSDimitry Andric bool HasExplicitObject = false; 42215f757f3fSDimitry Andric int ExplicitObjectOffset = 0; 42225f757f3fSDimitry Andric if (Function->hasCXXExplicitFunctionObjectParameter()) { 42235f757f3fSDimitry Andric HasExplicitObject = true; 42245f757f3fSDimitry Andric ExplicitObjectOffset = 1; 42255f757f3fSDimitry Andric } 42260b57cec5SDimitry Andric 42270b57cec5SDimitry Andric unsigned FirstInnerIndex = getFirstInnerIndex(FunctionTemplate); 42280b57cec5SDimitry Andric 42290b57cec5SDimitry Andric // C++ [temp.deduct.call]p1: 42300b57cec5SDimitry Andric // Template argument deduction is done by comparing each function template 42310b57cec5SDimitry Andric // parameter type (call it P) with the type of the corresponding argument 42320b57cec5SDimitry Andric // of the call (call it A) as described below. 42335f757f3fSDimitry Andric if (Args.size() < Function->getMinRequiredExplicitArguments() && 42345f757f3fSDimitry Andric !PartialOverloading) 42350b57cec5SDimitry Andric return TDK_TooFewArguments; 42365f757f3fSDimitry Andric else if (TooManyArguments(NumParams, Args.size() + ExplicitObjectOffset, 42375f757f3fSDimitry Andric PartialOverloading)) { 4238a7dea167SDimitry Andric const auto *Proto = Function->getType()->castAs<FunctionProtoType>(); 42390b57cec5SDimitry Andric if (Proto->isTemplateVariadic()) 42400b57cec5SDimitry Andric /* Do nothing */; 42410b57cec5SDimitry Andric else if (!Proto->isVariadic()) 42420b57cec5SDimitry Andric return TDK_TooManyArguments; 42430b57cec5SDimitry Andric } 42440b57cec5SDimitry Andric 42450b57cec5SDimitry Andric // The types of the parameters from which we will perform template argument 42460b57cec5SDimitry Andric // deduction. 42470b57cec5SDimitry Andric LocalInstantiationScope InstScope(*this); 42480b57cec5SDimitry Andric TemplateParameterList *TemplateParams 42490b57cec5SDimitry Andric = FunctionTemplate->getTemplateParameters(); 42500b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> Deduced; 42510b57cec5SDimitry Andric SmallVector<QualType, 8> ParamTypes; 42520b57cec5SDimitry Andric unsigned NumExplicitlySpecified = 0; 42530b57cec5SDimitry Andric if (ExplicitTemplateArgs) { 42545ffd83dbSDimitry Andric TemplateDeductionResult Result; 42555ffd83dbSDimitry Andric runWithSufficientStackSpace(Info.getLocation(), [&] { 42565ffd83dbSDimitry Andric Result = SubstituteExplicitTemplateArguments( 42575ffd83dbSDimitry Andric FunctionTemplate, *ExplicitTemplateArgs, Deduced, ParamTypes, nullptr, 42580b57cec5SDimitry Andric Info); 42595ffd83dbSDimitry Andric }); 42600b57cec5SDimitry Andric if (Result) 42610b57cec5SDimitry Andric return Result; 42620b57cec5SDimitry Andric 42630b57cec5SDimitry Andric NumExplicitlySpecified = Deduced.size(); 42640b57cec5SDimitry Andric } else { 42650b57cec5SDimitry Andric // Just fill in the parameter types from the function declaration. 42660b57cec5SDimitry Andric for (unsigned I = 0; I != NumParams; ++I) 42670b57cec5SDimitry Andric ParamTypes.push_back(Function->getParamDecl(I)->getType()); 42680b57cec5SDimitry Andric } 42690b57cec5SDimitry Andric 42700b57cec5SDimitry Andric SmallVector<OriginalCallArg, 8> OriginalCallArgs; 42710b57cec5SDimitry Andric 42720b57cec5SDimitry Andric // Deduce an argument of type ParamType from an expression with index ArgIdx. 42735f757f3fSDimitry Andric auto DeduceCallArgument = [&](QualType ParamType, unsigned ArgIdx, 42745f757f3fSDimitry Andric bool ExplicitObjetArgument) { 42750b57cec5SDimitry Andric // C++ [demp.deduct.call]p1: (DR1391) 42760b57cec5SDimitry Andric // Template argument deduction is done by comparing each function template 42770b57cec5SDimitry Andric // parameter that contains template-parameters that participate in 42780b57cec5SDimitry Andric // template argument deduction ... 42790b57cec5SDimitry Andric if (!hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) 42800b57cec5SDimitry Andric return Sema::TDK_Success; 42810b57cec5SDimitry Andric 42825f757f3fSDimitry Andric if (ExplicitObjetArgument) { 42830b57cec5SDimitry Andric // ... with the type of the corresponding argument 42840b57cec5SDimitry Andric return DeduceTemplateArgumentsFromCallArgument( 42855f757f3fSDimitry Andric *this, TemplateParams, FirstInnerIndex, ParamType, ObjectType, 42865f757f3fSDimitry Andric ObjectClassification, 42875f757f3fSDimitry Andric /*Arg=*/nullptr, Info, Deduced, OriginalCallArgs, 42885f757f3fSDimitry Andric /*Decomposed*/ false, ArgIdx, /*TDF*/ 0); 42895f757f3fSDimitry Andric } 42905f757f3fSDimitry Andric 42915f757f3fSDimitry Andric // ... with the type of the corresponding argument 42925f757f3fSDimitry Andric return DeduceTemplateArgumentsFromCallArgument( 42935f757f3fSDimitry Andric *this, TemplateParams, FirstInnerIndex, ParamType, 42945f757f3fSDimitry Andric Args[ArgIdx]->getType(), Args[ArgIdx]->Classify(getASTContext()), 42955f757f3fSDimitry Andric Args[ArgIdx], Info, Deduced, OriginalCallArgs, /*Decomposed*/ false, 42965f757f3fSDimitry Andric ArgIdx, /*TDF*/ 0); 42970b57cec5SDimitry Andric }; 42980b57cec5SDimitry Andric 42990b57cec5SDimitry Andric // Deduce template arguments from the function parameters. 43000b57cec5SDimitry Andric Deduced.resize(TemplateParams->size()); 43010b57cec5SDimitry Andric SmallVector<QualType, 8> ParamTypesForArgChecking; 43020b57cec5SDimitry Andric for (unsigned ParamIdx = 0, NumParamTypes = ParamTypes.size(), ArgIdx = 0; 43030b57cec5SDimitry Andric ParamIdx != NumParamTypes; ++ParamIdx) { 43040b57cec5SDimitry Andric QualType ParamType = ParamTypes[ParamIdx]; 43050b57cec5SDimitry Andric 43060b57cec5SDimitry Andric const PackExpansionType *ParamExpansion = 43070b57cec5SDimitry Andric dyn_cast<PackExpansionType>(ParamType); 43080b57cec5SDimitry Andric if (!ParamExpansion) { 43090b57cec5SDimitry Andric // Simple case: matching a function parameter to a function argument. 43105f757f3fSDimitry Andric if (ArgIdx >= Args.size() && !(HasExplicitObject && ParamIdx == 0)) 43110b57cec5SDimitry Andric break; 43120b57cec5SDimitry Andric 43130b57cec5SDimitry Andric ParamTypesForArgChecking.push_back(ParamType); 43145f757f3fSDimitry Andric 43155f757f3fSDimitry Andric if (ParamIdx == 0 && HasExplicitObject) { 43165f757f3fSDimitry Andric if (auto Result = DeduceCallArgument(ParamType, 0, 43175f757f3fSDimitry Andric /*ExplicitObjetArgument=*/true)) 43185f757f3fSDimitry Andric return Result; 43195f757f3fSDimitry Andric continue; 43205f757f3fSDimitry Andric } 43215f757f3fSDimitry Andric 43225f757f3fSDimitry Andric if (auto Result = DeduceCallArgument(ParamType, ArgIdx++, 43235f757f3fSDimitry Andric /*ExplicitObjetArgument=*/false)) 43240b57cec5SDimitry Andric return Result; 43250b57cec5SDimitry Andric 43260b57cec5SDimitry Andric continue; 43270b57cec5SDimitry Andric } 43280b57cec5SDimitry Andric 432906c3fb27SDimitry Andric bool IsTrailingPack = ParamIdx + 1 == NumParamTypes; 433006c3fb27SDimitry Andric 43310b57cec5SDimitry Andric QualType ParamPattern = ParamExpansion->getPattern(); 43320b57cec5SDimitry Andric PackDeductionScope PackScope(*this, TemplateParams, Deduced, Info, 433306c3fb27SDimitry Andric ParamPattern, 433406c3fb27SDimitry Andric AggregateDeductionCandidate && IsTrailingPack); 43350b57cec5SDimitry Andric 43360b57cec5SDimitry Andric // C++0x [temp.deduct.call]p1: 43370b57cec5SDimitry Andric // For a function parameter pack that occurs at the end of the 43380b57cec5SDimitry Andric // parameter-declaration-list, the type A of each remaining argument of 43390b57cec5SDimitry Andric // the call is compared with the type P of the declarator-id of the 43400b57cec5SDimitry Andric // function parameter pack. Each comparison deduces template arguments 43410b57cec5SDimitry Andric // for subsequent positions in the template parameter packs expanded by 43420b57cec5SDimitry Andric // the function parameter pack. When a function parameter pack appears 43430b57cec5SDimitry Andric // in a non-deduced context [not at the end of the list], the type of 43440b57cec5SDimitry Andric // that parameter pack is never deduced. 43450b57cec5SDimitry Andric // 43460b57cec5SDimitry Andric // FIXME: The above rule allows the size of the parameter pack to change 43470b57cec5SDimitry Andric // after we skip it (in the non-deduced case). That makes no sense, so 43480b57cec5SDimitry Andric // we instead notionally deduce the pack against N arguments, where N is 43490b57cec5SDimitry Andric // the length of the explicitly-specified pack if it's expanded by the 43500b57cec5SDimitry Andric // parameter pack and 0 otherwise, and we treat each deduction as a 43510b57cec5SDimitry Andric // non-deduced context. 435206c3fb27SDimitry Andric if (IsTrailingPack || PackScope.hasFixedArity()) { 43530b57cec5SDimitry Andric for (; ArgIdx < Args.size() && PackScope.hasNextElement(); 43540b57cec5SDimitry Andric PackScope.nextPackElement(), ++ArgIdx) { 43550b57cec5SDimitry Andric ParamTypesForArgChecking.push_back(ParamPattern); 43565f757f3fSDimitry Andric if (auto Result = DeduceCallArgument(ParamPattern, ArgIdx, 43575f757f3fSDimitry Andric /*ExplicitObjetArgument=*/false)) 43580b57cec5SDimitry Andric return Result; 43590b57cec5SDimitry Andric } 43600b57cec5SDimitry Andric } else { 43610b57cec5SDimitry Andric // If the parameter type contains an explicitly-specified pack that we 43620b57cec5SDimitry Andric // could not expand, skip the number of parameters notionally created 43630b57cec5SDimitry Andric // by the expansion. 4364bdd1243dSDimitry Andric std::optional<unsigned> NumExpansions = 4365bdd1243dSDimitry Andric ParamExpansion->getNumExpansions(); 43660b57cec5SDimitry Andric if (NumExpansions && !PackScope.isPartiallyExpanded()) { 43670b57cec5SDimitry Andric for (unsigned I = 0; I != *NumExpansions && ArgIdx < Args.size(); 43680b57cec5SDimitry Andric ++I, ++ArgIdx) { 43690b57cec5SDimitry Andric ParamTypesForArgChecking.push_back(ParamPattern); 43700b57cec5SDimitry Andric // FIXME: Should we add OriginalCallArgs for these? What if the 43710b57cec5SDimitry Andric // corresponding argument is a list? 43720b57cec5SDimitry Andric PackScope.nextPackElement(); 43730b57cec5SDimitry Andric } 43740b57cec5SDimitry Andric } 43750b57cec5SDimitry Andric } 43760b57cec5SDimitry Andric 43770b57cec5SDimitry Andric // Build argument packs for each of the parameter packs expanded by this 43780b57cec5SDimitry Andric // pack expansion. 43790b57cec5SDimitry Andric if (auto Result = PackScope.finish()) 43800b57cec5SDimitry Andric return Result; 43810b57cec5SDimitry Andric } 43820b57cec5SDimitry Andric 43830b57cec5SDimitry Andric // Capture the context in which the function call is made. This is the context 43840b57cec5SDimitry Andric // that is needed when the accessibility of template arguments is checked. 43850b57cec5SDimitry Andric DeclContext *CallingCtx = CurContext; 43860b57cec5SDimitry Andric 43875ffd83dbSDimitry Andric TemplateDeductionResult Result; 43885ffd83dbSDimitry Andric runWithSufficientStackSpace(Info.getLocation(), [&] { 43895ffd83dbSDimitry Andric Result = FinishTemplateArgumentDeduction( 43900b57cec5SDimitry Andric FunctionTemplate, Deduced, NumExplicitlySpecified, Specialization, Info, 43910b57cec5SDimitry Andric &OriginalCallArgs, PartialOverloading, [&, CallingCtx]() { 43920b57cec5SDimitry Andric ContextRAII SavedContext(*this, CallingCtx); 43930b57cec5SDimitry Andric return CheckNonDependent(ParamTypesForArgChecking); 43940b57cec5SDimitry Andric }); 43955ffd83dbSDimitry Andric }); 43965ffd83dbSDimitry Andric return Result; 43970b57cec5SDimitry Andric } 43980b57cec5SDimitry Andric 43990b57cec5SDimitry Andric QualType Sema::adjustCCAndNoReturn(QualType ArgFunctionType, 44000b57cec5SDimitry Andric QualType FunctionType, 44010b57cec5SDimitry Andric bool AdjustExceptionSpec) { 44020b57cec5SDimitry Andric if (ArgFunctionType.isNull()) 44030b57cec5SDimitry Andric return ArgFunctionType; 44040b57cec5SDimitry Andric 4405a7dea167SDimitry Andric const auto *FunctionTypeP = FunctionType->castAs<FunctionProtoType>(); 4406a7dea167SDimitry Andric const auto *ArgFunctionTypeP = ArgFunctionType->castAs<FunctionProtoType>(); 44070b57cec5SDimitry Andric FunctionProtoType::ExtProtoInfo EPI = ArgFunctionTypeP->getExtProtoInfo(); 44080b57cec5SDimitry Andric bool Rebuild = false; 44090b57cec5SDimitry Andric 44100b57cec5SDimitry Andric CallingConv CC = FunctionTypeP->getCallConv(); 44110b57cec5SDimitry Andric if (EPI.ExtInfo.getCC() != CC) { 44120b57cec5SDimitry Andric EPI.ExtInfo = EPI.ExtInfo.withCallingConv(CC); 44130b57cec5SDimitry Andric Rebuild = true; 44140b57cec5SDimitry Andric } 44150b57cec5SDimitry Andric 44160b57cec5SDimitry Andric bool NoReturn = FunctionTypeP->getNoReturnAttr(); 44170b57cec5SDimitry Andric if (EPI.ExtInfo.getNoReturn() != NoReturn) { 44180b57cec5SDimitry Andric EPI.ExtInfo = EPI.ExtInfo.withNoReturn(NoReturn); 44190b57cec5SDimitry Andric Rebuild = true; 44200b57cec5SDimitry Andric } 44210b57cec5SDimitry Andric 44220b57cec5SDimitry Andric if (AdjustExceptionSpec && (FunctionTypeP->hasExceptionSpec() || 44230b57cec5SDimitry Andric ArgFunctionTypeP->hasExceptionSpec())) { 44240b57cec5SDimitry Andric EPI.ExceptionSpec = FunctionTypeP->getExtProtoInfo().ExceptionSpec; 44250b57cec5SDimitry Andric Rebuild = true; 44260b57cec5SDimitry Andric } 44270b57cec5SDimitry Andric 44280b57cec5SDimitry Andric if (!Rebuild) 44290b57cec5SDimitry Andric return ArgFunctionType; 44300b57cec5SDimitry Andric 44310b57cec5SDimitry Andric return Context.getFunctionType(ArgFunctionTypeP->getReturnType(), 44320b57cec5SDimitry Andric ArgFunctionTypeP->getParamTypes(), EPI); 44330b57cec5SDimitry Andric } 44340b57cec5SDimitry Andric 44350b57cec5SDimitry Andric /// Deduce template arguments when taking the address of a function 44360b57cec5SDimitry Andric /// template (C++ [temp.deduct.funcaddr]) or matching a specialization to 44370b57cec5SDimitry Andric /// a template. 44380b57cec5SDimitry Andric /// 44390b57cec5SDimitry Andric /// \param FunctionTemplate the function template for which we are performing 44400b57cec5SDimitry Andric /// template argument deduction. 44410b57cec5SDimitry Andric /// 44420b57cec5SDimitry Andric /// \param ExplicitTemplateArgs the explicitly-specified template 44430b57cec5SDimitry Andric /// arguments. 44440b57cec5SDimitry Andric /// 44450b57cec5SDimitry Andric /// \param ArgFunctionType the function type that will be used as the 44460b57cec5SDimitry Andric /// "argument" type (A) when performing template argument deduction from the 44470b57cec5SDimitry Andric /// function template's function type. This type may be NULL, if there is no 44480b57cec5SDimitry Andric /// argument type to compare against, in C++0x [temp.arg.explicit]p3. 44490b57cec5SDimitry Andric /// 44500b57cec5SDimitry Andric /// \param Specialization if template argument deduction was successful, 44510b57cec5SDimitry Andric /// this will be set to the function template specialization produced by 44520b57cec5SDimitry Andric /// template argument deduction. 44530b57cec5SDimitry Andric /// 44540b57cec5SDimitry Andric /// \param Info the argument will be updated to provide additional information 44550b57cec5SDimitry Andric /// about template argument deduction. 44560b57cec5SDimitry Andric /// 44570b57cec5SDimitry Andric /// \param IsAddressOfFunction If \c true, we are deducing as part of taking 44580b57cec5SDimitry Andric /// the address of a function template per [temp.deduct.funcaddr] and 44590b57cec5SDimitry Andric /// [over.over]. If \c false, we are looking up a function template 44600b57cec5SDimitry Andric /// specialization based on its signature, per [temp.deduct.decl]. 44610b57cec5SDimitry Andric /// 44620b57cec5SDimitry Andric /// \returns the result of template argument deduction. 44630b57cec5SDimitry Andric Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( 44640b57cec5SDimitry Andric FunctionTemplateDecl *FunctionTemplate, 44650b57cec5SDimitry Andric TemplateArgumentListInfo *ExplicitTemplateArgs, QualType ArgFunctionType, 44660b57cec5SDimitry Andric FunctionDecl *&Specialization, TemplateDeductionInfo &Info, 44670b57cec5SDimitry Andric bool IsAddressOfFunction) { 44680b57cec5SDimitry Andric if (FunctionTemplate->isInvalidDecl()) 44690b57cec5SDimitry Andric return TDK_Invalid; 44700b57cec5SDimitry Andric 44710b57cec5SDimitry Andric FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 44720b57cec5SDimitry Andric TemplateParameterList *TemplateParams 44730b57cec5SDimitry Andric = FunctionTemplate->getTemplateParameters(); 44740b57cec5SDimitry Andric QualType FunctionType = Function->getType(); 44750b57cec5SDimitry Andric 44760b57cec5SDimitry Andric // Substitute any explicit template arguments. 44770b57cec5SDimitry Andric LocalInstantiationScope InstScope(*this); 44780b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> Deduced; 44790b57cec5SDimitry Andric unsigned NumExplicitlySpecified = 0; 44800b57cec5SDimitry Andric SmallVector<QualType, 4> ParamTypes; 44810b57cec5SDimitry Andric if (ExplicitTemplateArgs) { 44825ffd83dbSDimitry Andric TemplateDeductionResult Result; 44835ffd83dbSDimitry Andric runWithSufficientStackSpace(Info.getLocation(), [&] { 44845ffd83dbSDimitry Andric Result = SubstituteExplicitTemplateArguments( 44855ffd83dbSDimitry Andric FunctionTemplate, *ExplicitTemplateArgs, Deduced, ParamTypes, 44865ffd83dbSDimitry Andric &FunctionType, Info); 44875ffd83dbSDimitry Andric }); 44885ffd83dbSDimitry Andric if (Result) 44890b57cec5SDimitry Andric return Result; 44900b57cec5SDimitry Andric 44910b57cec5SDimitry Andric NumExplicitlySpecified = Deduced.size(); 44920b57cec5SDimitry Andric } 44930b57cec5SDimitry Andric 44940b57cec5SDimitry Andric // When taking the address of a function, we require convertibility of 44950b57cec5SDimitry Andric // the resulting function type. Otherwise, we allow arbitrary mismatches 44960b57cec5SDimitry Andric // of calling convention and noreturn. 44970b57cec5SDimitry Andric if (!IsAddressOfFunction) 44980b57cec5SDimitry Andric ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, FunctionType, 44990b57cec5SDimitry Andric /*AdjustExceptionSpec*/false); 45000b57cec5SDimitry Andric 45010b57cec5SDimitry Andric // Unevaluated SFINAE context. 45020b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 45030b57cec5SDimitry Andric *this, Sema::ExpressionEvaluationContext::Unevaluated); 45040b57cec5SDimitry Andric SFINAETrap Trap(*this); 45050b57cec5SDimitry Andric 45060b57cec5SDimitry Andric Deduced.resize(TemplateParams->size()); 45070b57cec5SDimitry Andric 45080b57cec5SDimitry Andric // If the function has a deduced return type, substitute it for a dependent 450906c3fb27SDimitry Andric // type so that we treat it as a non-deduced context in what follows. 45100b57cec5SDimitry Andric bool HasDeducedReturnType = false; 451106c3fb27SDimitry Andric if (getLangOpts().CPlusPlus14 && 45120b57cec5SDimitry Andric Function->getReturnType()->getContainedAutoType()) { 4513349cc55cSDimitry Andric FunctionType = SubstAutoTypeDependent(FunctionType); 45140b57cec5SDimitry Andric HasDeducedReturnType = true; 45150b57cec5SDimitry Andric } 45160b57cec5SDimitry Andric 4517349cc55cSDimitry Andric if (!ArgFunctionType.isNull() && !FunctionType.isNull()) { 45180b57cec5SDimitry Andric unsigned TDF = 45190b57cec5SDimitry Andric TDF_TopLevelParameterTypeList | TDF_AllowCompatibleFunctionType; 45200b57cec5SDimitry Andric // Deduce template arguments from the function type. 45210b57cec5SDimitry Andric if (TemplateDeductionResult Result 45220b57cec5SDimitry Andric = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, 45230b57cec5SDimitry Andric FunctionType, ArgFunctionType, 45240b57cec5SDimitry Andric Info, Deduced, TDF)) 45250b57cec5SDimitry Andric return Result; 45260b57cec5SDimitry Andric } 45270b57cec5SDimitry Andric 45285ffd83dbSDimitry Andric TemplateDeductionResult Result; 45295ffd83dbSDimitry Andric runWithSufficientStackSpace(Info.getLocation(), [&] { 45305ffd83dbSDimitry Andric Result = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 45310b57cec5SDimitry Andric NumExplicitlySpecified, 45325ffd83dbSDimitry Andric Specialization, Info); 45335ffd83dbSDimitry Andric }); 45345ffd83dbSDimitry Andric if (Result) 45350b57cec5SDimitry Andric return Result; 45360b57cec5SDimitry Andric 45370b57cec5SDimitry Andric // If the function has a deduced return type, deduce it now, so we can check 45380b57cec5SDimitry Andric // that the deduced function type matches the requested type. 453906c3fb27SDimitry Andric if (HasDeducedReturnType && IsAddressOfFunction && 45400b57cec5SDimitry Andric Specialization->getReturnType()->isUndeducedType() && 45410b57cec5SDimitry Andric DeduceReturnType(Specialization, Info.getLocation(), false)) 45420b57cec5SDimitry Andric return TDK_MiscellaneousDeductionFailure; 45430b57cec5SDimitry Andric 454406c3fb27SDimitry Andric if (IsAddressOfFunction && getLangOpts().CPlusPlus20 && 454506c3fb27SDimitry Andric Specialization->isImmediateEscalating() && 454606c3fb27SDimitry Andric CheckIfFunctionSpecializationIsImmediate(Specialization, 454706c3fb27SDimitry Andric Info.getLocation())) 454806c3fb27SDimitry Andric return TDK_MiscellaneousDeductionFailure; 454906c3fb27SDimitry Andric 45500b57cec5SDimitry Andric // If the function has a dependent exception specification, resolve it now, 45510b57cec5SDimitry Andric // so we can check that the exception specification matches. 45520b57cec5SDimitry Andric auto *SpecializationFPT = 45530b57cec5SDimitry Andric Specialization->getType()->castAs<FunctionProtoType>(); 45540b57cec5SDimitry Andric if (getLangOpts().CPlusPlus17 && 45550b57cec5SDimitry Andric isUnresolvedExceptionSpec(SpecializationFPT->getExceptionSpecType()) && 45560b57cec5SDimitry Andric !ResolveExceptionSpec(Info.getLocation(), SpecializationFPT)) 45570b57cec5SDimitry Andric return TDK_MiscellaneousDeductionFailure; 45580b57cec5SDimitry Andric 45590b57cec5SDimitry Andric // Adjust the exception specification of the argument to match the 45600b57cec5SDimitry Andric // substituted and resolved type we just formed. (Calling convention and 45610b57cec5SDimitry Andric // noreturn can't be dependent, so we don't actually need this for them 45620b57cec5SDimitry Andric // right now.) 45630b57cec5SDimitry Andric QualType SpecializationType = Specialization->getType(); 456406c3fb27SDimitry Andric if (!IsAddressOfFunction) { 45650b57cec5SDimitry Andric ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, SpecializationType, 45660b57cec5SDimitry Andric /*AdjustExceptionSpec*/true); 45670b57cec5SDimitry Andric 456806c3fb27SDimitry Andric // Revert placeholder types in the return type back to undeduced types so 456906c3fb27SDimitry Andric // that the comparison below compares the declared return types. 457006c3fb27SDimitry Andric if (HasDeducedReturnType) { 457106c3fb27SDimitry Andric SpecializationType = SubstAutoType(SpecializationType, QualType()); 457206c3fb27SDimitry Andric ArgFunctionType = SubstAutoType(ArgFunctionType, QualType()); 457306c3fb27SDimitry Andric } 457406c3fb27SDimitry Andric } 457506c3fb27SDimitry Andric 45760b57cec5SDimitry Andric // If the requested function type does not match the actual type of the 45770b57cec5SDimitry Andric // specialization with respect to arguments of compatible pointer to function 45780b57cec5SDimitry Andric // types, template argument deduction fails. 45790b57cec5SDimitry Andric if (!ArgFunctionType.isNull()) { 458006c3fb27SDimitry Andric if (IsAddressOfFunction 458106c3fb27SDimitry Andric ? !isSameOrCompatibleFunctionType( 45820b57cec5SDimitry Andric Context.getCanonicalType(SpecializationType), 458306c3fb27SDimitry Andric Context.getCanonicalType(ArgFunctionType)) 458406c3fb27SDimitry Andric : !Context.hasSameType(SpecializationType, ArgFunctionType)) { 458506c3fb27SDimitry Andric Info.FirstArg = TemplateArgument(SpecializationType); 458606c3fb27SDimitry Andric Info.SecondArg = TemplateArgument(ArgFunctionType); 458706c3fb27SDimitry Andric return TDK_NonDeducedMismatch; 458806c3fb27SDimitry Andric } 45890b57cec5SDimitry Andric } 45900b57cec5SDimitry Andric 45910b57cec5SDimitry Andric return TDK_Success; 45920b57cec5SDimitry Andric } 45930b57cec5SDimitry Andric 45940b57cec5SDimitry Andric /// Deduce template arguments for a templated conversion 45950b57cec5SDimitry Andric /// function (C++ [temp.deduct.conv]) and, if successful, produce a 45960b57cec5SDimitry Andric /// conversion function template specialization. 45975f757f3fSDimitry Andric Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( 45985f757f3fSDimitry Andric FunctionTemplateDecl *ConversionTemplate, QualType ObjectType, 45995f757f3fSDimitry Andric Expr::Classification ObjectClassification, QualType ToType, 46005f757f3fSDimitry Andric CXXConversionDecl *&Specialization, TemplateDeductionInfo &Info) { 46010b57cec5SDimitry Andric if (ConversionTemplate->isInvalidDecl()) 46020b57cec5SDimitry Andric return TDK_Invalid; 46030b57cec5SDimitry Andric 46040b57cec5SDimitry Andric CXXConversionDecl *ConversionGeneric 46050b57cec5SDimitry Andric = cast<CXXConversionDecl>(ConversionTemplate->getTemplatedDecl()); 46060b57cec5SDimitry Andric 46070b57cec5SDimitry Andric QualType FromType = ConversionGeneric->getConversionType(); 46080b57cec5SDimitry Andric 46090b57cec5SDimitry Andric // Canonicalize the types for deduction. 46100b57cec5SDimitry Andric QualType P = Context.getCanonicalType(FromType); 46110b57cec5SDimitry Andric QualType A = Context.getCanonicalType(ToType); 46120b57cec5SDimitry Andric 46130b57cec5SDimitry Andric // C++0x [temp.deduct.conv]p2: 46140b57cec5SDimitry Andric // If P is a reference type, the type referred to by P is used for 46150b57cec5SDimitry Andric // type deduction. 46160b57cec5SDimitry Andric if (const ReferenceType *PRef = P->getAs<ReferenceType>()) 46170b57cec5SDimitry Andric P = PRef->getPointeeType(); 46180b57cec5SDimitry Andric 46190b57cec5SDimitry Andric // C++0x [temp.deduct.conv]p4: 46200b57cec5SDimitry Andric // [...] If A is a reference type, the type referred to by A is used 46210b57cec5SDimitry Andric // for type deduction. 46220b57cec5SDimitry Andric if (const ReferenceType *ARef = A->getAs<ReferenceType>()) { 46230b57cec5SDimitry Andric A = ARef->getPointeeType(); 46240b57cec5SDimitry Andric // We work around a defect in the standard here: cv-qualifiers are also 46250b57cec5SDimitry Andric // removed from P and A in this case, unless P was a reference type. This 46260b57cec5SDimitry Andric // seems to mostly match what other compilers are doing. 46270b57cec5SDimitry Andric if (!FromType->getAs<ReferenceType>()) { 46280b57cec5SDimitry Andric A = A.getUnqualifiedType(); 46290b57cec5SDimitry Andric P = P.getUnqualifiedType(); 46300b57cec5SDimitry Andric } 46310b57cec5SDimitry Andric 46320b57cec5SDimitry Andric // C++ [temp.deduct.conv]p3: 46330b57cec5SDimitry Andric // 46340b57cec5SDimitry Andric // If A is not a reference type: 46350b57cec5SDimitry Andric } else { 46360b57cec5SDimitry Andric assert(!A->isReferenceType() && "Reference types were handled above"); 46370b57cec5SDimitry Andric 46380b57cec5SDimitry Andric // - If P is an array type, the pointer type produced by the 46390b57cec5SDimitry Andric // array-to-pointer standard conversion (4.2) is used in place 46400b57cec5SDimitry Andric // of P for type deduction; otherwise, 46410b57cec5SDimitry Andric if (P->isArrayType()) 46420b57cec5SDimitry Andric P = Context.getArrayDecayedType(P); 46430b57cec5SDimitry Andric // - If P is a function type, the pointer type produced by the 46440b57cec5SDimitry Andric // function-to-pointer standard conversion (4.3) is used in 46450b57cec5SDimitry Andric // place of P for type deduction; otherwise, 46460b57cec5SDimitry Andric else if (P->isFunctionType()) 46470b57cec5SDimitry Andric P = Context.getPointerType(P); 46480b57cec5SDimitry Andric // - If P is a cv-qualified type, the top level cv-qualifiers of 46490b57cec5SDimitry Andric // P's type are ignored for type deduction. 46500b57cec5SDimitry Andric else 46510b57cec5SDimitry Andric P = P.getUnqualifiedType(); 46520b57cec5SDimitry Andric 46530b57cec5SDimitry Andric // C++0x [temp.deduct.conv]p4: 46540b57cec5SDimitry Andric // If A is a cv-qualified type, the top level cv-qualifiers of A's 46550b57cec5SDimitry Andric // type are ignored for type deduction. If A is a reference type, the type 46560b57cec5SDimitry Andric // referred to by A is used for type deduction. 46570b57cec5SDimitry Andric A = A.getUnqualifiedType(); 46580b57cec5SDimitry Andric } 46590b57cec5SDimitry Andric 46600b57cec5SDimitry Andric // Unevaluated SFINAE context. 46610b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 46620b57cec5SDimitry Andric *this, Sema::ExpressionEvaluationContext::Unevaluated); 46630b57cec5SDimitry Andric SFINAETrap Trap(*this); 46640b57cec5SDimitry Andric 46650b57cec5SDimitry Andric // C++ [temp.deduct.conv]p1: 46660b57cec5SDimitry Andric // Template argument deduction is done by comparing the return 46670b57cec5SDimitry Andric // type of the template conversion function (call it P) with the 46680b57cec5SDimitry Andric // type that is required as the result of the conversion (call it 46690b57cec5SDimitry Andric // A) as described in 14.8.2.4. 46700b57cec5SDimitry Andric TemplateParameterList *TemplateParams 46710b57cec5SDimitry Andric = ConversionTemplate->getTemplateParameters(); 46720b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> Deduced; 46730b57cec5SDimitry Andric Deduced.resize(TemplateParams->size()); 46740b57cec5SDimitry Andric 46750b57cec5SDimitry Andric // C++0x [temp.deduct.conv]p4: 46760b57cec5SDimitry Andric // In general, the deduction process attempts to find template 46770b57cec5SDimitry Andric // argument values that will make the deduced A identical to 46780b57cec5SDimitry Andric // A. However, there are two cases that allow a difference: 46790b57cec5SDimitry Andric unsigned TDF = 0; 46800b57cec5SDimitry Andric // - If the original A is a reference type, A can be more 46810b57cec5SDimitry Andric // cv-qualified than the deduced A (i.e., the type referred to 46820b57cec5SDimitry Andric // by the reference) 46830b57cec5SDimitry Andric if (ToType->isReferenceType()) 46840b57cec5SDimitry Andric TDF |= TDF_ArgWithReferenceType; 46850b57cec5SDimitry Andric // - The deduced A can be another pointer or pointer to member 46860b57cec5SDimitry Andric // type that can be converted to A via a qualification 46870b57cec5SDimitry Andric // conversion. 46880b57cec5SDimitry Andric // 46890b57cec5SDimitry Andric // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when 46900b57cec5SDimitry Andric // both P and A are pointers or member pointers. In this case, we 46910b57cec5SDimitry Andric // just ignore cv-qualifiers completely). 46920b57cec5SDimitry Andric if ((P->isPointerType() && A->isPointerType()) || 46930b57cec5SDimitry Andric (P->isMemberPointerType() && A->isMemberPointerType())) 46940b57cec5SDimitry Andric TDF |= TDF_IgnoreQualifiers; 46955f757f3fSDimitry Andric 46965f757f3fSDimitry Andric SmallVector<Sema::OriginalCallArg, 1> OriginalCallArgs; 46975f757f3fSDimitry Andric if (ConversionGeneric->isExplicitObjectMemberFunction()) { 46985f757f3fSDimitry Andric QualType ParamType = ConversionGeneric->getParamDecl(0)->getType(); 46995f757f3fSDimitry Andric if (TemplateDeductionResult Result = 47005f757f3fSDimitry Andric DeduceTemplateArgumentsFromCallArgument( 47015f757f3fSDimitry Andric *this, TemplateParams, getFirstInnerIndex(ConversionTemplate), 47025f757f3fSDimitry Andric ParamType, ObjectType, ObjectClassification, 47035f757f3fSDimitry Andric /*Arg=*/nullptr, Info, Deduced, OriginalCallArgs, 47045f757f3fSDimitry Andric /*Decomposed*/ false, 0, /*TDF*/ 0)) 47055f757f3fSDimitry Andric return Result; 47065f757f3fSDimitry Andric } 47075f757f3fSDimitry Andric 47080b57cec5SDimitry Andric if (TemplateDeductionResult Result 47090b57cec5SDimitry Andric = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, 47100b57cec5SDimitry Andric P, A, Info, Deduced, TDF)) 47110b57cec5SDimitry Andric return Result; 47120b57cec5SDimitry Andric 47130b57cec5SDimitry Andric // Create an Instantiation Scope for finalizing the operator. 47140b57cec5SDimitry Andric LocalInstantiationScope InstScope(*this); 47150b57cec5SDimitry Andric // Finish template argument deduction. 47160b57cec5SDimitry Andric FunctionDecl *ConversionSpecialized = nullptr; 47175ffd83dbSDimitry Andric TemplateDeductionResult Result; 47185ffd83dbSDimitry Andric runWithSufficientStackSpace(Info.getLocation(), [&] { 47195ffd83dbSDimitry Andric Result = FinishTemplateArgumentDeduction(ConversionTemplate, Deduced, 0, 47205f757f3fSDimitry Andric ConversionSpecialized, Info, 47215f757f3fSDimitry Andric &OriginalCallArgs); 47225ffd83dbSDimitry Andric }); 47230b57cec5SDimitry Andric Specialization = cast_or_null<CXXConversionDecl>(ConversionSpecialized); 47240b57cec5SDimitry Andric return Result; 47250b57cec5SDimitry Andric } 47260b57cec5SDimitry Andric 47270b57cec5SDimitry Andric /// Deduce template arguments for a function template when there is 47280b57cec5SDimitry Andric /// nothing to deduce against (C++0x [temp.arg.explicit]p3). 47290b57cec5SDimitry Andric /// 47300b57cec5SDimitry Andric /// \param FunctionTemplate the function template for which we are performing 47310b57cec5SDimitry Andric /// template argument deduction. 47320b57cec5SDimitry Andric /// 47330b57cec5SDimitry Andric /// \param ExplicitTemplateArgs the explicitly-specified template 47340b57cec5SDimitry Andric /// arguments. 47350b57cec5SDimitry Andric /// 47360b57cec5SDimitry Andric /// \param Specialization if template argument deduction was successful, 47370b57cec5SDimitry Andric /// this will be set to the function template specialization produced by 47380b57cec5SDimitry Andric /// template argument deduction. 47390b57cec5SDimitry Andric /// 47400b57cec5SDimitry Andric /// \param Info the argument will be updated to provide additional information 47410b57cec5SDimitry Andric /// about template argument deduction. 47420b57cec5SDimitry Andric /// 47430b57cec5SDimitry Andric /// \param IsAddressOfFunction If \c true, we are deducing as part of taking 47440b57cec5SDimitry Andric /// the address of a function template in a context where we do not have a 47450b57cec5SDimitry Andric /// target type, per [over.over]. If \c false, we are looking up a function 47460b57cec5SDimitry Andric /// template specialization based on its signature, which only happens when 47470b57cec5SDimitry Andric /// deducing a function parameter type from an argument that is a template-id 47480b57cec5SDimitry Andric /// naming a function template specialization. 47490b57cec5SDimitry Andric /// 47500b57cec5SDimitry Andric /// \returns the result of template argument deduction. 47510b57cec5SDimitry Andric Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( 47520b57cec5SDimitry Andric FunctionTemplateDecl *FunctionTemplate, 47530b57cec5SDimitry Andric TemplateArgumentListInfo *ExplicitTemplateArgs, 47540b57cec5SDimitry Andric FunctionDecl *&Specialization, TemplateDeductionInfo &Info, 47550b57cec5SDimitry Andric bool IsAddressOfFunction) { 47560b57cec5SDimitry Andric return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, 47570b57cec5SDimitry Andric QualType(), Specialization, Info, 47580b57cec5SDimitry Andric IsAddressOfFunction); 47590b57cec5SDimitry Andric } 47600b57cec5SDimitry Andric 47610b57cec5SDimitry Andric namespace { 47620b57cec5SDimitry Andric struct DependentAuto { bool IsPack; }; 47630b57cec5SDimitry Andric 47640b57cec5SDimitry Andric /// Substitute the 'auto' specifier or deduced template specialization type 47650b57cec5SDimitry Andric /// specifier within a type for a given replacement type. 47660b57cec5SDimitry Andric class SubstituteDeducedTypeTransform : 47670b57cec5SDimitry Andric public TreeTransform<SubstituteDeducedTypeTransform> { 47680b57cec5SDimitry Andric QualType Replacement; 47690b57cec5SDimitry Andric bool ReplacementIsPack; 47700b57cec5SDimitry Andric bool UseTypeSugar; 47711db9f3b2SDimitry Andric using inherited = TreeTransform<SubstituteDeducedTypeTransform>; 47720b57cec5SDimitry Andric 47730b57cec5SDimitry Andric public: 47740b57cec5SDimitry Andric SubstituteDeducedTypeTransform(Sema &SemaRef, DependentAuto DA) 477504eeddc0SDimitry Andric : TreeTransform<SubstituteDeducedTypeTransform>(SemaRef), 47760b57cec5SDimitry Andric ReplacementIsPack(DA.IsPack), UseTypeSugar(true) {} 47770b57cec5SDimitry Andric 47780b57cec5SDimitry Andric SubstituteDeducedTypeTransform(Sema &SemaRef, QualType Replacement, 47790b57cec5SDimitry Andric bool UseTypeSugar = true) 47800b57cec5SDimitry Andric : TreeTransform<SubstituteDeducedTypeTransform>(SemaRef), 47810b57cec5SDimitry Andric Replacement(Replacement), ReplacementIsPack(false), 47820b57cec5SDimitry Andric UseTypeSugar(UseTypeSugar) {} 47830b57cec5SDimitry Andric 47840b57cec5SDimitry Andric QualType TransformDesugared(TypeLocBuilder &TLB, DeducedTypeLoc TL) { 47850b57cec5SDimitry Andric assert(isa<TemplateTypeParmType>(Replacement) && 47860b57cec5SDimitry Andric "unexpected unsugared replacement kind"); 47870b57cec5SDimitry Andric QualType Result = Replacement; 47880b57cec5SDimitry Andric TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result); 47890b57cec5SDimitry Andric NewTL.setNameLoc(TL.getNameLoc()); 47900b57cec5SDimitry Andric return Result; 47910b57cec5SDimitry Andric } 47920b57cec5SDimitry Andric 47930b57cec5SDimitry Andric QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) { 47940b57cec5SDimitry Andric // If we're building the type pattern to deduce against, don't wrap the 47950b57cec5SDimitry Andric // substituted type in an AutoType. Certain template deduction rules 47960b57cec5SDimitry Andric // apply only when a template type parameter appears directly (and not if 47970b57cec5SDimitry Andric // the parameter is found through desugaring). For instance: 47980b57cec5SDimitry Andric // auto &&lref = lvalue; 47990b57cec5SDimitry Andric // must transform into "rvalue reference to T" not "rvalue reference to 48000b57cec5SDimitry Andric // auto type deduced as T" in order for [temp.deduct.call]p3 to apply. 48010b57cec5SDimitry Andric // 48020b57cec5SDimitry Andric // FIXME: Is this still necessary? 48030b57cec5SDimitry Andric if (!UseTypeSugar) 48040b57cec5SDimitry Andric return TransformDesugared(TLB, TL); 48050b57cec5SDimitry Andric 48060b57cec5SDimitry Andric QualType Result = SemaRef.Context.getAutoType( 48070b57cec5SDimitry Andric Replacement, TL.getTypePtr()->getKeyword(), Replacement.isNull(), 480855e4f9d5SDimitry Andric ReplacementIsPack, TL.getTypePtr()->getTypeConstraintConcept(), 480955e4f9d5SDimitry Andric TL.getTypePtr()->getTypeConstraintArguments()); 48100b57cec5SDimitry Andric auto NewTL = TLB.push<AutoTypeLoc>(Result); 481155e4f9d5SDimitry Andric NewTL.copy(TL); 48120b57cec5SDimitry Andric return Result; 48130b57cec5SDimitry Andric } 48140b57cec5SDimitry Andric 48150b57cec5SDimitry Andric QualType TransformDeducedTemplateSpecializationType( 48160b57cec5SDimitry Andric TypeLocBuilder &TLB, DeducedTemplateSpecializationTypeLoc TL) { 48170b57cec5SDimitry Andric if (!UseTypeSugar) 48180b57cec5SDimitry Andric return TransformDesugared(TLB, TL); 48190b57cec5SDimitry Andric 48200b57cec5SDimitry Andric QualType Result = SemaRef.Context.getDeducedTemplateSpecializationType( 48210b57cec5SDimitry Andric TL.getTypePtr()->getTemplateName(), 48220b57cec5SDimitry Andric Replacement, Replacement.isNull()); 48230b57cec5SDimitry Andric auto NewTL = TLB.push<DeducedTemplateSpecializationTypeLoc>(Result); 48240b57cec5SDimitry Andric NewTL.setNameLoc(TL.getNameLoc()); 48250b57cec5SDimitry Andric return Result; 48260b57cec5SDimitry Andric } 48270b57cec5SDimitry Andric 48280b57cec5SDimitry Andric ExprResult TransformLambdaExpr(LambdaExpr *E) { 48290b57cec5SDimitry Andric // Lambdas never need to be transformed. 48300b57cec5SDimitry Andric return E; 48310b57cec5SDimitry Andric } 48321db9f3b2SDimitry Andric bool TransformExceptionSpec(SourceLocation Loc, 48331db9f3b2SDimitry Andric FunctionProtoType::ExceptionSpecInfo &ESI, 48341db9f3b2SDimitry Andric SmallVectorImpl<QualType> &Exceptions, 48351db9f3b2SDimitry Andric bool &Changed) { 48361db9f3b2SDimitry Andric if (ESI.Type == EST_Uninstantiated) { 48371db9f3b2SDimitry Andric ESI.instantiate(); 48381db9f3b2SDimitry Andric Changed = true; 48391db9f3b2SDimitry Andric } 48401db9f3b2SDimitry Andric return inherited::TransformExceptionSpec(Loc, ESI, Exceptions, Changed); 48411db9f3b2SDimitry Andric } 48420b57cec5SDimitry Andric 48430b57cec5SDimitry Andric QualType Apply(TypeLoc TL) { 48440b57cec5SDimitry Andric // Create some scratch storage for the transformed type locations. 48450b57cec5SDimitry Andric // FIXME: We're just going to throw this information away. Don't build it. 48460b57cec5SDimitry Andric TypeLocBuilder TLB; 48470b57cec5SDimitry Andric TLB.reserve(TL.getFullDataSize()); 48480b57cec5SDimitry Andric return TransformType(TLB, TL); 48490b57cec5SDimitry Andric } 48500b57cec5SDimitry Andric }; 48510b57cec5SDimitry Andric 48520b57cec5SDimitry Andric } // namespace 48530b57cec5SDimitry Andric 4854bdd1243dSDimitry Andric static bool CheckDeducedPlaceholderConstraints(Sema &S, const AutoType &Type, 4855bdd1243dSDimitry Andric AutoTypeLoc TypeLoc, 4856bdd1243dSDimitry Andric QualType Deduced) { 485755e4f9d5SDimitry Andric ConstraintSatisfaction Satisfaction; 485855e4f9d5SDimitry Andric ConceptDecl *Concept = Type.getTypeConstraintConcept(); 485955e4f9d5SDimitry Andric TemplateArgumentListInfo TemplateArgs(TypeLoc.getLAngleLoc(), 486055e4f9d5SDimitry Andric TypeLoc.getRAngleLoc()); 486155e4f9d5SDimitry Andric TemplateArgs.addArgument( 486255e4f9d5SDimitry Andric TemplateArgumentLoc(TemplateArgument(Deduced), 486355e4f9d5SDimitry Andric S.Context.getTrivialTypeSourceInfo( 486455e4f9d5SDimitry Andric Deduced, TypeLoc.getNameLoc()))); 486555e4f9d5SDimitry Andric for (unsigned I = 0, C = TypeLoc.getNumArgs(); I != C; ++I) 486655e4f9d5SDimitry Andric TemplateArgs.addArgument(TypeLoc.getArgLoc(I)); 486755e4f9d5SDimitry Andric 4868bdd1243dSDimitry Andric llvm::SmallVector<TemplateArgument, 4> SugaredConverted, CanonicalConverted; 486955e4f9d5SDimitry Andric if (S.CheckTemplateArgumentList(Concept, SourceLocation(), TemplateArgs, 4870bdd1243dSDimitry Andric /*PartialTemplateArgs=*/false, 4871bdd1243dSDimitry Andric SugaredConverted, CanonicalConverted)) 4872bdd1243dSDimitry Andric return true; 4873bdd1243dSDimitry Andric MultiLevelTemplateArgumentList MLTAL(Concept, CanonicalConverted, 4874bdd1243dSDimitry Andric /*Final=*/false); 487555e4f9d5SDimitry Andric if (S.CheckConstraintSatisfaction(Concept, {Concept->getConstraintExpr()}, 4876bdd1243dSDimitry Andric MLTAL, TypeLoc.getLocalSourceRange(), 487755e4f9d5SDimitry Andric Satisfaction)) 4878bdd1243dSDimitry Andric return true; 487955e4f9d5SDimitry Andric if (!Satisfaction.IsSatisfied) { 488055e4f9d5SDimitry Andric std::string Buf; 488155e4f9d5SDimitry Andric llvm::raw_string_ostream OS(Buf); 488255e4f9d5SDimitry Andric OS << "'" << Concept->getName(); 488355e4f9d5SDimitry Andric if (TypeLoc.hasExplicitTemplateArgs()) { 4884fe6060f1SDimitry Andric printTemplateArgumentList( 4885fe6060f1SDimitry Andric OS, Type.getTypeConstraintArguments(), S.getPrintingPolicy(), 4886fe6060f1SDimitry Andric Type.getTypeConstraintConcept()->getTemplateParameters()); 488755e4f9d5SDimitry Andric } 488855e4f9d5SDimitry Andric OS << "'"; 488955e4f9d5SDimitry Andric OS.flush(); 489055e4f9d5SDimitry Andric S.Diag(TypeLoc.getConceptNameLoc(), 489155e4f9d5SDimitry Andric diag::err_placeholder_constraints_not_satisfied) 489255e4f9d5SDimitry Andric << Deduced << Buf << TypeLoc.getLocalSourceRange(); 489355e4f9d5SDimitry Andric S.DiagnoseUnsatisfiedConstraint(Satisfaction); 4894bdd1243dSDimitry Andric return true; 489555e4f9d5SDimitry Andric } 4896bdd1243dSDimitry Andric return false; 489755e4f9d5SDimitry Andric } 489855e4f9d5SDimitry Andric 48990b57cec5SDimitry Andric /// Deduce the type for an auto type-specifier (C++11 [dcl.spec.auto]p6) 49000b57cec5SDimitry Andric /// 49010b57cec5SDimitry Andric /// Note that this is done even if the initializer is dependent. (This is 49020b57cec5SDimitry Andric /// necessary to support partial ordering of templates using 'auto'.) 49030b57cec5SDimitry Andric /// A dependent type will be produced when deducing from a dependent type. 49040b57cec5SDimitry Andric /// 49050b57cec5SDimitry Andric /// \param Type the type pattern using the auto type-specifier. 49060b57cec5SDimitry Andric /// \param Init the initializer for the variable whose type is to be deduced. 49070b57cec5SDimitry Andric /// \param Result if type deduction was successful, this will be set to the 49080b57cec5SDimitry Andric /// deduced type. 4909bdd1243dSDimitry Andric /// \param Info the argument will be updated to provide additional information 4910bdd1243dSDimitry Andric /// about template argument deduction. 4911bdd1243dSDimitry Andric /// \param DependentDeduction Set if we should permit deduction in 49120b57cec5SDimitry Andric /// dependent cases. This is necessary for template partial ordering with 4913bdd1243dSDimitry Andric /// 'auto' template parameters. The template parameter depth to be used 4914bdd1243dSDimitry Andric /// should be specified in the 'Info' parameter. 491555e4f9d5SDimitry Andric /// \param IgnoreConstraints Set if we should not fail if the deduced type does 491655e4f9d5SDimitry Andric /// not satisfy the type-constraint in the auto type. 491706c3fb27SDimitry Andric Sema::TemplateDeductionResult 491806c3fb27SDimitry Andric Sema::DeduceAutoType(TypeLoc Type, Expr *Init, QualType &Result, 491906c3fb27SDimitry Andric TemplateDeductionInfo &Info, bool DependentDeduction, 492006c3fb27SDimitry Andric bool IgnoreConstraints, 492106c3fb27SDimitry Andric TemplateSpecCandidateSet *FailedTSC) { 4922bdd1243dSDimitry Andric assert(DependentDeduction || Info.getDeducedDepth() == 0); 49235ffd83dbSDimitry Andric if (Init->containsErrors()) 4924bdd1243dSDimitry Andric return TDK_AlreadyDiagnosed; 4925bdd1243dSDimitry Andric 4926bdd1243dSDimitry Andric const AutoType *AT = Type.getType()->getContainedAutoType(); 4927bdd1243dSDimitry Andric assert(AT); 4928bdd1243dSDimitry Andric 4929bdd1243dSDimitry Andric if (Init->getType()->isNonOverloadPlaceholderType() || AT->isDecltypeAuto()) { 49300b57cec5SDimitry Andric ExprResult NonPlaceholder = CheckPlaceholderExpr(Init); 49310b57cec5SDimitry Andric if (NonPlaceholder.isInvalid()) 4932bdd1243dSDimitry Andric return TDK_AlreadyDiagnosed; 49330b57cec5SDimitry Andric Init = NonPlaceholder.get(); 49340b57cec5SDimitry Andric } 49350b57cec5SDimitry Andric 49360b57cec5SDimitry Andric DependentAuto DependentResult = { 49370b57cec5SDimitry Andric /*.IsPack = */ (bool)Type.getAs<PackExpansionTypeLoc>()}; 49380b57cec5SDimitry Andric 4939bdd1243dSDimitry Andric if (!DependentDeduction && 4940480093f4SDimitry Andric (Type.getType()->isDependentType() || Init->isTypeDependent() || 4941480093f4SDimitry Andric Init->containsUnexpandedParameterPack())) { 49420b57cec5SDimitry Andric Result = SubstituteDeducedTypeTransform(*this, DependentResult).Apply(Type); 49430b57cec5SDimitry Andric assert(!Result.isNull() && "substituting DependentTy can't fail"); 4944bdd1243dSDimitry Andric return TDK_Success; 49450b57cec5SDimitry Andric } 49460b57cec5SDimitry Andric 49475f757f3fSDimitry Andric // Make sure that we treat 'char[]' equaly as 'char*' in C23 mode. 49485f757f3fSDimitry Andric auto *String = dyn_cast<StringLiteral>(Init); 49495f757f3fSDimitry Andric if (getLangOpts().C23 && String && Type.getType()->isArrayType()) { 49505f757f3fSDimitry Andric Diag(Type.getBeginLoc(), diag::ext_c23_auto_non_plain_identifier); 49515f757f3fSDimitry Andric TypeLoc TL = TypeLoc(Init->getType(), Type.getOpaqueData()); 49525f757f3fSDimitry Andric Result = SubstituteDeducedTypeTransform(*this, DependentResult).Apply(TL); 49535f757f3fSDimitry Andric assert(!Result.isNull() && "substituting DependentTy can't fail"); 49545f757f3fSDimitry Andric return TDK_Success; 49555f757f3fSDimitry Andric } 49565f757f3fSDimitry Andric 49575f757f3fSDimitry Andric // Emit a warning if 'auto*' is used in pedantic and in C23 mode. 49585f757f3fSDimitry Andric if (getLangOpts().C23 && Type.getType()->isPointerType()) { 49595f757f3fSDimitry Andric Diag(Type.getBeginLoc(), diag::ext_c23_auto_non_plain_identifier); 49605f757f3fSDimitry Andric } 49615f757f3fSDimitry Andric 4962bdd1243dSDimitry Andric auto *InitList = dyn_cast<InitListExpr>(Init); 4963bdd1243dSDimitry Andric if (!getLangOpts().CPlusPlus && InitList) { 49645f757f3fSDimitry Andric Diag(Init->getBeginLoc(), diag::err_auto_init_list_from_c) 49655f757f3fSDimitry Andric << (int)AT->getKeyword() << getLangOpts().C23; 4966bdd1243dSDimitry Andric return TDK_AlreadyDiagnosed; 49670b57cec5SDimitry Andric } 49680b57cec5SDimitry Andric 49690b57cec5SDimitry Andric // Deduce type of TemplParam in Func(Init) 49700b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 1> Deduced; 49710b57cec5SDimitry Andric Deduced.resize(1); 49720b57cec5SDimitry Andric 49730b57cec5SDimitry Andric // If deduction failed, don't diagnose if the initializer is dependent; it 49740b57cec5SDimitry Andric // might acquire a matching type in the instantiation. 4975bdd1243dSDimitry Andric auto DeductionFailed = [&](TemplateDeductionResult TDK) { 49760b57cec5SDimitry Andric if (Init->isTypeDependent()) { 49770b57cec5SDimitry Andric Result = 49780b57cec5SDimitry Andric SubstituteDeducedTypeTransform(*this, DependentResult).Apply(Type); 49790b57cec5SDimitry Andric assert(!Result.isNull() && "substituting DependentTy can't fail"); 4980bdd1243dSDimitry Andric return TDK_Success; 49810b57cec5SDimitry Andric } 4982bdd1243dSDimitry Andric return TDK; 49830b57cec5SDimitry Andric }; 49840b57cec5SDimitry Andric 49850b57cec5SDimitry Andric SmallVector<OriginalCallArg, 4> OriginalCallArgs; 49860b57cec5SDimitry Andric 4987bdd1243dSDimitry Andric QualType DeducedType; 4988bdd1243dSDimitry Andric // If this is a 'decltype(auto)' specifier, do the decltype dance. 4989bdd1243dSDimitry Andric if (AT->isDecltypeAuto()) { 49900b57cec5SDimitry Andric if (InitList) { 4991bdd1243dSDimitry Andric Diag(Init->getBeginLoc(), diag::err_decltype_auto_initializer_list); 4992bdd1243dSDimitry Andric return TDK_AlreadyDiagnosed; 4993bdd1243dSDimitry Andric } 49940b57cec5SDimitry Andric 4995bdd1243dSDimitry Andric DeducedType = getDecltypeForExpr(Init); 4996bdd1243dSDimitry Andric assert(!DeducedType.isNull()); 4997bdd1243dSDimitry Andric } else { 4998bdd1243dSDimitry Andric LocalInstantiationScope InstScope(*this); 4999bdd1243dSDimitry Andric 5000bdd1243dSDimitry Andric // Build template<class TemplParam> void Func(FuncParam); 5001bdd1243dSDimitry Andric SourceLocation Loc = Init->getExprLoc(); 5002bdd1243dSDimitry Andric TemplateTypeParmDecl *TemplParam = TemplateTypeParmDecl::Create( 5003bdd1243dSDimitry Andric Context, nullptr, SourceLocation(), Loc, Info.getDeducedDepth(), 0, 5004bdd1243dSDimitry Andric nullptr, false, false, false); 5005bdd1243dSDimitry Andric QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0); 5006bdd1243dSDimitry Andric NamedDecl *TemplParamPtr = TemplParam; 5007bdd1243dSDimitry Andric FixedSizeTemplateParameterListStorage<1, false> TemplateParamsSt( 5008bdd1243dSDimitry Andric Context, Loc, Loc, TemplParamPtr, Loc, nullptr); 5009bdd1243dSDimitry Andric 5010bdd1243dSDimitry Andric if (InitList) { 5011bdd1243dSDimitry Andric // Notionally, we substitute std::initializer_list<T> for 'auto' and 5012bdd1243dSDimitry Andric // deduce against that. Such deduction only succeeds if removing 5013bdd1243dSDimitry Andric // cv-qualifiers and references results in std::initializer_list<T>. 5014bdd1243dSDimitry Andric if (!Type.getType().getNonReferenceType()->getAs<AutoType>()) 5015bdd1243dSDimitry Andric return TDK_Invalid; 5016a7dea167SDimitry Andric 50170b57cec5SDimitry Andric SourceRange DeducedFromInitRange; 5018bdd1243dSDimitry Andric for (Expr *Init : InitList->inits()) { 5019bdd1243dSDimitry Andric // Resolving a core issue: a braced-init-list containing any designators 5020bdd1243dSDimitry Andric // is a non-deduced context. 5021bdd1243dSDimitry Andric if (isa<DesignatedInitExpr>(Init)) 5022bdd1243dSDimitry Andric return TDK_Invalid; 50230b57cec5SDimitry Andric if (auto TDK = DeduceTemplateArgumentsFromCallArgument( 50245f757f3fSDimitry Andric *this, TemplateParamsSt.get(), 0, TemplArg, Init->getType(), 50255f757f3fSDimitry Andric Init->Classify(getASTContext()), Init, Info, Deduced, 5026bdd1243dSDimitry Andric OriginalCallArgs, /*Decomposed=*/true, 5027bdd1243dSDimitry Andric /*ArgIdx=*/0, /*TDF=*/0)) { 5028bdd1243dSDimitry Andric if (TDK == TDK_Inconsistent) { 5029bdd1243dSDimitry Andric Diag(Info.getLocation(), diag::err_auto_inconsistent_deduction) 5030bdd1243dSDimitry Andric << Info.FirstArg << Info.SecondArg << DeducedFromInitRange 5031bdd1243dSDimitry Andric << Init->getSourceRange(); 5032bdd1243dSDimitry Andric return DeductionFailed(TDK_AlreadyDiagnosed); 5033bdd1243dSDimitry Andric } 5034bdd1243dSDimitry Andric return DeductionFailed(TDK); 5035bdd1243dSDimitry Andric } 50360b57cec5SDimitry Andric 50370b57cec5SDimitry Andric if (DeducedFromInitRange.isInvalid() && 50380b57cec5SDimitry Andric Deduced[0].getKind() != TemplateArgument::Null) 50390b57cec5SDimitry Andric DeducedFromInitRange = Init->getSourceRange(); 50400b57cec5SDimitry Andric } 50410b57cec5SDimitry Andric } else { 50420b57cec5SDimitry Andric if (!getLangOpts().CPlusPlus && Init->refersToBitField()) { 50430b57cec5SDimitry Andric Diag(Loc, diag::err_auto_bitfield); 5044bdd1243dSDimitry Andric return TDK_AlreadyDiagnosed; 50450b57cec5SDimitry Andric } 5046bdd1243dSDimitry Andric QualType FuncParam = 5047bdd1243dSDimitry Andric SubstituteDeducedTypeTransform(*this, TemplArg).Apply(Type); 5048bdd1243dSDimitry Andric assert(!FuncParam.isNull() && 5049bdd1243dSDimitry Andric "substituting template parameter for 'auto' failed"); 50500b57cec5SDimitry Andric if (auto TDK = DeduceTemplateArgumentsFromCallArgument( 50515f757f3fSDimitry Andric *this, TemplateParamsSt.get(), 0, FuncParam, Init->getType(), 50525f757f3fSDimitry Andric Init->Classify(getASTContext()), Init, Info, Deduced, 505306c3fb27SDimitry Andric OriginalCallArgs, /*Decomposed=*/false, /*ArgIdx=*/0, /*TDF=*/0, 505406c3fb27SDimitry Andric FailedTSC)) 5055bdd1243dSDimitry Andric return DeductionFailed(TDK); 50560b57cec5SDimitry Andric } 50570b57cec5SDimitry Andric 50580b57cec5SDimitry Andric // Could be null if somehow 'auto' appears in a non-deduced context. 50590b57cec5SDimitry Andric if (Deduced[0].getKind() != TemplateArgument::Type) 5060bdd1243dSDimitry Andric return DeductionFailed(TDK_Incomplete); 5061bdd1243dSDimitry Andric DeducedType = Deduced[0].getAsType(); 50620b57cec5SDimitry Andric 50630b57cec5SDimitry Andric if (InitList) { 50640b57cec5SDimitry Andric DeducedType = BuildStdInitializerList(DeducedType, Loc); 50650b57cec5SDimitry Andric if (DeducedType.isNull()) 5066bdd1243dSDimitry Andric return TDK_AlreadyDiagnosed; 5067bdd1243dSDimitry Andric } 50680b57cec5SDimitry Andric } 50690b57cec5SDimitry Andric 5070bdd1243dSDimitry Andric if (!Result.isNull()) { 5071bdd1243dSDimitry Andric if (!Context.hasSameType(DeducedType, Result)) { 5072bdd1243dSDimitry Andric Info.FirstArg = Result; 5073bdd1243dSDimitry Andric Info.SecondArg = DeducedType; 5074bdd1243dSDimitry Andric return DeductionFailed(TDK_Inconsistent); 507555e4f9d5SDimitry Andric } 5076bdd1243dSDimitry Andric DeducedType = Context.getCommonSugaredType(Result, DeducedType); 507755e4f9d5SDimitry Andric } 507855e4f9d5SDimitry Andric 5079bdd1243dSDimitry Andric if (AT->isConstrained() && !IgnoreConstraints && 5080bdd1243dSDimitry Andric CheckDeducedPlaceholderConstraints( 5081bdd1243dSDimitry Andric *this, *AT, Type.getContainedAutoTypeLoc(), DeducedType)) 5082bdd1243dSDimitry Andric return TDK_AlreadyDiagnosed; 5083bdd1243dSDimitry Andric 50840b57cec5SDimitry Andric Result = SubstituteDeducedTypeTransform(*this, DeducedType).Apply(Type); 50850b57cec5SDimitry Andric if (Result.isNull()) 5086bdd1243dSDimitry Andric return TDK_AlreadyDiagnosed; 50870b57cec5SDimitry Andric 50880b57cec5SDimitry Andric // Check that the deduced argument type is compatible with the original 50890b57cec5SDimitry Andric // argument type per C++ [temp.deduct.call]p4. 50900b57cec5SDimitry Andric QualType DeducedA = InitList ? Deduced[0].getAsType() : Result; 50910b57cec5SDimitry Andric for (const OriginalCallArg &OriginalArg : OriginalCallArgs) { 50920b57cec5SDimitry Andric assert((bool)InitList == OriginalArg.DecomposedParam && 50930b57cec5SDimitry Andric "decomposed non-init-list in auto deduction?"); 50940b57cec5SDimitry Andric if (auto TDK = 50950b57cec5SDimitry Andric CheckOriginalCallArgDeduction(*this, Info, OriginalArg, DeducedA)) { 50960b57cec5SDimitry Andric Result = QualType(); 5097bdd1243dSDimitry Andric return DeductionFailed(TDK); 50980b57cec5SDimitry Andric } 50990b57cec5SDimitry Andric } 51000b57cec5SDimitry Andric 5101bdd1243dSDimitry Andric return TDK_Success; 51020b57cec5SDimitry Andric } 51030b57cec5SDimitry Andric 51040b57cec5SDimitry Andric QualType Sema::SubstAutoType(QualType TypeWithAuto, 51050b57cec5SDimitry Andric QualType TypeToReplaceAuto) { 5106349cc55cSDimitry Andric assert(TypeToReplaceAuto != Context.DependentTy); 51070b57cec5SDimitry Andric return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto) 51080b57cec5SDimitry Andric .TransformType(TypeWithAuto); 51090b57cec5SDimitry Andric } 51100b57cec5SDimitry Andric 51110b57cec5SDimitry Andric TypeSourceInfo *Sema::SubstAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto, 51120b57cec5SDimitry Andric QualType TypeToReplaceAuto) { 5113349cc55cSDimitry Andric assert(TypeToReplaceAuto != Context.DependentTy); 51140b57cec5SDimitry Andric return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto) 51150b57cec5SDimitry Andric .TransformType(TypeWithAuto); 51160b57cec5SDimitry Andric } 51170b57cec5SDimitry Andric 5118349cc55cSDimitry Andric QualType Sema::SubstAutoTypeDependent(QualType TypeWithAuto) { 5119349cc55cSDimitry Andric return SubstituteDeducedTypeTransform(*this, DependentAuto{false}) 5120349cc55cSDimitry Andric .TransformType(TypeWithAuto); 5121349cc55cSDimitry Andric } 5122349cc55cSDimitry Andric 5123349cc55cSDimitry Andric TypeSourceInfo * 5124349cc55cSDimitry Andric Sema::SubstAutoTypeSourceInfoDependent(TypeSourceInfo *TypeWithAuto) { 5125349cc55cSDimitry Andric return SubstituteDeducedTypeTransform(*this, DependentAuto{false}) 5126349cc55cSDimitry Andric .TransformType(TypeWithAuto); 5127349cc55cSDimitry Andric } 5128349cc55cSDimitry Andric 51290b57cec5SDimitry Andric QualType Sema::ReplaceAutoType(QualType TypeWithAuto, 51300b57cec5SDimitry Andric QualType TypeToReplaceAuto) { 51310b57cec5SDimitry Andric return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto, 51320b57cec5SDimitry Andric /*UseTypeSugar*/ false) 51330b57cec5SDimitry Andric .TransformType(TypeWithAuto); 51340b57cec5SDimitry Andric } 51350b57cec5SDimitry Andric 5136e63539f3SDimitry Andric TypeSourceInfo *Sema::ReplaceAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto, 5137e63539f3SDimitry Andric QualType TypeToReplaceAuto) { 5138e63539f3SDimitry Andric return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto, 5139e63539f3SDimitry Andric /*UseTypeSugar*/ false) 5140e63539f3SDimitry Andric .TransformType(TypeWithAuto); 5141e63539f3SDimitry Andric } 5142e63539f3SDimitry Andric 51430b57cec5SDimitry Andric void Sema::DiagnoseAutoDeductionFailure(VarDecl *VDecl, Expr *Init) { 51440b57cec5SDimitry Andric if (isa<InitListExpr>(Init)) 51450b57cec5SDimitry Andric Diag(VDecl->getLocation(), 51460b57cec5SDimitry Andric VDecl->isInitCapture() 51470b57cec5SDimitry Andric ? diag::err_init_capture_deduction_failure_from_init_list 51480b57cec5SDimitry Andric : diag::err_auto_var_deduction_failure_from_init_list) 51490b57cec5SDimitry Andric << VDecl->getDeclName() << VDecl->getType() << Init->getSourceRange(); 51500b57cec5SDimitry Andric else 51510b57cec5SDimitry Andric Diag(VDecl->getLocation(), 51520b57cec5SDimitry Andric VDecl->isInitCapture() ? diag::err_init_capture_deduction_failure 51530b57cec5SDimitry Andric : diag::err_auto_var_deduction_failure) 51540b57cec5SDimitry Andric << VDecl->getDeclName() << VDecl->getType() << Init->getType() 51550b57cec5SDimitry Andric << Init->getSourceRange(); 51560b57cec5SDimitry Andric } 51570b57cec5SDimitry Andric 51580b57cec5SDimitry Andric bool Sema::DeduceReturnType(FunctionDecl *FD, SourceLocation Loc, 51590b57cec5SDimitry Andric bool Diagnose) { 51600b57cec5SDimitry Andric assert(FD->getReturnType()->isUndeducedType()); 51610b57cec5SDimitry Andric 51620b57cec5SDimitry Andric // For a lambda's conversion operator, deduce any 'auto' or 'decltype(auto)' 51630b57cec5SDimitry Andric // within the return type from the call operator's type. 51640b57cec5SDimitry Andric if (isLambdaConversionOperator(FD)) { 51650b57cec5SDimitry Andric CXXRecordDecl *Lambda = cast<CXXMethodDecl>(FD)->getParent(); 51660b57cec5SDimitry Andric FunctionDecl *CallOp = Lambda->getLambdaCallOperator(); 51670b57cec5SDimitry Andric 51680b57cec5SDimitry Andric // For a generic lambda, instantiate the call operator if needed. 51690b57cec5SDimitry Andric if (auto *Args = FD->getTemplateSpecializationArgs()) { 51700b57cec5SDimitry Andric CallOp = InstantiateFunctionDeclaration( 51710b57cec5SDimitry Andric CallOp->getDescribedFunctionTemplate(), Args, Loc); 51720b57cec5SDimitry Andric if (!CallOp || CallOp->isInvalidDecl()) 51730b57cec5SDimitry Andric return true; 51740b57cec5SDimitry Andric 51750b57cec5SDimitry Andric // We might need to deduce the return type by instantiating the definition 51760b57cec5SDimitry Andric // of the operator() function. 5177a7dea167SDimitry Andric if (CallOp->getReturnType()->isUndeducedType()) { 5178a7dea167SDimitry Andric runWithSufficientStackSpace(Loc, [&] { 51790b57cec5SDimitry Andric InstantiateFunctionDefinition(Loc, CallOp); 5180a7dea167SDimitry Andric }); 5181a7dea167SDimitry Andric } 51820b57cec5SDimitry Andric } 51830b57cec5SDimitry Andric 51840b57cec5SDimitry Andric if (CallOp->isInvalidDecl()) 51850b57cec5SDimitry Andric return true; 51860b57cec5SDimitry Andric assert(!CallOp->getReturnType()->isUndeducedType() && 51870b57cec5SDimitry Andric "failed to deduce lambda return type"); 51880b57cec5SDimitry Andric 51890b57cec5SDimitry Andric // Build the new return type from scratch. 5190e8d8bef9SDimitry Andric CallingConv RetTyCC = FD->getReturnType() 5191e8d8bef9SDimitry Andric ->getPointeeType() 5192e8d8bef9SDimitry Andric ->castAs<FunctionType>() 5193e8d8bef9SDimitry Andric ->getCallConv(); 51940b57cec5SDimitry Andric QualType RetType = getLambdaConversionFunctionResultType( 5195e8d8bef9SDimitry Andric CallOp->getType()->castAs<FunctionProtoType>(), RetTyCC); 51960b57cec5SDimitry Andric if (FD->getReturnType()->getAs<PointerType>()) 51970b57cec5SDimitry Andric RetType = Context.getPointerType(RetType); 51980b57cec5SDimitry Andric else { 51990b57cec5SDimitry Andric assert(FD->getReturnType()->getAs<BlockPointerType>()); 52000b57cec5SDimitry Andric RetType = Context.getBlockPointerType(RetType); 52010b57cec5SDimitry Andric } 52020b57cec5SDimitry Andric Context.adjustDeducedFunctionResultType(FD, RetType); 52030b57cec5SDimitry Andric return false; 52040b57cec5SDimitry Andric } 52050b57cec5SDimitry Andric 5206a7dea167SDimitry Andric if (FD->getTemplateInstantiationPattern()) { 5207a7dea167SDimitry Andric runWithSufficientStackSpace(Loc, [&] { 52080b57cec5SDimitry Andric InstantiateFunctionDefinition(Loc, FD); 5209a7dea167SDimitry Andric }); 5210a7dea167SDimitry Andric } 52110b57cec5SDimitry Andric 52120b57cec5SDimitry Andric bool StillUndeduced = FD->getReturnType()->isUndeducedType(); 52130b57cec5SDimitry Andric if (StillUndeduced && Diagnose && !FD->isInvalidDecl()) { 52140b57cec5SDimitry Andric Diag(Loc, diag::err_auto_fn_used_before_defined) << FD; 52150b57cec5SDimitry Andric Diag(FD->getLocation(), diag::note_callee_decl) << FD; 52160b57cec5SDimitry Andric } 52170b57cec5SDimitry Andric 52180b57cec5SDimitry Andric return StillUndeduced; 52190b57cec5SDimitry Andric } 52200b57cec5SDimitry Andric 522106c3fb27SDimitry Andric bool Sema::CheckIfFunctionSpecializationIsImmediate(FunctionDecl *FD, 522206c3fb27SDimitry Andric SourceLocation Loc) { 522306c3fb27SDimitry Andric assert(FD->isImmediateEscalating()); 522406c3fb27SDimitry Andric 522506c3fb27SDimitry Andric if (isLambdaConversionOperator(FD)) { 522606c3fb27SDimitry Andric CXXRecordDecl *Lambda = cast<CXXMethodDecl>(FD)->getParent(); 522706c3fb27SDimitry Andric FunctionDecl *CallOp = Lambda->getLambdaCallOperator(); 522806c3fb27SDimitry Andric 522906c3fb27SDimitry Andric // For a generic lambda, instantiate the call operator if needed. 523006c3fb27SDimitry Andric if (auto *Args = FD->getTemplateSpecializationArgs()) { 523106c3fb27SDimitry Andric CallOp = InstantiateFunctionDeclaration( 523206c3fb27SDimitry Andric CallOp->getDescribedFunctionTemplate(), Args, Loc); 523306c3fb27SDimitry Andric if (!CallOp || CallOp->isInvalidDecl()) 523406c3fb27SDimitry Andric return true; 523506c3fb27SDimitry Andric runWithSufficientStackSpace( 523606c3fb27SDimitry Andric Loc, [&] { InstantiateFunctionDefinition(Loc, CallOp); }); 523706c3fb27SDimitry Andric } 523806c3fb27SDimitry Andric return CallOp->isInvalidDecl(); 523906c3fb27SDimitry Andric } 524006c3fb27SDimitry Andric 524106c3fb27SDimitry Andric if (FD->getTemplateInstantiationPattern()) { 524206c3fb27SDimitry Andric runWithSufficientStackSpace( 524306c3fb27SDimitry Andric Loc, [&] { InstantiateFunctionDefinition(Loc, FD); }); 524406c3fb27SDimitry Andric } 524506c3fb27SDimitry Andric return false; 524606c3fb27SDimitry Andric } 524706c3fb27SDimitry Andric 52480b57cec5SDimitry Andric /// If this is a non-static member function, 52490b57cec5SDimitry Andric static void 52500b57cec5SDimitry Andric AddImplicitObjectParameterType(ASTContext &Context, 52510b57cec5SDimitry Andric CXXMethodDecl *Method, 52520b57cec5SDimitry Andric SmallVectorImpl<QualType> &ArgTypes) { 52530b57cec5SDimitry Andric // C++11 [temp.func.order]p3: 52540b57cec5SDimitry Andric // [...] The new parameter is of type "reference to cv A," where cv are 52550b57cec5SDimitry Andric // the cv-qualifiers of the function template (if any) and A is 52560b57cec5SDimitry Andric // the class of which the function template is a member. 52570b57cec5SDimitry Andric // 52580b57cec5SDimitry Andric // The standard doesn't say explicitly, but we pick the appropriate kind of 52590b57cec5SDimitry Andric // reference type based on [over.match.funcs]p4. 52605f757f3fSDimitry Andric assert(Method && Method->isImplicitObjectMemberFunction() && 52615f757f3fSDimitry Andric "expected an implicit objet function"); 52620b57cec5SDimitry Andric QualType ArgTy = Context.getTypeDeclType(Method->getParent()); 52630b57cec5SDimitry Andric ArgTy = Context.getQualifiedType(ArgTy, Method->getMethodQualifiers()); 52640b57cec5SDimitry Andric if (Method->getRefQualifier() == RQ_RValue) 52650b57cec5SDimitry Andric ArgTy = Context.getRValueReferenceType(ArgTy); 52660b57cec5SDimitry Andric else 52670b57cec5SDimitry Andric ArgTy = Context.getLValueReferenceType(ArgTy); 52680b57cec5SDimitry Andric ArgTypes.push_back(ArgTy); 52690b57cec5SDimitry Andric } 52700b57cec5SDimitry Andric 52710b57cec5SDimitry Andric /// Determine whether the function template \p FT1 is at least as 52720b57cec5SDimitry Andric /// specialized as \p FT2. 52730b57cec5SDimitry Andric static bool isAtLeastAsSpecializedAs(Sema &S, 52740b57cec5SDimitry Andric SourceLocation Loc, 52750b57cec5SDimitry Andric FunctionTemplateDecl *FT1, 52760b57cec5SDimitry Andric FunctionTemplateDecl *FT2, 52770b57cec5SDimitry Andric TemplatePartialOrderingContext TPOC, 52785ffd83dbSDimitry Andric unsigned NumCallArguments1, 52795ffd83dbSDimitry Andric bool Reversed) { 52805ffd83dbSDimitry Andric assert(!Reversed || TPOC == TPOC_Call); 52815ffd83dbSDimitry Andric 52820b57cec5SDimitry Andric FunctionDecl *FD1 = FT1->getTemplatedDecl(); 52830b57cec5SDimitry Andric FunctionDecl *FD2 = FT2->getTemplatedDecl(); 52840b57cec5SDimitry Andric const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); 52850b57cec5SDimitry Andric const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); 52860b57cec5SDimitry Andric 52870b57cec5SDimitry Andric assert(Proto1 && Proto2 && "Function templates must have prototypes"); 52880b57cec5SDimitry Andric TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); 52890b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> Deduced; 52900b57cec5SDimitry Andric Deduced.resize(TemplateParams->size()); 52910b57cec5SDimitry Andric 52920b57cec5SDimitry Andric // C++0x [temp.deduct.partial]p3: 52930b57cec5SDimitry Andric // The types used to determine the ordering depend on the context in which 52940b57cec5SDimitry Andric // the partial ordering is done: 52950b57cec5SDimitry Andric TemplateDeductionInfo Info(Loc); 52960b57cec5SDimitry Andric SmallVector<QualType, 4> Args2; 52970b57cec5SDimitry Andric switch (TPOC) { 52980b57cec5SDimitry Andric case TPOC_Call: { 52990b57cec5SDimitry Andric // - In the context of a function call, the function parameter types are 53000b57cec5SDimitry Andric // used. 53010b57cec5SDimitry Andric CXXMethodDecl *Method1 = dyn_cast<CXXMethodDecl>(FD1); 53020b57cec5SDimitry Andric CXXMethodDecl *Method2 = dyn_cast<CXXMethodDecl>(FD2); 53030b57cec5SDimitry Andric 53040b57cec5SDimitry Andric // C++11 [temp.func.order]p3: 53050b57cec5SDimitry Andric // [...] If only one of the function templates is a non-static 53060b57cec5SDimitry Andric // member, that function template is considered to have a new 53070b57cec5SDimitry Andric // first parameter inserted in its function parameter list. The 53080b57cec5SDimitry Andric // new parameter is of type "reference to cv A," where cv are 53090b57cec5SDimitry Andric // the cv-qualifiers of the function template (if any) and A is 53100b57cec5SDimitry Andric // the class of which the function template is a member. 53110b57cec5SDimitry Andric // 53120b57cec5SDimitry Andric // Note that we interpret this to mean "if one of the function 53130b57cec5SDimitry Andric // templates is a non-static member and the other is a non-member"; 53140b57cec5SDimitry Andric // otherwise, the ordering rules for static functions against non-static 53150b57cec5SDimitry Andric // functions don't make any sense. 53160b57cec5SDimitry Andric // 53170b57cec5SDimitry Andric // C++98/03 doesn't have this provision but we've extended DR532 to cover 53180b57cec5SDimitry Andric // it as wording was broken prior to it. 53190b57cec5SDimitry Andric SmallVector<QualType, 4> Args1; 53200b57cec5SDimitry Andric 53210b57cec5SDimitry Andric unsigned NumComparedArguments = NumCallArguments1; 53220b57cec5SDimitry Andric 53235f757f3fSDimitry Andric if (!Method2 && Method1 && Method1->isImplicitObjectMemberFunction()) { 53240b57cec5SDimitry Andric // Compare 'this' from Method1 against first parameter from Method2. 53250b57cec5SDimitry Andric AddImplicitObjectParameterType(S.Context, Method1, Args1); 53260b57cec5SDimitry Andric ++NumComparedArguments; 53275f757f3fSDimitry Andric } else if (!Method1 && Method2 && 53285f757f3fSDimitry Andric Method2->isImplicitObjectMemberFunction()) { 53290b57cec5SDimitry Andric // Compare 'this' from Method2 against first parameter from Method1. 53300b57cec5SDimitry Andric AddImplicitObjectParameterType(S.Context, Method2, Args2); 53315f757f3fSDimitry Andric } else if (Method1 && Method2 && Reversed && 53325f757f3fSDimitry Andric Method1->isImplicitObjectMemberFunction() && 53335f757f3fSDimitry Andric Method2->isImplicitObjectMemberFunction()) { 53345ffd83dbSDimitry Andric // Compare 'this' from Method1 against second parameter from Method2 53355ffd83dbSDimitry Andric // and 'this' from Method2 against second parameter from Method1. 53365ffd83dbSDimitry Andric AddImplicitObjectParameterType(S.Context, Method1, Args1); 53375ffd83dbSDimitry Andric AddImplicitObjectParameterType(S.Context, Method2, Args2); 53385ffd83dbSDimitry Andric ++NumComparedArguments; 53390b57cec5SDimitry Andric } 53400b57cec5SDimitry Andric 53410b57cec5SDimitry Andric Args1.insert(Args1.end(), Proto1->param_type_begin(), 53420b57cec5SDimitry Andric Proto1->param_type_end()); 53430b57cec5SDimitry Andric Args2.insert(Args2.end(), Proto2->param_type_begin(), 53440b57cec5SDimitry Andric Proto2->param_type_end()); 53450b57cec5SDimitry Andric 53460b57cec5SDimitry Andric // C++ [temp.func.order]p5: 53470b57cec5SDimitry Andric // The presence of unused ellipsis and default arguments has no effect on 53480b57cec5SDimitry Andric // the partial ordering of function templates. 53490b57cec5SDimitry Andric if (Args1.size() > NumComparedArguments) 53500b57cec5SDimitry Andric Args1.resize(NumComparedArguments); 53510b57cec5SDimitry Andric if (Args2.size() > NumComparedArguments) 53520b57cec5SDimitry Andric Args2.resize(NumComparedArguments); 53535ffd83dbSDimitry Andric if (Reversed) 53545ffd83dbSDimitry Andric std::reverse(Args2.begin(), Args2.end()); 5355349cc55cSDimitry Andric 53560b57cec5SDimitry Andric if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(), 53570b57cec5SDimitry Andric Args1.data(), Args1.size(), Info, Deduced, 53580b57cec5SDimitry Andric TDF_None, /*PartialOrdering=*/true)) 53590b57cec5SDimitry Andric return false; 53600b57cec5SDimitry Andric 53610b57cec5SDimitry Andric break; 53620b57cec5SDimitry Andric } 53630b57cec5SDimitry Andric 53640b57cec5SDimitry Andric case TPOC_Conversion: 53650b57cec5SDimitry Andric // - In the context of a call to a conversion operator, the return types 53660b57cec5SDimitry Andric // of the conversion function templates are used. 53670b57cec5SDimitry Andric if (DeduceTemplateArgumentsByTypeMatch( 53680b57cec5SDimitry Andric S, TemplateParams, Proto2->getReturnType(), Proto1->getReturnType(), 53690b57cec5SDimitry Andric Info, Deduced, TDF_None, 53700b57cec5SDimitry Andric /*PartialOrdering=*/true)) 53710b57cec5SDimitry Andric return false; 53720b57cec5SDimitry Andric break; 53730b57cec5SDimitry Andric 53740b57cec5SDimitry Andric case TPOC_Other: 53750b57cec5SDimitry Andric // - In other contexts (14.6.6.2) the function template's function type 53760b57cec5SDimitry Andric // is used. 53770b57cec5SDimitry Andric if (DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, 53780b57cec5SDimitry Andric FD2->getType(), FD1->getType(), 53790b57cec5SDimitry Andric Info, Deduced, TDF_None, 53800b57cec5SDimitry Andric /*PartialOrdering=*/true)) 53810b57cec5SDimitry Andric return false; 53820b57cec5SDimitry Andric break; 53830b57cec5SDimitry Andric } 53840b57cec5SDimitry Andric 53850b57cec5SDimitry Andric // C++0x [temp.deduct.partial]p11: 53860b57cec5SDimitry Andric // In most cases, all template parameters must have values in order for 53870b57cec5SDimitry Andric // deduction to succeed, but for partial ordering purposes a template 53880b57cec5SDimitry Andric // parameter may remain without a value provided it is not used in the 53890b57cec5SDimitry Andric // types being used for partial ordering. [ Note: a template parameter used 53900b57cec5SDimitry Andric // in a non-deduced context is considered used. -end note] 53910b57cec5SDimitry Andric unsigned ArgIdx = 0, NumArgs = Deduced.size(); 53920b57cec5SDimitry Andric for (; ArgIdx != NumArgs; ++ArgIdx) 53930b57cec5SDimitry Andric if (Deduced[ArgIdx].isNull()) 53940b57cec5SDimitry Andric break; 53950b57cec5SDimitry Andric 53960b57cec5SDimitry Andric // FIXME: We fail to implement [temp.deduct.type]p1 along this path. We need 53970b57cec5SDimitry Andric // to substitute the deduced arguments back into the template and check that 53980b57cec5SDimitry Andric // we get the right type. 53990b57cec5SDimitry Andric 54000b57cec5SDimitry Andric if (ArgIdx == NumArgs) { 54010b57cec5SDimitry Andric // All template arguments were deduced. FT1 is at least as specialized 54020b57cec5SDimitry Andric // as FT2. 54030b57cec5SDimitry Andric return true; 54040b57cec5SDimitry Andric } 54050b57cec5SDimitry Andric 54060b57cec5SDimitry Andric // Figure out which template parameters were used. 54070b57cec5SDimitry Andric llvm::SmallBitVector UsedParameters(TemplateParams->size()); 54080b57cec5SDimitry Andric switch (TPOC) { 54090b57cec5SDimitry Andric case TPOC_Call: 54100b57cec5SDimitry Andric for (unsigned I = 0, N = Args2.size(); I != N; ++I) 54110b57cec5SDimitry Andric ::MarkUsedTemplateParameters(S.Context, Args2[I], false, 54120b57cec5SDimitry Andric TemplateParams->getDepth(), 54130b57cec5SDimitry Andric UsedParameters); 54140b57cec5SDimitry Andric break; 54150b57cec5SDimitry Andric 54160b57cec5SDimitry Andric case TPOC_Conversion: 54170b57cec5SDimitry Andric ::MarkUsedTemplateParameters(S.Context, Proto2->getReturnType(), false, 54180b57cec5SDimitry Andric TemplateParams->getDepth(), UsedParameters); 54190b57cec5SDimitry Andric break; 54200b57cec5SDimitry Andric 54210b57cec5SDimitry Andric case TPOC_Other: 54220b57cec5SDimitry Andric ::MarkUsedTemplateParameters(S.Context, FD2->getType(), false, 54230b57cec5SDimitry Andric TemplateParams->getDepth(), 54240b57cec5SDimitry Andric UsedParameters); 54250b57cec5SDimitry Andric break; 54260b57cec5SDimitry Andric } 54270b57cec5SDimitry Andric 54280b57cec5SDimitry Andric for (; ArgIdx != NumArgs; ++ArgIdx) 54290b57cec5SDimitry Andric // If this argument had no value deduced but was used in one of the types 54300b57cec5SDimitry Andric // used for partial ordering, then deduction fails. 54310b57cec5SDimitry Andric if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) 54320b57cec5SDimitry Andric return false; 54330b57cec5SDimitry Andric 54340b57cec5SDimitry Andric return true; 54350b57cec5SDimitry Andric } 54360b57cec5SDimitry Andric 54370b57cec5SDimitry Andric /// Returns the more specialized function template according 54380b57cec5SDimitry Andric /// to the rules of function template partial ordering (C++ [temp.func.order]). 54390b57cec5SDimitry Andric /// 54400b57cec5SDimitry Andric /// \param FT1 the first function template 54410b57cec5SDimitry Andric /// 54420b57cec5SDimitry Andric /// \param FT2 the second function template 54430b57cec5SDimitry Andric /// 54440b57cec5SDimitry Andric /// \param TPOC the context in which we are performing partial ordering of 54450b57cec5SDimitry Andric /// function templates. 54460b57cec5SDimitry Andric /// 54470b57cec5SDimitry Andric /// \param NumCallArguments1 The number of arguments in the call to FT1, used 54480b57cec5SDimitry Andric /// only when \c TPOC is \c TPOC_Call. 54490b57cec5SDimitry Andric /// 54500b57cec5SDimitry Andric /// \param NumCallArguments2 The number of arguments in the call to FT2, used 54510b57cec5SDimitry Andric /// only when \c TPOC is \c TPOC_Call. 54520b57cec5SDimitry Andric /// 54535ffd83dbSDimitry Andric /// \param Reversed If \c true, exactly one of FT1 and FT2 is an overload 54545ffd83dbSDimitry Andric /// candidate with a reversed parameter order. In this case, the corresponding 54555ffd83dbSDimitry Andric /// P/A pairs between FT1 and FT2 are reversed. 54565ffd83dbSDimitry Andric /// 54570b57cec5SDimitry Andric /// \returns the more specialized function template. If neither 54580b57cec5SDimitry Andric /// template is more specialized, returns NULL. 545981ad6265SDimitry Andric FunctionTemplateDecl *Sema::getMoreSpecializedTemplate( 546081ad6265SDimitry Andric FunctionTemplateDecl *FT1, FunctionTemplateDecl *FT2, SourceLocation Loc, 546181ad6265SDimitry Andric TemplatePartialOrderingContext TPOC, unsigned NumCallArguments1, 5462bdd1243dSDimitry Andric unsigned NumCallArguments2, bool Reversed) { 5463480093f4SDimitry Andric 5464bdd1243dSDimitry Andric bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, 5465bdd1243dSDimitry Andric NumCallArguments1, Reversed); 5466bdd1243dSDimitry Andric bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC, 5467bdd1243dSDimitry Andric NumCallArguments2, Reversed); 5468bdd1243dSDimitry Andric 5469bdd1243dSDimitry Andric // C++ [temp.deduct.partial]p10: 5470bdd1243dSDimitry Andric // F is more specialized than G if F is at least as specialized as G and G 5471bdd1243dSDimitry Andric // is not at least as specialized as F. 5472bdd1243dSDimitry Andric if (Better1 != Better2) // We have a clear winner 5473bdd1243dSDimitry Andric return Better1 ? FT1 : FT2; 5474bdd1243dSDimitry Andric 5475bdd1243dSDimitry Andric if (!Better1 && !Better2) // Neither is better than the other 547681ad6265SDimitry Andric return nullptr; 5477bdd1243dSDimitry Andric 5478bdd1243dSDimitry Andric // C++ [temp.deduct.partial]p11: 5479bdd1243dSDimitry Andric // ... and if G has a trailing function parameter pack for which F does not 5480bdd1243dSDimitry Andric // have a corresponding parameter, and if F does not have a trailing 5481bdd1243dSDimitry Andric // function parameter pack, then F is more specialized than G. 5482bdd1243dSDimitry Andric FunctionDecl *FD1 = FT1->getTemplatedDecl(); 5483bdd1243dSDimitry Andric FunctionDecl *FD2 = FT2->getTemplatedDecl(); 5484bdd1243dSDimitry Andric unsigned NumParams1 = FD1->getNumParams(); 5485bdd1243dSDimitry Andric unsigned NumParams2 = FD2->getNumParams(); 5486bdd1243dSDimitry Andric bool Variadic1 = NumParams1 && FD1->parameters().back()->isParameterPack(); 5487bdd1243dSDimitry Andric bool Variadic2 = NumParams2 && FD2->parameters().back()->isParameterPack(); 5488bdd1243dSDimitry Andric if (Variadic1 != Variadic2) { 5489bdd1243dSDimitry Andric if (Variadic1 && NumParams1 > NumParams2) 5490bdd1243dSDimitry Andric return FT2; 5491bdd1243dSDimitry Andric if (Variadic2 && NumParams2 > NumParams1) 5492bdd1243dSDimitry Andric return FT1; 5493bdd1243dSDimitry Andric } 5494bdd1243dSDimitry Andric 5495bdd1243dSDimitry Andric // This a speculative fix for CWG1432 (Similar to the fix for CWG1395) that 5496bdd1243dSDimitry Andric // there is no wording or even resolution for this issue. 5497bdd1243dSDimitry Andric for (int i = 0, e = std::min(NumParams1, NumParams2); i < e; ++i) { 5498bdd1243dSDimitry Andric QualType T1 = FD1->getParamDecl(i)->getType().getCanonicalType(); 5499bdd1243dSDimitry Andric QualType T2 = FD2->getParamDecl(i)->getType().getCanonicalType(); 5500bdd1243dSDimitry Andric auto *TST1 = dyn_cast<TemplateSpecializationType>(T1); 5501bdd1243dSDimitry Andric auto *TST2 = dyn_cast<TemplateSpecializationType>(T2); 5502bdd1243dSDimitry Andric if (!TST1 || !TST2) 5503bdd1243dSDimitry Andric continue; 5504bdd1243dSDimitry Andric const TemplateArgument &TA1 = TST1->template_arguments().back(); 5505bdd1243dSDimitry Andric if (TA1.getKind() == TemplateArgument::Pack) { 5506bdd1243dSDimitry Andric assert(TST1->template_arguments().size() == 5507bdd1243dSDimitry Andric TST2->template_arguments().size()); 5508bdd1243dSDimitry Andric const TemplateArgument &TA2 = TST2->template_arguments().back(); 5509bdd1243dSDimitry Andric assert(TA2.getKind() == TemplateArgument::Pack); 5510bdd1243dSDimitry Andric unsigned PackSize1 = TA1.pack_size(); 5511bdd1243dSDimitry Andric unsigned PackSize2 = TA2.pack_size(); 5512bdd1243dSDimitry Andric bool IsPackExpansion1 = 5513bdd1243dSDimitry Andric PackSize1 && TA1.pack_elements().back().isPackExpansion(); 5514bdd1243dSDimitry Andric bool IsPackExpansion2 = 5515bdd1243dSDimitry Andric PackSize2 && TA2.pack_elements().back().isPackExpansion(); 5516bdd1243dSDimitry Andric if (PackSize1 != PackSize2 && IsPackExpansion1 != IsPackExpansion2) { 5517bdd1243dSDimitry Andric if (PackSize1 > PackSize2 && IsPackExpansion1) 5518bdd1243dSDimitry Andric return FT2; 5519bdd1243dSDimitry Andric if (PackSize1 < PackSize2 && IsPackExpansion2) 5520bdd1243dSDimitry Andric return FT1; 5521bdd1243dSDimitry Andric } 5522bdd1243dSDimitry Andric } 5523bdd1243dSDimitry Andric } 5524bdd1243dSDimitry Andric 5525bdd1243dSDimitry Andric if (!Context.getLangOpts().CPlusPlus20) 5526bdd1243dSDimitry Andric return nullptr; 5527bdd1243dSDimitry Andric 5528bdd1243dSDimitry Andric // Match GCC on not implementing [temp.func.order]p6.2.1. 5529bdd1243dSDimitry Andric 5530bdd1243dSDimitry Andric // C++20 [temp.func.order]p6: 5531bdd1243dSDimitry Andric // If deduction against the other template succeeds for both transformed 5532bdd1243dSDimitry Andric // templates, constraints can be considered as follows: 5533bdd1243dSDimitry Andric 5534bdd1243dSDimitry Andric // C++20 [temp.func.order]p6.1: 5535bdd1243dSDimitry Andric // If their template-parameter-lists (possibly including template-parameters 5536bdd1243dSDimitry Andric // invented for an abbreviated function template ([dcl.fct])) or function 5537bdd1243dSDimitry Andric // parameter lists differ in length, neither template is more specialized 5538bdd1243dSDimitry Andric // than the other. 5539bdd1243dSDimitry Andric TemplateParameterList *TPL1 = FT1->getTemplateParameters(); 5540bdd1243dSDimitry Andric TemplateParameterList *TPL2 = FT2->getTemplateParameters(); 5541bdd1243dSDimitry Andric if (TPL1->size() != TPL2->size() || NumParams1 != NumParams2) 5542bdd1243dSDimitry Andric return nullptr; 5543bdd1243dSDimitry Andric 5544bdd1243dSDimitry Andric // C++20 [temp.func.order]p6.2.2: 5545bdd1243dSDimitry Andric // Otherwise, if the corresponding template-parameters of the 5546bdd1243dSDimitry Andric // template-parameter-lists are not equivalent ([temp.over.link]) or if the 5547bdd1243dSDimitry Andric // function parameters that positionally correspond between the two 5548bdd1243dSDimitry Andric // templates are not of the same type, neither template is more specialized 5549bdd1243dSDimitry Andric // than the other. 555006c3fb27SDimitry Andric if (!TemplateParameterListsAreEqual(TPL1, TPL2, false, 555106c3fb27SDimitry Andric Sema::TPL_TemplateParamsEquivalent)) 5552bdd1243dSDimitry Andric return nullptr; 5553bdd1243dSDimitry Andric 5554bdd1243dSDimitry Andric for (unsigned i = 0; i < NumParams1; ++i) 5555bdd1243dSDimitry Andric if (!Context.hasSameType(FD1->getParamDecl(i)->getType(), 5556bdd1243dSDimitry Andric FD2->getParamDecl(i)->getType())) 5557bdd1243dSDimitry Andric return nullptr; 5558bdd1243dSDimitry Andric 5559bdd1243dSDimitry Andric // C++20 [temp.func.order]p6.3: 5560bdd1243dSDimitry Andric // Otherwise, if the context in which the partial ordering is done is 5561bdd1243dSDimitry Andric // that of a call to a conversion function and the return types of the 5562bdd1243dSDimitry Andric // templates are not the same, then neither template is more specialized 5563bdd1243dSDimitry Andric // than the other. 5564bdd1243dSDimitry Andric if (TPOC == TPOC_Conversion && 5565bdd1243dSDimitry Andric !Context.hasSameType(FD1->getReturnType(), FD2->getReturnType())) 5566bdd1243dSDimitry Andric return nullptr; 5567bdd1243dSDimitry Andric 5568480093f4SDimitry Andric llvm::SmallVector<const Expr *, 3> AC1, AC2; 5569480093f4SDimitry Andric FT1->getAssociatedConstraints(AC1); 5570480093f4SDimitry Andric FT2->getAssociatedConstraints(AC2); 5571480093f4SDimitry Andric bool AtLeastAsConstrained1, AtLeastAsConstrained2; 5572480093f4SDimitry Andric if (IsAtLeastAsConstrained(FT1, AC1, FT2, AC2, AtLeastAsConstrained1)) 5573480093f4SDimitry Andric return nullptr; 5574480093f4SDimitry Andric if (IsAtLeastAsConstrained(FT2, AC2, FT1, AC1, AtLeastAsConstrained2)) 5575480093f4SDimitry Andric return nullptr; 5576480093f4SDimitry Andric if (AtLeastAsConstrained1 == AtLeastAsConstrained2) 5577480093f4SDimitry Andric return nullptr; 5578480093f4SDimitry Andric return AtLeastAsConstrained1 ? FT1 : FT2; 55790b57cec5SDimitry Andric } 55800b57cec5SDimitry Andric 55810b57cec5SDimitry Andric /// Determine if the two templates are equivalent. 55820b57cec5SDimitry Andric static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) { 55830b57cec5SDimitry Andric if (T1 == T2) 55840b57cec5SDimitry Andric return true; 55850b57cec5SDimitry Andric 55860b57cec5SDimitry Andric if (!T1 || !T2) 55870b57cec5SDimitry Andric return false; 55880b57cec5SDimitry Andric 55890b57cec5SDimitry Andric return T1->getCanonicalDecl() == T2->getCanonicalDecl(); 55900b57cec5SDimitry Andric } 55910b57cec5SDimitry Andric 55920b57cec5SDimitry Andric /// Retrieve the most specialized of the given function template 55930b57cec5SDimitry Andric /// specializations. 55940b57cec5SDimitry Andric /// 55950b57cec5SDimitry Andric /// \param SpecBegin the start iterator of the function template 55960b57cec5SDimitry Andric /// specializations that we will be comparing. 55970b57cec5SDimitry Andric /// 55980b57cec5SDimitry Andric /// \param SpecEnd the end iterator of the function template 55990b57cec5SDimitry Andric /// specializations, paired with \p SpecBegin. 56000b57cec5SDimitry Andric /// 56010b57cec5SDimitry Andric /// \param Loc the location where the ambiguity or no-specializations 56020b57cec5SDimitry Andric /// diagnostic should occur. 56030b57cec5SDimitry Andric /// 56040b57cec5SDimitry Andric /// \param NoneDiag partial diagnostic used to diagnose cases where there are 56050b57cec5SDimitry Andric /// no matching candidates. 56060b57cec5SDimitry Andric /// 56070b57cec5SDimitry Andric /// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one 56080b57cec5SDimitry Andric /// occurs. 56090b57cec5SDimitry Andric /// 56100b57cec5SDimitry Andric /// \param CandidateDiag partial diagnostic used for each function template 56110b57cec5SDimitry Andric /// specialization that is a candidate in the ambiguous ordering. One parameter 56120b57cec5SDimitry Andric /// in this diagnostic should be unbound, which will correspond to the string 56130b57cec5SDimitry Andric /// describing the template arguments for the function template specialization. 56140b57cec5SDimitry Andric /// 56150b57cec5SDimitry Andric /// \returns the most specialized function template specialization, if 56160b57cec5SDimitry Andric /// found. Otherwise, returns SpecEnd. 56170b57cec5SDimitry Andric UnresolvedSetIterator Sema::getMostSpecialized( 56180b57cec5SDimitry Andric UnresolvedSetIterator SpecBegin, UnresolvedSetIterator SpecEnd, 56190b57cec5SDimitry Andric TemplateSpecCandidateSet &FailedCandidates, 56200b57cec5SDimitry Andric SourceLocation Loc, const PartialDiagnostic &NoneDiag, 56210b57cec5SDimitry Andric const PartialDiagnostic &AmbigDiag, const PartialDiagnostic &CandidateDiag, 56220b57cec5SDimitry Andric bool Complain, QualType TargetType) { 56230b57cec5SDimitry Andric if (SpecBegin == SpecEnd) { 56240b57cec5SDimitry Andric if (Complain) { 56250b57cec5SDimitry Andric Diag(Loc, NoneDiag); 56260b57cec5SDimitry Andric FailedCandidates.NoteCandidates(*this, Loc); 56270b57cec5SDimitry Andric } 56280b57cec5SDimitry Andric return SpecEnd; 56290b57cec5SDimitry Andric } 56300b57cec5SDimitry Andric 56310b57cec5SDimitry Andric if (SpecBegin + 1 == SpecEnd) 56320b57cec5SDimitry Andric return SpecBegin; 56330b57cec5SDimitry Andric 56340b57cec5SDimitry Andric // Find the function template that is better than all of the templates it 56350b57cec5SDimitry Andric // has been compared to. 56360b57cec5SDimitry Andric UnresolvedSetIterator Best = SpecBegin; 56370b57cec5SDimitry Andric FunctionTemplateDecl *BestTemplate 56380b57cec5SDimitry Andric = cast<FunctionDecl>(*Best)->getPrimaryTemplate(); 56390b57cec5SDimitry Andric assert(BestTemplate && "Not a function template specialization?"); 56400b57cec5SDimitry Andric for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) { 56410b57cec5SDimitry Andric FunctionTemplateDecl *Challenger 56420b57cec5SDimitry Andric = cast<FunctionDecl>(*I)->getPrimaryTemplate(); 56430b57cec5SDimitry Andric assert(Challenger && "Not a function template specialization?"); 56440b57cec5SDimitry Andric if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, 56450b57cec5SDimitry Andric Loc, TPOC_Other, 0, 0), 56460b57cec5SDimitry Andric Challenger)) { 56470b57cec5SDimitry Andric Best = I; 56480b57cec5SDimitry Andric BestTemplate = Challenger; 56490b57cec5SDimitry Andric } 56500b57cec5SDimitry Andric } 56510b57cec5SDimitry Andric 56520b57cec5SDimitry Andric // Make sure that the "best" function template is more specialized than all 56530b57cec5SDimitry Andric // of the others. 56540b57cec5SDimitry Andric bool Ambiguous = false; 56550b57cec5SDimitry Andric for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { 56560b57cec5SDimitry Andric FunctionTemplateDecl *Challenger 56570b57cec5SDimitry Andric = cast<FunctionDecl>(*I)->getPrimaryTemplate(); 56580b57cec5SDimitry Andric if (I != Best && 56590b57cec5SDimitry Andric !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, 56600b57cec5SDimitry Andric Loc, TPOC_Other, 0, 0), 56610b57cec5SDimitry Andric BestTemplate)) { 56620b57cec5SDimitry Andric Ambiguous = true; 56630b57cec5SDimitry Andric break; 56640b57cec5SDimitry Andric } 56650b57cec5SDimitry Andric } 56660b57cec5SDimitry Andric 56670b57cec5SDimitry Andric if (!Ambiguous) { 56680b57cec5SDimitry Andric // We found an answer. Return it. 56690b57cec5SDimitry Andric return Best; 56700b57cec5SDimitry Andric } 56710b57cec5SDimitry Andric 56720b57cec5SDimitry Andric // Diagnose the ambiguity. 56730b57cec5SDimitry Andric if (Complain) { 56740b57cec5SDimitry Andric Diag(Loc, AmbigDiag); 56750b57cec5SDimitry Andric 56760b57cec5SDimitry Andric // FIXME: Can we order the candidates in some sane way? 56770b57cec5SDimitry Andric for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { 56780b57cec5SDimitry Andric PartialDiagnostic PD = CandidateDiag; 56790b57cec5SDimitry Andric const auto *FD = cast<FunctionDecl>(*I); 56800b57cec5SDimitry Andric PD << FD << getTemplateArgumentBindingsText( 56810b57cec5SDimitry Andric FD->getPrimaryTemplate()->getTemplateParameters(), 56820b57cec5SDimitry Andric *FD->getTemplateSpecializationArgs()); 56830b57cec5SDimitry Andric if (!TargetType.isNull()) 56840b57cec5SDimitry Andric HandleFunctionTypeMismatch(PD, FD->getType(), TargetType); 56850b57cec5SDimitry Andric Diag((*I)->getLocation(), PD); 56860b57cec5SDimitry Andric } 56870b57cec5SDimitry Andric } 56880b57cec5SDimitry Andric 56890b57cec5SDimitry Andric return SpecEnd; 56900b57cec5SDimitry Andric } 56910b57cec5SDimitry Andric 56920b57cec5SDimitry Andric /// Determine whether one partial specialization, P1, is at least as 56930b57cec5SDimitry Andric /// specialized than another, P2. 56940b57cec5SDimitry Andric /// 56950b57cec5SDimitry Andric /// \tparam TemplateLikeDecl The kind of P2, which must be a 56960b57cec5SDimitry Andric /// TemplateDecl or {Class,Var}TemplatePartialSpecializationDecl. 56970b57cec5SDimitry Andric /// \param T1 The injected-class-name of P1 (faked for a variable template). 56980b57cec5SDimitry Andric /// \param T2 The injected-class-name of P2 (faked for a variable template). 56990b57cec5SDimitry Andric template<typename TemplateLikeDecl> 57000b57cec5SDimitry Andric static bool isAtLeastAsSpecializedAs(Sema &S, QualType T1, QualType T2, 57010b57cec5SDimitry Andric TemplateLikeDecl *P2, 57020b57cec5SDimitry Andric TemplateDeductionInfo &Info) { 57030b57cec5SDimitry Andric // C++ [temp.class.order]p1: 57040b57cec5SDimitry Andric // For two class template partial specializations, the first is at least as 57050b57cec5SDimitry Andric // specialized as the second if, given the following rewrite to two 57060b57cec5SDimitry Andric // function templates, the first function template is at least as 57070b57cec5SDimitry Andric // specialized as the second according to the ordering rules for function 57080b57cec5SDimitry Andric // templates (14.6.6.2): 57090b57cec5SDimitry Andric // - the first function template has the same template parameters as the 57100b57cec5SDimitry Andric // first partial specialization and has a single function parameter 57110b57cec5SDimitry Andric // whose type is a class template specialization with the template 57120b57cec5SDimitry Andric // arguments of the first partial specialization, and 57130b57cec5SDimitry Andric // - the second function template has the same template parameters as the 57140b57cec5SDimitry Andric // second partial specialization and has a single function parameter 57150b57cec5SDimitry Andric // whose type is a class template specialization with the template 57160b57cec5SDimitry Andric // arguments of the second partial specialization. 57170b57cec5SDimitry Andric // 57180b57cec5SDimitry Andric // Rather than synthesize function templates, we merely perform the 57190b57cec5SDimitry Andric // equivalent partial ordering by performing deduction directly on 57200b57cec5SDimitry Andric // the template arguments of the class template partial 57210b57cec5SDimitry Andric // specializations. This computation is slightly simpler than the 57220b57cec5SDimitry Andric // general problem of function template partial ordering, because 57230b57cec5SDimitry Andric // class template partial specializations are more constrained. We 57240b57cec5SDimitry Andric // know that every template parameter is deducible from the class 57250b57cec5SDimitry Andric // template partial specialization's template arguments, for 57260b57cec5SDimitry Andric // example. 57270b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> Deduced; 57280b57cec5SDimitry Andric 57290b57cec5SDimitry Andric // Determine whether P1 is at least as specialized as P2. 57300b57cec5SDimitry Andric Deduced.resize(P2->getTemplateParameters()->size()); 57310b57cec5SDimitry Andric if (DeduceTemplateArgumentsByTypeMatch(S, P2->getTemplateParameters(), 57320b57cec5SDimitry Andric T2, T1, Info, Deduced, TDF_None, 57330b57cec5SDimitry Andric /*PartialOrdering=*/true)) 57340b57cec5SDimitry Andric return false; 57350b57cec5SDimitry Andric 57360b57cec5SDimitry Andric SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), 57370b57cec5SDimitry Andric Deduced.end()); 57380b57cec5SDimitry Andric Sema::InstantiatingTemplate Inst(S, Info.getLocation(), P2, DeducedArgs, 57390b57cec5SDimitry Andric Info); 5740fe6060f1SDimitry Andric if (Inst.isInvalid()) 5741fe6060f1SDimitry Andric return false; 5742fe6060f1SDimitry Andric 5743bdd1243dSDimitry Andric const auto *TST1 = cast<TemplateSpecializationType>(T1); 57445ffd83dbSDimitry Andric bool AtLeastAsSpecialized; 57455ffd83dbSDimitry Andric S.runWithSufficientStackSpace(Info.getLocation(), [&] { 57465ffd83dbSDimitry Andric AtLeastAsSpecialized = !FinishTemplateArgumentDeduction( 57470b57cec5SDimitry Andric S, P2, /*IsPartialOrdering=*/true, 57480b57cec5SDimitry Andric TemplateArgumentList(TemplateArgumentList::OnStack, 57490b57cec5SDimitry Andric TST1->template_arguments()), 57505ffd83dbSDimitry Andric Deduced, Info); 57515ffd83dbSDimitry Andric }); 57525ffd83dbSDimitry Andric return AtLeastAsSpecialized; 57530b57cec5SDimitry Andric } 57540b57cec5SDimitry Andric 5755bdd1243dSDimitry Andric namespace { 5756bdd1243dSDimitry Andric // A dummy class to return nullptr instead of P2 when performing "more 5757bdd1243dSDimitry Andric // specialized than primary" check. 5758bdd1243dSDimitry Andric struct GetP2 { 5759bdd1243dSDimitry Andric template <typename T1, typename T2, 5760bdd1243dSDimitry Andric std::enable_if_t<std::is_same_v<T1, T2>, bool> = true> 5761bdd1243dSDimitry Andric T2 *operator()(T1 *, T2 *P2) { 5762bdd1243dSDimitry Andric return P2; 5763bdd1243dSDimitry Andric } 5764bdd1243dSDimitry Andric template <typename T1, typename T2, 5765bdd1243dSDimitry Andric std::enable_if_t<!std::is_same_v<T1, T2>, bool> = true> 5766bdd1243dSDimitry Andric T1 *operator()(T1 *, T2 *) { 5767bdd1243dSDimitry Andric return nullptr; 5768bdd1243dSDimitry Andric } 5769bdd1243dSDimitry Andric }; 5770bdd1243dSDimitry Andric 5771bdd1243dSDimitry Andric // The assumption is that two template argument lists have the same size. 5772bdd1243dSDimitry Andric struct TemplateArgumentListAreEqual { 5773bdd1243dSDimitry Andric ASTContext &Ctx; 5774bdd1243dSDimitry Andric TemplateArgumentListAreEqual(ASTContext &Ctx) : Ctx(Ctx) {} 5775bdd1243dSDimitry Andric 5776bdd1243dSDimitry Andric template <typename T1, typename T2, 5777bdd1243dSDimitry Andric std::enable_if_t<std::is_same_v<T1, T2>, bool> = true> 5778bdd1243dSDimitry Andric bool operator()(T1 *PS1, T2 *PS2) { 5779bdd1243dSDimitry Andric ArrayRef<TemplateArgument> Args1 = PS1->getTemplateArgs().asArray(), 5780bdd1243dSDimitry Andric Args2 = PS2->getTemplateArgs().asArray(); 5781bdd1243dSDimitry Andric 5782bdd1243dSDimitry Andric for (unsigned I = 0, E = Args1.size(); I < E; ++I) { 5783bdd1243dSDimitry Andric // We use profile, instead of structural comparison of the arguments, 5784bdd1243dSDimitry Andric // because canonicalization can't do the right thing for dependent 5785bdd1243dSDimitry Andric // expressions. 5786bdd1243dSDimitry Andric llvm::FoldingSetNodeID IDA, IDB; 5787bdd1243dSDimitry Andric Args1[I].Profile(IDA, Ctx); 5788bdd1243dSDimitry Andric Args2[I].Profile(IDB, Ctx); 5789bdd1243dSDimitry Andric if (IDA != IDB) 5790bdd1243dSDimitry Andric return false; 5791bdd1243dSDimitry Andric } 5792bdd1243dSDimitry Andric return true; 5793bdd1243dSDimitry Andric } 5794bdd1243dSDimitry Andric 5795bdd1243dSDimitry Andric template <typename T1, typename T2, 5796bdd1243dSDimitry Andric std::enable_if_t<!std::is_same_v<T1, T2>, bool> = true> 5797bdd1243dSDimitry Andric bool operator()(T1 *Spec, T2 *Primary) { 5798bdd1243dSDimitry Andric ArrayRef<TemplateArgument> Args1 = Spec->getTemplateArgs().asArray(), 5799bdd1243dSDimitry Andric Args2 = Primary->getInjectedTemplateArgs(); 5800bdd1243dSDimitry Andric 5801bdd1243dSDimitry Andric for (unsigned I = 0, E = Args1.size(); I < E; ++I) { 5802bdd1243dSDimitry Andric // We use profile, instead of structural comparison of the arguments, 5803bdd1243dSDimitry Andric // because canonicalization can't do the right thing for dependent 5804bdd1243dSDimitry Andric // expressions. 5805bdd1243dSDimitry Andric llvm::FoldingSetNodeID IDA, IDB; 5806bdd1243dSDimitry Andric Args1[I].Profile(IDA, Ctx); 5807bdd1243dSDimitry Andric // Unlike the specialization arguments, the injected arguments are not 5808bdd1243dSDimitry Andric // always canonical. 5809bdd1243dSDimitry Andric Ctx.getCanonicalTemplateArgument(Args2[I]).Profile(IDB, Ctx); 5810bdd1243dSDimitry Andric if (IDA != IDB) 5811bdd1243dSDimitry Andric return false; 5812bdd1243dSDimitry Andric } 5813bdd1243dSDimitry Andric return true; 5814bdd1243dSDimitry Andric } 5815bdd1243dSDimitry Andric }; 5816bdd1243dSDimitry Andric } // namespace 5817bdd1243dSDimitry Andric 5818bdd1243dSDimitry Andric /// Returns the more specialized template specialization between T1/P1 and 5819bdd1243dSDimitry Andric /// T2/P2. 5820bdd1243dSDimitry Andric /// - If IsMoreSpecialThanPrimaryCheck is true, T1/P1 is the partial 5821bdd1243dSDimitry Andric /// specialization and T2/P2 is the primary template. 5822bdd1243dSDimitry Andric /// - otherwise, both T1/P1 and T2/P2 are the partial specialization. 5823bdd1243dSDimitry Andric /// 5824bdd1243dSDimitry Andric /// \param T1 the type of the first template partial specialization 5825bdd1243dSDimitry Andric /// 5826bdd1243dSDimitry Andric /// \param T2 if IsMoreSpecialThanPrimaryCheck is true, the type of the second 5827bdd1243dSDimitry Andric /// template partial specialization; otherwise, the type of the 5828bdd1243dSDimitry Andric /// primary template. 5829bdd1243dSDimitry Andric /// 5830bdd1243dSDimitry Andric /// \param P1 the first template partial specialization 5831bdd1243dSDimitry Andric /// 5832bdd1243dSDimitry Andric /// \param P2 if IsMoreSpecialThanPrimaryCheck is true, the second template 5833bdd1243dSDimitry Andric /// partial specialization; otherwise, the primary template. 5834bdd1243dSDimitry Andric /// 5835bdd1243dSDimitry Andric /// \returns - If IsMoreSpecialThanPrimaryCheck is true, returns P1 if P1 is 5836bdd1243dSDimitry Andric /// more specialized, returns nullptr if P1 is not more specialized. 5837bdd1243dSDimitry Andric /// - otherwise, returns the more specialized template partial 5838bdd1243dSDimitry Andric /// specialization. If neither partial specialization is more 5839bdd1243dSDimitry Andric /// specialized, returns NULL. 5840bdd1243dSDimitry Andric template <typename TemplateLikeDecl, typename PrimaryDel> 5841bdd1243dSDimitry Andric static TemplateLikeDecl * 5842bdd1243dSDimitry Andric getMoreSpecialized(Sema &S, QualType T1, QualType T2, TemplateLikeDecl *P1, 5843bdd1243dSDimitry Andric PrimaryDel *P2, TemplateDeductionInfo &Info) { 5844bdd1243dSDimitry Andric constexpr bool IsMoreSpecialThanPrimaryCheck = 5845bdd1243dSDimitry Andric !std::is_same_v<TemplateLikeDecl, PrimaryDel>; 5846bdd1243dSDimitry Andric 5847bdd1243dSDimitry Andric bool Better1 = isAtLeastAsSpecializedAs(S, T1, T2, P2, Info); 5848bdd1243dSDimitry Andric if (IsMoreSpecialThanPrimaryCheck && !Better1) 5849bdd1243dSDimitry Andric return nullptr; 5850bdd1243dSDimitry Andric 5851bdd1243dSDimitry Andric bool Better2 = isAtLeastAsSpecializedAs(S, T2, T1, P1, Info); 5852bdd1243dSDimitry Andric if (IsMoreSpecialThanPrimaryCheck && !Better2) 5853bdd1243dSDimitry Andric return P1; 5854bdd1243dSDimitry Andric 5855bdd1243dSDimitry Andric // C++ [temp.deduct.partial]p10: 5856bdd1243dSDimitry Andric // F is more specialized than G if F is at least as specialized as G and G 5857bdd1243dSDimitry Andric // is not at least as specialized as F. 5858bdd1243dSDimitry Andric if (Better1 != Better2) // We have a clear winner 5859bdd1243dSDimitry Andric return Better1 ? P1 : GetP2()(P1, P2); 5860bdd1243dSDimitry Andric 5861bdd1243dSDimitry Andric if (!Better1 && !Better2) 5862bdd1243dSDimitry Andric return nullptr; 5863bdd1243dSDimitry Andric 5864bdd1243dSDimitry Andric // This a speculative fix for CWG1432 (Similar to the fix for CWG1395) that 5865bdd1243dSDimitry Andric // there is no wording or even resolution for this issue. 5866bdd1243dSDimitry Andric auto *TST1 = cast<TemplateSpecializationType>(T1); 5867bdd1243dSDimitry Andric auto *TST2 = cast<TemplateSpecializationType>(T2); 5868bdd1243dSDimitry Andric const TemplateArgument &TA1 = TST1->template_arguments().back(); 5869bdd1243dSDimitry Andric if (TA1.getKind() == TemplateArgument::Pack) { 5870bdd1243dSDimitry Andric assert(TST1->template_arguments().size() == 5871bdd1243dSDimitry Andric TST2->template_arguments().size()); 5872bdd1243dSDimitry Andric const TemplateArgument &TA2 = TST2->template_arguments().back(); 5873bdd1243dSDimitry Andric assert(TA2.getKind() == TemplateArgument::Pack); 5874bdd1243dSDimitry Andric unsigned PackSize1 = TA1.pack_size(); 5875bdd1243dSDimitry Andric unsigned PackSize2 = TA2.pack_size(); 5876bdd1243dSDimitry Andric bool IsPackExpansion1 = 5877bdd1243dSDimitry Andric PackSize1 && TA1.pack_elements().back().isPackExpansion(); 5878bdd1243dSDimitry Andric bool IsPackExpansion2 = 5879bdd1243dSDimitry Andric PackSize2 && TA2.pack_elements().back().isPackExpansion(); 5880bdd1243dSDimitry Andric if (PackSize1 != PackSize2 && IsPackExpansion1 != IsPackExpansion2) { 5881bdd1243dSDimitry Andric if (PackSize1 > PackSize2 && IsPackExpansion1) 5882bdd1243dSDimitry Andric return GetP2()(P1, P2); 5883bdd1243dSDimitry Andric if (PackSize1 < PackSize2 && IsPackExpansion2) 5884bdd1243dSDimitry Andric return P1; 5885bdd1243dSDimitry Andric } 5886bdd1243dSDimitry Andric } 5887bdd1243dSDimitry Andric 5888bdd1243dSDimitry Andric if (!S.Context.getLangOpts().CPlusPlus20) 5889bdd1243dSDimitry Andric return nullptr; 5890bdd1243dSDimitry Andric 5891bdd1243dSDimitry Andric // Match GCC on not implementing [temp.func.order]p6.2.1. 5892bdd1243dSDimitry Andric 5893bdd1243dSDimitry Andric // C++20 [temp.func.order]p6: 5894bdd1243dSDimitry Andric // If deduction against the other template succeeds for both transformed 5895bdd1243dSDimitry Andric // templates, constraints can be considered as follows: 5896bdd1243dSDimitry Andric 5897bdd1243dSDimitry Andric TemplateParameterList *TPL1 = P1->getTemplateParameters(); 5898bdd1243dSDimitry Andric TemplateParameterList *TPL2 = P2->getTemplateParameters(); 5899bdd1243dSDimitry Andric if (TPL1->size() != TPL2->size()) 5900bdd1243dSDimitry Andric return nullptr; 5901bdd1243dSDimitry Andric 5902bdd1243dSDimitry Andric // C++20 [temp.func.order]p6.2.2: 5903bdd1243dSDimitry Andric // Otherwise, if the corresponding template-parameters of the 5904bdd1243dSDimitry Andric // template-parameter-lists are not equivalent ([temp.over.link]) or if the 5905bdd1243dSDimitry Andric // function parameters that positionally correspond between the two 5906bdd1243dSDimitry Andric // templates are not of the same type, neither template is more specialized 5907bdd1243dSDimitry Andric // than the other. 590806c3fb27SDimitry Andric if (!S.TemplateParameterListsAreEqual(TPL1, TPL2, false, 590906c3fb27SDimitry Andric Sema::TPL_TemplateParamsEquivalent)) 5910bdd1243dSDimitry Andric return nullptr; 5911bdd1243dSDimitry Andric 5912bdd1243dSDimitry Andric if (!TemplateArgumentListAreEqual(S.getASTContext())(P1, P2)) 5913bdd1243dSDimitry Andric return nullptr; 5914bdd1243dSDimitry Andric 5915bdd1243dSDimitry Andric llvm::SmallVector<const Expr *, 3> AC1, AC2; 5916bdd1243dSDimitry Andric P1->getAssociatedConstraints(AC1); 5917bdd1243dSDimitry Andric P2->getAssociatedConstraints(AC2); 5918bdd1243dSDimitry Andric bool AtLeastAsConstrained1, AtLeastAsConstrained2; 5919bdd1243dSDimitry Andric if (S.IsAtLeastAsConstrained(P1, AC1, P2, AC2, AtLeastAsConstrained1) || 5920bdd1243dSDimitry Andric (IsMoreSpecialThanPrimaryCheck && !AtLeastAsConstrained1)) 5921bdd1243dSDimitry Andric return nullptr; 5922bdd1243dSDimitry Andric if (S.IsAtLeastAsConstrained(P2, AC2, P1, AC1, AtLeastAsConstrained2)) 5923bdd1243dSDimitry Andric return nullptr; 5924bdd1243dSDimitry Andric if (AtLeastAsConstrained1 == AtLeastAsConstrained2) 5925bdd1243dSDimitry Andric return nullptr; 5926bdd1243dSDimitry Andric return AtLeastAsConstrained1 ? P1 : GetP2()(P1, P2); 5927bdd1243dSDimitry Andric } 5928bdd1243dSDimitry Andric 59290b57cec5SDimitry Andric /// Returns the more specialized class template partial specialization 59300b57cec5SDimitry Andric /// according to the rules of partial ordering of class template partial 59310b57cec5SDimitry Andric /// specializations (C++ [temp.class.order]). 59320b57cec5SDimitry Andric /// 59330b57cec5SDimitry Andric /// \param PS1 the first class template partial specialization 59340b57cec5SDimitry Andric /// 59350b57cec5SDimitry Andric /// \param PS2 the second class template partial specialization 59360b57cec5SDimitry Andric /// 59370b57cec5SDimitry Andric /// \returns the more specialized class template partial specialization. If 59380b57cec5SDimitry Andric /// neither partial specialization is more specialized, returns NULL. 59390b57cec5SDimitry Andric ClassTemplatePartialSpecializationDecl * 59400b57cec5SDimitry Andric Sema::getMoreSpecializedPartialSpecialization( 59410b57cec5SDimitry Andric ClassTemplatePartialSpecializationDecl *PS1, 59420b57cec5SDimitry Andric ClassTemplatePartialSpecializationDecl *PS2, 59430b57cec5SDimitry Andric SourceLocation Loc) { 59440b57cec5SDimitry Andric QualType PT1 = PS1->getInjectedSpecializationType(); 59450b57cec5SDimitry Andric QualType PT2 = PS2->getInjectedSpecializationType(); 59460b57cec5SDimitry Andric 59470b57cec5SDimitry Andric TemplateDeductionInfo Info(Loc); 5948bdd1243dSDimitry Andric return getMoreSpecialized(*this, PT1, PT2, PS1, PS2, Info); 59490b57cec5SDimitry Andric } 59500b57cec5SDimitry Andric 59510b57cec5SDimitry Andric bool Sema::isMoreSpecializedThanPrimary( 59520b57cec5SDimitry Andric ClassTemplatePartialSpecializationDecl *Spec, TemplateDeductionInfo &Info) { 59530b57cec5SDimitry Andric ClassTemplateDecl *Primary = Spec->getSpecializedTemplate(); 59540b57cec5SDimitry Andric QualType PrimaryT = Primary->getInjectedClassNameSpecialization(); 59550b57cec5SDimitry Andric QualType PartialT = Spec->getInjectedSpecializationType(); 5956bdd1243dSDimitry Andric 5957bdd1243dSDimitry Andric ClassTemplatePartialSpecializationDecl *MaybeSpec = 5958bdd1243dSDimitry Andric getMoreSpecialized(*this, PartialT, PrimaryT, Spec, Primary, Info); 5959bdd1243dSDimitry Andric if (MaybeSpec) 5960480093f4SDimitry Andric Info.clearSFINAEDiagnostic(); 5961bdd1243dSDimitry Andric return MaybeSpec; 59620b57cec5SDimitry Andric } 59630b57cec5SDimitry Andric 59640b57cec5SDimitry Andric VarTemplatePartialSpecializationDecl * 59650b57cec5SDimitry Andric Sema::getMoreSpecializedPartialSpecialization( 59660b57cec5SDimitry Andric VarTemplatePartialSpecializationDecl *PS1, 59670b57cec5SDimitry Andric VarTemplatePartialSpecializationDecl *PS2, SourceLocation Loc) { 59680b57cec5SDimitry Andric // Pretend the variable template specializations are class template 59690b57cec5SDimitry Andric // specializations and form a fake injected class name type for comparison. 59700b57cec5SDimitry Andric assert(PS1->getSpecializedTemplate() == PS2->getSpecializedTemplate() && 59710b57cec5SDimitry Andric "the partial specializations being compared should specialize" 59720b57cec5SDimitry Andric " the same template."); 59730b57cec5SDimitry Andric TemplateName Name(PS1->getSpecializedTemplate()); 59740b57cec5SDimitry Andric TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); 59750b57cec5SDimitry Andric QualType PT1 = Context.getTemplateSpecializationType( 59760b57cec5SDimitry Andric CanonTemplate, PS1->getTemplateArgs().asArray()); 59770b57cec5SDimitry Andric QualType PT2 = Context.getTemplateSpecializationType( 59780b57cec5SDimitry Andric CanonTemplate, PS2->getTemplateArgs().asArray()); 59790b57cec5SDimitry Andric 59800b57cec5SDimitry Andric TemplateDeductionInfo Info(Loc); 5981bdd1243dSDimitry Andric return getMoreSpecialized(*this, PT1, PT2, PS1, PS2, Info); 59820b57cec5SDimitry Andric } 59830b57cec5SDimitry Andric 59840b57cec5SDimitry Andric bool Sema::isMoreSpecializedThanPrimary( 59850b57cec5SDimitry Andric VarTemplatePartialSpecializationDecl *Spec, TemplateDeductionInfo &Info) { 5986bdd1243dSDimitry Andric VarTemplateDecl *Primary = Spec->getSpecializedTemplate(); 59870b57cec5SDimitry Andric TemplateName CanonTemplate = 59880b57cec5SDimitry Andric Context.getCanonicalTemplateName(TemplateName(Primary)); 59890b57cec5SDimitry Andric QualType PrimaryT = Context.getTemplateSpecializationType( 5990bdd1243dSDimitry Andric CanonTemplate, Primary->getInjectedTemplateArgs()); 59910b57cec5SDimitry Andric QualType PartialT = Context.getTemplateSpecializationType( 59920b57cec5SDimitry Andric CanonTemplate, Spec->getTemplateArgs().asArray()); 5993480093f4SDimitry Andric 5994bdd1243dSDimitry Andric VarTemplatePartialSpecializationDecl *MaybeSpec = 5995bdd1243dSDimitry Andric getMoreSpecialized(*this, PartialT, PrimaryT, Spec, Primary, Info); 5996bdd1243dSDimitry Andric if (MaybeSpec) 5997480093f4SDimitry Andric Info.clearSFINAEDiagnostic(); 5998bdd1243dSDimitry Andric return MaybeSpec; 59990b57cec5SDimitry Andric } 60000b57cec5SDimitry Andric 60010b57cec5SDimitry Andric bool Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs( 60020b57cec5SDimitry Andric TemplateParameterList *P, TemplateDecl *AArg, SourceLocation Loc) { 60030b57cec5SDimitry Andric // C++1z [temp.arg.template]p4: (DR 150) 60040b57cec5SDimitry Andric // A template template-parameter P is at least as specialized as a 60050b57cec5SDimitry Andric // template template-argument A if, given the following rewrite to two 60060b57cec5SDimitry Andric // function templates... 60070b57cec5SDimitry Andric 60080b57cec5SDimitry Andric // Rather than synthesize function templates, we merely perform the 60090b57cec5SDimitry Andric // equivalent partial ordering by performing deduction directly on 60100b57cec5SDimitry Andric // the template parameter lists of the template template parameters. 60110b57cec5SDimitry Andric // 60120b57cec5SDimitry Andric // Given an invented class template X with the template parameter list of 60130b57cec5SDimitry Andric // A (including default arguments): 60140b57cec5SDimitry Andric TemplateName X = Context.getCanonicalTemplateName(TemplateName(AArg)); 60150b57cec5SDimitry Andric TemplateParameterList *A = AArg->getTemplateParameters(); 60160b57cec5SDimitry Andric 60170b57cec5SDimitry Andric // - Each function template has a single function parameter whose type is 60180b57cec5SDimitry Andric // a specialization of X with template arguments corresponding to the 60190b57cec5SDimitry Andric // template parameters from the respective function template 60200b57cec5SDimitry Andric SmallVector<TemplateArgument, 8> AArgs; 60210b57cec5SDimitry Andric Context.getInjectedTemplateArgs(A, AArgs); 60220b57cec5SDimitry Andric 60230b57cec5SDimitry Andric // Check P's arguments against A's parameter list. This will fill in default 60240b57cec5SDimitry Andric // template arguments as needed. AArgs are already correct by construction. 60250b57cec5SDimitry Andric // We can't just use CheckTemplateIdType because that will expand alias 60260b57cec5SDimitry Andric // templates. 60270b57cec5SDimitry Andric SmallVector<TemplateArgument, 4> PArgs; 60280b57cec5SDimitry Andric { 60290b57cec5SDimitry Andric SFINAETrap Trap(*this); 60300b57cec5SDimitry Andric 60310b57cec5SDimitry Andric Context.getInjectedTemplateArgs(P, PArgs); 6032480093f4SDimitry Andric TemplateArgumentListInfo PArgList(P->getLAngleLoc(), 6033480093f4SDimitry Andric P->getRAngleLoc()); 60340b57cec5SDimitry Andric for (unsigned I = 0, N = P->size(); I != N; ++I) { 60350b57cec5SDimitry Andric // Unwrap packs that getInjectedTemplateArgs wrapped around pack 60360b57cec5SDimitry Andric // expansions, to form an "as written" argument list. 60370b57cec5SDimitry Andric TemplateArgument Arg = PArgs[I]; 60380b57cec5SDimitry Andric if (Arg.getKind() == TemplateArgument::Pack) { 60390b57cec5SDimitry Andric assert(Arg.pack_size() == 1 && Arg.pack_begin()->isPackExpansion()); 60400b57cec5SDimitry Andric Arg = *Arg.pack_begin(); 60410b57cec5SDimitry Andric } 60420b57cec5SDimitry Andric PArgList.addArgument(getTrivialTemplateArgumentLoc( 60430b57cec5SDimitry Andric Arg, QualType(), P->getParam(I)->getLocation())); 60440b57cec5SDimitry Andric } 60450b57cec5SDimitry Andric PArgs.clear(); 60460b57cec5SDimitry Andric 60470b57cec5SDimitry Andric // C++1z [temp.arg.template]p3: 60480b57cec5SDimitry Andric // If the rewrite produces an invalid type, then P is not at least as 60490b57cec5SDimitry Andric // specialized as A. 6050bdd1243dSDimitry Andric SmallVector<TemplateArgument, 4> SugaredPArgs; 6051bdd1243dSDimitry Andric if (CheckTemplateArgumentList(AArg, Loc, PArgList, false, SugaredPArgs, 6052bdd1243dSDimitry Andric PArgs) || 60530b57cec5SDimitry Andric Trap.hasErrorOccurred()) 60540b57cec5SDimitry Andric return false; 60550b57cec5SDimitry Andric } 60560b57cec5SDimitry Andric 6057bdd1243dSDimitry Andric QualType AType = Context.getCanonicalTemplateSpecializationType(X, AArgs); 6058bdd1243dSDimitry Andric QualType PType = Context.getCanonicalTemplateSpecializationType(X, PArgs); 60590b57cec5SDimitry Andric 60600b57cec5SDimitry Andric // ... the function template corresponding to P is at least as specialized 60610b57cec5SDimitry Andric // as the function template corresponding to A according to the partial 60620b57cec5SDimitry Andric // ordering rules for function templates. 60630b57cec5SDimitry Andric TemplateDeductionInfo Info(Loc, A->getDepth()); 60640b57cec5SDimitry Andric return isAtLeastAsSpecializedAs(*this, PType, AType, AArg, Info); 60650b57cec5SDimitry Andric } 60660b57cec5SDimitry Andric 6067480093f4SDimitry Andric namespace { 6068480093f4SDimitry Andric struct MarkUsedTemplateParameterVisitor : 6069480093f4SDimitry Andric RecursiveASTVisitor<MarkUsedTemplateParameterVisitor> { 6070480093f4SDimitry Andric llvm::SmallBitVector &Used; 6071480093f4SDimitry Andric unsigned Depth; 6072480093f4SDimitry Andric 6073480093f4SDimitry Andric MarkUsedTemplateParameterVisitor(llvm::SmallBitVector &Used, 6074480093f4SDimitry Andric unsigned Depth) 6075480093f4SDimitry Andric : Used(Used), Depth(Depth) { } 6076480093f4SDimitry Andric 6077480093f4SDimitry Andric bool VisitTemplateTypeParmType(TemplateTypeParmType *T) { 6078480093f4SDimitry Andric if (T->getDepth() == Depth) 6079480093f4SDimitry Andric Used[T->getIndex()] = true; 6080480093f4SDimitry Andric return true; 6081480093f4SDimitry Andric } 6082480093f4SDimitry Andric 6083480093f4SDimitry Andric bool TraverseTemplateName(TemplateName Template) { 6084bdd1243dSDimitry Andric if (auto *TTP = llvm::dyn_cast_or_null<TemplateTemplateParmDecl>( 6085bdd1243dSDimitry Andric Template.getAsTemplateDecl())) 6086480093f4SDimitry Andric if (TTP->getDepth() == Depth) 6087480093f4SDimitry Andric Used[TTP->getIndex()] = true; 6088480093f4SDimitry Andric RecursiveASTVisitor<MarkUsedTemplateParameterVisitor>:: 6089480093f4SDimitry Andric TraverseTemplateName(Template); 6090480093f4SDimitry Andric return true; 6091480093f4SDimitry Andric } 6092480093f4SDimitry Andric 6093480093f4SDimitry Andric bool VisitDeclRefExpr(DeclRefExpr *E) { 6094480093f4SDimitry Andric if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(E->getDecl())) 6095480093f4SDimitry Andric if (NTTP->getDepth() == Depth) 6096480093f4SDimitry Andric Used[NTTP->getIndex()] = true; 6097480093f4SDimitry Andric return true; 6098480093f4SDimitry Andric } 6099480093f4SDimitry Andric }; 6100480093f4SDimitry Andric } 6101480093f4SDimitry Andric 61020b57cec5SDimitry Andric /// Mark the template parameters that are used by the given 61030b57cec5SDimitry Andric /// expression. 61040b57cec5SDimitry Andric static void 61050b57cec5SDimitry Andric MarkUsedTemplateParameters(ASTContext &Ctx, 61060b57cec5SDimitry Andric const Expr *E, 61070b57cec5SDimitry Andric bool OnlyDeduced, 61080b57cec5SDimitry Andric unsigned Depth, 61090b57cec5SDimitry Andric llvm::SmallBitVector &Used) { 6110480093f4SDimitry Andric if (!OnlyDeduced) { 6111480093f4SDimitry Andric MarkUsedTemplateParameterVisitor(Used, Depth) 6112480093f4SDimitry Andric .TraverseStmt(const_cast<Expr *>(E)); 6113480093f4SDimitry Andric return; 6114480093f4SDimitry Andric } 6115480093f4SDimitry Andric 61160b57cec5SDimitry Andric // We can deduce from a pack expansion. 61170b57cec5SDimitry Andric if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E)) 61180b57cec5SDimitry Andric E = Expansion->getPattern(); 61190b57cec5SDimitry Andric 6120e8d8bef9SDimitry Andric const NonTypeTemplateParmDecl *NTTP = getDeducedParameterFromExpr(E, Depth); 61210b57cec5SDimitry Andric if (!NTTP) 61220b57cec5SDimitry Andric return; 61230b57cec5SDimitry Andric 61240b57cec5SDimitry Andric if (NTTP->getDepth() == Depth) 61250b57cec5SDimitry Andric Used[NTTP->getIndex()] = true; 61260b57cec5SDimitry Andric 61270b57cec5SDimitry Andric // In C++17 mode, additional arguments may be deduced from the type of a 61280b57cec5SDimitry Andric // non-type argument. 61290b57cec5SDimitry Andric if (Ctx.getLangOpts().CPlusPlus17) 61300b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, NTTP->getType(), OnlyDeduced, Depth, Used); 61310b57cec5SDimitry Andric } 61320b57cec5SDimitry Andric 61330b57cec5SDimitry Andric /// Mark the template parameters that are used by the given 61340b57cec5SDimitry Andric /// nested name specifier. 61350b57cec5SDimitry Andric static void 61360b57cec5SDimitry Andric MarkUsedTemplateParameters(ASTContext &Ctx, 61370b57cec5SDimitry Andric NestedNameSpecifier *NNS, 61380b57cec5SDimitry Andric bool OnlyDeduced, 61390b57cec5SDimitry Andric unsigned Depth, 61400b57cec5SDimitry Andric llvm::SmallBitVector &Used) { 61410b57cec5SDimitry Andric if (!NNS) 61420b57cec5SDimitry Andric return; 61430b57cec5SDimitry Andric 61440b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, NNS->getPrefix(), OnlyDeduced, Depth, 61450b57cec5SDimitry Andric Used); 61460b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, QualType(NNS->getAsType(), 0), 61470b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 61480b57cec5SDimitry Andric } 61490b57cec5SDimitry Andric 61500b57cec5SDimitry Andric /// Mark the template parameters that are used by the given 61510b57cec5SDimitry Andric /// template name. 61520b57cec5SDimitry Andric static void 61530b57cec5SDimitry Andric MarkUsedTemplateParameters(ASTContext &Ctx, 61540b57cec5SDimitry Andric TemplateName Name, 61550b57cec5SDimitry Andric bool OnlyDeduced, 61560b57cec5SDimitry Andric unsigned Depth, 61570b57cec5SDimitry Andric llvm::SmallBitVector &Used) { 61580b57cec5SDimitry Andric if (TemplateDecl *Template = Name.getAsTemplateDecl()) { 61590b57cec5SDimitry Andric if (TemplateTemplateParmDecl *TTP 61600b57cec5SDimitry Andric = dyn_cast<TemplateTemplateParmDecl>(Template)) { 61610b57cec5SDimitry Andric if (TTP->getDepth() == Depth) 61620b57cec5SDimitry Andric Used[TTP->getIndex()] = true; 61630b57cec5SDimitry Andric } 61640b57cec5SDimitry Andric return; 61650b57cec5SDimitry Andric } 61660b57cec5SDimitry Andric 61670b57cec5SDimitry Andric if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) 61680b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, QTN->getQualifier(), OnlyDeduced, 61690b57cec5SDimitry Andric Depth, Used); 61700b57cec5SDimitry Andric if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) 61710b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, DTN->getQualifier(), OnlyDeduced, 61720b57cec5SDimitry Andric Depth, Used); 61730b57cec5SDimitry Andric } 61740b57cec5SDimitry Andric 61750b57cec5SDimitry Andric /// Mark the template parameters that are used by the given 61760b57cec5SDimitry Andric /// type. 61770b57cec5SDimitry Andric static void 61780b57cec5SDimitry Andric MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, 61790b57cec5SDimitry Andric bool OnlyDeduced, 61800b57cec5SDimitry Andric unsigned Depth, 61810b57cec5SDimitry Andric llvm::SmallBitVector &Used) { 61820b57cec5SDimitry Andric if (T.isNull()) 61830b57cec5SDimitry Andric return; 61840b57cec5SDimitry Andric 61850b57cec5SDimitry Andric // Non-dependent types have nothing deducible 61860b57cec5SDimitry Andric if (!T->isDependentType()) 61870b57cec5SDimitry Andric return; 61880b57cec5SDimitry Andric 61890b57cec5SDimitry Andric T = Ctx.getCanonicalType(T); 61900b57cec5SDimitry Andric switch (T->getTypeClass()) { 61910b57cec5SDimitry Andric case Type::Pointer: 61920b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 61930b57cec5SDimitry Andric cast<PointerType>(T)->getPointeeType(), 61940b57cec5SDimitry Andric OnlyDeduced, 61950b57cec5SDimitry Andric Depth, 61960b57cec5SDimitry Andric Used); 61970b57cec5SDimitry Andric break; 61980b57cec5SDimitry Andric 61990b57cec5SDimitry Andric case Type::BlockPointer: 62000b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 62010b57cec5SDimitry Andric cast<BlockPointerType>(T)->getPointeeType(), 62020b57cec5SDimitry Andric OnlyDeduced, 62030b57cec5SDimitry Andric Depth, 62040b57cec5SDimitry Andric Used); 62050b57cec5SDimitry Andric break; 62060b57cec5SDimitry Andric 62070b57cec5SDimitry Andric case Type::LValueReference: 62080b57cec5SDimitry Andric case Type::RValueReference: 62090b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 62100b57cec5SDimitry Andric cast<ReferenceType>(T)->getPointeeType(), 62110b57cec5SDimitry Andric OnlyDeduced, 62120b57cec5SDimitry Andric Depth, 62130b57cec5SDimitry Andric Used); 62140b57cec5SDimitry Andric break; 62150b57cec5SDimitry Andric 62160b57cec5SDimitry Andric case Type::MemberPointer: { 62170b57cec5SDimitry Andric const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); 62180b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, MemPtr->getPointeeType(), OnlyDeduced, 62190b57cec5SDimitry Andric Depth, Used); 62200b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, QualType(MemPtr->getClass(), 0), 62210b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 62220b57cec5SDimitry Andric break; 62230b57cec5SDimitry Andric } 62240b57cec5SDimitry Andric 62250b57cec5SDimitry Andric case Type::DependentSizedArray: 62260b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 62270b57cec5SDimitry Andric cast<DependentSizedArrayType>(T)->getSizeExpr(), 62280b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 62290b57cec5SDimitry Andric // Fall through to check the element type 6230bdd1243dSDimitry Andric [[fallthrough]]; 62310b57cec5SDimitry Andric 62320b57cec5SDimitry Andric case Type::ConstantArray: 62330b57cec5SDimitry Andric case Type::IncompleteArray: 62340b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 62350b57cec5SDimitry Andric cast<ArrayType>(T)->getElementType(), 62360b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 62370b57cec5SDimitry Andric break; 62380b57cec5SDimitry Andric 62390b57cec5SDimitry Andric case Type::Vector: 62400b57cec5SDimitry Andric case Type::ExtVector: 62410b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 62420b57cec5SDimitry Andric cast<VectorType>(T)->getElementType(), 62430b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 62440b57cec5SDimitry Andric break; 62450b57cec5SDimitry Andric 62460b57cec5SDimitry Andric case Type::DependentVector: { 62470b57cec5SDimitry Andric const auto *VecType = cast<DependentVectorType>(T); 62480b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, 62490b57cec5SDimitry Andric Depth, Used); 62500b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, Depth, 62510b57cec5SDimitry Andric Used); 62520b57cec5SDimitry Andric break; 62530b57cec5SDimitry Andric } 62540b57cec5SDimitry Andric case Type::DependentSizedExtVector: { 62550b57cec5SDimitry Andric const DependentSizedExtVectorType *VecType 62560b57cec5SDimitry Andric = cast<DependentSizedExtVectorType>(T); 62570b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, 62580b57cec5SDimitry Andric Depth, Used); 62590b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, 62600b57cec5SDimitry Andric Depth, Used); 62610b57cec5SDimitry Andric break; 62620b57cec5SDimitry Andric } 62630b57cec5SDimitry Andric 62640b57cec5SDimitry Andric case Type::DependentAddressSpace: { 62650b57cec5SDimitry Andric const DependentAddressSpaceType *DependentASType = 62660b57cec5SDimitry Andric cast<DependentAddressSpaceType>(T); 62670b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, DependentASType->getPointeeType(), 62680b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 62690b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 62700b57cec5SDimitry Andric DependentASType->getAddrSpaceExpr(), 62710b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 62720b57cec5SDimitry Andric break; 62730b57cec5SDimitry Andric } 62740b57cec5SDimitry Andric 62755ffd83dbSDimitry Andric case Type::ConstantMatrix: { 62765ffd83dbSDimitry Andric const ConstantMatrixType *MatType = cast<ConstantMatrixType>(T); 62775ffd83dbSDimitry Andric MarkUsedTemplateParameters(Ctx, MatType->getElementType(), OnlyDeduced, 62785ffd83dbSDimitry Andric Depth, Used); 62795ffd83dbSDimitry Andric break; 62805ffd83dbSDimitry Andric } 62815ffd83dbSDimitry Andric 62825ffd83dbSDimitry Andric case Type::DependentSizedMatrix: { 62835ffd83dbSDimitry Andric const DependentSizedMatrixType *MatType = cast<DependentSizedMatrixType>(T); 62845ffd83dbSDimitry Andric MarkUsedTemplateParameters(Ctx, MatType->getElementType(), OnlyDeduced, 62855ffd83dbSDimitry Andric Depth, Used); 62865ffd83dbSDimitry Andric MarkUsedTemplateParameters(Ctx, MatType->getRowExpr(), OnlyDeduced, Depth, 62875ffd83dbSDimitry Andric Used); 62885ffd83dbSDimitry Andric MarkUsedTemplateParameters(Ctx, MatType->getColumnExpr(), OnlyDeduced, 62895ffd83dbSDimitry Andric Depth, Used); 62905ffd83dbSDimitry Andric break; 62915ffd83dbSDimitry Andric } 62925ffd83dbSDimitry Andric 62930b57cec5SDimitry Andric case Type::FunctionProto: { 62940b57cec5SDimitry Andric const FunctionProtoType *Proto = cast<FunctionProtoType>(T); 62950b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, Proto->getReturnType(), OnlyDeduced, Depth, 62960b57cec5SDimitry Andric Used); 62970b57cec5SDimitry Andric for (unsigned I = 0, N = Proto->getNumParams(); I != N; ++I) { 62980b57cec5SDimitry Andric // C++17 [temp.deduct.type]p5: 62990b57cec5SDimitry Andric // The non-deduced contexts are: [...] 63000b57cec5SDimitry Andric // -- A function parameter pack that does not occur at the end of the 63010b57cec5SDimitry Andric // parameter-declaration-list. 63020b57cec5SDimitry Andric if (!OnlyDeduced || I + 1 == N || 63030b57cec5SDimitry Andric !Proto->getParamType(I)->getAs<PackExpansionType>()) { 63040b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, Proto->getParamType(I), OnlyDeduced, 63050b57cec5SDimitry Andric Depth, Used); 63060b57cec5SDimitry Andric } else { 63070b57cec5SDimitry Andric // FIXME: C++17 [temp.deduct.call]p1: 63080b57cec5SDimitry Andric // When a function parameter pack appears in a non-deduced context, 63090b57cec5SDimitry Andric // the type of that pack is never deduced. 63100b57cec5SDimitry Andric // 63110b57cec5SDimitry Andric // We should also track a set of "never deduced" parameters, and 63120b57cec5SDimitry Andric // subtract that from the list of deduced parameters after marking. 63130b57cec5SDimitry Andric } 63140b57cec5SDimitry Andric } 63150b57cec5SDimitry Andric if (auto *E = Proto->getNoexceptExpr()) 63160b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, E, OnlyDeduced, Depth, Used); 63170b57cec5SDimitry Andric break; 63180b57cec5SDimitry Andric } 63190b57cec5SDimitry Andric 63200b57cec5SDimitry Andric case Type::TemplateTypeParm: { 63210b57cec5SDimitry Andric const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T); 63220b57cec5SDimitry Andric if (TTP->getDepth() == Depth) 63230b57cec5SDimitry Andric Used[TTP->getIndex()] = true; 63240b57cec5SDimitry Andric break; 63250b57cec5SDimitry Andric } 63260b57cec5SDimitry Andric 63270b57cec5SDimitry Andric case Type::SubstTemplateTypeParmPack: { 63280b57cec5SDimitry Andric const SubstTemplateTypeParmPackType *Subst 63290b57cec5SDimitry Andric = cast<SubstTemplateTypeParmPackType>(T); 6330bdd1243dSDimitry Andric if (Subst->getReplacedParameter()->getDepth() == Depth) 6331bdd1243dSDimitry Andric Used[Subst->getIndex()] = true; 63320b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, Subst->getArgumentPack(), 63330b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 63340b57cec5SDimitry Andric break; 63350b57cec5SDimitry Andric } 63360b57cec5SDimitry Andric 63370b57cec5SDimitry Andric case Type::InjectedClassName: 63380b57cec5SDimitry Andric T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); 6339bdd1243dSDimitry Andric [[fallthrough]]; 63400b57cec5SDimitry Andric 63410b57cec5SDimitry Andric case Type::TemplateSpecialization: { 63420b57cec5SDimitry Andric const TemplateSpecializationType *Spec 63430b57cec5SDimitry Andric = cast<TemplateSpecializationType>(T); 63440b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, Spec->getTemplateName(), OnlyDeduced, 63450b57cec5SDimitry Andric Depth, Used); 63460b57cec5SDimitry Andric 63470b57cec5SDimitry Andric // C++0x [temp.deduct.type]p9: 63480b57cec5SDimitry Andric // If the template argument list of P contains a pack expansion that is 63490b57cec5SDimitry Andric // not the last template argument, the entire template argument list is a 63500b57cec5SDimitry Andric // non-deduced context. 63510b57cec5SDimitry Andric if (OnlyDeduced && 63520b57cec5SDimitry Andric hasPackExpansionBeforeEnd(Spec->template_arguments())) 63530b57cec5SDimitry Andric break; 63540b57cec5SDimitry Andric 6355bdd1243dSDimitry Andric for (const auto &Arg : Spec->template_arguments()) 6356bdd1243dSDimitry Andric MarkUsedTemplateParameters(Ctx, Arg, OnlyDeduced, Depth, Used); 63570b57cec5SDimitry Andric break; 63580b57cec5SDimitry Andric } 63590b57cec5SDimitry Andric 63600b57cec5SDimitry Andric case Type::Complex: 63610b57cec5SDimitry Andric if (!OnlyDeduced) 63620b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 63630b57cec5SDimitry Andric cast<ComplexType>(T)->getElementType(), 63640b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 63650b57cec5SDimitry Andric break; 63660b57cec5SDimitry Andric 63670b57cec5SDimitry Andric case Type::Atomic: 63680b57cec5SDimitry Andric if (!OnlyDeduced) 63690b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 63700b57cec5SDimitry Andric cast<AtomicType>(T)->getValueType(), 63710b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 63720b57cec5SDimitry Andric break; 63730b57cec5SDimitry Andric 63740b57cec5SDimitry Andric case Type::DependentName: 63750b57cec5SDimitry Andric if (!OnlyDeduced) 63760b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 63770b57cec5SDimitry Andric cast<DependentNameType>(T)->getQualifier(), 63780b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 63790b57cec5SDimitry Andric break; 63800b57cec5SDimitry Andric 63810b57cec5SDimitry Andric case Type::DependentTemplateSpecialization: { 63820b57cec5SDimitry Andric // C++14 [temp.deduct.type]p5: 63830b57cec5SDimitry Andric // The non-deduced contexts are: 63840b57cec5SDimitry Andric // -- The nested-name-specifier of a type that was specified using a 63850b57cec5SDimitry Andric // qualified-id 63860b57cec5SDimitry Andric // 63870b57cec5SDimitry Andric // C++14 [temp.deduct.type]p6: 63880b57cec5SDimitry Andric // When a type name is specified in a way that includes a non-deduced 63890b57cec5SDimitry Andric // context, all of the types that comprise that type name are also 63900b57cec5SDimitry Andric // non-deduced. 63910b57cec5SDimitry Andric if (OnlyDeduced) 63920b57cec5SDimitry Andric break; 63930b57cec5SDimitry Andric 63940b57cec5SDimitry Andric const DependentTemplateSpecializationType *Spec 63950b57cec5SDimitry Andric = cast<DependentTemplateSpecializationType>(T); 63960b57cec5SDimitry Andric 63970b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, Spec->getQualifier(), 63980b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 63990b57cec5SDimitry Andric 6400bdd1243dSDimitry Andric for (const auto &Arg : Spec->template_arguments()) 6401bdd1243dSDimitry Andric MarkUsedTemplateParameters(Ctx, Arg, OnlyDeduced, Depth, Used); 64020b57cec5SDimitry Andric break; 64030b57cec5SDimitry Andric } 64040b57cec5SDimitry Andric 64050b57cec5SDimitry Andric case Type::TypeOf: 64060b57cec5SDimitry Andric if (!OnlyDeduced) 6407bdd1243dSDimitry Andric MarkUsedTemplateParameters(Ctx, cast<TypeOfType>(T)->getUnmodifiedType(), 64080b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 64090b57cec5SDimitry Andric break; 64100b57cec5SDimitry Andric 64110b57cec5SDimitry Andric case Type::TypeOfExpr: 64120b57cec5SDimitry Andric if (!OnlyDeduced) 64130b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 64140b57cec5SDimitry Andric cast<TypeOfExprType>(T)->getUnderlyingExpr(), 64150b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 64160b57cec5SDimitry Andric break; 64170b57cec5SDimitry Andric 64180b57cec5SDimitry Andric case Type::Decltype: 64190b57cec5SDimitry Andric if (!OnlyDeduced) 64200b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 64210b57cec5SDimitry Andric cast<DecltypeType>(T)->getUnderlyingExpr(), 64220b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 64230b57cec5SDimitry Andric break; 64240b57cec5SDimitry Andric 64250b57cec5SDimitry Andric case Type::UnaryTransform: 64260b57cec5SDimitry Andric if (!OnlyDeduced) 64270b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 64280b57cec5SDimitry Andric cast<UnaryTransformType>(T)->getUnderlyingType(), 64290b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 64300b57cec5SDimitry Andric break; 64310b57cec5SDimitry Andric 64320b57cec5SDimitry Andric case Type::PackExpansion: 64330b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 64340b57cec5SDimitry Andric cast<PackExpansionType>(T)->getPattern(), 64350b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 64360b57cec5SDimitry Andric break; 64370b57cec5SDimitry Andric 64380b57cec5SDimitry Andric case Type::Auto: 64390b57cec5SDimitry Andric case Type::DeducedTemplateSpecialization: 64400b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 64410b57cec5SDimitry Andric cast<DeducedType>(T)->getDeducedType(), 64420b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 64430b57cec5SDimitry Andric break; 64440eae32dcSDimitry Andric case Type::DependentBitInt: 64455ffd83dbSDimitry Andric MarkUsedTemplateParameters(Ctx, 64460eae32dcSDimitry Andric cast<DependentBitIntType>(T)->getNumBitsExpr(), 64475ffd83dbSDimitry Andric OnlyDeduced, Depth, Used); 64485ffd83dbSDimitry Andric break; 64490b57cec5SDimitry Andric 64500b57cec5SDimitry Andric // None of these types have any template parameters in them. 64510b57cec5SDimitry Andric case Type::Builtin: 64520b57cec5SDimitry Andric case Type::VariableArray: 64530b57cec5SDimitry Andric case Type::FunctionNoProto: 64540b57cec5SDimitry Andric case Type::Record: 64550b57cec5SDimitry Andric case Type::Enum: 64560b57cec5SDimitry Andric case Type::ObjCInterface: 64570b57cec5SDimitry Andric case Type::ObjCObject: 64580b57cec5SDimitry Andric case Type::ObjCObjectPointer: 64590b57cec5SDimitry Andric case Type::UnresolvedUsing: 64600b57cec5SDimitry Andric case Type::Pipe: 64610eae32dcSDimitry Andric case Type::BitInt: 64620b57cec5SDimitry Andric #define TYPE(Class, Base) 64630b57cec5SDimitry Andric #define ABSTRACT_TYPE(Class, Base) 64640b57cec5SDimitry Andric #define DEPENDENT_TYPE(Class, Base) 64650b57cec5SDimitry Andric #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 6466a7dea167SDimitry Andric #include "clang/AST/TypeNodes.inc" 64670b57cec5SDimitry Andric break; 64680b57cec5SDimitry Andric } 64690b57cec5SDimitry Andric } 64700b57cec5SDimitry Andric 64710b57cec5SDimitry Andric /// Mark the template parameters that are used by this 64720b57cec5SDimitry Andric /// template argument. 64730b57cec5SDimitry Andric static void 64740b57cec5SDimitry Andric MarkUsedTemplateParameters(ASTContext &Ctx, 64750b57cec5SDimitry Andric const TemplateArgument &TemplateArg, 64760b57cec5SDimitry Andric bool OnlyDeduced, 64770b57cec5SDimitry Andric unsigned Depth, 64780b57cec5SDimitry Andric llvm::SmallBitVector &Used) { 64790b57cec5SDimitry Andric switch (TemplateArg.getKind()) { 64800b57cec5SDimitry Andric case TemplateArgument::Null: 64810b57cec5SDimitry Andric case TemplateArgument::Integral: 64820b57cec5SDimitry Andric case TemplateArgument::Declaration: 64830b57cec5SDimitry Andric case TemplateArgument::NullPtr: 6484*7a6dacacSDimitry Andric case TemplateArgument::StructuralValue: 64850b57cec5SDimitry Andric break; 64860b57cec5SDimitry Andric 64870b57cec5SDimitry Andric case TemplateArgument::Type: 64880b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, TemplateArg.getAsType(), OnlyDeduced, 64890b57cec5SDimitry Andric Depth, Used); 64900b57cec5SDimitry Andric break; 64910b57cec5SDimitry Andric 64920b57cec5SDimitry Andric case TemplateArgument::Template: 64930b57cec5SDimitry Andric case TemplateArgument::TemplateExpansion: 64940b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 64950b57cec5SDimitry Andric TemplateArg.getAsTemplateOrTemplatePattern(), 64960b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 64970b57cec5SDimitry Andric break; 64980b57cec5SDimitry Andric 64990b57cec5SDimitry Andric case TemplateArgument::Expression: 65000b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, TemplateArg.getAsExpr(), OnlyDeduced, 65010b57cec5SDimitry Andric Depth, Used); 65020b57cec5SDimitry Andric break; 65030b57cec5SDimitry Andric 65040b57cec5SDimitry Andric case TemplateArgument::Pack: 65050b57cec5SDimitry Andric for (const auto &P : TemplateArg.pack_elements()) 65060b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, P, OnlyDeduced, Depth, Used); 65070b57cec5SDimitry Andric break; 65080b57cec5SDimitry Andric } 65090b57cec5SDimitry Andric } 65100b57cec5SDimitry Andric 6511480093f4SDimitry Andric /// Mark which template parameters are used in a given expression. 6512480093f4SDimitry Andric /// 6513480093f4SDimitry Andric /// \param E the expression from which template parameters will be deduced. 6514480093f4SDimitry Andric /// 6515480093f4SDimitry Andric /// \param Used a bit vector whose elements will be set to \c true 6516480093f4SDimitry Andric /// to indicate when the corresponding template parameter will be 6517480093f4SDimitry Andric /// deduced. 6518480093f4SDimitry Andric void 6519480093f4SDimitry Andric Sema::MarkUsedTemplateParameters(const Expr *E, bool OnlyDeduced, 6520480093f4SDimitry Andric unsigned Depth, 6521480093f4SDimitry Andric llvm::SmallBitVector &Used) { 6522480093f4SDimitry Andric ::MarkUsedTemplateParameters(Context, E, OnlyDeduced, Depth, Used); 6523480093f4SDimitry Andric } 6524480093f4SDimitry Andric 65250b57cec5SDimitry Andric /// Mark which template parameters can be deduced from a given 65260b57cec5SDimitry Andric /// template argument list. 65270b57cec5SDimitry Andric /// 65280b57cec5SDimitry Andric /// \param TemplateArgs the template argument list from which template 65290b57cec5SDimitry Andric /// parameters will be deduced. 65300b57cec5SDimitry Andric /// 65310b57cec5SDimitry Andric /// \param Used a bit vector whose elements will be set to \c true 65320b57cec5SDimitry Andric /// to indicate when the corresponding template parameter will be 65330b57cec5SDimitry Andric /// deduced. 65340b57cec5SDimitry Andric void 65350b57cec5SDimitry Andric Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, 65360b57cec5SDimitry Andric bool OnlyDeduced, unsigned Depth, 65370b57cec5SDimitry Andric llvm::SmallBitVector &Used) { 65380b57cec5SDimitry Andric // C++0x [temp.deduct.type]p9: 65390b57cec5SDimitry Andric // If the template argument list of P contains a pack expansion that is not 65400b57cec5SDimitry Andric // the last template argument, the entire template argument list is a 65410b57cec5SDimitry Andric // non-deduced context. 65420b57cec5SDimitry Andric if (OnlyDeduced && 65430b57cec5SDimitry Andric hasPackExpansionBeforeEnd(TemplateArgs.asArray())) 65440b57cec5SDimitry Andric return; 65450b57cec5SDimitry Andric 65460b57cec5SDimitry Andric for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) 65470b57cec5SDimitry Andric ::MarkUsedTemplateParameters(Context, TemplateArgs[I], OnlyDeduced, 65480b57cec5SDimitry Andric Depth, Used); 65490b57cec5SDimitry Andric } 65500b57cec5SDimitry Andric 65510b57cec5SDimitry Andric /// Marks all of the template parameters that will be deduced by a 65520b57cec5SDimitry Andric /// call to the given function template. 65530b57cec5SDimitry Andric void Sema::MarkDeducedTemplateParameters( 65540b57cec5SDimitry Andric ASTContext &Ctx, const FunctionTemplateDecl *FunctionTemplate, 65550b57cec5SDimitry Andric llvm::SmallBitVector &Deduced) { 65560b57cec5SDimitry Andric TemplateParameterList *TemplateParams 65570b57cec5SDimitry Andric = FunctionTemplate->getTemplateParameters(); 65580b57cec5SDimitry Andric Deduced.clear(); 65590b57cec5SDimitry Andric Deduced.resize(TemplateParams->size()); 65600b57cec5SDimitry Andric 65610b57cec5SDimitry Andric FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 65620b57cec5SDimitry Andric for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) 65630b57cec5SDimitry Andric ::MarkUsedTemplateParameters(Ctx, Function->getParamDecl(I)->getType(), 65640b57cec5SDimitry Andric true, TemplateParams->getDepth(), Deduced); 65650b57cec5SDimitry Andric } 65660b57cec5SDimitry Andric 65670b57cec5SDimitry Andric bool hasDeducibleTemplateParameters(Sema &S, 65680b57cec5SDimitry Andric FunctionTemplateDecl *FunctionTemplate, 65690b57cec5SDimitry Andric QualType T) { 65700b57cec5SDimitry Andric if (!T->isDependentType()) 65710b57cec5SDimitry Andric return false; 65720b57cec5SDimitry Andric 65730b57cec5SDimitry Andric TemplateParameterList *TemplateParams 65740b57cec5SDimitry Andric = FunctionTemplate->getTemplateParameters(); 65750b57cec5SDimitry Andric llvm::SmallBitVector Deduced(TemplateParams->size()); 65760b57cec5SDimitry Andric ::MarkUsedTemplateParameters(S.Context, T, true, TemplateParams->getDepth(), 65770b57cec5SDimitry Andric Deduced); 65780b57cec5SDimitry Andric 65790b57cec5SDimitry Andric return Deduced.any(); 65800b57cec5SDimitry Andric } 6581