Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 1 | //===- SemaTemplateDeduction.cpp - Template Argument Deduction ------------===// |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 6 | // |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 8 | // |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 9 | // This file implements C++ template argument deduction. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 12 | |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 13 | #include "clang/Sema/TemplateDeduction.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "TreeTransform.h" |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 15 | #include "TypeLocBuilder.h" |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTLambda.h" |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 18 | #include "clang/AST/Decl.h" |
| 19 | #include "clang/AST/DeclAccessPair.h" |
| 20 | #include "clang/AST/DeclBase.h" |
| 21 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclTemplate.h" |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclarationName.h" |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 24 | #include "clang/AST/Expr.h" |
| 25 | #include "clang/AST/ExprCXX.h" |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 26 | #include "clang/AST/NestedNameSpecifier.h" |
| 27 | #include "clang/AST/TemplateBase.h" |
| 28 | #include "clang/AST/TemplateName.h" |
| 29 | #include "clang/AST/Type.h" |
| 30 | #include "clang/AST/TypeLoc.h" |
| 31 | #include "clang/AST/UnresolvedSet.h" |
| 32 | #include "clang/Basic/AddressSpaces.h" |
| 33 | #include "clang/Basic/ExceptionSpecificationType.h" |
| 34 | #include "clang/Basic/LLVM.h" |
| 35 | #include "clang/Basic/LangOptions.h" |
| 36 | #include "clang/Basic/PartialDiagnostic.h" |
| 37 | #include "clang/Basic/SourceLocation.h" |
| 38 | #include "clang/Basic/Specifiers.h" |
| 39 | #include "clang/Sema/Ownership.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 40 | #include "clang/Sema/Sema.h" |
| 41 | #include "clang/Sema/Template.h" |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/APInt.h" |
| 43 | #include "llvm/ADT/APSInt.h" |
| 44 | #include "llvm/ADT/ArrayRef.h" |
| 45 | #include "llvm/ADT/DenseMap.h" |
| 46 | #include "llvm/ADT/FoldingSet.h" |
| 47 | #include "llvm/ADT/Optional.h" |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 48 | #include "llvm/ADT/SmallBitVector.h" |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 49 | #include "llvm/ADT/SmallPtrSet.h" |
| 50 | #include "llvm/ADT/SmallVector.h" |
| 51 | #include "llvm/Support/Casting.h" |
| 52 | #include "llvm/Support/Compiler.h" |
| 53 | #include "llvm/Support/ErrorHandling.h" |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 54 | #include <algorithm> |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 55 | #include <cassert> |
| 56 | #include <tuple> |
| 57 | #include <utility> |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 58 | |
| 59 | namespace clang { |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 60 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 61 | /// Various flags that control template argument deduction. |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 62 | /// |
| 63 | /// These flags can be bitwise-OR'd together. |
| 64 | enum TemplateDeductionFlags { |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 65 | /// No template argument deduction flags, which indicates the |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 66 | /// strictest results for template argument deduction (as used for, e.g., |
| 67 | /// matching class template partial specializations). |
| 68 | TDF_None = 0, |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 69 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 70 | /// Within template argument deduction from a function call, we are |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 71 | /// matching with a parameter type for which the original parameter was |
| 72 | /// a reference. |
| 73 | TDF_ParamWithReferenceType = 0x1, |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 74 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 75 | /// Within template argument deduction from a function call, we |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 76 | /// are matching in a case where we ignore cv-qualifiers. |
| 77 | TDF_IgnoreQualifiers = 0x02, |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 78 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 79 | /// Within template argument deduction from a function call, |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 80 | /// we are matching in a case where we can perform template argument |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 81 | /// deduction from a template-id of a derived class of the argument type. |
Douglas Gregor | 406f634 | 2009-09-14 20:00:47 +0000 | [diff] [blame] | 82 | TDF_DerivedClass = 0x04, |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 83 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 84 | /// Allow non-dependent types to differ, e.g., when performing |
Douglas Gregor | 406f634 | 2009-09-14 20:00:47 +0000 | [diff] [blame] | 85 | /// template argument deduction from a function call where conversions |
| 86 | /// may apply. |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 87 | TDF_SkipNonDependent = 0x08, |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 88 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 89 | /// Whether we are performing template argument deduction for |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 90 | /// parameters and arguments in a top-level template argument |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 91 | TDF_TopLevelParameterTypeList = 0x10, |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 92 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 93 | /// Within template argument deduction from overload resolution per |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 94 | /// C++ [over.over] allow matching function types that are compatible in |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 95 | /// terms of noreturn and default calling convention adjustments, or |
| 96 | /// similarly matching a declared template specialization against a |
| 97 | /// possible template, per C++ [temp.deduct.decl]. In either case, permit |
| 98 | /// deduction where the parameter is a function type that can be converted |
| 99 | /// to the argument type. |
| 100 | TDF_AllowCompatibleFunctionType = 0x20, |
Richard Smith | b884ed1 | 2018-07-11 23:19:41 +0000 | [diff] [blame] | 101 | |
| 102 | /// Within template argument deduction for a conversion function, we are |
| 103 | /// matching with an argument type for which the original argument was |
| 104 | /// a reference. |
| 105 | TDF_ArgWithReferenceType = 0x40, |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 106 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 107 | } |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 108 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 109 | using namespace clang; |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 110 | using namespace sema; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 111 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 112 | /// Compare two APSInts, extending and switching the sign as |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 113 | /// necessary to compare their values regardless of underlying type. |
| 114 | static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) { |
| 115 | if (Y.getBitWidth() > X.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 116 | X = X.extend(Y.getBitWidth()); |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 117 | else if (Y.getBitWidth() < X.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 118 | Y = Y.extend(X.getBitWidth()); |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 119 | |
| 120 | // If there is a signedness mismatch, correct it. |
| 121 | if (X.isSigned() != Y.isSigned()) { |
| 122 | // If the signed value is negative, then the values cannot be the same. |
| 123 | if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative())) |
| 124 | return false; |
| 125 | |
| 126 | Y.setIsSigned(true); |
| 127 | X.setIsSigned(true); |
| 128 | } |
| 129 | |
| 130 | return X == Y; |
| 131 | } |
| 132 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 133 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 134 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 135 | TemplateParameterList *TemplateParams, |
| 136 | const TemplateArgument &Param, |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 137 | TemplateArgument Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 138 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 139 | SmallVectorImpl<DeducedTemplateArgument> &Deduced); |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 140 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 141 | static Sema::TemplateDeductionResult |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 142 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 143 | TemplateParameterList *TemplateParams, |
| 144 | QualType Param, |
| 145 | QualType Arg, |
| 146 | TemplateDeductionInfo &Info, |
| 147 | SmallVectorImpl<DeducedTemplateArgument> & |
| 148 | Deduced, |
| 149 | unsigned TDF, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 150 | bool PartialOrdering = false, |
| 151 | bool DeducedFromArrayBound = false); |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 152 | |
| 153 | static Sema::TemplateDeductionResult |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 154 | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 155 | ArrayRef<TemplateArgument> Params, |
| 156 | ArrayRef<TemplateArgument> Args, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 157 | TemplateDeductionInfo &Info, |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 158 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 159 | bool NumberOfArgumentsMustMatch); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 160 | |
Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 161 | static void MarkUsedTemplateParameters(ASTContext &Ctx, |
| 162 | const TemplateArgument &TemplateArg, |
| 163 | bool OnlyDeduced, unsigned Depth, |
| 164 | llvm::SmallBitVector &Used); |
| 165 | |
| 166 | static void MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
| 167 | bool OnlyDeduced, unsigned Level, |
| 168 | llvm::SmallBitVector &Deduced); |
| 169 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 170 | /// If the given expression is of a form that permits the deduction |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 171 | /// of a non-type template parameter, return the declaration of that |
| 172 | /// non-type template parameter. |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 173 | static NonTypeTemplateParmDecl * |
| 174 | getDeducedParameterFromExpr(TemplateDeductionInfo &Info, Expr *E) { |
Richard Smith | 7ebb07c | 2012-07-08 04:37:51 +0000 | [diff] [blame] | 175 | // If we are within an alias template, the expression may have undergone |
| 176 | // any number of parameter substitutions already. |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 177 | while (true) { |
Richard Smith | 7ebb07c | 2012-07-08 04:37:51 +0000 | [diff] [blame] | 178 | if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E)) |
| 179 | E = IC->getSubExpr(); |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 180 | else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(E)) |
| 181 | E = CE->getSubExpr(); |
Richard Smith | 7ebb07c | 2012-07-08 04:37:51 +0000 | [diff] [blame] | 182 | else if (SubstNonTypeTemplateParmExpr *Subst = |
| 183 | dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 184 | E = Subst->getReplacement(); |
| 185 | else |
| 186 | break; |
| 187 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 189 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 190 | if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) |
| 191 | if (NTTP->getDepth() == Info.getDeducedDepth()) |
| 192 | return NTTP; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 193 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 194 | return nullptr; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 197 | /// Determine whether two declaration pointers refer to the same |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 198 | /// declaration. |
| 199 | static bool isSameDeclaration(Decl *X, Decl *Y) { |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 200 | if (NamedDecl *NX = dyn_cast<NamedDecl>(X)) |
| 201 | X = NX->getUnderlyingDecl(); |
| 202 | if (NamedDecl *NY = dyn_cast<NamedDecl>(Y)) |
| 203 | Y = NY->getUnderlyingDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 204 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 205 | return X->getCanonicalDecl() == Y->getCanonicalDecl(); |
| 206 | } |
| 207 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 208 | /// Verify that the given, deduced template arguments are compatible. |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 209 | /// |
| 210 | /// \returns The deduced template argument, or a NULL template argument if |
| 211 | /// the deduced template arguments were incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 212 | static DeducedTemplateArgument |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 213 | checkDeducedTemplateArguments(ASTContext &Context, |
| 214 | const DeducedTemplateArgument &X, |
| 215 | const DeducedTemplateArgument &Y) { |
| 216 | // We have no deduction for one or both of the arguments; they're compatible. |
| 217 | if (X.isNull()) |
| 218 | return Y; |
| 219 | if (Y.isNull()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 220 | return X; |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 221 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 222 | // If we have two non-type template argument values deduced for the same |
| 223 | // parameter, they must both match the type of the parameter, and thus must |
| 224 | // match each other's type. As we're only keeping one of them, we must check |
| 225 | // for that now. The exception is that if either was deduced from an array |
| 226 | // bound, the type is permitted to differ. |
| 227 | if (!X.wasDeducedFromArrayBound() && !Y.wasDeducedFromArrayBound()) { |
| 228 | QualType XType = X.getNonTypeTemplateArgumentType(); |
| 229 | if (!XType.isNull()) { |
| 230 | QualType YType = Y.getNonTypeTemplateArgumentType(); |
| 231 | if (YType.isNull() || !Context.hasSameType(XType, YType)) |
| 232 | return DeducedTemplateArgument(); |
| 233 | } |
| 234 | } |
| 235 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 236 | switch (X.getKind()) { |
| 237 | case TemplateArgument::Null: |
| 238 | llvm_unreachable("Non-deduced template arguments handled above"); |
| 239 | |
| 240 | case TemplateArgument::Type: |
| 241 | // If two template type arguments have the same type, they're compatible. |
| 242 | if (Y.getKind() == TemplateArgument::Type && |
| 243 | Context.hasSameType(X.getAsType(), Y.getAsType())) |
| 244 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 245 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 246 | // If one of the two arguments was deduced from an array bound, the other |
| 247 | // supersedes it. |
| 248 | if (X.wasDeducedFromArrayBound() != Y.wasDeducedFromArrayBound()) |
| 249 | return X.wasDeducedFromArrayBound() ? Y : X; |
| 250 | |
| 251 | // The arguments are not compatible. |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 252 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 253 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 254 | case TemplateArgument::Integral: |
| 255 | // If we deduced a constant in one case and either a dependent expression or |
| 256 | // declaration in another case, keep the integral constant. |
| 257 | // If both are integral constants with the same value, keep that value. |
| 258 | if (Y.getKind() == TemplateArgument::Expression || |
| 259 | Y.getKind() == TemplateArgument::Declaration || |
| 260 | (Y.getKind() == TemplateArgument::Integral && |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 261 | hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()))) |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 262 | return X.wasDeducedFromArrayBound() ? Y : X; |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 263 | |
| 264 | // All other combinations are incompatible. |
| 265 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 266 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 267 | case TemplateArgument::Template: |
| 268 | if (Y.getKind() == TemplateArgument::Template && |
| 269 | Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate())) |
| 270 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 271 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 272 | // All other combinations are incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 273 | return DeducedTemplateArgument(); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 274 | |
| 275 | case TemplateArgument::TemplateExpansion: |
| 276 | if (Y.getKind() == TemplateArgument::TemplateExpansion && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 277 | Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(), |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 278 | Y.getAsTemplateOrTemplatePattern())) |
| 279 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 280 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 281 | // All other combinations are incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 282 | return DeducedTemplateArgument(); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 283 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 284 | case TemplateArgument::Expression: { |
| 285 | if (Y.getKind() != TemplateArgument::Expression) |
| 286 | return checkDeducedTemplateArguments(Context, Y, X); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 287 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 288 | // Compare the expressions for equality |
| 289 | llvm::FoldingSetNodeID ID1, ID2; |
| 290 | X.getAsExpr()->Profile(ID1, Context, true); |
| 291 | Y.getAsExpr()->Profile(ID2, Context, true); |
| 292 | if (ID1 == ID2) |
| 293 | return X.wasDeducedFromArrayBound() ? Y : X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 294 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 295 | // Differing dependent expressions are incompatible. |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 296 | return DeducedTemplateArgument(); |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 297 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 298 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 299 | case TemplateArgument::Declaration: |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 300 | assert(!X.wasDeducedFromArrayBound()); |
| 301 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 302 | // If we deduced a declaration and a dependent expression, keep the |
| 303 | // declaration. |
| 304 | if (Y.getKind() == TemplateArgument::Expression) |
| 305 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 306 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 307 | // If we deduced a declaration and an integral constant, keep the |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 308 | // integral constant and whichever type did not come from an array |
| 309 | // bound. |
| 310 | if (Y.getKind() == TemplateArgument::Integral) { |
| 311 | if (Y.wasDeducedFromArrayBound()) |
| 312 | return TemplateArgument(Context, Y.getAsIntegral(), |
| 313 | X.getParamTypeForDecl()); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 314 | return Y; |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 315 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 316 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 317 | // If we deduced two declarations, make sure that they refer to the |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 318 | // same declaration. |
| 319 | if (Y.getKind() == TemplateArgument::Declaration && |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 320 | isSameDeclaration(X.getAsDecl(), Y.getAsDecl())) |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 321 | return X; |
| 322 | |
| 323 | // All other combinations are incompatible. |
| 324 | return DeducedTemplateArgument(); |
| 325 | |
| 326 | case TemplateArgument::NullPtr: |
| 327 | // If we deduced a null pointer and a dependent expression, keep the |
| 328 | // null pointer. |
| 329 | if (Y.getKind() == TemplateArgument::Expression) |
| 330 | return X; |
| 331 | |
| 332 | // If we deduced a null pointer and an integral constant, keep the |
| 333 | // integral constant. |
| 334 | if (Y.getKind() == TemplateArgument::Integral) |
| 335 | return Y; |
| 336 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 337 | // If we deduced two null pointers, they are the same. |
| 338 | if (Y.getKind() == TemplateArgument::NullPtr) |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 339 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 340 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 341 | // All other combinations are incompatible. |
| 342 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 343 | |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 344 | case TemplateArgument::Pack: { |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 345 | if (Y.getKind() != TemplateArgument::Pack || |
| 346 | X.pack_size() != Y.pack_size()) |
| 347 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 348 | |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 349 | llvm::SmallVector<TemplateArgument, 8> NewPack; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 350 | for (TemplateArgument::pack_iterator XA = X.pack_begin(), |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 351 | XAEnd = X.pack_end(), |
| 352 | YA = Y.pack_begin(); |
| 353 | XA != XAEnd; ++XA, ++YA) { |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 354 | TemplateArgument Merged = checkDeducedTemplateArguments( |
| 355 | Context, DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()), |
| 356 | DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound())); |
| 357 | if (Merged.isNull()) |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 358 | return DeducedTemplateArgument(); |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 359 | NewPack.push_back(Merged); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 360 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 361 | |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 362 | return DeducedTemplateArgument( |
| 363 | TemplateArgument::CreatePackCopy(Context, NewPack), |
| 364 | X.wasDeducedFromArrayBound() && Y.wasDeducedFromArrayBound()); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 365 | } |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 366 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 367 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 368 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 371 | /// Deduce the value of the given non-type template parameter |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 372 | /// as the given deduced template argument. All non-type template parameter |
| 373 | /// deduction is funneled through here. |
Benjamin Kramer | 7320b99 | 2016-06-15 14:20:56 +0000 | [diff] [blame] | 374 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 375 | Sema &S, TemplateParameterList *TemplateParams, |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 376 | NonTypeTemplateParmDecl *NTTP, const DeducedTemplateArgument &NewDeduced, |
| 377 | QualType ValueType, TemplateDeductionInfo &Info, |
Benjamin Kramer | 7320b99 | 2016-06-15 14:20:56 +0000 | [diff] [blame] | 378 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 379 | assert(NTTP->getDepth() == Info.getDeducedDepth() && |
| 380 | "deducing non-type template argument with wrong depth"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 381 | |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 382 | DeducedTemplateArgument Result = checkDeducedTemplateArguments( |
| 383 | S.Context, Deduced[NTTP->getIndex()], NewDeduced); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 384 | if (Result.isNull()) { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 385 | Info.Param = NTTP; |
| 386 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 387 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 388 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 389 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 390 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 391 | Deduced[NTTP->getIndex()] = Result; |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 392 | if (!S.getLangOpts().CPlusPlus17) |
Richard Smith | d92eddf | 2016-12-27 06:14:37 +0000 | [diff] [blame] | 393 | return Sema::TDK_Success; |
| 394 | |
Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 395 | if (NTTP->isExpandedParameterPack()) |
| 396 | // FIXME: We may still need to deduce parts of the type here! But we |
| 397 | // don't have any way to find which slice of the type to use, and the |
| 398 | // type stored on the NTTP itself is nonsense. Perhaps the type of an |
| 399 | // expanded NTTP should be a pack expansion type? |
| 400 | return Sema::TDK_Success; |
| 401 | |
Richard Smith | 7bfcc05 | 2017-12-01 21:24:36 +0000 | [diff] [blame] | 402 | // Get the type of the parameter for deduction. If it's a (dependent) array |
| 403 | // or function type, we will not have decayed it yet, so do that now. |
| 404 | QualType ParamType = S.Context.getAdjustedParameterType(NTTP->getType()); |
Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 405 | if (auto *Expansion = dyn_cast<PackExpansionType>(ParamType)) |
| 406 | ParamType = Expansion->getPattern(); |
| 407 | |
Richard Smith | d92eddf | 2016-12-27 06:14:37 +0000 | [diff] [blame] | 408 | // FIXME: It's not clear how deduction of a parameter of reference |
| 409 | // type from an argument (of non-reference type) should be performed. |
| 410 | // For now, we just remove reference types from both sides and let |
| 411 | // the final check for matching types sort out the mess. |
| 412 | return DeduceTemplateArgumentsByTypeMatch( |
Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 413 | S, TemplateParams, ParamType.getNonReferenceType(), |
Richard Smith | d92eddf | 2016-12-27 06:14:37 +0000 | [diff] [blame] | 414 | ValueType.getNonReferenceType(), Info, Deduced, TDF_SkipNonDependent, |
| 415 | /*PartialOrdering=*/false, |
| 416 | /*ArrayBound=*/NewDeduced.wasDeducedFromArrayBound()); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 419 | /// Deduce the value of the given non-type template parameter |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 420 | /// from the given integral constant. |
| 421 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| 422 | Sema &S, TemplateParameterList *TemplateParams, |
| 423 | NonTypeTemplateParmDecl *NTTP, const llvm::APSInt &Value, |
| 424 | QualType ValueType, bool DeducedFromArrayBound, TemplateDeductionInfo &Info, |
| 425 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 426 | return DeduceNonTypeTemplateArgument( |
| 427 | S, TemplateParams, NTTP, |
| 428 | DeducedTemplateArgument(S.Context, Value, ValueType, |
| 429 | DeducedFromArrayBound), |
| 430 | ValueType, Info, Deduced); |
| 431 | } |
| 432 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 433 | /// Deduce the value of the given non-type template parameter |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 434 | /// from the given null pointer template argument type. |
| 435 | static Sema::TemplateDeductionResult DeduceNullPtrTemplateArgument( |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 436 | Sema &S, TemplateParameterList *TemplateParams, |
| 437 | NonTypeTemplateParmDecl *NTTP, QualType NullPtrType, |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 438 | TemplateDeductionInfo &Info, |
| 439 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 440 | Expr *Value = |
| 441 | S.ImpCastExprToType(new (S.Context) CXXNullPtrLiteralExpr( |
| 442 | S.Context.NullPtrTy, NTTP->getLocation()), |
| 443 | NullPtrType, CK_NullToPointer) |
| 444 | .get(); |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 445 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 446 | DeducedTemplateArgument(Value), |
| 447 | Value->getType(), Info, Deduced); |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 450 | /// Deduce the value of the given non-type template parameter |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 451 | /// from the given type- or value-dependent expression. |
| 452 | /// |
| 453 | /// \returns true if deduction succeeded, false otherwise. |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 454 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| 455 | Sema &S, TemplateParameterList *TemplateParams, |
| 456 | NonTypeTemplateParmDecl *NTTP, Expr *Value, TemplateDeductionInfo &Info, |
| 457 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 458 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 459 | DeducedTemplateArgument(Value), |
| 460 | Value->getType(), Info, Deduced); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 463 | /// Deduce the value of the given non-type template parameter |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 464 | /// from the given declaration. |
| 465 | /// |
| 466 | /// \returns true if deduction succeeded, false otherwise. |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 467 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| 468 | Sema &S, TemplateParameterList *TemplateParams, |
| 469 | NonTypeTemplateParmDecl *NTTP, ValueDecl *D, QualType T, |
| 470 | TemplateDeductionInfo &Info, |
| 471 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 472 | D = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 473 | TemplateArgument New(D, T); |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 474 | return DeduceNonTypeTemplateArgument( |
| 475 | S, TemplateParams, NTTP, DeducedTemplateArgument(New), T, Info, Deduced); |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 478 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 479 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 480 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 481 | TemplateName Param, |
| 482 | TemplateName Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 483 | TemplateDeductionInfo &Info, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 484 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 485 | TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 486 | if (!ParamDecl) { |
| 487 | // The parameter type is dependent and is not a template template parameter, |
| 488 | // so there is nothing that we can deduce. |
| 489 | return Sema::TDK_Success; |
| 490 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 491 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 492 | if (TemplateTemplateParmDecl *TempParam |
| 493 | = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 494 | // If we're not deducing at this depth, there's nothing to deduce. |
| 495 | if (TempParam->getDepth() != Info.getDeducedDepth()) |
| 496 | return Sema::TDK_Success; |
| 497 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 498 | DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 499 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 500 | Deduced[TempParam->getIndex()], |
| 501 | NewDeduced); |
| 502 | if (Result.isNull()) { |
| 503 | Info.Param = TempParam; |
| 504 | Info.FirstArg = Deduced[TempParam->getIndex()]; |
| 505 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 506 | return Sema::TDK_Inconsistent; |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 507 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 508 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 509 | Deduced[TempParam->getIndex()] = Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 510 | return Sema::TDK_Success; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 511 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 512 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 513 | // Verify that the two template names are equivalent. |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 514 | if (S.Context.hasSameTemplateName(Param, Arg)) |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 515 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 516 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 517 | // Mismatch of non-dependent template parameter to argument. |
| 518 | Info.FirstArg = TemplateArgument(Param); |
| 519 | Info.SecondArg = TemplateArgument(Arg); |
| 520 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 523 | /// Deduce the template arguments by comparing the template parameter |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 524 | /// type (which is a template-id) with the template argument type. |
| 525 | /// |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 526 | /// \param S the Sema |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 527 | /// |
| 528 | /// \param TemplateParams the template parameters that we are deducing |
| 529 | /// |
| 530 | /// \param Param the parameter type |
| 531 | /// |
| 532 | /// \param Arg the argument type |
| 533 | /// |
| 534 | /// \param Info information about the template argument deduction itself |
| 535 | /// |
| 536 | /// \param Deduced the deduced template arguments |
| 537 | /// |
| 538 | /// \returns the result of template argument deduction so far. Note that a |
| 539 | /// "success" result means that template argument deduction has not yet failed, |
| 540 | /// but it may still fail, later, for other reasons. |
| 541 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 542 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 543 | TemplateParameterList *TemplateParams, |
| 544 | const TemplateSpecializationType *Param, |
| 545 | QualType Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 546 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 547 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 548 | assert(Arg.isCanonical() && "Argument type must be canonical"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 549 | |
Richard Smith | 1337318 | 2018-01-04 01:24:17 +0000 | [diff] [blame] | 550 | // Treat an injected-class-name as its underlying template-id. |
| 551 | if (auto *Injected = dyn_cast<InjectedClassNameType>(Arg)) |
| 552 | Arg = Injected->getInjectedSpecializationType(); |
| 553 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 554 | // Check whether the template argument is a dependent template-id. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 555 | if (const TemplateSpecializationType *SpecArg |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 556 | = dyn_cast<TemplateSpecializationType>(Arg)) { |
| 557 | // Perform template argument deduction for the template name. |
| 558 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 559 | = DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 560 | Param->getTemplateName(), |
| 561 | SpecArg->getTemplateName(), |
| 562 | Info, Deduced)) |
| 563 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 564 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 565 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 566 | // Perform template argument deduction on each template |
Douglas Gregor | d80ea20 | 2010-12-22 18:55:49 +0000 | [diff] [blame] | 567 | // argument. Ignore any missing/extra arguments, since they could be |
| 568 | // filled in by default arguments. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 569 | return DeduceTemplateArguments(S, TemplateParams, |
| 570 | Param->template_arguments(), |
| 571 | SpecArg->template_arguments(), Info, Deduced, |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 572 | /*NumberOfArgumentsMustMatch=*/false); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 573 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 574 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 575 | // If the argument type is a class template specialization, we |
| 576 | // perform template argument deduction using its template |
| 577 | // arguments. |
| 578 | const RecordType *RecordArg = dyn_cast<RecordType>(Arg); |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 579 | if (!RecordArg) { |
| 580 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 581 | Info.SecondArg = TemplateArgument(Arg); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 582 | return Sema::TDK_NonDeducedMismatch; |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 583 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 584 | |
| 585 | ClassTemplateSpecializationDecl *SpecArg |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 586 | = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 587 | if (!SpecArg) { |
| 588 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 589 | Info.SecondArg = TemplateArgument(Arg); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 590 | return Sema::TDK_NonDeducedMismatch; |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 591 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 592 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 593 | // Perform template argument deduction for the template name. |
| 594 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 595 | = DeduceTemplateArguments(S, |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 596 | TemplateParams, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 597 | Param->getTemplateName(), |
| 598 | TemplateName(SpecArg->getSpecializedTemplate()), |
| 599 | Info, Deduced)) |
| 600 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 601 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 602 | // Perform template argument deduction for the template arguments. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 603 | return DeduceTemplateArguments(S, TemplateParams, Param->template_arguments(), |
| 604 | SpecArg->getTemplateArgs().asArray(), Info, |
| 605 | Deduced, /*NumberOfArgumentsMustMatch=*/true); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 608 | /// Determines whether the given type is an opaque type that |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 609 | /// might be more qualified when instantiated. |
| 610 | static bool IsPossiblyOpaquelyQualifiedType(QualType T) { |
| 611 | switch (T->getTypeClass()) { |
| 612 | case Type::TypeOfExpr: |
| 613 | case Type::TypeOf: |
| 614 | case Type::DependentName: |
| 615 | case Type::Decltype: |
| 616 | case Type::UnresolvedUsing: |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 617 | case Type::TemplateTypeParm: |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 618 | return true; |
| 619 | |
| 620 | case Type::ConstantArray: |
| 621 | case Type::IncompleteArray: |
| 622 | case Type::VariableArray: |
| 623 | case Type::DependentSizedArray: |
| 624 | return IsPossiblyOpaquelyQualifiedType( |
| 625 | cast<ArrayType>(T)->getElementType()); |
| 626 | |
| 627 | default: |
| 628 | return false; |
| 629 | } |
| 630 | } |
| 631 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 632 | /// Helper function to build a TemplateParameter when we don't |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 633 | /// know its type statically. |
| 634 | static TemplateParameter makeTemplateParameter(Decl *D) { |
| 635 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) |
| 636 | return TemplateParameter(TTP); |
Craig Topper | 4b482ee | 2013-07-08 04:24:47 +0000 | [diff] [blame] | 637 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 638 | return TemplateParameter(NTTP); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 639 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 640 | return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); |
| 641 | } |
| 642 | |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 643 | /// If \p Param is an expanded parameter pack, get the number of expansions. |
| 644 | static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { |
| 645 | if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 646 | if (NTTP->isExpandedParameterPack()) |
| 647 | return NTTP->getNumExpansionTypes(); |
| 648 | |
| 649 | if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param)) |
| 650 | if (TTP->isExpandedParameterPack()) |
| 651 | return TTP->getNumExpansionTemplateParameters(); |
| 652 | |
| 653 | return None; |
| 654 | } |
| 655 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 656 | /// A pack that we're currently deducing. |
| 657 | struct clang::DeducedPack { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 658 | // The index of the pack. |
| 659 | unsigned Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 660 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 661 | // The old value of the pack before we started deducing it. |
| 662 | DeducedTemplateArgument Saved; |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 663 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 664 | // A deferred value of this pack from an inner deduction, that couldn't be |
| 665 | // deduced because this deduction hadn't happened yet. |
| 666 | DeducedTemplateArgument DeferredDeduction; |
| 667 | |
| 668 | // The new value of the pack. |
| 669 | SmallVector<DeducedTemplateArgument, 4> New; |
| 670 | |
| 671 | // The outer deduction for this pack, if any. |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 672 | DeducedPack *Outer = nullptr; |
| 673 | |
| 674 | DeducedPack(unsigned Index) : Index(Index) {} |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 675 | }; |
| 676 | |
Benjamin Kramer | d5748c7 | 2015-03-23 12:31:05 +0000 | [diff] [blame] | 677 | namespace { |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 678 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 679 | /// A scope in which we're performing pack deduction. |
| 680 | class PackDeductionScope { |
| 681 | public: |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 682 | /// Prepare to deduce the packs named within Pattern. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 683 | PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, |
| 684 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 685 | TemplateDeductionInfo &Info, TemplateArgument Pattern) |
| 686 | : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 687 | unsigned NumNamedPacks = addPacks(Pattern); |
| 688 | finishConstruction(NumNamedPacks); |
| 689 | } |
| 690 | |
| 691 | /// Prepare to directly deduce arguments of the parameter with index \p Index. |
| 692 | PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, |
| 693 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 694 | TemplateDeductionInfo &Info, unsigned Index) |
| 695 | : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { |
| 696 | addPack(Index); |
| 697 | finishConstruction(1); |
| 698 | } |
| 699 | |
| 700 | private: |
| 701 | void addPack(unsigned Index) { |
| 702 | // Save the deduced template argument for the parameter pack expanded |
| 703 | // by this pack expansion, then clear out the deduction. |
| 704 | DeducedPack Pack(Index); |
| 705 | Pack.Saved = Deduced[Index]; |
| 706 | Deduced[Index] = TemplateArgument(); |
| 707 | |
| 708 | // FIXME: What if we encounter multiple packs with different numbers of |
| 709 | // pre-expanded expansions? (This should already have been diagnosed |
| 710 | // during substitution.) |
| 711 | if (Optional<unsigned> ExpandedPackExpansions = |
| 712 | getExpandedPackSize(TemplateParams->getParam(Index))) |
| 713 | FixedNumExpansions = ExpandedPackExpansions; |
| 714 | |
| 715 | Packs.push_back(Pack); |
| 716 | } |
| 717 | |
| 718 | unsigned addPacks(TemplateArgument Pattern) { |
| 719 | // Compute the set of template parameter indices that correspond to |
| 720 | // parameter packs expanded by the pack expansion. |
| 721 | llvm::SmallBitVector SawIndices(TemplateParams->size()); |
| 722 | |
| 723 | auto AddPack = [&](unsigned Index) { |
| 724 | if (SawIndices[Index]) |
| 725 | return; |
| 726 | SawIndices[Index] = true; |
| 727 | addPack(Index); |
| 728 | }; |
| 729 | |
| 730 | // First look for unexpanded packs in the pattern. |
| 731 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 732 | S.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
| 733 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 734 | unsigned Depth, Index; |
| 735 | std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); |
| 736 | if (Depth == Info.getDeducedDepth()) |
| 737 | AddPack(Index); |
| 738 | } |
| 739 | assert(!Packs.empty() && "Pack expansion without unexpanded packs?"); |
| 740 | |
| 741 | unsigned NumNamedPacks = Packs.size(); |
| 742 | |
| 743 | // We can also have deduced template parameters that do not actually |
| 744 | // appear in the pattern, but can be deduced by it (the type of a non-type |
| 745 | // template parameter pack, in particular). These won't have prevented us |
| 746 | // from partially expanding the pack. |
| 747 | llvm::SmallBitVector Used(TemplateParams->size()); |
| 748 | MarkUsedTemplateParameters(S.Context, Pattern, /*OnlyDeduced*/true, |
| 749 | Info.getDeducedDepth(), Used); |
| 750 | for (int Index = Used.find_first(); Index != -1; |
| 751 | Index = Used.find_next(Index)) |
| 752 | if (TemplateParams->getParam(Index)->isParameterPack()) |
| 753 | AddPack(Index); |
| 754 | |
| 755 | return NumNamedPacks; |
| 756 | } |
| 757 | |
| 758 | void finishConstruction(unsigned NumNamedPacks) { |
Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 759 | // Dig out the partially-substituted pack, if there is one. |
| 760 | const TemplateArgument *PartialPackArgs = nullptr; |
| 761 | unsigned NumPartialPackArgs = 0; |
| 762 | std::pair<unsigned, unsigned> PartialPackDepthIndex(-1u, -1u); |
| 763 | if (auto *Scope = S.CurrentInstantiationScope) |
| 764 | if (auto *Partial = Scope->getPartiallySubstitutedPack( |
| 765 | &PartialPackArgs, &NumPartialPackArgs)) |
| 766 | PartialPackDepthIndex = getDepthAndIndex(Partial); |
| 767 | |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 768 | // This pack expansion will have been partially or fully expanded if |
| 769 | // it only names explicitly-specified parameter packs (including the |
| 770 | // partially-substituted one, if any). |
| 771 | bool IsExpanded = true; |
| 772 | for (unsigned I = 0; I != NumNamedPacks; ++I) { |
| 773 | if (Packs[I].Index >= Info.getNumExplicitArgs()) { |
| 774 | IsExpanded = false; |
| 775 | IsPartiallyExpanded = false; |
| 776 | break; |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 777 | } |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 778 | if (PartialPackDepthIndex == |
| 779 | std::make_pair(Info.getDeducedDepth(), Packs[I].Index)) { |
| 780 | IsPartiallyExpanded = true; |
| 781 | } |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 782 | } |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 783 | |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 784 | // Skip over the pack elements that were expanded into separate arguments. |
| 785 | // If we partially expanded, this is the number of partial arguments. |
| 786 | if (IsPartiallyExpanded) |
| 787 | PackElements += NumPartialPackArgs; |
| 788 | else if (IsExpanded) |
| 789 | PackElements += *FixedNumExpansions; |
| 790 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 791 | for (auto &Pack : Packs) { |
| 792 | if (Info.PendingDeducedPacks.size() > Pack.Index) |
| 793 | Pack.Outer = Info.PendingDeducedPacks[Pack.Index]; |
| 794 | else |
| 795 | Info.PendingDeducedPacks.resize(Pack.Index + 1); |
| 796 | Info.PendingDeducedPacks[Pack.Index] = &Pack; |
| 797 | |
Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 798 | if (PartialPackDepthIndex == |
| 799 | std::make_pair(Info.getDeducedDepth(), Pack.Index)) { |
| 800 | Pack.New.append(PartialPackArgs, PartialPackArgs + NumPartialPackArgs); |
| 801 | // We pre-populate the deduced value of the partially-substituted |
| 802 | // pack with the specified value. This is not entirely correct: the |
| 803 | // value is supposed to have been substituted, not deduced, but the |
| 804 | // cases where this is observable require an exact type match anyway. |
| 805 | // |
| 806 | // FIXME: If we could represent a "depth i, index j, pack elem k" |
| 807 | // parameter, we could substitute the partially-substituted pack |
| 808 | // everywhere and avoid this. |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 809 | if (!IsPartiallyExpanded) |
Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 810 | Deduced[Pack.Index] = Pack.New[PackElements]; |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 811 | } |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 812 | } |
| 813 | } |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 814 | |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 815 | public: |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 816 | ~PackDeductionScope() { |
| 817 | for (auto &Pack : Packs) |
| 818 | Info.PendingDeducedPacks[Pack.Index] = Pack.Outer; |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 819 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 820 | |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 821 | /// Determine whether this pack has already been partially expanded into a |
| 822 | /// sequence of (prior) function parameters / template arguments. |
Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 823 | bool isPartiallyExpanded() { return IsPartiallyExpanded; } |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 824 | |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 825 | /// Determine whether this pack expansion scope has a known, fixed arity. |
| 826 | /// This happens if it involves a pack from an outer template that has |
| 827 | /// (notionally) already been expanded. |
| 828 | bool hasFixedArity() { return FixedNumExpansions.hasValue(); } |
| 829 | |
| 830 | /// Determine whether the next element of the argument is still part of this |
| 831 | /// pack. This is the case unless the pack is already expanded to a fixed |
| 832 | /// length. |
| 833 | bool hasNextElement() { |
| 834 | return !FixedNumExpansions || *FixedNumExpansions > PackElements; |
| 835 | } |
| 836 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 837 | /// Move to deducing the next element in each pack that is being deduced. |
| 838 | void nextPackElement() { |
| 839 | // Capture the deduced template arguments for each parameter pack expanded |
| 840 | // by this pack expansion, add them to the list of arguments we've deduced |
| 841 | // for that pack, then clear out the deduced argument. |
| 842 | for (auto &Pack : Packs) { |
| 843 | DeducedTemplateArgument &DeducedArg = Deduced[Pack.Index]; |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 844 | if (!Pack.New.empty() || !DeducedArg.isNull()) { |
| 845 | while (Pack.New.size() < PackElements) |
| 846 | Pack.New.push_back(DeducedTemplateArgument()); |
Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 847 | if (Pack.New.size() == PackElements) |
| 848 | Pack.New.push_back(DeducedArg); |
| 849 | else |
| 850 | Pack.New[PackElements] = DeducedArg; |
| 851 | DeducedArg = Pack.New.size() > PackElements + 1 |
| 852 | ? Pack.New[PackElements + 1] |
| 853 | : DeducedTemplateArgument(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 854 | } |
| 855 | } |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 856 | ++PackElements; |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 859 | /// Finish template argument deduction for a set of argument packs, |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 860 | /// producing the argument packs and checking for consistency with prior |
| 861 | /// deductions. |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 862 | Sema::TemplateDeductionResult |
| 863 | finish(bool TreatNoDeductionsAsNonDeduced = true) { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 864 | // Build argument packs for each of the parameter packs expanded by this |
| 865 | // pack expansion. |
| 866 | for (auto &Pack : Packs) { |
| 867 | // Put back the old value for this pack. |
| 868 | Deduced[Pack.Index] = Pack.Saved; |
| 869 | |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 870 | // If we are deducing the size of this pack even if we didn't deduce any |
| 871 | // values for it, then make sure we build a pack of the right size. |
| 872 | // FIXME: Should we always deduce the size, even if the pack appears in |
| 873 | // a non-deduced context? |
| 874 | if (!TreatNoDeductionsAsNonDeduced) |
| 875 | Pack.New.resize(PackElements); |
| 876 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 877 | // Build or find a new value for this pack. |
| 878 | DeducedTemplateArgument NewPack; |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 879 | if (PackElements && Pack.New.empty()) { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 880 | if (Pack.DeferredDeduction.isNull()) { |
| 881 | // We were not able to deduce anything for this parameter pack |
| 882 | // (because it only appeared in non-deduced contexts), so just |
| 883 | // restore the saved argument pack. |
| 884 | continue; |
| 885 | } |
| 886 | |
| 887 | NewPack = Pack.DeferredDeduction; |
| 888 | Pack.DeferredDeduction = TemplateArgument(); |
| 889 | } else if (Pack.New.empty()) { |
| 890 | // If we deduced an empty argument pack, create it now. |
| 891 | NewPack = DeducedTemplateArgument(TemplateArgument::getEmptyPack()); |
| 892 | } else { |
| 893 | TemplateArgument *ArgumentPack = |
| 894 | new (S.Context) TemplateArgument[Pack.New.size()]; |
| 895 | std::copy(Pack.New.begin(), Pack.New.end(), ArgumentPack); |
| 896 | NewPack = DeducedTemplateArgument( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 897 | TemplateArgument(llvm::makeArrayRef(ArgumentPack, Pack.New.size())), |
Richard Smith | 7fa88bb | 2017-02-21 07:22:31 +0000 | [diff] [blame] | 898 | // FIXME: This is wrong, it's possible that some pack elements are |
| 899 | // deduced from an array bound and others are not: |
| 900 | // template<typename ...T, T ...V> void g(const T (&...p)[V]); |
| 901 | // g({1, 2, 3}, {{}, {}}); |
| 902 | // ... should deduce T = {int, size_t (from array bound)}. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 903 | Pack.New[0].wasDeducedFromArrayBound()); |
| 904 | } |
| 905 | |
| 906 | // Pick where we're going to put the merged pack. |
| 907 | DeducedTemplateArgument *Loc; |
| 908 | if (Pack.Outer) { |
| 909 | if (Pack.Outer->DeferredDeduction.isNull()) { |
| 910 | // Defer checking this pack until we have a complete pack to compare |
| 911 | // it against. |
| 912 | Pack.Outer->DeferredDeduction = NewPack; |
| 913 | continue; |
| 914 | } |
| 915 | Loc = &Pack.Outer->DeferredDeduction; |
| 916 | } else { |
| 917 | Loc = &Deduced[Pack.Index]; |
| 918 | } |
| 919 | |
| 920 | // Check the new pack matches any previous value. |
| 921 | DeducedTemplateArgument OldPack = *Loc; |
| 922 | DeducedTemplateArgument Result = |
| 923 | checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 924 | |
| 925 | // If we deferred a deduction of this pack, check that one now too. |
| 926 | if (!Result.isNull() && !Pack.DeferredDeduction.isNull()) { |
| 927 | OldPack = Result; |
| 928 | NewPack = Pack.DeferredDeduction; |
| 929 | Result = checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 930 | } |
| 931 | |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 932 | NamedDecl *Param = TemplateParams->getParam(Pack.Index); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 933 | if (Result.isNull()) { |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 934 | Info.Param = makeTemplateParameter(Param); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 935 | Info.FirstArg = OldPack; |
| 936 | Info.SecondArg = NewPack; |
| 937 | return Sema::TDK_Inconsistent; |
| 938 | } |
| 939 | |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 940 | // If we have a pre-expanded pack and we didn't deduce enough elements |
| 941 | // for it, fail deduction. |
| 942 | if (Optional<unsigned> Expansions = getExpandedPackSize(Param)) { |
| 943 | if (*Expansions != PackElements) { |
| 944 | Info.Param = makeTemplateParameter(Param); |
| 945 | Info.FirstArg = Result; |
| 946 | return Sema::TDK_IncompletePack; |
| 947 | } |
| 948 | } |
| 949 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 950 | *Loc = Result; |
| 951 | } |
| 952 | |
| 953 | return Sema::TDK_Success; |
| 954 | } |
| 955 | |
| 956 | private: |
| 957 | Sema &S; |
| 958 | TemplateParameterList *TemplateParams; |
| 959 | SmallVectorImpl<DeducedTemplateArgument> &Deduced; |
| 960 | TemplateDeductionInfo &Info; |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 961 | unsigned PackElements = 0; |
Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 962 | bool IsPartiallyExpanded = false; |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 963 | /// The number of expansions, if we have a fully-expanded pack in this scope. |
| 964 | Optional<unsigned> FixedNumExpansions; |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 965 | |
| 966 | SmallVector<DeducedPack, 2> Packs; |
| 967 | }; |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 968 | |
Benjamin Kramer | d5748c7 | 2015-03-23 12:31:05 +0000 | [diff] [blame] | 969 | } // namespace |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 970 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 971 | /// Deduce the template arguments by comparing the list of parameter |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 972 | /// types to the list of argument types, as in the parameter-type-lists of |
| 973 | /// function types (C++ [temp.deduct.type]p10). |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 974 | /// |
| 975 | /// \param S The semantic analysis object within which we are deducing |
| 976 | /// |
| 977 | /// \param TemplateParams The template parameters that we are deducing |
| 978 | /// |
| 979 | /// \param Params The list of parameter types |
| 980 | /// |
| 981 | /// \param NumParams The number of types in \c Params |
| 982 | /// |
| 983 | /// \param Args The list of argument types |
| 984 | /// |
| 985 | /// \param NumArgs The number of types in \c Args |
| 986 | /// |
| 987 | /// \param Info information about the template argument deduction itself |
| 988 | /// |
| 989 | /// \param Deduced the deduced template arguments |
| 990 | /// |
| 991 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
| 992 | /// how template argument deduction is performed. |
| 993 | /// |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 994 | /// \param PartialOrdering If true, we are performing template argument |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 995 | /// deduction for during partial ordering for a call |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 996 | /// (C++0x [temp.deduct.partial]). |
| 997 | /// |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 998 | /// \returns the result of template argument deduction so far. Note that a |
| 999 | /// "success" result means that template argument deduction has not yet failed, |
| 1000 | /// but it may still fail, later, for other reasons. |
| 1001 | static Sema::TemplateDeductionResult |
| 1002 | DeduceTemplateArguments(Sema &S, |
| 1003 | TemplateParameterList *TemplateParams, |
| 1004 | const QualType *Params, unsigned NumParams, |
| 1005 | const QualType *Args, unsigned NumArgs, |
| 1006 | TemplateDeductionInfo &Info, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 1007 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1008 | unsigned TDF, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1009 | bool PartialOrdering = false) { |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1010 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1011 | // Similarly, if P has a form that contains (T), then each parameter type |
| 1012 | // Pi of the respective parameter-type- list of P is compared with the |
| 1013 | // corresponding parameter type Ai of the corresponding parameter-type-list |
| 1014 | // of A. [...] |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1015 | unsigned ArgIdx = 0, ParamIdx = 0; |
| 1016 | for (; ParamIdx != NumParams; ++ParamIdx) { |
| 1017 | // Check argument types. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1018 | const PackExpansionType *Expansion |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1019 | = dyn_cast<PackExpansionType>(Params[ParamIdx]); |
| 1020 | if (!Expansion) { |
| 1021 | // Simple case: compare the parameter and argument types at this point. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1022 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1023 | // Make sure we have an argument. |
| 1024 | if (ArgIdx >= NumArgs) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 1025 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1026 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1027 | if (isa<PackExpansionType>(Args[ArgIdx])) { |
| 1028 | // C++0x [temp.deduct.type]p22: |
| 1029 | // If the original function parameter associated with A is a function |
| 1030 | // parameter pack and the function parameter associated with P is not |
| 1031 | // a function parameter pack, then template argument deduction fails. |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 1032 | return Sema::TDK_MiscellaneousDeductionFailure; |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1033 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1034 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1035 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1036 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1037 | Params[ParamIdx], Args[ArgIdx], |
| 1038 | Info, Deduced, TDF, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1039 | PartialOrdering)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1040 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1041 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1042 | ++ArgIdx; |
| 1043 | continue; |
| 1044 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1045 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1046 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1047 | // If the parameter-declaration corresponding to Pi is a function |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1048 | // parameter pack, then the type of its declarator- id is compared with |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1049 | // each remaining parameter type in the parameter-type-list of A. Each |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1050 | // comparison deduces template arguments for subsequent positions in the |
| 1051 | // template parameter packs expanded by the function parameter pack. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1052 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1053 | QualType Pattern = Expansion->getPattern(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1054 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1055 | |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 1056 | // A pack scope with fixed arity is not really a pack any more, so is not |
| 1057 | // a non-deduced context. |
| 1058 | if (ParamIdx + 1 == NumParams || PackScope.hasFixedArity()) { |
| 1059 | for (; ArgIdx < NumArgs && PackScope.hasNextElement(); ++ArgIdx) { |
Richard Smith | c379d1a | 2018-07-12 23:32:39 +0000 | [diff] [blame] | 1060 | // Deduce template arguments from the pattern. |
| 1061 | if (Sema::TemplateDeductionResult Result |
| 1062 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, Pattern, |
| 1063 | Args[ArgIdx], Info, Deduced, |
| 1064 | TDF, PartialOrdering)) |
| 1065 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1066 | |
Richard Smith | c379d1a | 2018-07-12 23:32:39 +0000 | [diff] [blame] | 1067 | PackScope.nextPackElement(); |
| 1068 | } |
| 1069 | } else { |
| 1070 | // C++0x [temp.deduct.type]p5: |
| 1071 | // The non-deduced contexts are: |
| 1072 | // - A function parameter pack that does not occur at the end of the |
| 1073 | // parameter-declaration-clause. |
| 1074 | // |
| 1075 | // FIXME: There is no wording to say what we should do in this case. We |
| 1076 | // choose to resolve this by applying the same rule that is applied for a |
| 1077 | // function call: that is, deduce all contained packs to their |
| 1078 | // explicitly-specified values (or to <> if there is no such value). |
| 1079 | // |
| 1080 | // This is seemingly-arbitrarily different from the case of a template-id |
| 1081 | // with a non-trailing pack-expansion in its arguments, which renders the |
| 1082 | // entire template-argument-list a non-deduced context. |
| 1083 | |
| 1084 | // If the parameter type contains an explicitly-specified pack that we |
| 1085 | // could not expand, skip the number of parameters notionally created |
| 1086 | // by the expansion. |
| 1087 | Optional<unsigned> NumExpansions = Expansion->getNumExpansions(); |
| 1088 | if (NumExpansions && !PackScope.isPartiallyExpanded()) { |
| 1089 | for (unsigned I = 0; I != *NumExpansions && ArgIdx < NumArgs; |
| 1090 | ++I, ++ArgIdx) |
| 1091 | PackScope.nextPackElement(); |
| 1092 | } |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1093 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1094 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1095 | // Build argument packs for each of the parameter packs expanded by this |
| 1096 | // pack expansion. |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 1097 | if (auto Result = PackScope.finish()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1098 | return Result; |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1099 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1100 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1101 | // Make sure we don't have any extra arguments. |
| 1102 | if (ArgIdx < NumArgs) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 1103 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1104 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1105 | return Sema::TDK_Success; |
| 1106 | } |
| 1107 | |
Richard Smith | 34c3250 | 2018-07-11 21:07:04 +0000 | [diff] [blame] | 1108 | /// Determine whether the parameter has qualifiers that the argument |
| 1109 | /// lacks. Put another way, determine whether there is no way to add |
| 1110 | /// a deduced set of qualifiers to the ParamType that would result in |
| 1111 | /// its qualifiers matching those of the ArgType. |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1112 | static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType, |
| 1113 | QualType ArgType) { |
| 1114 | Qualifiers ParamQs = ParamType.getQualifiers(); |
| 1115 | Qualifiers ArgQs = ArgType.getQualifiers(); |
| 1116 | |
| 1117 | if (ParamQs == ArgQs) |
| 1118 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1119 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1120 | // Mismatched (but not missing) Objective-C GC attributes. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1121 | if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() && |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1122 | ParamQs.hasObjCGCAttr()) |
| 1123 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1124 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1125 | // Mismatched (but not missing) address spaces. |
| 1126 | if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() && |
| 1127 | ParamQs.hasAddressSpace()) |
| 1128 | return true; |
| 1129 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1130 | // Mismatched (but not missing) Objective-C lifetime qualifiers. |
| 1131 | if (ParamQs.getObjCLifetime() != ArgQs.getObjCLifetime() && |
| 1132 | ParamQs.hasObjCLifetime()) |
| 1133 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1134 | |
Richard Smith | 34c3250 | 2018-07-11 21:07:04 +0000 | [diff] [blame] | 1135 | // CVR qualifiers inconsistent or a superset. |
| 1136 | return (ParamQs.getCVRQualifiers() & ~ArgQs.getCVRQualifiers()) != 0; |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1139 | /// Compare types for equality with respect to possibly compatible |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1140 | /// function types (noreturn adjustment, implicit calling conventions). If any |
| 1141 | /// of parameter and argument is not a function, just perform type comparison. |
| 1142 | /// |
| 1143 | /// \param Param the template parameter type. |
| 1144 | /// |
| 1145 | /// \param Arg the argument type. |
| 1146 | bool Sema::isSameOrCompatibleFunctionType(CanQualType Param, |
| 1147 | CanQualType Arg) { |
| 1148 | const FunctionType *ParamFunction = Param->getAs<FunctionType>(), |
| 1149 | *ArgFunction = Arg->getAs<FunctionType>(); |
| 1150 | |
| 1151 | // Just compare if not functions. |
| 1152 | if (!ParamFunction || !ArgFunction) |
| 1153 | return Param == Arg; |
| 1154 | |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 1155 | // Noreturn and noexcept adjustment. |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1156 | QualType AdjustedParam; |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 1157 | if (IsFunctionConversion(Param, Arg, AdjustedParam)) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1158 | return Arg == Context.getCanonicalType(AdjustedParam); |
| 1159 | |
| 1160 | // FIXME: Compatible calling conventions. |
| 1161 | |
| 1162 | return Param == Arg; |
| 1163 | } |
| 1164 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1165 | /// Get the index of the first template parameter that was originally from the |
| 1166 | /// innermost template-parameter-list. This is 0 except when we concatenate |
| 1167 | /// the template parameter lists of a class template and a constructor template |
| 1168 | /// when forming an implicit deduction guide. |
| 1169 | static unsigned getFirstInnerIndex(FunctionTemplateDecl *FTD) { |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1170 | auto *Guide = dyn_cast<CXXDeductionGuideDecl>(FTD->getTemplatedDecl()); |
| 1171 | if (!Guide || !Guide->isImplicit()) |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1172 | return 0; |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1173 | return Guide->getDeducedTemplate()->getTemplateParameters()->size(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | /// Determine whether a type denotes a forwarding reference. |
| 1177 | static bool isForwardingReference(QualType Param, unsigned FirstInnerIndex) { |
| 1178 | // C++1z [temp.deduct.call]p3: |
| 1179 | // A forwarding reference is an rvalue reference to a cv-unqualified |
| 1180 | // template parameter that does not represent a template parameter of a |
| 1181 | // class template. |
| 1182 | if (auto *ParamRef = Param->getAs<RValueReferenceType>()) { |
| 1183 | if (ParamRef->getPointeeType().getQualifiers()) |
| 1184 | return false; |
| 1185 | auto *TypeParm = ParamRef->getPointeeType()->getAs<TemplateTypeParmType>(); |
| 1186 | return TypeParm && TypeParm->getIndex() >= FirstInnerIndex; |
| 1187 | } |
| 1188 | return false; |
| 1189 | } |
| 1190 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 1191 | /// Deduce the template arguments by comparing the parameter type and |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1192 | /// the argument type (C++ [temp.deduct.type]). |
| 1193 | /// |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1194 | /// \param S the semantic analysis object within which we are deducing |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1195 | /// |
| 1196 | /// \param TemplateParams the template parameters that we are deducing |
| 1197 | /// |
| 1198 | /// \param ParamIn the parameter type |
| 1199 | /// |
| 1200 | /// \param ArgIn the argument type |
| 1201 | /// |
| 1202 | /// \param Info information about the template argument deduction itself |
| 1203 | /// |
| 1204 | /// \param Deduced the deduced template arguments |
| 1205 | /// |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1206 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1207 | /// how template argument deduction is performed. |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1208 | /// |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1209 | /// \param PartialOrdering Whether we're performing template argument deduction |
| 1210 | /// in the context of partial ordering (C++0x [temp.deduct.partial]). |
| 1211 | /// |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1212 | /// \returns the result of template argument deduction so far. Note that a |
| 1213 | /// "success" result means that template argument deduction has not yet failed, |
| 1214 | /// but it may still fail, later, for other reasons. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1215 | static Sema::TemplateDeductionResult |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1216 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 1217 | TemplateParameterList *TemplateParams, |
| 1218 | QualType ParamIn, QualType ArgIn, |
| 1219 | TemplateDeductionInfo &Info, |
| 1220 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 1221 | unsigned TDF, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1222 | bool PartialOrdering, |
| 1223 | bool DeducedFromArrayBound) { |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1224 | // We only want to look at the canonical types, since typedefs and |
| 1225 | // sugar are not part of template argument deduction. |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1226 | QualType Param = S.Context.getCanonicalType(ParamIn); |
| 1227 | QualType Arg = S.Context.getCanonicalType(ArgIn); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1228 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1229 | // If the argument type is a pack expansion, look at its pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1230 | // This isn't explicitly called out |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1231 | if (const PackExpansionType *ArgExpansion |
| 1232 | = dyn_cast<PackExpansionType>(Arg)) |
| 1233 | Arg = ArgExpansion->getPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1234 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1235 | if (PartialOrdering) { |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1236 | // C++11 [temp.deduct.partial]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1237 | // Before the partial ordering is done, certain transformations are |
| 1238 | // performed on the types used for partial ordering: |
| 1239 | // - If P is a reference type, P is replaced by the type referred to. |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1240 | const ReferenceType *ParamRef = Param->getAs<ReferenceType>(); |
| 1241 | if (ParamRef) |
| 1242 | Param = ParamRef->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1243 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1244 | // - If A is a reference type, A is replaced by the type referred to. |
| 1245 | const ReferenceType *ArgRef = Arg->getAs<ReferenceType>(); |
| 1246 | if (ArgRef) |
| 1247 | Arg = ArgRef->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1248 | |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1249 | if (ParamRef && ArgRef && S.Context.hasSameUnqualifiedType(Param, Arg)) { |
| 1250 | // C++11 [temp.deduct.partial]p9: |
| 1251 | // If, for a given type, deduction succeeds in both directions (i.e., |
| 1252 | // the types are identical after the transformations above) and both |
| 1253 | // P and A were reference types [...]: |
| 1254 | // - if [one type] was an lvalue reference and [the other type] was |
| 1255 | // not, [the other type] is not considered to be at least as |
| 1256 | // specialized as [the first type] |
| 1257 | // - if [one type] is more cv-qualified than [the other type], |
| 1258 | // [the other type] is not considered to be at least as specialized |
| 1259 | // as [the first type] |
| 1260 | // Objective-C ARC adds: |
| 1261 | // - [one type] has non-trivial lifetime, [the other type] has |
| 1262 | // __unsafe_unretained lifetime, and the types are otherwise |
| 1263 | // identical |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1264 | // |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1265 | // A is "considered to be at least as specialized" as P iff deduction |
| 1266 | // succeeds, so we model this as a deduction failure. Note that |
| 1267 | // [the first type] is P and [the other type] is A here; the standard |
| 1268 | // gets this backwards. |
Douglas Gregor | 85894a8 | 2011-04-30 17:07:52 +0000 | [diff] [blame] | 1269 | Qualifiers ParamQuals = Param.getQualifiers(); |
| 1270 | Qualifiers ArgQuals = Arg.getQualifiers(); |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1271 | if ((ParamRef->isLValueReferenceType() && |
| 1272 | !ArgRef->isLValueReferenceType()) || |
| 1273 | ParamQuals.isStrictSupersetOf(ArgQuals) || |
| 1274 | (ParamQuals.hasNonTrivialObjCLifetime() && |
| 1275 | ArgQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone && |
| 1276 | ParamQuals.withoutObjCLifetime() == |
| 1277 | ArgQuals.withoutObjCLifetime())) { |
| 1278 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1279 | Info.SecondArg = TemplateArgument(ArgIn); |
| 1280 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 6beabee | 2014-01-02 19:42:02 +0000 | [diff] [blame] | 1281 | } |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1282 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1283 | |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1284 | // C++11 [temp.deduct.partial]p7: |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1285 | // Remove any top-level cv-qualifiers: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1286 | // - If P is a cv-qualified type, P is replaced by the cv-unqualified |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1287 | // version of P. |
| 1288 | Param = Param.getUnqualifiedType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1289 | // - If A is a cv-qualified type, A is replaced by the cv-unqualified |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1290 | // version of A. |
| 1291 | Arg = Arg.getUnqualifiedType(); |
| 1292 | } else { |
| 1293 | // C++0x [temp.deduct.call]p4 bullet 1: |
| 1294 | // - If the original P is a reference type, the deduced A (i.e., the type |
| 1295 | // referred to by the reference) can be more cv-qualified than the |
| 1296 | // transformed A. |
| 1297 | if (TDF & TDF_ParamWithReferenceType) { |
| 1298 | Qualifiers Quals; |
| 1299 | QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals); |
| 1300 | Quals.setCVRQualifiers(Quals.getCVRQualifiers() & |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 1301 | Arg.getCVRQualifiers()); |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1302 | Param = S.Context.getQualifiedType(UnqualParam, Quals); |
| 1303 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1304 | |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1305 | if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) { |
| 1306 | // C++0x [temp.deduct.type]p10: |
| 1307 | // If P and A are function types that originated from deduction when |
| 1308 | // taking the address of a function template (14.8.2.2) or when deducing |
| 1309 | // template arguments from a function declaration (14.8.2.6) and Pi and |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1310 | // Ai are parameters of the top-level parameter-type-list of P and A, |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1311 | // respectively, Pi is adjusted if it is a forwarding reference and Ai |
| 1312 | // is an lvalue reference, in |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1313 | // which case the type of Pi is changed to be the template parameter |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1314 | // type (i.e., T&& is changed to simply T). [ Note: As a result, when |
| 1315 | // Pi is T&& and Ai is X&, the adjusted Pi will be T, causing T to be |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1316 | // deduced as X&. - end note ] |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1317 | TDF &= ~TDF_TopLevelParameterTypeList; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1318 | if (isForwardingReference(Param, 0) && Arg->isLValueReferenceType()) |
| 1319 | Param = Param->getPointeeType(); |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1320 | } |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1321 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1322 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1323 | // C++ [temp.deduct.type]p9: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1324 | // A template type argument T, a template template argument TT or a |
| 1325 | // template non-type argument i can be deduced if P and A have one of |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1326 | // the following forms: |
| 1327 | // |
| 1328 | // T |
| 1329 | // cv-list T |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1330 | if (const TemplateTypeParmType *TemplateTypeParm |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1331 | = Param->getAs<TemplateTypeParmType>()) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1332 | // Just skip any attempts to deduce from a placeholder type or a parameter |
| 1333 | // at a different depth. |
| 1334 | if (Arg->isPlaceholderType() || |
| 1335 | Info.getDeducedDepth() != TemplateTypeParm->getDepth()) |
Douglas Gregor | 4ea5dec | 2011-09-22 15:57:07 +0000 | [diff] [blame] | 1336 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1337 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1338 | unsigned Index = TemplateTypeParm->getIndex(); |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1339 | bool RecanonicalizeArg = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1340 | |
Douglas Gregor | 6045482 | 2009-07-22 20:02:25 +0000 | [diff] [blame] | 1341 | // If the argument type is an array type, move the qualifiers up to the |
| 1342 | // top level, so they can be matched with the qualifiers on the parameter. |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1343 | if (isa<ArrayType>(Arg)) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1344 | Qualifiers Quals; |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1345 | Arg = S.Context.getUnqualifiedArrayType(Arg, Quals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1346 | if (Quals) { |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1347 | Arg = S.Context.getQualifiedType(Arg, Quals); |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1348 | RecanonicalizeArg = true; |
| 1349 | } |
| 1350 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1351 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1352 | // The argument type can not be less qualified than the parameter |
| 1353 | // type. |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1354 | if (!(TDF & TDF_IgnoreQualifiers) && |
| 1355 | hasInconsistentOrSupersetQualifiersOf(Param, Arg)) { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1356 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1357 | Info.FirstArg = TemplateArgument(Param); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1358 | Info.SecondArg = TemplateArgument(Arg); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1359 | return Sema::TDK_Underqualified; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1360 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1361 | |
Lei Liu | 413f3c5 | 2018-05-03 01:43:23 +0000 | [diff] [blame] | 1362 | // Do not match a function type with a cv-qualified type. |
| 1363 | // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1584 |
| 1364 | if (Arg->isFunctionType() && Param.hasQualifiers()) { |
| 1365 | return Sema::TDK_NonDeducedMismatch; |
| 1366 | } |
| 1367 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1368 | assert(TemplateTypeParm->getDepth() == Info.getDeducedDepth() && |
| 1369 | "saw template type parameter with wrong depth"); |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1370 | assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function"); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1371 | QualType DeducedType = Arg; |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1372 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1373 | // Remove any qualifiers on the parameter from the deduced type. |
| 1374 | // We checked the qualifiers for consistency above. |
| 1375 | Qualifiers DeducedQs = DeducedType.getQualifiers(); |
| 1376 | Qualifiers ParamQs = Param.getQualifiers(); |
| 1377 | DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers()); |
| 1378 | if (ParamQs.hasObjCGCAttr()) |
| 1379 | DeducedQs.removeObjCGCAttr(); |
| 1380 | if (ParamQs.hasAddressSpace()) |
| 1381 | DeducedQs.removeAddressSpace(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1382 | if (ParamQs.hasObjCLifetime()) |
| 1383 | DeducedQs.removeObjCLifetime(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1384 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1385 | // Objective-C ARC: |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1386 | // If template deduction would produce a lifetime qualifier on a type |
| 1387 | // that is not a lifetime type, template argument deduction fails. |
| 1388 | if (ParamQs.hasObjCLifetime() && !DeducedType->isObjCLifetimeType() && |
| 1389 | !DeducedType->isDependentType()) { |
| 1390 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1391 | Info.FirstArg = TemplateArgument(Param); |
| 1392 | Info.SecondArg = TemplateArgument(Arg); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1393 | return Sema::TDK_Underqualified; |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1394 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1395 | |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1396 | // Objective-C ARC: |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1397 | // If template deduction would produce an argument type with lifetime type |
| 1398 | // but no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1399 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1400 | DeducedType->isObjCLifetimeType() && |
| 1401 | !DeducedQs.hasObjCLifetime()) |
| 1402 | DeducedQs.setObjCLifetime(Qualifiers::OCL_Strong); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1403 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1404 | DeducedType = S.Context.getQualifiedType(DeducedType.getUnqualifiedType(), |
| 1405 | DeducedQs); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1406 | |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1407 | if (RecanonicalizeArg) |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1408 | DeducedType = S.Context.getCanonicalType(DeducedType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1409 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1410 | DeducedTemplateArgument NewDeduced(DeducedType, DeducedFromArrayBound); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1411 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1412 | Deduced[Index], |
| 1413 | NewDeduced); |
| 1414 | if (Result.isNull()) { |
| 1415 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1416 | Info.FirstArg = Deduced[Index]; |
| 1417 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1418 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1419 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1420 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1421 | Deduced[Index] = Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1422 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1425 | // Set up the template argument deduction information for a failure. |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1426 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1427 | Info.SecondArg = TemplateArgument(ArgIn); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1428 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 1429 | // If the parameter is an already-substituted template parameter |
| 1430 | // pack, do nothing: we don't know which of its arguments to look |
| 1431 | // at, so we have to wait until all of the parameter packs in this |
| 1432 | // expansion have arguments. |
| 1433 | if (isa<SubstTemplateTypeParmPackType>(Param)) |
| 1434 | return Sema::TDK_Success; |
| 1435 | |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1436 | // Check the cv-qualifiers on the parameter and argument types. |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1437 | CanQualType CanParam = S.Context.getCanonicalType(Param); |
| 1438 | CanQualType CanArg = S.Context.getCanonicalType(Arg); |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1439 | if (!(TDF & TDF_IgnoreQualifiers)) { |
| 1440 | if (TDF & TDF_ParamWithReferenceType) { |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1441 | if (hasInconsistentOrSupersetQualifiersOf(Param, Arg)) |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1442 | return Sema::TDK_NonDeducedMismatch; |
Richard Smith | b884ed1 | 2018-07-11 23:19:41 +0000 | [diff] [blame] | 1443 | } else if (TDF & TDF_ArgWithReferenceType) { |
| 1444 | // C++ [temp.deduct.conv]p4: |
| 1445 | // If the original A is a reference type, A can be more cv-qualified |
| 1446 | // than the deduced A |
| 1447 | if (!Arg.getQualifiers().compatiblyIncludes(Param.getQualifiers())) |
| 1448 | return Sema::TDK_NonDeducedMismatch; |
| 1449 | |
| 1450 | // Strip out all extra qualifiers from the argument to figure out the |
| 1451 | // type we're converting to, prior to the qualification conversion. |
| 1452 | Qualifiers Quals; |
| 1453 | Arg = S.Context.getUnqualifiedArrayType(Arg, Quals); |
| 1454 | Arg = S.Context.getQualifiedType(Arg, Param.getQualifiers()); |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 1455 | } else if (!IsPossiblyOpaquelyQualifiedType(Param)) { |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1456 | if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1457 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1458 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1459 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1460 | // If the parameter type is not dependent, there is nothing to deduce. |
| 1461 | if (!Param->isDependentType()) { |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1462 | if (!(TDF & TDF_SkipNonDependent)) { |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 1463 | bool NonDeduced = |
| 1464 | (TDF & TDF_AllowCompatibleFunctionType) |
| 1465 | ? !S.isSameOrCompatibleFunctionType(CanParam, CanArg) |
| 1466 | : Param != Arg; |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1467 | if (NonDeduced) { |
| 1468 | return Sema::TDK_NonDeducedMismatch; |
| 1469 | } |
| 1470 | } |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1471 | return Sema::TDK_Success; |
| 1472 | } |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1473 | } else if (!Param->isDependentType()) { |
| 1474 | CanQualType ParamUnqualType = CanParam.getUnqualifiedType(), |
| 1475 | ArgUnqualType = CanArg.getUnqualifiedType(); |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 1476 | bool Success = |
| 1477 | (TDF & TDF_AllowCompatibleFunctionType) |
| 1478 | ? S.isSameOrCompatibleFunctionType(ParamUnqualType, ArgUnqualType) |
| 1479 | : ParamUnqualType == ArgUnqualType; |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1480 | if (Success) |
| 1481 | return Sema::TDK_Success; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1482 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1483 | |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1484 | switch (Param->getTypeClass()) { |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1485 | // Non-canonical types cannot appear here. |
| 1486 | #define NON_CANONICAL_TYPE(Class, Base) \ |
| 1487 | case Type::Class: llvm_unreachable("deducing non-canonical type: " #Class); |
| 1488 | #define TYPE(Class, Base) |
| 1489 | #include "clang/AST/TypeNodes.def" |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1490 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1491 | case Type::TemplateTypeParm: |
| 1492 | case Type::SubstTemplateTypeParmPack: |
| 1493 | llvm_unreachable("Type nodes handled above"); |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1494 | |
| 1495 | // These types cannot be dependent, so simply check whether the types are |
| 1496 | // the same. |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1497 | case Type::Builtin: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1498 | case Type::VariableArray: |
| 1499 | case Type::Vector: |
| 1500 | case Type::FunctionNoProto: |
| 1501 | case Type::Record: |
| 1502 | case Type::Enum: |
| 1503 | case Type::ObjCObject: |
| 1504 | case Type::ObjCInterface: |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 1505 | case Type::ObjCObjectPointer: |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1506 | if (TDF & TDF_SkipNonDependent) |
| 1507 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1508 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1509 | if (TDF & TDF_IgnoreQualifiers) { |
| 1510 | Param = Param.getUnqualifiedType(); |
| 1511 | Arg = Arg.getUnqualifiedType(); |
| 1512 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1513 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1514 | return Param == Arg? Sema::TDK_Success : Sema::TDK_NonDeducedMismatch; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1515 | |
| 1516 | // _Complex T [placeholder extension] |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1517 | case Type::Complex: |
| 1518 | if (const ComplexType *ComplexArg = Arg->getAs<ComplexType>()) |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1519 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1520 | cast<ComplexType>(Param)->getElementType(), |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1521 | ComplexArg->getElementType(), |
| 1522 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1523 | |
| 1524 | return Sema::TDK_NonDeducedMismatch; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1525 | |
| 1526 | // _Atomic T [extension] |
| 1527 | case Type::Atomic: |
| 1528 | if (const AtomicType *AtomicArg = Arg->getAs<AtomicType>()) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1529 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1530 | cast<AtomicType>(Param)->getValueType(), |
| 1531 | AtomicArg->getValueType(), |
| 1532 | Info, Deduced, TDF); |
| 1533 | |
| 1534 | return Sema::TDK_NonDeducedMismatch; |
| 1535 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1536 | // T * |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1537 | case Type::Pointer: { |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1538 | QualType PointeeType; |
| 1539 | if (const PointerType *PointerArg = Arg->getAs<PointerType>()) { |
| 1540 | PointeeType = PointerArg->getPointeeType(); |
| 1541 | } else if (const ObjCObjectPointerType *PointerArg |
| 1542 | = Arg->getAs<ObjCObjectPointerType>()) { |
| 1543 | PointeeType = PointerArg->getPointeeType(); |
| 1544 | } else { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1545 | return Sema::TDK_NonDeducedMismatch; |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1546 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1547 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1548 | unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1549 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1550 | cast<PointerType>(Param)->getPointeeType(), |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1551 | PointeeType, |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1552 | Info, Deduced, SubTDF); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1553 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1554 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1555 | // T & |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1556 | case Type::LValueReference: { |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 1557 | const LValueReferenceType *ReferenceArg = |
| 1558 | Arg->getAs<LValueReferenceType>(); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1559 | if (!ReferenceArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1560 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1561 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1562 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1563 | cast<LValueReferenceType>(Param)->getPointeeType(), |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1564 | ReferenceArg->getPointeeType(), Info, Deduced, 0); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1565 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1566 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1567 | // T && [C++0x] |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1568 | case Type::RValueReference: { |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 1569 | const RValueReferenceType *ReferenceArg = |
| 1570 | Arg->getAs<RValueReferenceType>(); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1571 | if (!ReferenceArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1572 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1573 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1574 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1575 | cast<RValueReferenceType>(Param)->getPointeeType(), |
| 1576 | ReferenceArg->getPointeeType(), |
| 1577 | Info, Deduced, 0); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1578 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1579 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1580 | // T [] (implied, but not stated explicitly) |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1581 | case Type::IncompleteArray: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1582 | const IncompleteArrayType *IncompleteArrayArg = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1583 | S.Context.getAsIncompleteArrayType(Arg); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1584 | if (!IncompleteArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1585 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1586 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1587 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1588 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1589 | S.Context.getAsIncompleteArrayType(Param)->getElementType(), |
| 1590 | IncompleteArrayArg->getElementType(), |
| 1591 | Info, Deduced, SubTDF); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1592 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1593 | |
| 1594 | // T [integer-constant] |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1595 | case Type::ConstantArray: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1596 | const ConstantArrayType *ConstantArrayArg = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1597 | S.Context.getAsConstantArrayType(Arg); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1598 | if (!ConstantArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1599 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1600 | |
| 1601 | const ConstantArrayType *ConstantArrayParm = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1602 | S.Context.getAsConstantArrayType(Param); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1603 | if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1604 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1605 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1606 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1607 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1608 | ConstantArrayParm->getElementType(), |
| 1609 | ConstantArrayArg->getElementType(), |
| 1610 | Info, Deduced, SubTDF); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1613 | // type [i] |
| 1614 | case Type::DependentSizedArray: { |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1615 | const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1616 | if (!ArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1617 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1618 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1619 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
| 1620 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1621 | // Check the element type of the arrays |
| 1622 | const DependentSizedArrayType *DependentArrayParm |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1623 | = S.Context.getAsDependentSizedArrayType(Param); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1624 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1625 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1626 | DependentArrayParm->getElementType(), |
| 1627 | ArrayArg->getElementType(), |
| 1628 | Info, Deduced, SubTDF)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1629 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1630 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1631 | // Determine the array bound is something we can deduce. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1632 | NonTypeTemplateParmDecl *NTTP |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1633 | = getDeducedParameterFromExpr(Info, DependentArrayParm->getSizeExpr()); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1634 | if (!NTTP) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1635 | return Sema::TDK_Success; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1636 | |
| 1637 | // We can perform template argument deduction for the given non-type |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1638 | // template parameter. |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1639 | assert(NTTP->getDepth() == Info.getDeducedDepth() && |
| 1640 | "saw non-type template parameter with wrong depth"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1641 | if (const ConstantArrayType *ConstantArrayArg |
Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1642 | = dyn_cast<ConstantArrayType>(ArrayArg)) { |
| 1643 | llvm::APSInt Size(ConstantArrayArg->getSize()); |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1644 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, Size, |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1645 | S.Context.getSizeType(), |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1646 | /*ArrayBound=*/true, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1647 | Info, Deduced); |
Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1648 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1649 | if (const DependentSizedArrayType *DependentArrayArg |
| 1650 | = dyn_cast<DependentSizedArrayType>(ArrayArg)) |
Douglas Gregor | 7a49ead | 2010-12-22 23:15:38 +0000 | [diff] [blame] | 1651 | if (DependentArrayArg->getSizeExpr()) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1652 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
Douglas Gregor | 7a49ead | 2010-12-22 23:15:38 +0000 | [diff] [blame] | 1653 | DependentArrayArg->getSizeExpr(), |
| 1654 | Info, Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1655 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1656 | // Incomplete type does not match a dependently-sized array type |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1657 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1658 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1659 | |
| 1660 | // type(*)(T) |
| 1661 | // T(*)() |
| 1662 | // T(*)(T) |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1663 | case Type::FunctionProto: { |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1664 | unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1665 | const FunctionProtoType *FunctionProtoArg = |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1666 | dyn_cast<FunctionProtoType>(Arg); |
| 1667 | if (!FunctionProtoArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1668 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1669 | |
| 1670 | const FunctionProtoType *FunctionProtoParam = |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1671 | cast<FunctionProtoType>(Param); |
Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1672 | |
Anastasia Stulova | c61eaa5 | 2019-01-28 11:37:49 +0000 | [diff] [blame] | 1673 | if (FunctionProtoParam->getMethodQuals() |
| 1674 | != FunctionProtoArg->getMethodQuals() || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1675 | FunctionProtoParam->getRefQualifier() |
Douglas Gregor | 54e462a | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1676 | != FunctionProtoArg->getRefQualifier() || |
| 1677 | FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1678 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1679 | |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1680 | // Check return types. |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 1681 | if (auto Result = DeduceTemplateArgumentsByTypeMatch( |
| 1682 | S, TemplateParams, FunctionProtoParam->getReturnType(), |
| 1683 | FunctionProtoArg->getReturnType(), Info, Deduced, 0)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1684 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1685 | |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 1686 | // Check parameter types. |
| 1687 | if (auto Result = DeduceTemplateArguments( |
| 1688 | S, TemplateParams, FunctionProtoParam->param_type_begin(), |
| 1689 | FunctionProtoParam->getNumParams(), |
| 1690 | FunctionProtoArg->param_type_begin(), |
| 1691 | FunctionProtoArg->getNumParams(), Info, Deduced, SubTDF)) |
| 1692 | return Result; |
| 1693 | |
| 1694 | if (TDF & TDF_AllowCompatibleFunctionType) |
| 1695 | return Sema::TDK_Success; |
| 1696 | |
| 1697 | // FIXME: Per core-2016/10/1019 (no corresponding core issue yet), permit |
| 1698 | // deducing through the noexcept-specifier if it's part of the canonical |
| 1699 | // type. libstdc++ relies on this. |
| 1700 | Expr *NoexceptExpr = FunctionProtoParam->getNoexceptExpr(); |
| 1701 | if (NonTypeTemplateParmDecl *NTTP = |
| 1702 | NoexceptExpr ? getDeducedParameterFromExpr(Info, NoexceptExpr) |
| 1703 | : nullptr) { |
| 1704 | assert(NTTP->getDepth() == Info.getDeducedDepth() && |
| 1705 | "saw non-type template parameter with wrong depth"); |
| 1706 | |
| 1707 | llvm::APSInt Noexcept(1); |
Richard Smith | eaf11ad | 2018-05-03 03:58:32 +0000 | [diff] [blame] | 1708 | switch (FunctionProtoArg->canThrow()) { |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 1709 | case CT_Cannot: |
| 1710 | Noexcept = 1; |
| 1711 | LLVM_FALLTHROUGH; |
| 1712 | |
| 1713 | case CT_Can: |
| 1714 | // We give E in noexcept(E) the "deduced from array bound" treatment. |
| 1715 | // FIXME: Should we? |
| 1716 | return DeduceNonTypeTemplateArgument( |
| 1717 | S, TemplateParams, NTTP, Noexcept, S.Context.BoolTy, |
| 1718 | /*ArrayBound*/true, Info, Deduced); |
| 1719 | |
| 1720 | case CT_Dependent: |
| 1721 | if (Expr *ArgNoexceptExpr = FunctionProtoArg->getNoexceptExpr()) |
| 1722 | return DeduceNonTypeTemplateArgument( |
| 1723 | S, TemplateParams, NTTP, ArgNoexceptExpr, Info, Deduced); |
| 1724 | // Can't deduce anything from throw(T...). |
| 1725 | break; |
| 1726 | } |
| 1727 | } |
| 1728 | // FIXME: Detect non-deduced exception specification mismatches? |
Richard Smith | 746e35e | 2018-07-12 18:49:13 +0000 | [diff] [blame] | 1729 | // |
| 1730 | // Careful about [temp.deduct.call] and [temp.deduct.conv], which allow |
| 1731 | // top-level differences in noexcept-specifications. |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 1732 | |
| 1733 | return Sema::TDK_Success; |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1734 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1735 | |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 1736 | case Type::InjectedClassName: |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1737 | // Treat a template's injected-class-name as if the template |
| 1738 | // specialization type had been used. |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1739 | Param = cast<InjectedClassNameType>(Param) |
| 1740 | ->getInjectedSpecializationType(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1741 | assert(isa<TemplateSpecializationType>(Param) && |
| 1742 | "injected class name is not a template specialization type"); |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 1743 | LLVM_FALLTHROUGH; |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1744 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1745 | // template-name<T> (where template-name refers to a class template) |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1746 | // template-name<i> |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1747 | // TT<T> |
| 1748 | // TT<i> |
| 1749 | // TT<> |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1750 | case Type::TemplateSpecialization: { |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1751 | const TemplateSpecializationType *SpecParam = |
| 1752 | cast<TemplateSpecializationType>(Param); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1753 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1754 | // When Arg cannot be a derived class, we can just try to deduce template |
| 1755 | // arguments from the template-id. |
| 1756 | const RecordType *RecordT = Arg->getAs<RecordType>(); |
| 1757 | if (!(TDF & TDF_DerivedClass) || !RecordT) |
| 1758 | return DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg, Info, |
| 1759 | Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1760 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1761 | SmallVector<DeducedTemplateArgument, 8> DeducedOrig(Deduced.begin(), |
| 1762 | Deduced.end()); |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1763 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1764 | Sema::TemplateDeductionResult Result = DeduceTemplateArguments( |
| 1765 | S, TemplateParams, SpecParam, Arg, Info, Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1766 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1767 | if (Result == Sema::TDK_Success) |
| 1768 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1769 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1770 | // We cannot inspect base classes as part of deduction when the type |
| 1771 | // is incomplete, so either instantiate any templates necessary to |
| 1772 | // complete the type, or skip over it if it cannot be completed. |
| 1773 | if (!S.isCompleteType(Info.getLocation(), Arg)) |
| 1774 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1775 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1776 | // C++14 [temp.deduct.call] p4b3: |
| 1777 | // If P is a class and P has the form simple-template-id, then the |
| 1778 | // transformed A can be a derived class of the deduced A. Likewise if |
| 1779 | // P is a pointer to a class of the form simple-template-id, the |
| 1780 | // transformed A can be a pointer to a derived class pointed to by the |
| 1781 | // deduced A. |
| 1782 | // |
| 1783 | // These alternatives are considered only if type deduction would |
| 1784 | // otherwise fail. If they yield more than one possible deduced A, the |
| 1785 | // type deduction fails. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1786 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1787 | // Reset the incorrectly deduced argument from above. |
| 1788 | Deduced = DeducedOrig; |
| 1789 | |
| 1790 | // Use data recursion to crawl through the list of base classes. |
| 1791 | // Visited contains the set of nodes we have already visited, while |
| 1792 | // ToVisit is our stack of records that we still need to visit. |
| 1793 | llvm::SmallPtrSet<const RecordType *, 8> Visited; |
| 1794 | SmallVector<const RecordType *, 8> ToVisit; |
| 1795 | ToVisit.push_back(RecordT); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1796 | bool Successful = false; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1797 | SmallVector<DeducedTemplateArgument, 8> SuccessfulDeduced; |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1798 | while (!ToVisit.empty()) { |
| 1799 | // Retrieve the next class in the inheritance hierarchy. |
| 1800 | const RecordType *NextT = ToVisit.pop_back_val(); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1801 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1802 | // If we have already seen this type, skip it. |
| 1803 | if (!Visited.insert(NextT).second) |
| 1804 | continue; |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1805 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1806 | // If this is a base class, try to perform template argument |
| 1807 | // deduction from it. |
| 1808 | if (NextT != RecordT) { |
| 1809 | TemplateDeductionInfo BaseInfo(Info.getLocation()); |
| 1810 | Sema::TemplateDeductionResult BaseResult = |
| 1811 | DeduceTemplateArguments(S, TemplateParams, SpecParam, |
| 1812 | QualType(NextT, 0), BaseInfo, Deduced); |
| 1813 | |
| 1814 | // If template argument deduction for this base was successful, |
| 1815 | // note that we had some success. Otherwise, ignore any deductions |
| 1816 | // from this base class. |
| 1817 | if (BaseResult == Sema::TDK_Success) { |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1818 | // If we've already seen some success, then deduction fails due to |
| 1819 | // an ambiguity (temp.deduct.call p5). |
| 1820 | if (Successful) |
| 1821 | return Sema::TDK_MiscellaneousDeductionFailure; |
| 1822 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1823 | Successful = true; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1824 | std::swap(SuccessfulDeduced, Deduced); |
| 1825 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1826 | Info.Param = BaseInfo.Param; |
| 1827 | Info.FirstArg = BaseInfo.FirstArg; |
| 1828 | Info.SecondArg = BaseInfo.SecondArg; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | Deduced = DeducedOrig; |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1832 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1833 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1834 | // Visit base classes |
| 1835 | CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl()); |
| 1836 | for (const auto &Base : Next->bases()) { |
| 1837 | assert(Base.getType()->isRecordType() && |
| 1838 | "Base class that isn't a record?"); |
| 1839 | ToVisit.push_back(Base.getType()->getAs<RecordType>()); |
| 1840 | } |
| 1841 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1842 | |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1843 | if (Successful) { |
| 1844 | std::swap(SuccessfulDeduced, Deduced); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1845 | return Sema::TDK_Success; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1846 | } |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1847 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1848 | return Result; |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1851 | // T type::* |
| 1852 | // T T::* |
| 1853 | // T (type::*)() |
| 1854 | // type (T::*)() |
| 1855 | // type (type::*)(T) |
| 1856 | // type (T::*)(T) |
| 1857 | // T (type::*)(T) |
| 1858 | // T (T::*)() |
| 1859 | // T (T::*)(T) |
| 1860 | case Type::MemberPointer: { |
| 1861 | const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); |
| 1862 | const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); |
| 1863 | if (!MemPtrArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1864 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1865 | |
David Majnemer | a381cda | 2015-11-30 20:34:28 +0000 | [diff] [blame] | 1866 | QualType ParamPointeeType = MemPtrParam->getPointeeType(); |
| 1867 | if (ParamPointeeType->isFunctionType()) |
| 1868 | S.adjustMemberFunctionCC(ParamPointeeType, /*IsStatic=*/true, |
| 1869 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
| 1870 | QualType ArgPointeeType = MemPtrArg->getPointeeType(); |
| 1871 | if (ArgPointeeType->isFunctionType()) |
| 1872 | S.adjustMemberFunctionCC(ArgPointeeType, /*IsStatic=*/true, |
| 1873 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
| 1874 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1875 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1876 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
David Majnemer | a381cda | 2015-11-30 20:34:28 +0000 | [diff] [blame] | 1877 | ParamPointeeType, |
| 1878 | ArgPointeeType, |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1879 | Info, Deduced, |
| 1880 | TDF & TDF_IgnoreQualifiers)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1881 | return Result; |
| 1882 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1883 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1884 | QualType(MemPtrParam->getClass(), 0), |
| 1885 | QualType(MemPtrArg->getClass(), 0), |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1886 | Info, Deduced, |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1887 | TDF & TDF_IgnoreQualifiers); |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1888 | } |
| 1889 | |
Anders Carlsson | 15f1dd1 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1890 | // (clang extension) |
| 1891 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1892 | // type(^)(T) |
| 1893 | // T(^)() |
| 1894 | // T(^)(T) |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1895 | case Type::BlockPointer: { |
| 1896 | const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); |
| 1897 | const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1898 | |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1899 | if (!BlockPtrArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1900 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1901 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1902 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1903 | BlockPtrParam->getPointeeType(), |
| 1904 | BlockPtrArg->getPointeeType(), |
| 1905 | Info, Deduced, 0); |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1908 | // (clang extension) |
| 1909 | // |
| 1910 | // T __attribute__(((ext_vector_type(<integral constant>)))) |
| 1911 | case Type::ExtVector: { |
| 1912 | const ExtVectorType *VectorParam = cast<ExtVectorType>(Param); |
| 1913 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1914 | // Make sure that the vectors have the same number of elements. |
| 1915 | if (VectorParam->getNumElements() != VectorArg->getNumElements()) |
| 1916 | return Sema::TDK_NonDeducedMismatch; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1917 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1918 | // Perform deduction on the element types. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1919 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1920 | VectorParam->getElementType(), |
| 1921 | VectorArg->getElementType(), |
| 1922 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1923 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1924 | |
| 1925 | if (const DependentSizedExtVectorType *VectorArg |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1926 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1927 | // We can't check the number of elements, since the argument has a |
| 1928 | // dependent number of elements. This can only occur during partial |
| 1929 | // ordering. |
| 1930 | |
| 1931 | // Perform deduction on the element types. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1932 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1933 | VectorParam->getElementType(), |
| 1934 | VectorArg->getElementType(), |
| 1935 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1936 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1937 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1938 | return Sema::TDK_NonDeducedMismatch; |
| 1939 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1940 | |
Erich Keane | f702b02 | 2018-07-13 19:46:04 +0000 | [diff] [blame] | 1941 | case Type::DependentVector: { |
| 1942 | const auto *VectorParam = cast<DependentVectorType>(Param); |
| 1943 | |
| 1944 | if (const auto *VectorArg = dyn_cast<VectorType>(Arg)) { |
| 1945 | // Perform deduction on the element types. |
| 1946 | if (Sema::TemplateDeductionResult Result = |
| 1947 | DeduceTemplateArgumentsByTypeMatch( |
| 1948 | S, TemplateParams, VectorParam->getElementType(), |
| 1949 | VectorArg->getElementType(), Info, Deduced, TDF)) |
| 1950 | return Result; |
| 1951 | |
| 1952 | // Perform deduction on the vector size, if we can. |
| 1953 | NonTypeTemplateParmDecl *NTTP = |
| 1954 | getDeducedParameterFromExpr(Info, VectorParam->getSizeExpr()); |
| 1955 | if (!NTTP) |
| 1956 | return Sema::TDK_Success; |
| 1957 | |
| 1958 | llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); |
| 1959 | ArgSize = VectorArg->getNumElements(); |
| 1960 | // Note that we use the "array bound" rules here; just like in that |
| 1961 | // case, we don't have any particular type for the vector size, but |
| 1962 | // we can provide one if necessary. |
| 1963 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, |
| 1964 | S.Context.UnsignedIntTy, true, |
| 1965 | Info, Deduced); |
| 1966 | } |
| 1967 | |
| 1968 | if (const auto *VectorArg = dyn_cast<DependentVectorType>(Arg)) { |
| 1969 | // Perform deduction on the element types. |
| 1970 | if (Sema::TemplateDeductionResult Result = |
| 1971 | DeduceTemplateArgumentsByTypeMatch( |
| 1972 | S, TemplateParams, VectorParam->getElementType(), |
| 1973 | VectorArg->getElementType(), Info, Deduced, TDF)) |
| 1974 | return Result; |
| 1975 | |
| 1976 | // Perform deduction on the vector size, if we can. |
| 1977 | NonTypeTemplateParmDecl *NTTP = getDeducedParameterFromExpr( |
| 1978 | Info, VectorParam->getSizeExpr()); |
| 1979 | if (!NTTP) |
| 1980 | return Sema::TDK_Success; |
| 1981 | |
| 1982 | return DeduceNonTypeTemplateArgument( |
| 1983 | S, TemplateParams, NTTP, VectorArg->getSizeExpr(), Info, Deduced); |
| 1984 | } |
| 1985 | |
| 1986 | return Sema::TDK_NonDeducedMismatch; |
| 1987 | } |
| 1988 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1989 | // (clang extension) |
| 1990 | // |
| 1991 | // T __attribute__(((ext_vector_type(N)))) |
| 1992 | case Type::DependentSizedExtVector: { |
| 1993 | const DependentSizedExtVectorType *VectorParam |
| 1994 | = cast<DependentSizedExtVectorType>(Param); |
| 1995 | |
| 1996 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1997 | // Perform deduction on the element types. |
| 1998 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1999 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 2000 | VectorParam->getElementType(), |
| 2001 | VectorArg->getElementType(), |
| 2002 | Info, Deduced, TDF)) |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2003 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2004 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2005 | // Perform deduction on the vector size, if we can. |
| 2006 | NonTypeTemplateParmDecl *NTTP |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2007 | = getDeducedParameterFromExpr(Info, VectorParam->getSizeExpr()); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2008 | if (!NTTP) |
| 2009 | return Sema::TDK_Success; |
| 2010 | |
| 2011 | llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); |
| 2012 | ArgSize = VectorArg->getNumElements(); |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2013 | // Note that we use the "array bound" rules here; just like in that |
| 2014 | // case, we don't have any particular type for the vector size, but |
| 2015 | // we can provide one if necessary. |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 2016 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2017 | S.Context.IntTy, true, Info, |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 2018 | Deduced); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2019 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2020 | |
| 2021 | if (const DependentSizedExtVectorType *VectorArg |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2022 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 2023 | // Perform deduction on the element types. |
| 2024 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 2025 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 2026 | VectorParam->getElementType(), |
| 2027 | VectorArg->getElementType(), |
| 2028 | Info, Deduced, TDF)) |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2029 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2030 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2031 | // Perform deduction on the vector size, if we can. |
| 2032 | NonTypeTemplateParmDecl *NTTP |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2033 | = getDeducedParameterFromExpr(Info, VectorParam->getSizeExpr()); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2034 | if (!NTTP) |
| 2035 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2036 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 2037 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 2038 | VectorArg->getSizeExpr(), |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2039 | Info, Deduced); |
| 2040 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2041 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2042 | return Sema::TDK_NonDeducedMismatch; |
| 2043 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2044 | |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 2045 | // (clang extension) |
| 2046 | // |
| 2047 | // T __attribute__(((address_space(N)))) |
| 2048 | case Type::DependentAddressSpace: { |
| 2049 | const DependentAddressSpaceType *AddressSpaceParam = |
| 2050 | cast<DependentAddressSpaceType>(Param); |
| 2051 | |
| 2052 | if (const DependentAddressSpaceType *AddressSpaceArg = |
| 2053 | dyn_cast<DependentAddressSpaceType>(Arg)) { |
| 2054 | // Perform deduction on the pointer type. |
| 2055 | if (Sema::TemplateDeductionResult Result = |
| 2056 | DeduceTemplateArgumentsByTypeMatch( |
| 2057 | S, TemplateParams, AddressSpaceParam->getPointeeType(), |
| 2058 | AddressSpaceArg->getPointeeType(), Info, Deduced, TDF)) |
| 2059 | return Result; |
| 2060 | |
| 2061 | // Perform deduction on the address space, if we can. |
| 2062 | NonTypeTemplateParmDecl *NTTP = getDeducedParameterFromExpr( |
| 2063 | Info, AddressSpaceParam->getAddrSpaceExpr()); |
| 2064 | if (!NTTP) |
| 2065 | return Sema::TDK_Success; |
| 2066 | |
| 2067 | return DeduceNonTypeTemplateArgument( |
| 2068 | S, TemplateParams, NTTP, AddressSpaceArg->getAddrSpaceExpr(), Info, |
| 2069 | Deduced); |
| 2070 | } |
| 2071 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 2072 | if (isTargetAddressSpace(Arg.getAddressSpace())) { |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 2073 | llvm::APSInt ArgAddressSpace(S.Context.getTypeSize(S.Context.IntTy), |
| 2074 | false); |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 2075 | ArgAddressSpace = toTargetAddressSpace(Arg.getAddressSpace()); |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 2076 | |
| 2077 | // Perform deduction on the pointer types. |
| 2078 | if (Sema::TemplateDeductionResult Result = |
| 2079 | DeduceTemplateArgumentsByTypeMatch( |
| 2080 | S, TemplateParams, AddressSpaceParam->getPointeeType(), |
| 2081 | S.Context.removeAddrSpaceQualType(Arg), Info, Deduced, TDF)) |
| 2082 | return Result; |
| 2083 | |
| 2084 | // Perform deduction on the address space, if we can. |
| 2085 | NonTypeTemplateParmDecl *NTTP = getDeducedParameterFromExpr( |
| 2086 | Info, AddressSpaceParam->getAddrSpaceExpr()); |
| 2087 | if (!NTTP) |
| 2088 | return Sema::TDK_Success; |
| 2089 | |
| 2090 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 2091 | ArgAddressSpace, S.Context.IntTy, |
| 2092 | true, Info, Deduced); |
| 2093 | } |
| 2094 | |
| 2095 | return Sema::TDK_NonDeducedMismatch; |
| 2096 | } |
| 2097 | |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 2098 | case Type::TypeOfExpr: |
| 2099 | case Type::TypeOf: |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 2100 | case Type::DependentName: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2101 | case Type::UnresolvedUsing: |
| 2102 | case Type::Decltype: |
| 2103 | case Type::UnaryTransform: |
| 2104 | case Type::Auto: |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 2105 | case Type::DeducedTemplateSpecialization: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 2106 | case Type::DependentTemplateSpecialization: |
| 2107 | case Type::PackExpansion: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 2108 | case Type::Pipe: |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 2109 | // No template argument deduction for these types |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2110 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2111 | } |
| 2112 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2113 | llvm_unreachable("Invalid Type Class!"); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2116 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2117 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2118 | TemplateParameterList *TemplateParams, |
| 2119 | const TemplateArgument &Param, |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 2120 | TemplateArgument Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2121 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2122 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 2123 | // If the template argument is a pack expansion, perform template argument |
| 2124 | // deduction against the pattern of that expansion. This only occurs during |
| 2125 | // partial ordering. |
| 2126 | if (Arg.isPackExpansion()) |
| 2127 | Arg = Arg.getPackExpansionPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2128 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2129 | switch (Param.getKind()) { |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2130 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2131 | llvm_unreachable("Null template argument in parameter list"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2132 | |
| 2133 | case TemplateArgument::Type: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2134 | if (Arg.getKind() == TemplateArgument::Type) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 2135 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 2136 | Param.getAsType(), |
| 2137 | Arg.getAsType(), |
| 2138 | Info, Deduced, 0); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2139 | Info.FirstArg = Param; |
| 2140 | Info.SecondArg = Arg; |
| 2141 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2142 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2143 | case TemplateArgument::Template: |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 2144 | if (Arg.getKind() == TemplateArgument::Template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2145 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2146 | Param.getAsTemplate(), |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 2147 | Arg.getAsTemplate(), Info, Deduced); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2148 | Info.FirstArg = Param; |
| 2149 | Info.SecondArg = Arg; |
| 2150 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2151 | |
| 2152 | case TemplateArgument::TemplateExpansion: |
| 2153 | llvm_unreachable("caller should handle pack expansions"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2154 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2155 | case TemplateArgument::Declaration: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2156 | if (Arg.getKind() == TemplateArgument::Declaration && |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 2157 | isSameDeclaration(Param.getAsDecl(), Arg.getAsDecl())) |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2158 | return Sema::TDK_Success; |
| 2159 | |
| 2160 | Info.FirstArg = Param; |
| 2161 | Info.SecondArg = Arg; |
| 2162 | return Sema::TDK_NonDeducedMismatch; |
| 2163 | |
| 2164 | case TemplateArgument::NullPtr: |
| 2165 | if (Arg.getKind() == TemplateArgument::NullPtr && |
| 2166 | S.Context.hasSameType(Param.getNullPtrType(), Arg.getNullPtrType())) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2167 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2168 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2169 | Info.FirstArg = Param; |
| 2170 | Info.SecondArg = Arg; |
| 2171 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2172 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2173 | case TemplateArgument::Integral: |
| 2174 | if (Arg.getKind() == TemplateArgument::Integral) { |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 2175 | if (hasSameExtendedValue(Param.getAsIntegral(), Arg.getAsIntegral())) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2176 | return Sema::TDK_Success; |
| 2177 | |
| 2178 | Info.FirstArg = Param; |
| 2179 | Info.SecondArg = Arg; |
| 2180 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2181 | } |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2182 | |
| 2183 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 2184 | Info.FirstArg = Param; |
| 2185 | Info.SecondArg = Arg; |
| 2186 | return Sema::TDK_NonDeducedMismatch; |
| 2187 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2188 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2189 | Info.FirstArg = Param; |
| 2190 | Info.SecondArg = Arg; |
| 2191 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2192 | |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 2193 | case TemplateArgument::Expression: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2194 | if (NonTypeTemplateParmDecl *NTTP |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2195 | = getDeducedParameterFromExpr(Info, Param.getAsExpr())) { |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2196 | if (Arg.getKind() == TemplateArgument::Integral) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 2197 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 2198 | Arg.getAsIntegral(), |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 2199 | Arg.getIntegralType(), |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2200 | /*ArrayBound=*/false, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2201 | Info, Deduced); |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 2202 | if (Arg.getKind() == TemplateArgument::NullPtr) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 2203 | return DeduceNullPtrTemplateArgument(S, TemplateParams, NTTP, |
| 2204 | Arg.getNullPtrType(), |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 2205 | Info, Deduced); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2206 | if (Arg.getKind() == TemplateArgument::Expression) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 2207 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 2208 | Arg.getAsExpr(), Info, Deduced); |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 2209 | if (Arg.getKind() == TemplateArgument::Declaration) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 2210 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 2211 | Arg.getAsDecl(), |
| 2212 | Arg.getParamTypeForDecl(), |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 2213 | Info, Deduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2214 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2215 | Info.FirstArg = Param; |
| 2216 | Info.SecondArg = Arg; |
| 2217 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2218 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2219 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2220 | // Can't deduce anything, but that's okay. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2221 | return Sema::TDK_Success; |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 2222 | |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 2223 | case TemplateArgument::Pack: |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2224 | llvm_unreachable("Argument packs should be expanded by the caller!"); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2225 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2226 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2227 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2230 | /// Determine whether there is a template argument to be used for |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2231 | /// deduction. |
| 2232 | /// |
| 2233 | /// This routine "expands" argument packs in-place, overriding its input |
| 2234 | /// parameters so that \c Args[ArgIdx] will be the available template argument. |
| 2235 | /// |
| 2236 | /// \returns true if there is another template argument (which will be at |
| 2237 | /// \c Args[ArgIdx]), false otherwise. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2238 | static bool hasTemplateArgumentForDeduction(ArrayRef<TemplateArgument> &Args, |
| 2239 | unsigned &ArgIdx) { |
| 2240 | if (ArgIdx == Args.size()) |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2241 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2242 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2243 | const TemplateArgument &Arg = Args[ArgIdx]; |
| 2244 | if (Arg.getKind() != TemplateArgument::Pack) |
| 2245 | return true; |
| 2246 | |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2247 | assert(ArgIdx == Args.size() - 1 && "Pack not at the end of argument list?"); |
| 2248 | Args = Arg.pack_elements(); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2249 | ArgIdx = 0; |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2250 | return ArgIdx < Args.size(); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2253 | /// Determine whether the given set of template arguments has a pack |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 2254 | /// expansion that is not the last template argument. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2255 | static bool hasPackExpansionBeforeEnd(ArrayRef<TemplateArgument> Args) { |
| 2256 | bool FoundPackExpansion = false; |
| 2257 | for (const auto &A : Args) { |
| 2258 | if (FoundPackExpansion) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 2259 | return true; |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2260 | |
| 2261 | if (A.getKind() == TemplateArgument::Pack) |
| 2262 | return hasPackExpansionBeforeEnd(A.pack_elements()); |
| 2263 | |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 2264 | // FIXME: If this is a fixed-arity pack expansion from an outer level of |
| 2265 | // templates, it should not be treated as a pack expansion. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2266 | if (A.isPackExpansion()) |
| 2267 | FoundPackExpansion = true; |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 2268 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2269 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 2270 | return false; |
| 2271 | } |
| 2272 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2273 | static Sema::TemplateDeductionResult |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 2274 | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2275 | ArrayRef<TemplateArgument> Params, |
| 2276 | ArrayRef<TemplateArgument> Args, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2277 | TemplateDeductionInfo &Info, |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 2278 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2279 | bool NumberOfArgumentsMustMatch) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2280 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2281 | // If the template argument list of P contains a pack expansion that is not |
| 2282 | // the last template argument, the entire template argument list is a |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2283 | // non-deduced context. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2284 | if (hasPackExpansionBeforeEnd(Params)) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 2285 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2286 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2287 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2288 | // If P has a form that contains <T> or <i>, then each argument Pi of the |
| 2289 | // respective template argument list P is compared with the corresponding |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2290 | // argument Ai of the corresponding template argument list of A. |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2291 | unsigned ArgIdx = 0, ParamIdx = 0; |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2292 | for (; hasTemplateArgumentForDeduction(Params, ParamIdx); ++ParamIdx) { |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2293 | if (!Params[ParamIdx].isPackExpansion()) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2294 | // The simple case: deduce template arguments by matching Pi and Ai. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2295 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2296 | // Check whether we have enough arguments. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2297 | if (!hasTemplateArgumentForDeduction(Args, ArgIdx)) |
Richard Smith | ec7176e | 2017-01-05 02:31:32 +0000 | [diff] [blame] | 2298 | return NumberOfArgumentsMustMatch |
| 2299 | ? Sema::TDK_MiscellaneousDeductionFailure |
| 2300 | : Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2301 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 2302 | // C++1z [temp.deduct.type]p9: |
| 2303 | // During partial ordering, if Ai was originally a pack expansion [and] |
| 2304 | // Pi is not a pack expansion, template argument deduction fails. |
| 2305 | if (Args[ArgIdx].isPackExpansion()) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 2306 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2307 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2308 | // Perform deduction for this Pi/Ai pair. |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2309 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 2310 | = DeduceTemplateArguments(S, TemplateParams, |
| 2311 | Params[ParamIdx], Args[ArgIdx], |
| 2312 | Info, Deduced)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2313 | return Result; |
| 2314 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2315 | // Move to the next argument. |
| 2316 | ++ArgIdx; |
| 2317 | continue; |
| 2318 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2319 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2320 | // The parameter is a pack expansion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2321 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2322 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2323 | // If Pi is a pack expansion, then the pattern of Pi is compared with |
| 2324 | // each remaining argument in the template argument list of A. Each |
| 2325 | // comparison deduces template arguments for subsequent positions in the |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2326 | // template parameter packs expanded by Pi. |
| 2327 | TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2328 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 2329 | // Prepare to deduce the packs within the pattern. |
| 2330 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2331 | |
| 2332 | // Keep track of the deduced template arguments for each parameter pack |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2333 | // expanded by this pack expansion (the outer index) and for each |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2334 | // template argument (the inner SmallVectors). |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 2335 | for (; hasTemplateArgumentForDeduction(Args, ArgIdx) && |
| 2336 | PackScope.hasNextElement(); |
| 2337 | ++ArgIdx) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2338 | // Deduce template arguments from the pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2339 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2340 | = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx], |
| 2341 | Info, Deduced)) |
| 2342 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2343 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 2344 | PackScope.nextPackElement(); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2345 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2346 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2347 | // Build argument packs for each of the parameter packs expanded by this |
| 2348 | // pack expansion. |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 2349 | if (auto Result = PackScope.finish()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2350 | return Result; |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2351 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2352 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2353 | return Sema::TDK_Success; |
| 2354 | } |
| 2355 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2356 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2357 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2358 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2359 | const TemplateArgumentList &ParamList, |
| 2360 | const TemplateArgumentList &ArgList, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2361 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2362 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2363 | return DeduceTemplateArguments(S, TemplateParams, ParamList.asArray(), |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 2364 | ArgList.asArray(), Info, Deduced, |
| 2365 | /*NumberOfArgumentsMustMatch*/false); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2366 | } |
| 2367 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2368 | /// Determine whether two template arguments are the same. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2369 | static bool isSameTemplateArg(ASTContext &Context, |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2370 | TemplateArgument X, |
| 2371 | const TemplateArgument &Y, |
| 2372 | bool PackExpansionMatchesPack = false) { |
| 2373 | // If we're checking deduced arguments (X) against original arguments (Y), |
| 2374 | // we will have flattened packs to non-expansions in X. |
| 2375 | if (PackExpansionMatchesPack && X.isPackExpansion() && !Y.isPackExpansion()) |
| 2376 | X = X.getPackExpansionPattern(); |
| 2377 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2378 | if (X.getKind() != Y.getKind()) |
| 2379 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2380 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2381 | switch (X.getKind()) { |
| 2382 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2383 | llvm_unreachable("Comparing NULL template argument"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2384 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2385 | case TemplateArgument::Type: |
| 2386 | return Context.getCanonicalType(X.getAsType()) == |
| 2387 | Context.getCanonicalType(Y.getAsType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2388 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2389 | case TemplateArgument::Declaration: |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 2390 | return isSameDeclaration(X.getAsDecl(), Y.getAsDecl()); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2391 | |
| 2392 | case TemplateArgument::NullPtr: |
| 2393 | return Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2394 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2395 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2396 | case TemplateArgument::TemplateExpansion: |
| 2397 | return Context.getCanonicalTemplateName( |
| 2398 | X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() == |
| 2399 | Context.getCanonicalTemplateName( |
| 2400 | Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2401 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2402 | case TemplateArgument::Integral: |
Richard Smith | 993f203 | 2016-12-25 20:21:12 +0000 | [diff] [blame] | 2403 | return hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2404 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2405 | case TemplateArgument::Expression: { |
| 2406 | llvm::FoldingSetNodeID XID, YID; |
| 2407 | X.getAsExpr()->Profile(XID, Context, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2408 | Y.getAsExpr()->Profile(YID, Context, true); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2409 | return XID == YID; |
| 2410 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2411 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2412 | case TemplateArgument::Pack: |
| 2413 | if (X.pack_size() != Y.pack_size()) |
| 2414 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2415 | |
| 2416 | for (TemplateArgument::pack_iterator XP = X.pack_begin(), |
| 2417 | XPEnd = X.pack_end(), |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2418 | YP = Y.pack_begin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2419 | XP != XPEnd; ++XP, ++YP) |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2420 | if (!isSameTemplateArg(Context, *XP, *YP, PackExpansionMatchesPack)) |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2421 | return false; |
| 2422 | |
| 2423 | return true; |
| 2424 | } |
| 2425 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2426 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2427 | } |
| 2428 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2429 | /// Allocate a TemplateArgumentLoc where all locations have |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2430 | /// been initialized to the given location. |
| 2431 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2432 | /// \param Arg The template argument we are producing template argument |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2433 | /// location information for. |
| 2434 | /// |
| 2435 | /// \param NTTPType For a declaration template argument, the type of |
| 2436 | /// the non-type template parameter that corresponds to this template |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2437 | /// argument. Can be null if no type sugar is available to add to the |
| 2438 | /// type from the template argument. |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2439 | /// |
| 2440 | /// \param Loc The source location to use for the resulting template |
| 2441 | /// argument. |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2442 | TemplateArgumentLoc |
| 2443 | Sema::getTrivialTemplateArgumentLoc(const TemplateArgument &Arg, |
| 2444 | QualType NTTPType, SourceLocation Loc) { |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2445 | switch (Arg.getKind()) { |
| 2446 | case TemplateArgument::Null: |
| 2447 | llvm_unreachable("Can't get a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2448 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2449 | case TemplateArgument::Type: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2450 | return TemplateArgumentLoc( |
| 2451 | Arg, Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2452 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2453 | case TemplateArgument::Declaration: { |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2454 | if (NTTPType.isNull()) |
| 2455 | NTTPType = Arg.getParamTypeForDecl(); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2456 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
| 2457 | .getAs<Expr>(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2458 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2459 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2460 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2461 | case TemplateArgument::NullPtr: { |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2462 | if (NTTPType.isNull()) |
| 2463 | NTTPType = Arg.getNullPtrType(); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2464 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
| 2465 | .getAs<Expr>(); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2466 | return TemplateArgumentLoc(TemplateArgument(NTTPType, /*isNullPtr*/true), |
| 2467 | E); |
| 2468 | } |
| 2469 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2470 | case TemplateArgument::Integral: { |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2471 | Expr *E = |
| 2472 | BuildExpressionFromIntegralTemplateArgument(Arg, Loc).getAs<Expr>(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2473 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2474 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2475 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2476 | case TemplateArgument::Template: |
| 2477 | case TemplateArgument::TemplateExpansion: { |
| 2478 | NestedNameSpecifierLocBuilder Builder; |
| 2479 | TemplateName Template = Arg.getAsTemplate(); |
| 2480 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2481 | Builder.MakeTrivial(Context, DTN->getQualifier(), Loc); |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 2482 | else if (QualifiedTemplateName *QTN = |
| 2483 | Template.getAsQualifiedTemplateName()) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2484 | Builder.MakeTrivial(Context, QTN->getQualifier(), Loc); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2485 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2486 | if (Arg.getKind() == TemplateArgument::Template) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2487 | return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(Context), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2488 | Loc); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2489 | |
| 2490 | return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(Context), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2491 | Loc, Loc); |
| 2492 | } |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2493 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2494 | case TemplateArgument::Expression: |
| 2495 | return TemplateArgumentLoc(Arg, Arg.getAsExpr()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2496 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2497 | case TemplateArgument::Pack: |
| 2498 | return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo()); |
| 2499 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2500 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2501 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2504 | /// Convert the given deduced template argument and add it to the set of |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2505 | /// fully-converted template arguments. |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2506 | static bool |
| 2507 | ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param, |
| 2508 | DeducedTemplateArgument Arg, |
| 2509 | NamedDecl *Template, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2510 | TemplateDeductionInfo &Info, |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2511 | bool IsDeduced, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2512 | SmallVectorImpl<TemplateArgument> &Output) { |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2513 | auto ConvertArg = [&](DeducedTemplateArgument Arg, |
| 2514 | unsigned ArgumentPackIndex) { |
| 2515 | // Convert the deduced template argument into a template |
| 2516 | // argument that we can check, almost as if the user had written |
| 2517 | // the template argument explicitly. |
| 2518 | TemplateArgumentLoc ArgLoc = |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2519 | S.getTrivialTemplateArgumentLoc(Arg, QualType(), Info.getLocation()); |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2520 | |
| 2521 | // Check the template argument, converting it as necessary. |
| 2522 | return S.CheckTemplateArgument( |
| 2523 | Param, ArgLoc, Template, Template->getLocation(), |
| 2524 | Template->getSourceRange().getEnd(), ArgumentPackIndex, Output, |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2525 | IsDeduced |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2526 | ? (Arg.wasDeducedFromArrayBound() ? Sema::CTAK_DeducedFromArrayBound |
| 2527 | : Sema::CTAK_Deduced) |
| 2528 | : Sema::CTAK_Specified); |
| 2529 | }; |
| 2530 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2531 | if (Arg.getKind() == TemplateArgument::Pack) { |
| 2532 | // This is a template argument pack, so check each of its arguments against |
| 2533 | // the template parameter. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2534 | SmallVector<TemplateArgument, 2> PackedArgsBuilder; |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 2535 | for (const auto &P : Arg.pack_elements()) { |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2536 | // When converting the deduced template argument, append it to the |
| 2537 | // general output list. We need to do this so that the template argument |
| 2538 | // checking logic has all of the prior template arguments available. |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 2539 | DeducedTemplateArgument InnerArg(P); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2540 | InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound()); |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2541 | assert(InnerArg.getKind() != TemplateArgument::Pack && |
| 2542 | "deduced nested pack"); |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 2543 | if (P.isNull()) { |
| 2544 | // We deduced arguments for some elements of this pack, but not for |
| 2545 | // all of them. This happens if we get a conditionally-non-deduced |
| 2546 | // context in a pack expansion (such as an overload set in one of the |
| 2547 | // arguments). |
| 2548 | S.Diag(Param->getLocation(), |
| 2549 | diag::err_template_arg_deduced_incomplete_pack) |
| 2550 | << Arg << Param; |
| 2551 | return true; |
| 2552 | } |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2553 | if (ConvertArg(InnerArg, PackedArgsBuilder.size())) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2554 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2555 | |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2556 | // Move the converted template argument into our argument pack. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 2557 | PackedArgsBuilder.push_back(Output.pop_back_val()); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2558 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2559 | |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2560 | // If the pack is empty, we still need to substitute into the parameter |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2561 | // itself, in case that substitution fails. |
| 2562 | if (PackedArgsBuilder.empty()) { |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2563 | LocalInstantiationScope Scope(S); |
Richard Smith | e824775 | 2016-12-22 07:24:39 +0000 | [diff] [blame] | 2564 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Output); |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2565 | MultiLevelTemplateArgumentList Args(TemplateArgs); |
| 2566 | |
| 2567 | if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2568 | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
| 2569 | NTTP, Output, |
| 2570 | Template->getSourceRange()); |
Simon Pilgrim | 6f3e1ea | 2016-12-26 18:11:49 +0000 | [diff] [blame] | 2571 | if (Inst.isInvalid() || |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2572 | S.SubstType(NTTP->getType(), Args, NTTP->getLocation(), |
| 2573 | NTTP->getDeclName()).isNull()) |
| 2574 | return true; |
| 2575 | } else if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 2576 | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
| 2577 | TTP, Output, |
| 2578 | Template->getSourceRange()); |
| 2579 | if (Inst.isInvalid() || !S.SubstDecl(TTP, S.CurContext, Args)) |
| 2580 | return true; |
| 2581 | } |
| 2582 | // For type parameters, no substitution is ever required. |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2583 | } |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2584 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2585 | // Create the resulting argument pack. |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 2586 | Output.push_back( |
| 2587 | TemplateArgument::CreatePackCopy(S.Context, PackedArgsBuilder)); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2588 | return false; |
| 2589 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2590 | |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2591 | return ConvertArg(Arg, 0); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2592 | } |
| 2593 | |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2594 | // FIXME: This should not be a template, but |
| 2595 | // ClassTemplatePartialSpecializationDecl sadly does not derive from |
| 2596 | // TemplateDecl. |
| 2597 | template<typename TemplateDeclT> |
| 2598 | static Sema::TemplateDeductionResult ConvertDeducedTemplateArguments( |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2599 | Sema &S, TemplateDeclT *Template, bool IsDeduced, |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2600 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2601 | TemplateDeductionInfo &Info, SmallVectorImpl<TemplateArgument> &Builder, |
| 2602 | LocalInstantiationScope *CurrentInstantiationScope = nullptr, |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 2603 | unsigned NumAlreadyConverted = 0, bool PartialOverloading = false) { |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2604 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| 2605 | |
| 2606 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 2607 | NamedDecl *Param = TemplateParams->getParam(I); |
| 2608 | |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 2609 | // C++0x [temp.arg.explicit]p3: |
| 2610 | // A trailing template parameter pack (14.5.3) not otherwise deduced will |
| 2611 | // be deduced to an empty sequence of template arguments. |
| 2612 | // FIXME: Where did the word "trailing" come from? |
| 2613 | if (Deduced[I].isNull() && Param->isTemplateParameterPack()) { |
| 2614 | if (auto Result = PackDeductionScope(S, TemplateParams, Deduced, Info, I) |
| 2615 | .finish(/*TreatNoDeductionsAsNonDeduced*/false)) |
| 2616 | return Result; |
| 2617 | } |
| 2618 | |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2619 | if (!Deduced[I].isNull()) { |
| 2620 | if (I < NumAlreadyConverted) { |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2621 | // We may have had explicitly-specified template arguments for a |
| 2622 | // template parameter pack (that may or may not have been extended |
| 2623 | // via additional deduced arguments). |
Richard Smith | 9c0c986 | 2017-01-05 20:27:28 +0000 | [diff] [blame] | 2624 | if (Param->isParameterPack() && CurrentInstantiationScope && |
| 2625 | CurrentInstantiationScope->getPartiallySubstitutedPack() == Param) { |
| 2626 | // Forget the partially-substituted pack; its substitution is now |
| 2627 | // complete. |
| 2628 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2629 | // We still need to check the argument in case it was extended by |
| 2630 | // deduction. |
| 2631 | } else { |
| 2632 | // We have already fully type-checked and converted this |
| 2633 | // argument, because it was explicitly-specified. Just record the |
| 2634 | // presence of this argument. |
| 2635 | Builder.push_back(Deduced[I]); |
| 2636 | continue; |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2637 | } |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2638 | } |
| 2639 | |
Richard Smith | 9c0c986 | 2017-01-05 20:27:28 +0000 | [diff] [blame] | 2640 | // We may have deduced this argument, so it still needs to be |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2641 | // checked and converted. |
| 2642 | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Template, Info, |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2643 | IsDeduced, Builder)) { |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2644 | Info.Param = makeTemplateParameter(Param); |
| 2645 | // FIXME: These template arguments are temporary. Free them! |
| 2646 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2647 | return Sema::TDK_SubstitutionFailure; |
| 2648 | } |
| 2649 | |
| 2650 | continue; |
| 2651 | } |
| 2652 | |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2653 | // Substitute into the default template argument, if available. |
| 2654 | bool HasDefaultArg = false; |
| 2655 | TemplateDecl *TD = dyn_cast<TemplateDecl>(Template); |
| 2656 | if (!TD) { |
Richard Smith | f8ba3fd | 2017-06-02 22:53:06 +0000 | [diff] [blame] | 2657 | assert(isa<ClassTemplatePartialSpecializationDecl>(Template) || |
| 2658 | isa<VarTemplatePartialSpecializationDecl>(Template)); |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2659 | return Sema::TDK_Incomplete; |
| 2660 | } |
| 2661 | |
| 2662 | TemplateArgumentLoc DefArg = S.SubstDefaultTemplateArgumentIfAvailable( |
| 2663 | TD, TD->getLocation(), TD->getSourceRange().getEnd(), Param, Builder, |
| 2664 | HasDefaultArg); |
| 2665 | |
| 2666 | // If there was no default argument, deduction is incomplete. |
| 2667 | if (DefArg.getArgument().isNull()) { |
| 2668 | Info.Param = makeTemplateParameter( |
| 2669 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
| 2670 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 2671 | if (PartialOverloading) break; |
| 2672 | |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2673 | return HasDefaultArg ? Sema::TDK_SubstitutionFailure |
| 2674 | : Sema::TDK_Incomplete; |
| 2675 | } |
| 2676 | |
| 2677 | // Check whether we can actually use the default argument. |
| 2678 | if (S.CheckTemplateArgument(Param, DefArg, TD, TD->getLocation(), |
| 2679 | TD->getSourceRange().getEnd(), 0, Builder, |
| 2680 | Sema::CTAK_Specified)) { |
| 2681 | Info.Param = makeTemplateParameter( |
| 2682 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
| 2683 | // FIXME: These template arguments are temporary. Free them! |
| 2684 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2685 | return Sema::TDK_SubstitutionFailure; |
| 2686 | } |
| 2687 | |
| 2688 | // If we get here, we successfully used the default template argument. |
| 2689 | } |
| 2690 | |
| 2691 | return Sema::TDK_Success; |
| 2692 | } |
| 2693 | |
Benjamin Kramer | 357c9e1 | 2017-02-11 12:21:17 +0000 | [diff] [blame] | 2694 | static DeclContext *getAsDeclContextOrEnclosing(Decl *D) { |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2695 | if (auto *DC = dyn_cast<DeclContext>(D)) |
| 2696 | return DC; |
| 2697 | return D->getDeclContext(); |
| 2698 | } |
| 2699 | |
| 2700 | template<typename T> struct IsPartialSpecialization { |
| 2701 | static constexpr bool value = false; |
| 2702 | }; |
| 2703 | template<> |
| 2704 | struct IsPartialSpecialization<ClassTemplatePartialSpecializationDecl> { |
| 2705 | static constexpr bool value = true; |
| 2706 | }; |
| 2707 | template<> |
| 2708 | struct IsPartialSpecialization<VarTemplatePartialSpecializationDecl> { |
| 2709 | static constexpr bool value = true; |
| 2710 | }; |
| 2711 | |
| 2712 | /// Complete template argument deduction for a partial specialization. |
| 2713 | template <typename T> |
| 2714 | static typename std::enable_if<IsPartialSpecialization<T>::value, |
| 2715 | Sema::TemplateDeductionResult>::type |
| 2716 | FinishTemplateArgumentDeduction( |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2717 | Sema &S, T *Partial, bool IsPartialOrdering, |
| 2718 | const TemplateArgumentList &TemplateArgs, |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2719 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2720 | TemplateDeductionInfo &Info) { |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2721 | // Unevaluated SFINAE context. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2722 | EnterExpressionEvaluationContext Unevaluated( |
| 2723 | S, Sema::ExpressionEvaluationContext::Unevaluated); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2724 | Sema::SFINAETrap Trap(S); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2725 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2726 | Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Partial)); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2727 | |
| 2728 | // C++ [temp.deduct.type]p2: |
| 2729 | // [...] or if any template argument remains neither deduced nor |
| 2730 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2731 | SmallVector<TemplateArgument, 4> Builder; |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2732 | if (auto Result = ConvertDeducedTemplateArguments( |
| 2733 | S, Partial, IsPartialOrdering, Deduced, Info, Builder)) |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2734 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2735 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2736 | // Form the template argument list from the deduced template arguments. |
| 2737 | TemplateArgumentList *DeducedArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2738 | = TemplateArgumentList::CreateCopy(S.Context, Builder); |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2739 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2740 | Info.reset(DeducedArgumentList); |
| 2741 | |
| 2742 | // Substitute the deduced template arguments into the template |
| 2743 | // arguments of the class template partial specialization, and |
| 2744 | // verify that the instantiated template arguments are both valid |
| 2745 | // and are equivalent to the template arguments originally provided |
| 2746 | // to the class template. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2747 | LocalInstantiationScope InstScope(S); |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2748 | auto *Template = Partial->getSpecializedTemplate(); |
| 2749 | const ASTTemplateArgumentListInfo *PartialTemplArgInfo = |
| 2750 | Partial->getTemplateArgsAsWritten(); |
| 2751 | const TemplateArgumentLoc *PartialTemplateArgs = |
| 2752 | PartialTemplArgInfo->getTemplateArgs(); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2753 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2754 | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, |
| 2755 | PartialTemplArgInfo->RAngleLoc); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2756 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2757 | if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs, |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2758 | InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { |
| 2759 | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; |
| 2760 | if (ParamIdx >= Partial->getTemplateParameters()->size()) |
| 2761 | ParamIdx = Partial->getTemplateParameters()->size() - 1; |
| 2762 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2763 | Decl *Param = const_cast<NamedDecl *>( |
| 2764 | Partial->getTemplateParameters()->getParam(ParamIdx)); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2765 | Info.Param = makeTemplateParameter(Param); |
| 2766 | Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); |
| 2767 | return Sema::TDK_SubstitutionFailure; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2770 | SmallVector<TemplateArgument, 4> ConvertedInstArgs; |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2771 | if (S.CheckTemplateArgumentList(Template, Partial->getLocation(), InstArgs, |
| 2772 | false, ConvertedInstArgs)) |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2773 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2774 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2775 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2776 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2777 | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2778 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 2779 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2780 | Info.FirstArg = TemplateArgs[I]; |
| 2781 | Info.SecondArg = InstArg; |
| 2782 | return Sema::TDK_NonDeducedMismatch; |
| 2783 | } |
| 2784 | } |
| 2785 | |
| 2786 | if (Trap.hasErrorOccurred()) |
| 2787 | return Sema::TDK_SubstitutionFailure; |
| 2788 | |
| 2789 | return Sema::TDK_Success; |
| 2790 | } |
| 2791 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2792 | /// Complete template argument deduction for a class or variable template, |
| 2793 | /// when partial ordering against a partial specialization. |
| 2794 | // FIXME: Factor out duplication with partial specialization version above. |
Benjamin Kramer | 357c9e1 | 2017-02-11 12:21:17 +0000 | [diff] [blame] | 2795 | static Sema::TemplateDeductionResult FinishTemplateArgumentDeduction( |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2796 | Sema &S, TemplateDecl *Template, bool PartialOrdering, |
| 2797 | const TemplateArgumentList &TemplateArgs, |
| 2798 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2799 | TemplateDeductionInfo &Info) { |
| 2800 | // Unevaluated SFINAE context. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2801 | EnterExpressionEvaluationContext Unevaluated( |
| 2802 | S, Sema::ExpressionEvaluationContext::Unevaluated); |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2803 | Sema::SFINAETrap Trap(S); |
| 2804 | |
| 2805 | Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Template)); |
| 2806 | |
| 2807 | // C++ [temp.deduct.type]p2: |
| 2808 | // [...] or if any template argument remains neither deduced nor |
| 2809 | // explicitly specified, template argument deduction fails. |
| 2810 | SmallVector<TemplateArgument, 4> Builder; |
| 2811 | if (auto Result = ConvertDeducedTemplateArguments( |
| 2812 | S, Template, /*IsDeduced*/PartialOrdering, Deduced, Info, Builder)) |
| 2813 | return Result; |
| 2814 | |
| 2815 | // Check that we produced the correct argument list. |
| 2816 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| 2817 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
| 2818 | TemplateArgument InstArg = Builder[I]; |
| 2819 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg, |
| 2820 | /*PackExpansionMatchesPack*/true)) { |
| 2821 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
| 2822 | Info.FirstArg = TemplateArgs[I]; |
| 2823 | Info.SecondArg = InstArg; |
| 2824 | return Sema::TDK_NonDeducedMismatch; |
| 2825 | } |
| 2826 | } |
| 2827 | |
| 2828 | if (Trap.hasErrorOccurred()) |
| 2829 | return Sema::TDK_SubstitutionFailure; |
| 2830 | |
| 2831 | return Sema::TDK_Success; |
| 2832 | } |
| 2833 | |
| 2834 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2835 | /// Perform template argument deduction to determine whether |
Larisse Voufo | 833b05a | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 2836 | /// the given template arguments match the given class template |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2837 | /// partial specialization per C++ [temp.class.spec.match]. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2838 | Sema::TemplateDeductionResult |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2839 | Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2840 | const TemplateArgumentList &TemplateArgs, |
| 2841 | TemplateDeductionInfo &Info) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 2842 | if (Partial->isInvalidDecl()) |
| 2843 | return TDK_Invalid; |
| 2844 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2845 | // C++ [temp.class.spec.match]p2: |
| 2846 | // A partial specialization matches a given actual template |
| 2847 | // argument list if the template arguments of the partial |
| 2848 | // specialization can be deduced from the actual template argument |
| 2849 | // list (14.8.2). |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2850 | |
| 2851 | // Unevaluated SFINAE context. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2852 | EnterExpressionEvaluationContext Unevaluated( |
| 2853 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2854 | SFINAETrap Trap(*this); |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2855 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2856 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2857 | Deduced.resize(Partial->getTemplateParameters()->size()); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2858 | if (TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2859 | = ::DeduceTemplateArguments(*this, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2860 | Partial->getTemplateParameters(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2861 | Partial->getTemplateArgs(), |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2862 | TemplateArgs, Info, Deduced)) |
| 2863 | return Result; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 2864 | |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2865 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2866 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2867 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2868 | if (Inst.isInvalid()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2869 | return TDK_InstantiationDepth; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2870 | |
Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2871 | if (Trap.hasErrorOccurred()) |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2872 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2873 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2874 | return ::FinishTemplateArgumentDeduction( |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 2875 | *this, Partial, /*IsPartialOrdering=*/false, TemplateArgs, Deduced, Info); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2876 | } |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2877 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2878 | /// Perform template argument deduction to determine whether |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2879 | /// the given template arguments match the given variable template |
| 2880 | /// partial specialization per C++ [temp.class.spec.match]. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2881 | Sema::TemplateDeductionResult |
| 2882 | Sema::DeduceTemplateArguments(VarTemplatePartialSpecializationDecl *Partial, |
| 2883 | const TemplateArgumentList &TemplateArgs, |
| 2884 | TemplateDeductionInfo &Info) { |
| 2885 | if (Partial->isInvalidDecl()) |
| 2886 | return TDK_Invalid; |
| 2887 | |
| 2888 | // C++ [temp.class.spec.match]p2: |
| 2889 | // A partial specialization matches a given actual template |
| 2890 | // argument list if the template arguments of the partial |
| 2891 | // specialization can be deduced from the actual template argument |
| 2892 | // list (14.8.2). |
| 2893 | |
| 2894 | // Unevaluated SFINAE context. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2895 | EnterExpressionEvaluationContext Unevaluated( |
| 2896 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2897 | SFINAETrap Trap(*this); |
| 2898 | |
| 2899 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 2900 | Deduced.resize(Partial->getTemplateParameters()->size()); |
| 2901 | if (TemplateDeductionResult Result = ::DeduceTemplateArguments( |
| 2902 | *this, Partial->getTemplateParameters(), Partial->getTemplateArgs(), |
| 2903 | TemplateArgs, Info, Deduced)) |
| 2904 | return Result; |
| 2905 | |
| 2906 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2907 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2908 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2909 | if (Inst.isInvalid()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2910 | return TDK_InstantiationDepth; |
| 2911 | |
| 2912 | if (Trap.hasErrorOccurred()) |
| 2913 | return Sema::TDK_SubstitutionFailure; |
| 2914 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2915 | return ::FinishTemplateArgumentDeduction( |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 2916 | *this, Partial, /*IsPartialOrdering=*/false, TemplateArgs, Deduced, Info); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2917 | } |
| 2918 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2919 | /// Determine whether the given type T is a simple-template-id type. |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2920 | static bool isSimpleTemplateIdType(QualType T) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2921 | if (const TemplateSpecializationType *Spec |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2922 | = T->getAs<TemplateSpecializationType>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2923 | return Spec->getTemplateName().getAsTemplateDecl() != nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2924 | |
Richard Smith | 1363e8f | 2017-09-07 07:22:36 +0000 | [diff] [blame] | 2925 | // C++17 [temp.local]p2: |
| 2926 | // the injected-class-name [...] is equivalent to the template-name followed |
| 2927 | // by the template-arguments of the class template specialization or partial |
| 2928 | // specialization enclosed in <> |
| 2929 | // ... which means it's equivalent to a simple-template-id. |
| 2930 | // |
| 2931 | // This only arises during class template argument deduction for a copy |
| 2932 | // deduction candidate, where it permits slicing. |
| 2933 | if (T->getAs<InjectedClassNameType>()) |
| 2934 | return true; |
| 2935 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2936 | return false; |
| 2937 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2938 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2939 | /// Substitute the explicitly-provided template arguments into the |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2940 | /// given function template according to C++ [temp.arg.explicit]. |
| 2941 | /// |
| 2942 | /// \param FunctionTemplate the function template into which the explicit |
| 2943 | /// template arguments will be substituted. |
| 2944 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2945 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2946 | /// arguments. |
| 2947 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2948 | /// \param Deduced the deduced template arguments, which will be populated |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2949 | /// with the converted and checked explicit template arguments. |
| 2950 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2951 | /// \param ParamTypes will be populated with the instantiated function |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2952 | /// parameters. |
| 2953 | /// |
| 2954 | /// \param FunctionType if non-NULL, the result type of the function template |
| 2955 | /// will also be instantiated and the pointed-to value will be updated with |
| 2956 | /// the instantiated function type. |
| 2957 | /// |
| 2958 | /// \param Info if substitution fails for any reason, this object will be |
| 2959 | /// populated with more information about the failure. |
| 2960 | /// |
| 2961 | /// \returns TDK_Success if substitution was successful, or some failure |
| 2962 | /// condition. |
| 2963 | Sema::TemplateDeductionResult |
| 2964 | Sema::SubstituteExplicitTemplateArguments( |
| 2965 | FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2966 | TemplateArgumentListInfo &ExplicitTemplateArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2967 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2968 | SmallVectorImpl<QualType> &ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2969 | QualType *FunctionType, |
| 2970 | TemplateDeductionInfo &Info) { |
| 2971 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 2972 | TemplateParameterList *TemplateParams |
| 2973 | = FunctionTemplate->getTemplateParameters(); |
| 2974 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2975 | if (ExplicitTemplateArgs.size() == 0) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2976 | // No arguments to substitute; just copy over the parameter types and |
| 2977 | // fill in the function type. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2978 | for (auto P : Function->parameters()) |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 2979 | ParamTypes.push_back(P->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2980 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2981 | if (FunctionType) |
| 2982 | *FunctionType = Function->getType(); |
| 2983 | return TDK_Success; |
| 2984 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2985 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2986 | // Unevaluated SFINAE context. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2987 | EnterExpressionEvaluationContext Unevaluated( |
| 2988 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2989 | SFINAETrap Trap(*this); |
| 2990 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2991 | // C++ [temp.arg.explicit]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2992 | // Template arguments that are present shall be specified in the |
| 2993 | // declaration order of their corresponding template-parameters. The |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2994 | // template argument list shall not specify more template-arguments than |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2995 | // there are corresponding template-parameters. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2996 | SmallVector<TemplateArgument, 4> Builder; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2997 | |
| 2998 | // Enter a new template instantiation context where we check the |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2999 | // explicitly-specified template arguments against this function template, |
| 3000 | // and then substitute them into the function parameter types. |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3001 | SmallVector<TemplateArgument, 4> DeducedArgs; |
Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 3002 | InstantiatingTemplate Inst( |
| 3003 | *this, Info.getLocation(), FunctionTemplate, DeducedArgs, |
| 3004 | CodeSynthesisContext::ExplicitTemplateArgumentSubstitution, Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3005 | if (Inst.isInvalid()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3006 | return TDK_InstantiationDepth; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3007 | |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 3008 | if (CheckTemplateArgumentList(FunctionTemplate, SourceLocation(), |
| 3009 | ExplicitTemplateArgs, true, Builder, false) || |
| 3010 | Trap.hasErrorOccurred()) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 3011 | unsigned Index = Builder.size(); |
Douglas Gregor | 62c281a | 2010-05-09 01:26:06 +0000 | [diff] [blame] | 3012 | if (Index >= TemplateParams->size()) |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 3013 | return TDK_SubstitutionFailure; |
Douglas Gregor | 62c281a | 2010-05-09 01:26:06 +0000 | [diff] [blame] | 3014 | Info.Param = makeTemplateParameter(TemplateParams->getParam(Index)); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3015 | return TDK_InvalidExplicitArguments; |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 3016 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3017 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3018 | // Form the template argument list from the explicitly-specified |
| 3019 | // template arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3020 | TemplateArgumentList *ExplicitArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3021 | = TemplateArgumentList::CreateCopy(Context, Builder); |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 3022 | Info.setExplicitArgs(ExplicitArgumentList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3023 | |
John McCall | 036855a | 2010-10-12 19:40:14 +0000 | [diff] [blame] | 3024 | // Template argument deduction and the final substitution should be |
| 3025 | // done in the context of the templated declaration. Explicit |
| 3026 | // argument substitution, on the other hand, needs to happen in the |
| 3027 | // calling context. |
| 3028 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
| 3029 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3030 | // If we deduced template arguments for a template parameter pack, |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3031 | // note that the template argument pack is partially substituted and record |
| 3032 | // the explicit template arguments. They'll be used as part of deduction |
| 3033 | // for this template parameter pack. |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 3034 | unsigned PartiallySubstitutedPackIndex = -1u; |
| 3035 | if (!Builder.empty()) { |
| 3036 | const TemplateArgument &Arg = Builder.back(); |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3037 | if (Arg.getKind() == TemplateArgument::Pack) { |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 3038 | auto *Param = TemplateParams->getParam(Builder.size() - 1); |
| 3039 | // If this is a fully-saturated fixed-size pack, it should be |
| 3040 | // fully-substituted, not partially-substituted. |
| 3041 | Optional<unsigned> Expansions = getExpandedPackSize(Param); |
| 3042 | if (!Expansions || Arg.pack_size() < *Expansions) { |
| 3043 | PartiallySubstitutedPackIndex = Builder.size() - 1; |
| 3044 | CurrentInstantiationScope->SetPartiallySubstitutedPack( |
| 3045 | Param, Arg.pack_begin(), Arg.pack_size()); |
| 3046 | } |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3047 | } |
| 3048 | } |
| 3049 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 3050 | const FunctionProtoType *Proto |
| 3051 | = Function->getType()->getAs<FunctionProtoType>(); |
| 3052 | assert(Proto && "Function template does not have a prototype?"); |
| 3053 | |
Richard Smith | 70b1304 | 2015-01-09 01:19:56 +0000 | [diff] [blame] | 3054 | // Isolate our substituted parameters from our caller. |
| 3055 | LocalInstantiationScope InstScope(*this, /*MergeWithOuterScope*/true); |
| 3056 | |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 3057 | ExtParameterInfoBuilder ExtParamInfos; |
| 3058 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3059 | // Instantiate the types of each of the function parameters given the |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 3060 | // explicitly-specified template arguments. If the function has a trailing |
| 3061 | // return type, substitute it after the arguments to ensure we substitute |
| 3062 | // in lexical order. |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 3063 | if (Proto->hasTrailingReturn()) { |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3064 | if (SubstParmTypes(Function->getLocation(), Function->parameters(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 3065 | Proto->getExtParameterInfosOrNull(), |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 3066 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 3067 | ParamTypes, /*params*/ nullptr, ExtParamInfos)) |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 3068 | return TDK_SubstitutionFailure; |
| 3069 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3070 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 3071 | // Instantiate the return type. |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 3072 | QualType ResultType; |
| 3073 | { |
| 3074 | // C++11 [expr.prim.general]p3: |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3075 | // If a declaration declares a member function or member function |
| 3076 | // template of a class X, the expression this is a prvalue of type |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 3077 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3078 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 3079 | // declarator. |
Mikael Nilsson | 9d2872d | 2018-12-13 10:15:27 +0000 | [diff] [blame] | 3080 | Qualifiers ThisTypeQuals; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3081 | CXXRecordDecl *ThisContext = nullptr; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 3082 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
| 3083 | ThisContext = Method->getParent(); |
Anastasia Stulova | c61eaa5 | 2019-01-28 11:37:49 +0000 | [diff] [blame] | 3084 | ThisTypeQuals = Method->getMethodQualifiers(); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 3085 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3086 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 3087 | CXXThisScopeRAII ThisScope(*this, ThisContext, ThisTypeQuals, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3088 | getLangOpts().CPlusPlus11); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3089 | |
| 3090 | ResultType = |
| 3091 | SubstType(Proto->getReturnType(), |
| 3092 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 3093 | Function->getTypeSpecStartLoc(), Function->getDeclName()); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 3094 | if (ResultType.isNull() || Trap.hasErrorOccurred()) |
| 3095 | return TDK_SubstitutionFailure; |
Michael Liao | 24337db | 2019-09-25 16:51:45 +0000 | [diff] [blame^] | 3096 | // CUDA: Kernel function must have 'void' return type. |
| 3097 | if (getLangOpts().CUDA) |
| 3098 | if (Function->hasAttr<CUDAGlobalAttr>() && !ResultType->isVoidType()) { |
| 3099 | Diag(Function->getLocation(), diag::err_kern_type_not_void_return) |
| 3100 | << Function->getType() << Function->getSourceRange(); |
| 3101 | return TDK_SubstitutionFailure; |
| 3102 | } |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 3103 | } |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 3104 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 3105 | // Instantiate the types of each of the function parameters given the |
| 3106 | // explicitly-specified template arguments if we didn't do so earlier. |
| 3107 | if (!Proto->hasTrailingReturn() && |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 3108 | SubstParmTypes(Function->getLocation(), Function->parameters(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 3109 | Proto->getExtParameterInfosOrNull(), |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 3110 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 3111 | ParamTypes, /*params*/ nullptr, ExtParamInfos)) |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 3112 | return TDK_SubstitutionFailure; |
| 3113 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3114 | if (FunctionType) { |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 3115 | auto EPI = Proto->getExtProtoInfo(); |
| 3116 | EPI.ExtParameterInfos = ExtParamInfos.getPointerOrNull(ParamTypes.size()); |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 3117 | |
| 3118 | // In C++1z onwards, exception specifications are part of the function type, |
| 3119 | // so substitution into the type must also substitute into the exception |
| 3120 | // specification. |
| 3121 | SmallVector<QualType, 4> ExceptionStorage; |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3122 | if (getLangOpts().CPlusPlus17 && |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 3123 | SubstExceptionSpec( |
| 3124 | Function->getLocation(), EPI.ExceptionSpec, ExceptionStorage, |
| 3125 | MultiLevelTemplateArgumentList(*ExplicitArgumentList))) |
| 3126 | return TDK_SubstitutionFailure; |
| 3127 | |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 3128 | *FunctionType = BuildFunctionType(ResultType, ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3129 | Function->getLocation(), |
Eli Friedman | d8725a9 | 2010-08-05 02:54:05 +0000 | [diff] [blame] | 3130 | Function->getDeclName(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 3131 | EPI); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3132 | if (FunctionType->isNull() || Trap.hasErrorOccurred()) |
| 3133 | return TDK_SubstitutionFailure; |
| 3134 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3135 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3136 | // C++ [temp.arg.explicit]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3137 | // Trailing template arguments that can be deduced (14.8.2) may be |
| 3138 | // omitted from the list of explicit template-arguments. If all of the |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3139 | // template arguments can be deduced, they may all be omitted; in this |
| 3140 | // case, the empty template argument list <> itself may also be omitted. |
| 3141 | // |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3142 | // Take all of the explicitly-specified arguments and put them into |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 3143 | // the set of deduced template arguments. The partially-substituted |
| 3144 | // parameter pack, however, will be set to NULL since the deduction |
| 3145 | // mechanism handles the partially-substituted argument pack directly. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3146 | Deduced.reserve(TemplateParams->size()); |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3147 | for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) { |
| 3148 | const TemplateArgument &Arg = ExplicitArgumentList->get(I); |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 3149 | if (I == PartiallySubstitutedPackIndex) |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3150 | Deduced.push_back(DeducedTemplateArgument()); |
| 3151 | else |
| 3152 | Deduced.push_back(Arg); |
| 3153 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3154 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3155 | return TDK_Success; |
| 3156 | } |
| 3157 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3158 | /// Check whether the deduced argument type for a call to a function |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3159 | /// template matches the actual argument type per C++ [temp.deduct.call]p4. |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3160 | static Sema::TemplateDeductionResult |
| 3161 | CheckOriginalCallArgDeduction(Sema &S, TemplateDeductionInfo &Info, |
| 3162 | Sema::OriginalCallArg OriginalArg, |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3163 | QualType DeducedA) { |
| 3164 | ASTContext &Context = S.Context; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3165 | |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3166 | auto Failed = [&]() -> Sema::TemplateDeductionResult { |
| 3167 | Info.FirstArg = TemplateArgument(DeducedA); |
| 3168 | Info.SecondArg = TemplateArgument(OriginalArg.OriginalArgType); |
| 3169 | Info.CallArgIndex = OriginalArg.ArgIdx; |
| 3170 | return OriginalArg.DecomposedParam ? Sema::TDK_DeducedMismatchNested |
| 3171 | : Sema::TDK_DeducedMismatch; |
| 3172 | }; |
| 3173 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3174 | QualType A = OriginalArg.OriginalArgType; |
| 3175 | QualType OriginalParamType = OriginalArg.OriginalParamType; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3176 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3177 | // Check for type equality (top-level cv-qualifiers are ignored). |
| 3178 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3179 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3180 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3181 | // Strip off references on the argument types; they aren't needed for |
| 3182 | // the following checks. |
| 3183 | if (const ReferenceType *DeducedARef = DeducedA->getAs<ReferenceType>()) |
| 3184 | DeducedA = DeducedARef->getPointeeType(); |
| 3185 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
| 3186 | A = ARef->getPointeeType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3187 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3188 | // C++ [temp.deduct.call]p4: |
| 3189 | // [...] However, there are three cases that allow a difference: |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3190 | // - If the original P is a reference type, the deduced A (i.e., the |
| 3191 | // type referred to by the reference) can be more cv-qualified than |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3192 | // the transformed A. |
| 3193 | if (const ReferenceType *OriginalParamRef |
| 3194 | = OriginalParamType->getAs<ReferenceType>()) { |
| 3195 | // We don't want to keep the reference around any more. |
| 3196 | OriginalParamType = OriginalParamRef->getPointeeType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3197 | |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 3198 | // FIXME: Resolve core issue (no number yet): if the original P is a |
| 3199 | // reference type and the transformed A is function type "noexcept F", |
| 3200 | // the deduced A can be F. |
| 3201 | QualType Tmp; |
| 3202 | if (A->isFunctionType() && S.IsFunctionConversion(A, DeducedA, Tmp)) |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3203 | return Sema::TDK_Success; |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 3204 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3205 | Qualifiers AQuals = A.getQualifiers(); |
| 3206 | Qualifiers DeducedAQuals = DeducedA.getQualifiers(); |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 3207 | |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 3208 | // Under Objective-C++ ARC, the deduced type may have implicitly |
| 3209 | // been given strong or (when dealing with a const reference) |
| 3210 | // unsafe_unretained lifetime. If so, update the original |
| 3211 | // qualifiers to include this lifetime. |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 3212 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 3213 | ((DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_Strong && |
| 3214 | AQuals.getObjCLifetime() == Qualifiers::OCL_None) || |
| 3215 | (DeducedAQuals.hasConst() && |
| 3216 | DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone))) { |
| 3217 | AQuals.setObjCLifetime(DeducedAQuals.getObjCLifetime()); |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 3218 | } |
| 3219 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3220 | if (AQuals == DeducedAQuals) { |
| 3221 | // Qualifiers match; there's nothing to do. |
| 3222 | } else if (!DeducedAQuals.compatiblyIncludes(AQuals)) { |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3223 | return Failed(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3224 | } else { |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3225 | // Qualifiers are compatible, so have the argument type adopt the |
| 3226 | // deduced argument type's qualifiers as if we had performed the |
| 3227 | // qualification conversion. |
| 3228 | A = Context.getQualifiedType(A.getUnqualifiedType(), DeducedAQuals); |
| 3229 | } |
| 3230 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3231 | |
| 3232 | // - The transformed A can be another pointer or pointer to member |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3233 | // type that can be converted to the deduced A via a function pointer |
| 3234 | // conversion and/or a qualification conversion. |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 3235 | // |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 3236 | // Also allow conversions which merely strip __attribute__((noreturn)) from |
| 3237 | // function types (recursively). |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3238 | bool ObjCLifetimeConversion = false; |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 3239 | QualType ResultTy; |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3240 | if ((A->isAnyPointerType() || A->isMemberPointerType()) && |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 3241 | (S.IsQualificationConversion(A, DeducedA, false, |
| 3242 | ObjCLifetimeConversion) || |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3243 | S.IsFunctionConversion(A, DeducedA, ResultTy))) |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3244 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3245 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3246 | // - If P is a class and P has the form simple-template-id, then the |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3247 | // transformed A can be a derived class of the deduced A. [...] |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3248 | // [...] Likewise, if P is a pointer to a class of the form |
| 3249 | // simple-template-id, the transformed A can be a pointer to a |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3250 | // derived class pointed to by the deduced A. |
| 3251 | if (const PointerType *OriginalParamPtr |
| 3252 | = OriginalParamType->getAs<PointerType>()) { |
| 3253 | if (const PointerType *DeducedAPtr = DeducedA->getAs<PointerType>()) { |
| 3254 | if (const PointerType *APtr = A->getAs<PointerType>()) { |
| 3255 | if (A->getPointeeType()->isRecordType()) { |
| 3256 | OriginalParamType = OriginalParamPtr->getPointeeType(); |
| 3257 | DeducedA = DeducedAPtr->getPointeeType(); |
| 3258 | A = APtr->getPointeeType(); |
| 3259 | } |
| 3260 | } |
| 3261 | } |
| 3262 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3263 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3264 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3265 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3266 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3267 | if (A->isRecordType() && isSimpleTemplateIdType(OriginalParamType) && |
Richard Smith | 148bc6a | 2018-02-20 23:47:12 +0000 | [diff] [blame] | 3268 | S.IsDerivedFrom(Info.getLocation(), A, DeducedA)) |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3269 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3270 | |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3271 | return Failed(); |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3272 | } |
| 3273 | |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3274 | /// Find the pack index for a particular parameter index in an instantiation of |
| 3275 | /// a function template with specific arguments. |
| 3276 | /// |
| 3277 | /// \return The pack index for whichever pack produced this parameter, or -1 |
| 3278 | /// if this was not produced by a parameter. Intended to be used as the |
| 3279 | /// ArgumentPackSubstitutionIndex for further substitutions. |
| 3280 | // FIXME: We should track this in OriginalCallArgs so we don't need to |
| 3281 | // reconstruct it here. |
| 3282 | static unsigned getPackIndexForParam(Sema &S, |
| 3283 | FunctionTemplateDecl *FunctionTemplate, |
| 3284 | const MultiLevelTemplateArgumentList &Args, |
| 3285 | unsigned ParamIdx) { |
| 3286 | unsigned Idx = 0; |
| 3287 | for (auto *PD : FunctionTemplate->getTemplatedDecl()->parameters()) { |
| 3288 | if (PD->isParameterPack()) { |
| 3289 | unsigned NumExpansions = |
| 3290 | S.getNumArgumentsInExpansion(PD->getType(), Args).getValueOr(1); |
| 3291 | if (Idx + NumExpansions > ParamIdx) |
| 3292 | return ParamIdx - Idx; |
| 3293 | Idx += NumExpansions; |
| 3294 | } else { |
| 3295 | if (Idx == ParamIdx) |
| 3296 | return -1; // Not a pack expansion |
| 3297 | ++Idx; |
| 3298 | } |
| 3299 | } |
| 3300 | |
| 3301 | llvm_unreachable("parameter index would not be produced from template"); |
| 3302 | } |
| 3303 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3304 | /// Finish template argument deduction for a function template, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3305 | /// checking the deduced template arguments for completeness and forming |
| 3306 | /// the function template specialization. |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3307 | /// |
| 3308 | /// \param OriginalCallArgs If non-NULL, the original call arguments against |
| 3309 | /// which the deduced argument types should be compared. |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3310 | Sema::TemplateDeductionResult Sema::FinishTemplateArgumentDeduction( |
| 3311 | FunctionTemplateDecl *FunctionTemplate, |
| 3312 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3313 | unsigned NumExplicitlySpecified, FunctionDecl *&Specialization, |
| 3314 | TemplateDeductionInfo &Info, |
| 3315 | SmallVectorImpl<OriginalCallArg> const *OriginalCallArgs, |
| 3316 | bool PartialOverloading, llvm::function_ref<bool()> CheckNonDependent) { |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3317 | // Unevaluated SFINAE context. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 3318 | EnterExpressionEvaluationContext Unevaluated( |
| 3319 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3320 | SFINAETrap Trap(*this); |
| 3321 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3322 | // Enter a new template instantiation context while we instantiate the |
| 3323 | // actual function declaration. |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3324 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 3325 | InstantiatingTemplate Inst( |
| 3326 | *this, Info.getLocation(), FunctionTemplate, DeducedArgs, |
| 3327 | CodeSynthesisContext::DeducedTemplateArgumentSubstitution, Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3328 | if (Inst.isInvalid()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3329 | return TDK_InstantiationDepth; |
| 3330 | |
John McCall | e23b871 | 2010-04-29 01:18:58 +0000 | [diff] [blame] | 3331 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
John McCall | 80e58cd | 2010-04-29 00:35:03 +0000 | [diff] [blame] | 3332 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3333 | // C++ [temp.deduct.type]p2: |
| 3334 | // [...] or if any template argument remains neither deduced nor |
| 3335 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3336 | SmallVector<TemplateArgument, 4> Builder; |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 3337 | if (auto Result = ConvertDeducedTemplateArguments( |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 3338 | *this, FunctionTemplate, /*IsDeduced*/true, Deduced, Info, Builder, |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 3339 | CurrentInstantiationScope, NumExplicitlySpecified, |
| 3340 | PartialOverloading)) |
| 3341 | return Result; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3342 | |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3343 | // C++ [temp.deduct.call]p10: [DR1391] |
| 3344 | // If deduction succeeds for all parameters that contain |
| 3345 | // template-parameters that participate in template argument deduction, |
| 3346 | // and all template arguments are explicitly specified, deduced, or |
| 3347 | // obtained from default template arguments, remaining parameters are then |
| 3348 | // compared with the corresponding arguments. For each remaining parameter |
| 3349 | // P with a type that was non-dependent before substitution of any |
| 3350 | // explicitly-specified template arguments, if the corresponding argument |
| 3351 | // A cannot be implicitly converted to P, deduction fails. |
| 3352 | if (CheckNonDependent()) |
| 3353 | return TDK_NonDependentConversionFailure; |
| 3354 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3355 | // Form the template argument list from the deduced template arguments. |
| 3356 | TemplateArgumentList *DeducedArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3357 | = TemplateArgumentList::CreateCopy(Context, Builder); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3358 | Info.reset(DeducedArgumentList); |
| 3359 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3360 | // Substitute the deduced template arguments into the function template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3361 | // declaration to produce the function template specialization. |
Douglas Gregor | 1614237 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 3362 | DeclContext *Owner = FunctionTemplate->getDeclContext(); |
| 3363 | if (FunctionTemplate->getFriendObjectKind()) |
| 3364 | Owner = FunctionTemplate->getLexicalDeclContext(); |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3365 | MultiLevelTemplateArgumentList SubstArgs(*DeducedArgumentList); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3366 | Specialization = cast_or_null<FunctionDecl>( |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3367 | SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner, SubstArgs)); |
Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 3368 | if (!Specialization || Specialization->isInvalidDecl()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3369 | return TDK_SubstitutionFailure; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3370 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3371 | assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == |
Douglas Gregor | 31fae89 | 2009-09-15 18:26:13 +0000 | [diff] [blame] | 3372 | FunctionTemplate->getCanonicalDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3373 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3374 | // If the template argument list is owned by the function template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3375 | // specialization, release it. |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 3376 | if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList && |
| 3377 | !Trap.hasErrorOccurred()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3378 | Info.take(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3379 | |
Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 3380 | // There may have been an error that did not prevent us from constructing a |
| 3381 | // declaration. Mark the declaration invalid and return with a substitution |
| 3382 | // failure. |
| 3383 | if (Trap.hasErrorOccurred()) { |
| 3384 | Specialization->setInvalidDecl(true); |
| 3385 | return TDK_SubstitutionFailure; |
| 3386 | } |
| 3387 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3388 | if (OriginalCallArgs) { |
| 3389 | // C++ [temp.deduct.call]p4: |
| 3390 | // In general, the deduction process attempts to find template argument |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3391 | // values that will make the deduced A identical to A (after the type A |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3392 | // is transformed as described above). [...] |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3393 | llvm::SmallDenseMap<std::pair<unsigned, QualType>, QualType> DeducedATypes; |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3394 | for (unsigned I = 0, N = OriginalCallArgs->size(); I != N; ++I) { |
| 3395 | OriginalCallArg OriginalArg = (*OriginalCallArgs)[I]; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3396 | |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3397 | auto ParamIdx = OriginalArg.ArgIdx; |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3398 | if (ParamIdx >= Specialization->getNumParams()) |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3399 | // FIXME: This presumably means a pack ended up smaller than we |
| 3400 | // expected while deducing. Should this not result in deduction |
| 3401 | // failure? Can it even happen? |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3402 | continue; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3403 | |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3404 | QualType DeducedA; |
| 3405 | if (!OriginalArg.DecomposedParam) { |
| 3406 | // P is one of the function parameters, just look up its substituted |
| 3407 | // type. |
| 3408 | DeducedA = Specialization->getParamDecl(ParamIdx)->getType(); |
| 3409 | } else { |
| 3410 | // P is a decomposed element of a parameter corresponding to a |
| 3411 | // braced-init-list argument. Substitute back into P to find the |
| 3412 | // deduced A. |
| 3413 | QualType &CacheEntry = |
| 3414 | DeducedATypes[{ParamIdx, OriginalArg.OriginalParamType}]; |
| 3415 | if (CacheEntry.isNull()) { |
| 3416 | ArgumentPackSubstitutionIndexRAII PackIndex( |
| 3417 | *this, getPackIndexForParam(*this, FunctionTemplate, SubstArgs, |
| 3418 | ParamIdx)); |
| 3419 | CacheEntry = |
| 3420 | SubstType(OriginalArg.OriginalParamType, SubstArgs, |
| 3421 | Specialization->getTypeSpecStartLoc(), |
| 3422 | Specialization->getDeclName()); |
| 3423 | } |
| 3424 | DeducedA = CacheEntry; |
| 3425 | } |
| 3426 | |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3427 | if (auto TDK = |
| 3428 | CheckOriginalCallArgDeduction(*this, Info, OriginalArg, DeducedA)) |
| 3429 | return TDK; |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3430 | } |
| 3431 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3432 | |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3433 | // If we suppressed any diagnostics while performing template argument |
| 3434 | // deduction, and if we haven't already instantiated this declaration, |
| 3435 | // keep track of these diagnostics. They'll be emitted if this specialization |
| 3436 | // is actually used. |
| 3437 | if (Info.diag_begin() != Info.diag_end()) { |
Craig Topper | 79be4cd | 2013-07-05 04:33:53 +0000 | [diff] [blame] | 3438 | SuppressedDiagnosticsMap::iterator |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3439 | Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl()); |
| 3440 | if (Pos == SuppressedDiagnostics.end()) |
| 3441 | SuppressedDiagnostics[Specialization->getCanonicalDecl()] |
| 3442 | .append(Info.diag_begin(), Info.diag_end()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3443 | } |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3444 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3445 | return TDK_Success; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3446 | } |
| 3447 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3448 | /// Gets the type of a function for template-argument-deducton |
| 3449 | /// purposes when it's considered as part of an overload set. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3450 | static QualType GetTypeOfFunction(Sema &S, const OverloadExpr::FindResult &R, |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3451 | FunctionDecl *Fn) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3452 | // We may need to deduce the return type of the function now. |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3453 | if (S.getLangOpts().CPlusPlus14 && Fn->getReturnType()->isUndeducedType() && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3454 | S.DeduceReturnType(Fn, R.Expression->getExprLoc(), /*Diagnose*/ false)) |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 3455 | return {}; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3456 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3457 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3458 | if (Method->isInstance()) { |
| 3459 | // An instance method that's referenced in a form that doesn't |
| 3460 | // look like a member pointer is just invalid. |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 3461 | if (!R.HasFormOfMemberPointer) |
| 3462 | return {}; |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3463 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3464 | return S.Context.getMemberPointerType(Fn->getType(), |
| 3465 | S.Context.getTypeDeclType(Method->getParent()).getTypePtr()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3466 | } |
| 3467 | |
| 3468 | if (!R.IsAddressOfOperand) return Fn->getType(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3469 | return S.Context.getPointerType(Fn->getType()); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3470 | } |
| 3471 | |
| 3472 | /// Apply the deduction rules for overload sets. |
| 3473 | /// |
| 3474 | /// \return the null type if this argument should be treated as an |
| 3475 | /// undeduced context |
| 3476 | static QualType |
| 3477 | ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams, |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3478 | Expr *Arg, QualType ParamType, |
| 3479 | bool ParamWasReference) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3480 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3481 | OverloadExpr::FindResult R = OverloadExpr::find(Arg); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3482 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3483 | OverloadExpr *Ovl = R.Expression; |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3484 | |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3485 | // C++0x [temp.deduct.call]p4 |
| 3486 | unsigned TDF = 0; |
| 3487 | if (ParamWasReference) |
| 3488 | TDF |= TDF_ParamWithReferenceType; |
| 3489 | if (R.IsAddressOfOperand) |
| 3490 | TDF |= TDF_IgnoreQualifiers; |
| 3491 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3492 | // C++0x [temp.deduct.call]p6: |
| 3493 | // When P is a function type, pointer to function type, or pointer |
| 3494 | // to member function type: |
| 3495 | |
| 3496 | if (!ParamType->isFunctionType() && |
| 3497 | !ParamType->isFunctionPointerType() && |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3498 | !ParamType->isMemberFunctionPointerType()) { |
| 3499 | if (Ovl->hasExplicitTemplateArgs()) { |
| 3500 | // But we can still look for an explicit specialization. |
| 3501 | if (FunctionDecl *ExplicitSpec |
| 3502 | = S.ResolveSingleFunctionTemplateSpecialization(Ovl)) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3503 | return GetTypeOfFunction(S, R, ExplicitSpec); |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3504 | } |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3505 | |
George Burgess IV | cc2f355 | 2016-03-19 21:51:45 +0000 | [diff] [blame] | 3506 | DeclAccessPair DAP; |
| 3507 | if (FunctionDecl *Viable = |
| 3508 | S.resolveAddressOfOnlyViableOverloadCandidate(Arg, DAP)) |
| 3509 | return GetTypeOfFunction(S, R, Viable); |
| 3510 | |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 3511 | return {}; |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3512 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3513 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3514 | // Gather the explicit template arguments, if any. |
| 3515 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 3516 | if (Ovl->hasExplicitTemplateArgs()) |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 3517 | Ovl->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3518 | QualType Match; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 3519 | for (UnresolvedSetIterator I = Ovl->decls_begin(), |
| 3520 | E = Ovl->decls_end(); I != E; ++I) { |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3521 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 3522 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3523 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) { |
| 3524 | // - If the argument is an overload set containing one or more |
| 3525 | // function templates, the parameter is treated as a |
| 3526 | // non-deduced context. |
| 3527 | if (!Ovl->hasExplicitTemplateArgs()) |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 3528 | return {}; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3529 | |
| 3530 | // Otherwise, see if we can resolve a function type |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3531 | FunctionDecl *Specialization = nullptr; |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3532 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3533 | if (S.DeduceTemplateArguments(FunTmpl, &ExplicitTemplateArgs, |
| 3534 | Specialization, Info)) |
| 3535 | continue; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3536 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3537 | D = Specialization; |
| 3538 | } |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3539 | |
| 3540 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3541 | QualType ArgType = GetTypeOfFunction(S, R, Fn); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3542 | if (ArgType.isNull()) continue; |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3543 | |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3544 | // Function-to-pointer conversion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3545 | if (!ParamWasReference && ParamType->isPointerType() && |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3546 | ArgType->isFunctionType()) |
| 3547 | ArgType = S.Context.getPointerType(ArgType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3548 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3549 | // - If the argument is an overload set (not containing function |
| 3550 | // templates), trial argument deduction is attempted using each |
| 3551 | // of the members of the set. If deduction succeeds for only one |
| 3552 | // of the overload set members, that member is used as the |
| 3553 | // argument value for the deduction. If deduction succeeds for |
| 3554 | // more than one member of the overload set the parameter is |
| 3555 | // treated as a non-deduced context. |
| 3556 | |
| 3557 | // We do all of this in a fresh context per C++0x [temp.deduct.type]p2: |
| 3558 | // Type deduction is done independently for each P/A pair, and |
| 3559 | // the deduced template argument values are then combined. |
| 3560 | // So we do not reject deductions which were made elsewhere. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3561 | SmallVector<DeducedTemplateArgument, 8> |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3562 | Deduced(TemplateParams->size()); |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3563 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3564 | Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3565 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
| 3566 | ArgType, Info, Deduced, TDF); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3567 | if (Result) continue; |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 3568 | if (!Match.isNull()) |
| 3569 | return {}; |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3570 | Match = ArgType; |
| 3571 | } |
| 3572 | |
| 3573 | return Match; |
| 3574 | } |
| 3575 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3576 | /// Perform the adjustments to the parameter and argument types |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3577 | /// described in C++ [temp.deduct.call]. |
| 3578 | /// |
| 3579 | /// \returns true if the caller should not attempt to perform any template |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3580 | /// argument deduction based on this P/A pair because the argument is an |
| 3581 | /// overloaded function set that could not be resolved. |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3582 | static bool AdjustFunctionParmAndArgTypesForDeduction( |
| 3583 | Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, |
| 3584 | QualType &ParamType, QualType &ArgType, Expr *Arg, unsigned &TDF) { |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3585 | // C++0x [temp.deduct.call]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3586 | // If P is a cv-qualified type, the top level cv-qualifiers of P's type |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3587 | // are ignored for type deduction. |
Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3588 | if (ParamType.hasQualifiers()) |
| 3589 | ParamType = ParamType.getUnqualifiedType(); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3590 | |
| 3591 | // [...] If P is a reference type, the type referred to by P is |
| 3592 | // used for type deduction. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3593 | const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>(); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3594 | if (ParamRefType) |
| 3595 | ParamType = ParamRefType->getPointeeType(); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3596 | |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3597 | // Overload sets usually make this parameter an undeduced context, |
| 3598 | // but there are sometimes special circumstances. Typically |
| 3599 | // involving a template-id-expr. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3600 | if (ArgType == S.Context.OverloadTy) { |
| 3601 | ArgType = ResolveOverloadForDeduction(S, TemplateParams, |
| 3602 | Arg, ParamType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3603 | ParamRefType != nullptr); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3604 | if (ArgType.isNull()) |
| 3605 | return true; |
| 3606 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3607 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3608 | if (ParamRefType) { |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3609 | // If the argument has incomplete array type, try to complete its type. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3610 | if (ArgType->isIncompleteArrayType()) { |
| 3611 | S.completeExprArrayBound(Arg); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3612 | ArgType = Arg->getType(); |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3613 | } |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3614 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3615 | // C++1z [temp.deduct.call]p3: |
| 3616 | // If P is a forwarding reference and the argument is an lvalue, the type |
| 3617 | // "lvalue reference to A" is used in place of A for type deduction. |
| 3618 | if (isForwardingReference(QualType(ParamRefType, 0), FirstInnerIndex) && |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3619 | Arg->isLValue()) |
| 3620 | ArgType = S.Context.getLValueReferenceType(ArgType); |
| 3621 | } else { |
| 3622 | // C++ [temp.deduct.call]p2: |
| 3623 | // If P is not a reference type: |
| 3624 | // - If A is an array type, the pointer type produced by the |
| 3625 | // array-to-pointer standard conversion (4.2) is used in place of |
| 3626 | // A for type deduction; otherwise, |
| 3627 | if (ArgType->isArrayType()) |
| 3628 | ArgType = S.Context.getArrayDecayedType(ArgType); |
| 3629 | // - If A is a function type, the pointer type produced by the |
| 3630 | // function-to-pointer standard conversion (4.3) is used in place |
| 3631 | // of A for type deduction; otherwise, |
| 3632 | else if (ArgType->isFunctionType()) |
| 3633 | ArgType = S.Context.getPointerType(ArgType); |
| 3634 | else { |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3635 | // - If A is a cv-qualified type, the top level cv-qualifiers of A's |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3636 | // type are ignored for type deduction. |
Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3637 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3638 | } |
| 3639 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3640 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3641 | // C++0x [temp.deduct.call]p4: |
| 3642 | // In general, the deduction process attempts to find template argument |
| 3643 | // values that will make the deduced A identical to A (after the type A |
| 3644 | // is transformed as described above). [...] |
| 3645 | TDF = TDF_SkipNonDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3646 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3647 | // - If the original P is a reference type, the deduced A (i.e., the |
| 3648 | // type referred to by the reference) can be more cv-qualified than |
| 3649 | // the transformed A. |
| 3650 | if (ParamRefType) |
| 3651 | TDF |= TDF_ParamWithReferenceType; |
| 3652 | // - The transformed A can be another pointer or pointer to member |
| 3653 | // type that can be converted to the deduced A via a qualification |
| 3654 | // conversion (4.4). |
| 3655 | if (ArgType->isPointerType() || ArgType->isMemberPointerType() || |
| 3656 | ArgType->isObjCObjectPointerType()) |
| 3657 | TDF |= TDF_IgnoreQualifiers; |
| 3658 | // - If P is a class and P has the form simple-template-id, then the |
| 3659 | // transformed A can be a derived class of the deduced A. Likewise, |
| 3660 | // if P is a pointer to a class of the form simple-template-id, the |
| 3661 | // transformed A can be a pointer to a derived class pointed to by |
| 3662 | // the deduced A. |
| 3663 | if (isSimpleTemplateIdType(ParamType) || |
| 3664 | (isa<PointerType>(ParamType) && |
| 3665 | isSimpleTemplateIdType( |
| 3666 | ParamType->getAs<PointerType>()->getPointeeType()))) |
| 3667 | TDF |= TDF_DerivedClass; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3668 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3669 | return false; |
| 3670 | } |
| 3671 | |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 3672 | static bool |
| 3673 | hasDeducibleTemplateParameters(Sema &S, FunctionTemplateDecl *FunctionTemplate, |
| 3674 | QualType T); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3675 | |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3676 | static Sema::TemplateDeductionResult DeduceTemplateArgumentsFromCallArgument( |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3677 | Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, |
| 3678 | QualType ParamType, Expr *Arg, TemplateDeductionInfo &Info, |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3679 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3680 | SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3681 | bool DecomposedParam, unsigned ArgIdx, unsigned TDF); |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3682 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3683 | /// Attempt template argument deduction from an initializer list |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3684 | /// deemed to be an argument in a function call. |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3685 | static Sema::TemplateDeductionResult DeduceFromInitializerList( |
| 3686 | Sema &S, TemplateParameterList *TemplateParams, QualType AdjustedParamType, |
| 3687 | InitListExpr *ILE, TemplateDeductionInfo &Info, |
| 3688 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3689 | SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, unsigned ArgIdx, |
| 3690 | unsigned TDF) { |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3691 | // C++ [temp.deduct.call]p1: (CWG 1591) |
| 3692 | // If removing references and cv-qualifiers from P gives |
| 3693 | // std::initializer_list<P0> or P0[N] for some P0 and N and the argument is |
| 3694 | // a non-empty initializer list, then deduction is performed instead for |
| 3695 | // each element of the initializer list, taking P0 as a function template |
| 3696 | // parameter type and the initializer element as its argument |
| 3697 | // |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3698 | // We've already removed references and cv-qualifiers here. |
Richard Smith | 9c5534c | 2017-01-05 04:16:30 +0000 | [diff] [blame] | 3699 | if (!ILE->getNumInits()) |
| 3700 | return Sema::TDK_Success; |
| 3701 | |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3702 | QualType ElTy; |
| 3703 | auto *ArrTy = S.Context.getAsArrayType(AdjustedParamType); |
| 3704 | if (ArrTy) |
| 3705 | ElTy = ArrTy->getElementType(); |
| 3706 | else if (!S.isStdInitializerList(AdjustedParamType, &ElTy)) { |
| 3707 | // Otherwise, an initializer list argument causes the parameter to be |
| 3708 | // considered a non-deduced context |
| 3709 | return Sema::TDK_Success; |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3710 | } |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3711 | |
Richard Smith | 0827541 | 2019-08-31 00:05:50 +0000 | [diff] [blame] | 3712 | // Resolving a core issue: a braced-init-list containing any designators is |
| 3713 | // a non-deduced context. |
| 3714 | for (Expr *E : ILE->inits()) |
| 3715 | if (isa<DesignatedInitExpr>(E)) |
| 3716 | return Sema::TDK_Success; |
| 3717 | |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3718 | // Deduction only needs to be done for dependent types. |
| 3719 | if (ElTy->isDependentType()) { |
| 3720 | for (Expr *E : ILE->inits()) { |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3721 | if (auto Result = DeduceTemplateArgumentsFromCallArgument( |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3722 | S, TemplateParams, 0, ElTy, E, Info, Deduced, OriginalCallArgs, true, |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3723 | ArgIdx, TDF)) |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3724 | return Result; |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3725 | } |
| 3726 | } |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3727 | |
| 3728 | // in the P0[N] case, if N is a non-type template parameter, N is deduced |
| 3729 | // from the length of the initializer list. |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3730 | if (auto *DependentArrTy = dyn_cast_or_null<DependentSizedArrayType>(ArrTy)) { |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3731 | // Determine the array bound is something we can deduce. |
| 3732 | if (NonTypeTemplateParmDecl *NTTP = |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3733 | getDeducedParameterFromExpr(Info, DependentArrTy->getSizeExpr())) { |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3734 | // We can perform template argument deduction for the given non-type |
| 3735 | // template parameter. |
Richard Smith | 7fa88bb | 2017-02-21 07:22:31 +0000 | [diff] [blame] | 3736 | // C++ [temp.deduct.type]p13: |
| 3737 | // The type of N in the type T[N] is std::size_t. |
| 3738 | QualType T = S.Context.getSizeType(); |
| 3739 | llvm::APInt Size(S.Context.getIntWidth(T), ILE->getNumInits()); |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3740 | if (auto Result = DeduceNonTypeTemplateArgument( |
Richard Smith | 7fa88bb | 2017-02-21 07:22:31 +0000 | [diff] [blame] | 3741 | S, TemplateParams, NTTP, llvm::APSInt(Size), T, |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3742 | /*ArrayBound=*/true, Info, Deduced)) |
| 3743 | return Result; |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3744 | } |
| 3745 | } |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3746 | |
| 3747 | return Sema::TDK_Success; |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3748 | } |
| 3749 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3750 | /// Perform template argument deduction per [temp.deduct.call] for a |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3751 | /// single parameter / argument pair. |
| 3752 | static Sema::TemplateDeductionResult DeduceTemplateArgumentsFromCallArgument( |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3753 | Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, |
| 3754 | QualType ParamType, Expr *Arg, TemplateDeductionInfo &Info, |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3755 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3756 | SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3757 | bool DecomposedParam, unsigned ArgIdx, unsigned TDF) { |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3758 | QualType ArgType = Arg->getType(); |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3759 | QualType OrigParamType = ParamType; |
| 3760 | |
| 3761 | // If P is a reference type [...] |
| 3762 | // If P is a cv-qualified type [...] |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3763 | if (AdjustFunctionParmAndArgTypesForDeduction( |
| 3764 | S, TemplateParams, FirstInnerIndex, ParamType, ArgType, Arg, TDF)) |
Richard Smith | 363ae81 | 2017-01-04 22:03:59 +0000 | [diff] [blame] | 3765 | return Sema::TDK_Success; |
| 3766 | |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3767 | // If [...] the argument is a non-empty initializer list [...] |
| 3768 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) |
| 3769 | return DeduceFromInitializerList(S, TemplateParams, ParamType, ILE, Info, |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3770 | Deduced, OriginalCallArgs, ArgIdx, TDF); |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3771 | |
| 3772 | // [...] the deduction process attempts to find template argument values |
| 3773 | // that will make the deduced A identical to A |
| 3774 | // |
| 3775 | // Keep track of the argument type and corresponding parameter index, |
| 3776 | // so we can check for compatibility between the deduced A and A. |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3777 | OriginalCallArgs.push_back( |
| 3778 | Sema::OriginalCallArg(OrigParamType, DecomposedParam, ArgIdx, ArgType)); |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3779 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3780 | ArgType, Info, Deduced, TDF); |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3781 | } |
| 3782 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 3783 | /// Perform template argument deduction from a function call |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3784 | /// (C++ [temp.deduct.call]). |
| 3785 | /// |
| 3786 | /// \param FunctionTemplate the function template for which we are performing |
| 3787 | /// template argument deduction. |
| 3788 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3789 | /// \param ExplicitTemplateArgs the explicit template arguments provided |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 3790 | /// for this call. |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3791 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3792 | /// \param Args the function call arguments |
| 3793 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3794 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3795 | /// this will be set to the function template specialization produced by |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3796 | /// template argument deduction. |
| 3797 | /// |
| 3798 | /// \param Info the argument will be updated to provide additional information |
| 3799 | /// about template argument deduction. |
| 3800 | /// |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3801 | /// \param CheckNonDependent A callback to invoke to check conversions for |
| 3802 | /// non-dependent parameters, between deduction and substitution, per DR1391. |
| 3803 | /// If this returns true, substitution will be skipped and we return |
| 3804 | /// TDK_NonDependentConversionFailure. The callback is passed the parameter |
| 3805 | /// types (after substituting explicit template arguments). |
| 3806 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3807 | /// \returns the result of template argument deduction. |
Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 3808 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3809 | FunctionTemplateDecl *FunctionTemplate, |
| 3810 | TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3811 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3812 | bool PartialOverloading, |
| 3813 | llvm::function_ref<bool(ArrayRef<QualType>)> CheckNonDependent) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3814 | if (FunctionTemplate->isInvalidDecl()) |
| 3815 | return TDK_Invalid; |
| 3816 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3817 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3818 | unsigned NumParams = Function->getNumParams(); |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3819 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3820 | unsigned FirstInnerIndex = getFirstInnerIndex(FunctionTemplate); |
| 3821 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3822 | // C++ [temp.deduct.call]p1: |
| 3823 | // Template argument deduction is done by comparing each function template |
| 3824 | // parameter type (call it P) with the type of the corresponding argument |
| 3825 | // of the call (call it A) as described below. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3826 | if (Args.size() < Function->getMinRequiredArguments() && !PartialOverloading) |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3827 | return TDK_TooFewArguments; |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3828 | else if (TooManyArguments(NumParams, Args.size(), PartialOverloading)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3829 | const FunctionProtoType *Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3830 | = Function->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3831 | if (Proto->isTemplateVariadic()) |
| 3832 | /* Do nothing */; |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3833 | else if (!Proto->isVariadic()) |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3834 | return TDK_TooManyArguments; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3835 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3836 | |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3837 | // The types of the parameters from which we will perform template argument |
| 3838 | // deduction. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3839 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3840 | TemplateParameterList *TemplateParams |
| 3841 | = FunctionTemplate->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3842 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3843 | SmallVector<QualType, 8> ParamTypes; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3844 | unsigned NumExplicitlySpecified = 0; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3845 | if (ExplicitTemplateArgs) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3846 | TemplateDeductionResult Result = |
| 3847 | SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3848 | *ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3849 | Deduced, |
| 3850 | ParamTypes, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3851 | nullptr, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3852 | Info); |
| 3853 | if (Result) |
| 3854 | return Result; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3855 | |
| 3856 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3857 | } else { |
| 3858 | // Just fill in the parameter types from the function declaration. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3859 | for (unsigned I = 0; I != NumParams; ++I) |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3860 | ParamTypes.push_back(Function->getParamDecl(I)->getType()); |
| 3861 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3862 | |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3863 | SmallVector<OriginalCallArg, 8> OriginalCallArgs; |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3864 | |
| 3865 | // Deduce an argument of type ParamType from an expression with index ArgIdx. |
| 3866 | auto DeduceCallArgument = [&](QualType ParamType, unsigned ArgIdx) { |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3867 | // C++ [demp.deduct.call]p1: (DR1391) |
| 3868 | // Template argument deduction is done by comparing each function template |
| 3869 | // parameter that contains template-parameters that participate in |
| 3870 | // template argument deduction ... |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 3871 | if (!hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3872 | return Sema::TDK_Success; |
| 3873 | |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3874 | // ... with the type of the corresponding argument |
| 3875 | return DeduceTemplateArgumentsFromCallArgument( |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3876 | *this, TemplateParams, FirstInnerIndex, ParamType, Args[ArgIdx], Info, Deduced, |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3877 | OriginalCallArgs, /*Decomposed*/false, ArgIdx, /*TDF*/ 0); |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3878 | }; |
| 3879 | |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3880 | // Deduce template arguments from the function parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3881 | Deduced.resize(TemplateParams->size()); |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3882 | SmallVector<QualType, 8> ParamTypesForArgChecking; |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3883 | for (unsigned ParamIdx = 0, NumParamTypes = ParamTypes.size(), ArgIdx = 0; |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3884 | ParamIdx != NumParamTypes; ++ParamIdx) { |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3885 | QualType ParamType = ParamTypes[ParamIdx]; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3886 | |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3887 | const PackExpansionType *ParamExpansion = |
| 3888 | dyn_cast<PackExpansionType>(ParamType); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3889 | if (!ParamExpansion) { |
| 3890 | // Simple case: matching a function parameter to a function argument. |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3891 | if (ArgIdx >= Args.size()) |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3892 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3893 | |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3894 | ParamTypesForArgChecking.push_back(ParamType); |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3895 | if (auto Result = DeduceCallArgument(ParamType, ArgIdx++)) |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3896 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3897 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3898 | continue; |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3899 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3900 | |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3901 | QualType ParamPattern = ParamExpansion->getPattern(); |
| 3902 | PackDeductionScope PackScope(*this, TemplateParams, Deduced, Info, |
| 3903 | ParamPattern); |
| 3904 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3905 | // C++0x [temp.deduct.call]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3906 | // For a function parameter pack that occurs at the end of the |
| 3907 | // parameter-declaration-list, the type A of each remaining argument of |
| 3908 | // the call is compared with the type P of the declarator-id of the |
| 3909 | // function parameter pack. Each comparison deduces template arguments |
| 3910 | // for subsequent positions in the template parameter packs expanded by |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3911 | // the function parameter pack. When a function parameter pack appears |
| 3912 | // in a non-deduced context [not at the end of the list], the type of |
| 3913 | // that parameter pack is never deduced. |
| 3914 | // |
| 3915 | // FIXME: The above rule allows the size of the parameter pack to change |
| 3916 | // after we skip it (in the non-deduced case). That makes no sense, so |
| 3917 | // we instead notionally deduce the pack against N arguments, where N is |
| 3918 | // the length of the explicitly-specified pack if it's expanded by the |
| 3919 | // parameter pack and 0 otherwise, and we treat each deduction as a |
| 3920 | // non-deduced context. |
Richard Smith | 4a8f351 | 2018-07-19 19:00:37 +0000 | [diff] [blame] | 3921 | if (ParamIdx + 1 == NumParamTypes || PackScope.hasFixedArity()) { |
| 3922 | for (; ArgIdx < Args.size() && PackScope.hasNextElement(); |
| 3923 | PackScope.nextPackElement(), ++ArgIdx) { |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3924 | ParamTypesForArgChecking.push_back(ParamPattern); |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3925 | if (auto Result = DeduceCallArgument(ParamPattern, ArgIdx)) |
| 3926 | return Result; |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3927 | } |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3928 | } else { |
| 3929 | // If the parameter type contains an explicitly-specified pack that we |
| 3930 | // could not expand, skip the number of parameters notionally created |
| 3931 | // by the expansion. |
| 3932 | Optional<unsigned> NumExpansions = ParamExpansion->getNumExpansions(); |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3933 | if (NumExpansions && !PackScope.isPartiallyExpanded()) { |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3934 | for (unsigned I = 0; I != *NumExpansions && ArgIdx < Args.size(); |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3935 | ++I, ++ArgIdx) { |
| 3936 | ParamTypesForArgChecking.push_back(ParamPattern); |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3937 | // FIXME: Should we add OriginalCallArgs for these? What if the |
| 3938 | // corresponding argument is a list? |
| 3939 | PackScope.nextPackElement(); |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3940 | } |
| 3941 | } |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3942 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3943 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3944 | // Build argument packs for each of the parameter packs expanded by this |
| 3945 | // pack expansion. |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 3946 | if (auto Result = PackScope.finish()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3947 | return Result; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3948 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3949 | |
Akira Hatanaka | 4ac16db | 2018-06-13 05:26:23 +0000 | [diff] [blame] | 3950 | // Capture the context in which the function call is made. This is the context |
| 3951 | // that is needed when the accessibility of template arguments is checked. |
| 3952 | DeclContext *CallingCtx = CurContext; |
| 3953 | |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3954 | return FinishTemplateArgumentDeduction( |
| 3955 | FunctionTemplate, Deduced, NumExplicitlySpecified, Specialization, Info, |
Akira Hatanaka | 4ac16db | 2018-06-13 05:26:23 +0000 | [diff] [blame] | 3956 | &OriginalCallArgs, PartialOverloading, [&, CallingCtx]() { |
| 3957 | ContextRAII SavedContext(*this, CallingCtx); |
| 3958 | return CheckNonDependent(ParamTypesForArgChecking); |
| 3959 | }); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3960 | } |
| 3961 | |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3962 | QualType Sema::adjustCCAndNoReturn(QualType ArgFunctionType, |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3963 | QualType FunctionType, |
| 3964 | bool AdjustExceptionSpec) { |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3965 | if (ArgFunctionType.isNull()) |
| 3966 | return ArgFunctionType; |
| 3967 | |
| 3968 | const FunctionProtoType *FunctionTypeP = |
| 3969 | FunctionType->castAs<FunctionProtoType>(); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3970 | const FunctionProtoType *ArgFunctionTypeP = |
| 3971 | ArgFunctionType->getAs<FunctionProtoType>(); |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3972 | |
| 3973 | FunctionProtoType::ExtProtoInfo EPI = ArgFunctionTypeP->getExtProtoInfo(); |
| 3974 | bool Rebuild = false; |
| 3975 | |
| 3976 | CallingConv CC = FunctionTypeP->getCallConv(); |
| 3977 | if (EPI.ExtInfo.getCC() != CC) { |
| 3978 | EPI.ExtInfo = EPI.ExtInfo.withCallingConv(CC); |
| 3979 | Rebuild = true; |
| 3980 | } |
| 3981 | |
| 3982 | bool NoReturn = FunctionTypeP->getNoReturnAttr(); |
| 3983 | if (EPI.ExtInfo.getNoReturn() != NoReturn) { |
| 3984 | EPI.ExtInfo = EPI.ExtInfo.withNoReturn(NoReturn); |
| 3985 | Rebuild = true; |
| 3986 | } |
| 3987 | |
| 3988 | if (AdjustExceptionSpec && (FunctionTypeP->hasExceptionSpec() || |
| 3989 | ArgFunctionTypeP->hasExceptionSpec())) { |
| 3990 | EPI.ExceptionSpec = FunctionTypeP->getExtProtoInfo().ExceptionSpec; |
| 3991 | Rebuild = true; |
| 3992 | } |
| 3993 | |
| 3994 | if (!Rebuild) |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3995 | return ArgFunctionType; |
| 3996 | |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3997 | return Context.getFunctionType(ArgFunctionTypeP->getReturnType(), |
| 3998 | ArgFunctionTypeP->getParamTypes(), EPI); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3999 | } |
| 4000 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4001 | /// Deduce template arguments when taking the address of a function |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4002 | /// template (C++ [temp.deduct.funcaddr]) or matching a specialization to |
| 4003 | /// a template. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4004 | /// |
| 4005 | /// \param FunctionTemplate the function template for which we are performing |
| 4006 | /// template argument deduction. |
| 4007 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 4008 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4009 | /// arguments. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4010 | /// |
| 4011 | /// \param ArgFunctionType the function type that will be used as the |
| 4012 | /// "argument" type (A) when performing template argument deduction from the |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4013 | /// function template's function type. This type may be NULL, if there is no |
| 4014 | /// argument type to compare against, in C++0x [temp.arg.explicit]p3. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4015 | /// |
| 4016 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4017 | /// this will be set to the function template specialization produced by |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4018 | /// template argument deduction. |
| 4019 | /// |
| 4020 | /// \param Info the argument will be updated to provide additional information |
| 4021 | /// about template argument deduction. |
| 4022 | /// |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4023 | /// \param IsAddressOfFunction If \c true, we are deducing as part of taking |
| 4024 | /// the address of a function template per [temp.deduct.funcaddr] and |
| 4025 | /// [over.over]. If \c false, we are looking up a function template |
| 4026 | /// specialization based on its signature, per [temp.deduct.decl]. |
| 4027 | /// |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4028 | /// \returns the result of template argument deduction. |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4029 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 4030 | FunctionTemplateDecl *FunctionTemplate, |
| 4031 | TemplateArgumentListInfo *ExplicitTemplateArgs, QualType ArgFunctionType, |
| 4032 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 4033 | bool IsAddressOfFunction) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 4034 | if (FunctionTemplate->isInvalidDecl()) |
| 4035 | return TDK_Invalid; |
| 4036 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4037 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 4038 | TemplateParameterList *TemplateParams |
| 4039 | = FunctionTemplate->getTemplateParameters(); |
| 4040 | QualType FunctionType = Function->getType(); |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4041 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4042 | // Substitute any explicit template arguments. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 4043 | LocalInstantiationScope InstScope(*this); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4044 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4045 | unsigned NumExplicitlySpecified = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4046 | SmallVector<QualType, 4> ParamTypes; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4047 | if (ExplicitTemplateArgs) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4048 | if (TemplateDeductionResult Result |
| 4049 | = SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 4050 | *ExplicitTemplateArgs, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4051 | Deduced, ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4052 | &FunctionType, Info)) |
| 4053 | return Result; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 4054 | |
| 4055 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4056 | } |
| 4057 | |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 4058 | // When taking the address of a function, we require convertibility of |
| 4059 | // the resulting function type. Otherwise, we allow arbitrary mismatches |
| 4060 | // of calling convention and noreturn. |
| 4061 | if (!IsAddressOfFunction) |
| 4062 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, FunctionType, |
| 4063 | /*AdjustExceptionSpec*/false); |
| 4064 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 4065 | // Unevaluated SFINAE context. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4066 | EnterExpressionEvaluationContext Unevaluated( |
| 4067 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4068 | SFINAETrap Trap(*this); |
| 4069 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 4070 | Deduced.resize(TemplateParams->size()); |
| 4071 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4072 | // If the function has a deduced return type, substitute it for a dependent |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4073 | // type so that we treat it as a non-deduced context in what follows. If we |
| 4074 | // are looking up by signature, the signature type should also have a deduced |
| 4075 | // return type, which we instead expect to exactly match. |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 4076 | bool HasDeducedReturnType = false; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4077 | if (getLangOpts().CPlusPlus14 && IsAddressOfFunction && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4078 | Function->getReturnType()->getContainedAutoType()) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4079 | FunctionType = SubstAutoType(FunctionType, Context.DependentTy); |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 4080 | HasDeducedReturnType = true; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4081 | } |
| 4082 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4083 | if (!ArgFunctionType.isNull()) { |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 4084 | unsigned TDF = |
| 4085 | TDF_TopLevelParameterTypeList | TDF_AllowCompatibleFunctionType; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4086 | // Deduce template arguments from the function type. |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4087 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4088 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 4089 | FunctionType, ArgFunctionType, |
| 4090 | Info, Deduced, TDF)) |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4091 | return Result; |
| 4092 | } |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 4093 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4094 | if (TemplateDeductionResult Result |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 4095 | = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
| 4096 | NumExplicitlySpecified, |
| 4097 | Specialization, Info)) |
| 4098 | return Result; |
| 4099 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4100 | // If the function has a deduced return type, deduce it now, so we can check |
| 4101 | // that the deduced function type matches the requested type. |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 4102 | if (HasDeducedReturnType && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4103 | Specialization->getReturnType()->isUndeducedType() && |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4104 | DeduceReturnType(Specialization, Info.getLocation(), false)) |
| 4105 | return TDK_MiscellaneousDeductionFailure; |
| 4106 | |
Richard Smith | 9095e5b | 2016-11-01 01:31:23 +0000 | [diff] [blame] | 4107 | // If the function has a dependent exception specification, resolve it now, |
| 4108 | // so we can check that the exception specification matches. |
| 4109 | auto *SpecializationFPT = |
| 4110 | Specialization->getType()->castAs<FunctionProtoType>(); |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 4111 | if (getLangOpts().CPlusPlus17 && |
Richard Smith | 9095e5b | 2016-11-01 01:31:23 +0000 | [diff] [blame] | 4112 | isUnresolvedExceptionSpec(SpecializationFPT->getExceptionSpecType()) && |
| 4113 | !ResolveExceptionSpec(Info.getLocation(), SpecializationFPT)) |
| 4114 | return TDK_MiscellaneousDeductionFailure; |
| 4115 | |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 4116 | // Adjust the exception specification of the argument to match the |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4117 | // substituted and resolved type we just formed. (Calling convention and |
| 4118 | // noreturn can't be dependent, so we don't actually need this for them |
| 4119 | // right now.) |
| 4120 | QualType SpecializationType = Specialization->getType(); |
| 4121 | if (!IsAddressOfFunction) |
| 4122 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, SpecializationType, |
| 4123 | /*AdjustExceptionSpec*/true); |
| 4124 | |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 4125 | // If the requested function type does not match the actual type of the |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 4126 | // specialization with respect to arguments of compatible pointer to function |
| 4127 | // types, template argument deduction fails. |
| 4128 | if (!ArgFunctionType.isNull()) { |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4129 | if (IsAddressOfFunction && |
| 4130 | !isSameOrCompatibleFunctionType( |
| 4131 | Context.getCanonicalType(SpecializationType), |
| 4132 | Context.getCanonicalType(ArgFunctionType))) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 4133 | return TDK_MiscellaneousDeductionFailure; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4134 | |
| 4135 | if (!IsAddressOfFunction && |
| 4136 | !Context.hasSameType(SpecializationType, ArgFunctionType)) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 4137 | return TDK_MiscellaneousDeductionFailure; |
| 4138 | } |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 4139 | |
| 4140 | return TDK_Success; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4141 | } |
| 4142 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4143 | /// Deduce template arguments for a templated conversion |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4144 | /// function (C++ [temp.deduct.conv]) and, if successful, produce a |
| 4145 | /// conversion function template specialization. |
| 4146 | Sema::TemplateDeductionResult |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 4147 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *ConversionTemplate, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4148 | QualType ToType, |
| 4149 | CXXConversionDecl *&Specialization, |
| 4150 | TemplateDeductionInfo &Info) { |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 4151 | if (ConversionTemplate->isInvalidDecl()) |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 4152 | return TDK_Invalid; |
| 4153 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 4154 | CXXConversionDecl *ConversionGeneric |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 4155 | = cast<CXXConversionDecl>(ConversionTemplate->getTemplatedDecl()); |
| 4156 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 4157 | QualType FromType = ConversionGeneric->getConversionType(); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4158 | |
| 4159 | // Canonicalize the types for deduction. |
| 4160 | QualType P = Context.getCanonicalType(FromType); |
| 4161 | QualType A = Context.getCanonicalType(ToType); |
| 4162 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 4163 | // C++0x [temp.deduct.conv]p2: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4164 | // If P is a reference type, the type referred to by P is used for |
| 4165 | // type deduction. |
| 4166 | if (const ReferenceType *PRef = P->getAs<ReferenceType>()) |
| 4167 | P = PRef->getPointeeType(); |
| 4168 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 4169 | // C++0x [temp.deduct.conv]p4: |
| 4170 | // [...] If A is a reference type, the type referred to by A is used |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4171 | // for type deduction. |
Richard Smith | b884ed1 | 2018-07-11 23:19:41 +0000 | [diff] [blame] | 4172 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) { |
| 4173 | A = ARef->getPointeeType(); |
| 4174 | // We work around a defect in the standard here: cv-qualifiers are also |
| 4175 | // removed from P and A in this case, unless P was a reference type. This |
| 4176 | // seems to mostly match what other compilers are doing. |
| 4177 | if (!FromType->getAs<ReferenceType>()) { |
| 4178 | A = A.getUnqualifiedType(); |
| 4179 | P = P.getUnqualifiedType(); |
| 4180 | } |
| 4181 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 4182 | // C++ [temp.deduct.conv]p3: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4183 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4184 | // If A is not a reference type: |
Richard Smith | b884ed1 | 2018-07-11 23:19:41 +0000 | [diff] [blame] | 4185 | } else { |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4186 | assert(!A->isReferenceType() && "Reference types were handled above"); |
| 4187 | |
| 4188 | // - If P is an array type, the pointer type produced by the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4189 | // array-to-pointer standard conversion (4.2) is used in place |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4190 | // of P for type deduction; otherwise, |
| 4191 | if (P->isArrayType()) |
| 4192 | P = Context.getArrayDecayedType(P); |
| 4193 | // - If P is a function type, the pointer type produced by the |
| 4194 | // function-to-pointer standard conversion (4.3) is used in |
| 4195 | // place of P for type deduction; otherwise, |
| 4196 | else if (P->isFunctionType()) |
| 4197 | P = Context.getPointerType(P); |
| 4198 | // - If P is a cv-qualified type, the top level cv-qualifiers of |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4199 | // P's type are ignored for type deduction. |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4200 | else |
| 4201 | P = P.getUnqualifiedType(); |
| 4202 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 4203 | // C++0x [temp.deduct.conv]p4: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4204 | // If A is a cv-qualified type, the top level cv-qualifiers of A's |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 4205 | // type are ignored for type deduction. If A is a reference type, the type |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 4206 | // referred to by A is used for type deduction. |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4207 | A = A.getUnqualifiedType(); |
| 4208 | } |
| 4209 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 4210 | // Unevaluated SFINAE context. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4211 | EnterExpressionEvaluationContext Unevaluated( |
| 4212 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4213 | SFINAETrap Trap(*this); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4214 | |
| 4215 | // C++ [temp.deduct.conv]p1: |
| 4216 | // Template argument deduction is done by comparing the return |
| 4217 | // type of the template conversion function (call it P) with the |
| 4218 | // type that is required as the result of the conversion (call it |
| 4219 | // A) as described in 14.8.2.4. |
| 4220 | TemplateParameterList *TemplateParams |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 4221 | = ConversionTemplate->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4222 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4223 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4224 | |
| 4225 | // C++0x [temp.deduct.conv]p4: |
| 4226 | // In general, the deduction process attempts to find template |
| 4227 | // argument values that will make the deduced A identical to |
| 4228 | // A. However, there are two cases that allow a difference: |
| 4229 | unsigned TDF = 0; |
| 4230 | // - If the original A is a reference type, A can be more |
| 4231 | // cv-qualified than the deduced A (i.e., the type referred to |
| 4232 | // by the reference) |
| 4233 | if (ToType->isReferenceType()) |
Richard Smith | b884ed1 | 2018-07-11 23:19:41 +0000 | [diff] [blame] | 4234 | TDF |= TDF_ArgWithReferenceType; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4235 | // - The deduced A can be another pointer or pointer to member |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4236 | // type that can be converted to A via a qualification |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4237 | // conversion. |
| 4238 | // |
| 4239 | // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when |
| 4240 | // both P and A are pointers or member pointers. In this case, we |
| 4241 | // just ignore cv-qualifiers completely). |
| 4242 | if ((P->isPointerType() && A->isPointerType()) || |
Douglas Gregor | 9f05ed5 | 2011-08-30 00:37:54 +0000 | [diff] [blame] | 4243 | (P->isMemberPointerType() && A->isMemberPointerType())) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4244 | TDF |= TDF_IgnoreQualifiers; |
| 4245 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4246 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 4247 | P, A, Info, Deduced, TDF)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4248 | return Result; |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 4249 | |
| 4250 | // Create an Instantiation Scope for finalizing the operator. |
| 4251 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4252 | // Finish template argument deduction. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4253 | FunctionDecl *ConversionSpecialized = nullptr; |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 4254 | TemplateDeductionResult Result |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 4255 | = FinishTemplateArgumentDeduction(ConversionTemplate, Deduced, 0, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 4256 | ConversionSpecialized, Info); |
| 4257 | Specialization = cast_or_null<CXXConversionDecl>(ConversionSpecialized); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4258 | return Result; |
| 4259 | } |
| 4260 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4261 | /// Deduce template arguments for a function template when there is |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4262 | /// nothing to deduce against (C++0x [temp.arg.explicit]p3). |
| 4263 | /// |
| 4264 | /// \param FunctionTemplate the function template for which we are performing |
| 4265 | /// template argument deduction. |
| 4266 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 4267 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4268 | /// arguments. |
| 4269 | /// |
| 4270 | /// \param Specialization if template argument deduction was successful, |
| 4271 | /// this will be set to the function template specialization produced by |
| 4272 | /// template argument deduction. |
| 4273 | /// |
| 4274 | /// \param Info the argument will be updated to provide additional information |
| 4275 | /// about template argument deduction. |
| 4276 | /// |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4277 | /// \param IsAddressOfFunction If \c true, we are deducing as part of taking |
| 4278 | /// the address of a function template in a context where we do not have a |
| 4279 | /// target type, per [over.over]. If \c false, we are looking up a function |
| 4280 | /// template specialization based on its signature, which only happens when |
| 4281 | /// deducing a function parameter type from an argument that is a template-id |
| 4282 | /// naming a function template specialization. |
| 4283 | /// |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4284 | /// \returns the result of template argument deduction. |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4285 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 4286 | FunctionTemplateDecl *FunctionTemplate, |
| 4287 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 4288 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 4289 | bool IsAddressOfFunction) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4290 | return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 4291 | QualType(), Specialization, Info, |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4292 | IsAddressOfFunction); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4293 | } |
| 4294 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4295 | namespace { |
Richard Smith | b2997f5 | 2019-05-21 20:10:50 +0000 | [diff] [blame] | 4296 | struct DependentAuto { bool IsPack; }; |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 4297 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4298 | /// Substitute the 'auto' specifier or deduced template specialization type |
| 4299 | /// specifier within a type for a given replacement type. |
| 4300 | class SubstituteDeducedTypeTransform : |
| 4301 | public TreeTransform<SubstituteDeducedTypeTransform> { |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4302 | QualType Replacement; |
Richard Smith | b2997f5 | 2019-05-21 20:10:50 +0000 | [diff] [blame] | 4303 | bool ReplacementIsPack; |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4304 | bool UseTypeSugar; |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 4305 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4306 | public: |
Richard Smith | b2997f5 | 2019-05-21 20:10:50 +0000 | [diff] [blame] | 4307 | SubstituteDeducedTypeTransform(Sema &SemaRef, DependentAuto DA) |
| 4308 | : TreeTransform<SubstituteDeducedTypeTransform>(SemaRef), Replacement(), |
| 4309 | ReplacementIsPack(DA.IsPack), UseTypeSugar(true) {} |
| 4310 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4311 | SubstituteDeducedTypeTransform(Sema &SemaRef, QualType Replacement, |
Richard Smith | b2997f5 | 2019-05-21 20:10:50 +0000 | [diff] [blame] | 4312 | bool UseTypeSugar = true) |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4313 | : TreeTransform<SubstituteDeducedTypeTransform>(SemaRef), |
Richard Smith | b2997f5 | 2019-05-21 20:10:50 +0000 | [diff] [blame] | 4314 | Replacement(Replacement), ReplacementIsPack(false), |
| 4315 | UseTypeSugar(UseTypeSugar) {} |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4316 | |
| 4317 | QualType TransformDesugared(TypeLocBuilder &TLB, DeducedTypeLoc TL) { |
| 4318 | assert(isa<TemplateTypeParmType>(Replacement) && |
| 4319 | "unexpected unsugared replacement kind"); |
| 4320 | QualType Result = Replacement; |
| 4321 | TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result); |
| 4322 | NewTL.setNameLoc(TL.getNameLoc()); |
| 4323 | return Result; |
| 4324 | } |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 4325 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4326 | QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) { |
| 4327 | // If we're building the type pattern to deduce against, don't wrap the |
| 4328 | // substituted type in an AutoType. Certain template deduction rules |
| 4329 | // apply only when a template type parameter appears directly (and not if |
| 4330 | // the parameter is found through desugaring). For instance: |
| 4331 | // auto &&lref = lvalue; |
| 4332 | // must transform into "rvalue reference to T" not "rvalue reference to |
| 4333 | // auto type deduced as T" in order for [temp.deduct.call]p3 to apply. |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4334 | // |
| 4335 | // FIXME: Is this still necessary? |
| 4336 | if (!UseTypeSugar) |
| 4337 | return TransformDesugared(TLB, TL); |
| 4338 | |
| 4339 | QualType Result = SemaRef.Context.getAutoType( |
Richard Smith | b2997f5 | 2019-05-21 20:10:50 +0000 | [diff] [blame] | 4340 | Replacement, TL.getTypePtr()->getKeyword(), Replacement.isNull(), |
| 4341 | ReplacementIsPack); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4342 | auto NewTL = TLB.push<AutoTypeLoc>(Result); |
| 4343 | NewTL.setNameLoc(TL.getNameLoc()); |
| 4344 | return Result; |
| 4345 | } |
| 4346 | |
| 4347 | QualType TransformDeducedTemplateSpecializationType( |
| 4348 | TypeLocBuilder &TLB, DeducedTemplateSpecializationTypeLoc TL) { |
| 4349 | if (!UseTypeSugar) |
| 4350 | return TransformDesugared(TLB, TL); |
| 4351 | |
| 4352 | QualType Result = SemaRef.Context.getDeducedTemplateSpecializationType( |
| 4353 | TL.getTypePtr()->getTemplateName(), |
| 4354 | Replacement, Replacement.isNull()); |
| 4355 | auto NewTL = TLB.push<DeducedTemplateSpecializationTypeLoc>(Result); |
| 4356 | NewTL.setNameLoc(TL.getNameLoc()); |
| 4357 | return Result; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4358 | } |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 4359 | |
| 4360 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 4361 | // Lambdas never need to be transformed. |
| 4362 | return E; |
| 4363 | } |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4364 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4365 | QualType Apply(TypeLoc TL) { |
| 4366 | // Create some scratch storage for the transformed type locations. |
| 4367 | // FIXME: We're just going to throw this information away. Don't build it. |
| 4368 | TypeLocBuilder TLB; |
| 4369 | TLB.reserve(TL.getFullDataSize()); |
| 4370 | return TransformType(TLB, TL); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4371 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4372 | }; |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 4373 | |
| 4374 | } // namespace |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4375 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4376 | Sema::DeduceAutoResult |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4377 | Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *&Init, QualType &Result, |
| 4378 | Optional<unsigned> DependentDeductionDepth) { |
| 4379 | return DeduceAutoType(Type->getTypeLoc(), Init, Result, |
| 4380 | DependentDeductionDepth); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4381 | } |
| 4382 | |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4383 | /// Attempt to produce an informative diagostic explaining why auto deduction |
| 4384 | /// failed. |
| 4385 | /// \return \c true if diagnosed, \c false if not. |
| 4386 | static bool diagnoseAutoDeductionFailure(Sema &S, |
| 4387 | Sema::TemplateDeductionResult TDK, |
| 4388 | TemplateDeductionInfo &Info, |
| 4389 | ArrayRef<SourceRange> Ranges) { |
| 4390 | switch (TDK) { |
| 4391 | case Sema::TDK_Inconsistent: { |
| 4392 | // Inconsistent deduction means we were deducing from an initializer list. |
| 4393 | auto D = S.Diag(Info.getLocation(), diag::err_auto_inconsistent_deduction); |
| 4394 | D << Info.FirstArg << Info.SecondArg; |
| 4395 | for (auto R : Ranges) |
| 4396 | D << R; |
| 4397 | return true; |
| 4398 | } |
| 4399 | |
| 4400 | // FIXME: Are there other cases for which a custom diagnostic is more useful |
| 4401 | // than the basic "types don't match" diagnostic? |
| 4402 | |
| 4403 | default: |
| 4404 | return false; |
| 4405 | } |
| 4406 | } |
| 4407 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4408 | /// Deduce the type for an auto type-specifier (C++11 [dcl.spec.auto]p6) |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4409 | /// |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4410 | /// Note that this is done even if the initializer is dependent. (This is |
| 4411 | /// necessary to support partial ordering of templates using 'auto'.) |
| 4412 | /// A dependent type will be produced when deducing from a dependent type. |
| 4413 | /// |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4414 | /// \param Type the type pattern using the auto type-specifier. |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4415 | /// \param Init the initializer for the variable whose type is to be deduced. |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4416 | /// \param Result if type deduction was successful, this will be set to the |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4417 | /// deduced type. |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4418 | /// \param DependentDeductionDepth Set if we should permit deduction in |
| 4419 | /// dependent cases. This is necessary for template partial ordering with |
| 4420 | /// 'auto' template parameters. The value specified is the template |
| 4421 | /// parameter depth at which we should perform 'auto' deduction. |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4422 | Sema::DeduceAutoResult |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4423 | Sema::DeduceAutoType(TypeLoc Type, Expr *&Init, QualType &Result, |
| 4424 | Optional<unsigned> DependentDeductionDepth) { |
John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4425 | if (Init->getType()->isNonOverloadPlaceholderType()) { |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4426 | ExprResult NonPlaceholder = CheckPlaceholderExpr(Init); |
| 4427 | if (NonPlaceholder.isInvalid()) |
| 4428 | return DAR_FailedAlreadyDiagnosed; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4429 | Init = NonPlaceholder.get(); |
John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4430 | } |
| 4431 | |
Richard Smith | b2997f5 | 2019-05-21 20:10:50 +0000 | [diff] [blame] | 4432 | DependentAuto DependentResult = { |
| 4433 | /*.IsPack = */ (bool)Type.getAs<PackExpansionTypeLoc>()}; |
| 4434 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4435 | if (!DependentDeductionDepth && |
| 4436 | (Type.getType()->isDependentType() || Init->isTypeDependent())) { |
Richard Smith | b2997f5 | 2019-05-21 20:10:50 +0000 | [diff] [blame] | 4437 | Result = SubstituteDeducedTypeTransform(*this, DependentResult).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4438 | assert(!Result.isNull() && "substituting DependentTy can't fail"); |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4439 | return DAR_Succeeded; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4440 | } |
| 4441 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4442 | // Find the depth of template parameter to synthesize. |
| 4443 | unsigned Depth = DependentDeductionDepth.getValueOr(0); |
| 4444 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4445 | // If this is a 'decltype(auto)' specifier, do the decltype dance. |
| 4446 | // Since 'decltype(auto)' can only occur at the top of the type, we |
| 4447 | // don't need to go digging for it. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4448 | if (const AutoType *AT = Type.getType()->getAs<AutoType>()) { |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4449 | if (AT->isDecltypeAuto()) { |
| 4450 | if (isa<InitListExpr>(Init)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4451 | Diag(Init->getBeginLoc(), diag::err_decltype_auto_initializer_list); |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4452 | return DAR_FailedAlreadyDiagnosed; |
| 4453 | } |
| 4454 | |
Akira Hatanaka | 0a84856 | 2019-01-10 20:12:16 +0000 | [diff] [blame] | 4455 | ExprResult ER = CheckPlaceholderExpr(Init); |
| 4456 | if (ER.isInvalid()) |
| 4457 | return DAR_FailedAlreadyDiagnosed; |
| 4458 | Init = ER.get(); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4459 | QualType Deduced = BuildDecltypeType(Init, Init->getBeginLoc(), false); |
David Majnemer | 3c20ab2 | 2015-07-01 00:29:28 +0000 | [diff] [blame] | 4460 | if (Deduced.isNull()) |
| 4461 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4462 | // FIXME: Support a non-canonical deduced type for 'auto'. |
| 4463 | Deduced = Context.getCanonicalType(Deduced); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4464 | Result = SubstituteDeducedTypeTransform(*this, Deduced).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4465 | if (Result.isNull()) |
| 4466 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4467 | return DAR_Succeeded; |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4468 | } else if (!getLangOpts().CPlusPlus) { |
| 4469 | if (isa<InitListExpr>(Init)) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4470 | Diag(Init->getBeginLoc(), diag::err_auto_init_list_from_c); |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4471 | return DAR_FailedAlreadyDiagnosed; |
| 4472 | } |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4473 | } |
| 4474 | } |
| 4475 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4476 | SourceLocation Loc = Init->getExprLoc(); |
| 4477 | |
| 4478 | LocalInstantiationScope InstScope(*this); |
| 4479 | |
| 4480 | // Build template<class TemplParam> void Func(FuncParam); |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4481 | TemplateTypeParmDecl *TemplParam = TemplateTypeParmDecl::Create( |
| 4482 | Context, nullptr, SourceLocation(), Loc, Depth, 0, nullptr, false, false); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4483 | QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0); |
| 4484 | NamedDecl *TemplParamPtr = TemplParam; |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 4485 | FixedSizeTemplateParameterListStorage<1, false> TemplateParamsSt( |
| 4486 | Loc, Loc, TemplParamPtr, Loc, nullptr); |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 4487 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4488 | QualType FuncParam = |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4489 | SubstituteDeducedTypeTransform(*this, TemplArg, /*UseTypeSugar*/false) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4490 | .Apply(Type); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4491 | assert(!FuncParam.isNull() && |
| 4492 | "substituting template parameter for 'auto' failed"); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4493 | |
| 4494 | // Deduce type of TemplParam in Func(Init) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4495 | SmallVector<DeducedTemplateArgument, 1> Deduced; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4496 | Deduced.resize(1); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4497 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4498 | TemplateDeductionInfo Info(Loc, Depth); |
| 4499 | |
| 4500 | // If deduction failed, don't diagnose if the initializer is dependent; it |
| 4501 | // might acquire a matching type in the instantiation. |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4502 | auto DeductionFailed = [&](TemplateDeductionResult TDK, |
| 4503 | ArrayRef<SourceRange> Ranges) -> DeduceAutoResult { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4504 | if (Init->isTypeDependent()) { |
Richard Smith | b2997f5 | 2019-05-21 20:10:50 +0000 | [diff] [blame] | 4505 | Result = |
| 4506 | SubstituteDeducedTypeTransform(*this, DependentResult).Apply(Type); |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4507 | assert(!Result.isNull() && "substituting DependentTy can't fail"); |
| 4508 | return DAR_Succeeded; |
| 4509 | } |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4510 | if (diagnoseAutoDeductionFailure(*this, TDK, Info, Ranges)) |
| 4511 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4512 | return DAR_Failed; |
| 4513 | }; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4514 | |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 4515 | SmallVector<OriginalCallArg, 4> OriginalCallArgs; |
| 4516 | |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 4517 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4518 | if (InitList) { |
Richard Smith | c8a32e5 | 2017-01-05 23:12:16 +0000 | [diff] [blame] | 4519 | // Notionally, we substitute std::initializer_list<T> for 'auto' and deduce |
| 4520 | // against that. Such deduction only succeeds if removing cv-qualifiers and |
| 4521 | // references results in std::initializer_list<T>. |
| 4522 | if (!Type.getType().getNonReferenceType()->getAs<AutoType>()) |
| 4523 | return DAR_Failed; |
| 4524 | |
Richard Smith | 0827541 | 2019-08-31 00:05:50 +0000 | [diff] [blame] | 4525 | // Resolving a core issue: a braced-init-list containing any designators is |
| 4526 | // a non-deduced context. |
| 4527 | for (Expr *E : InitList->inits()) |
| 4528 | if (isa<DesignatedInitExpr>(E)) |
| 4529 | return DAR_Failed; |
| 4530 | |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4531 | SourceRange DeducedFromInitRange; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4532 | for (unsigned i = 0, e = InitList->getNumInits(); i < e; ++i) { |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4533 | Expr *Init = InitList->getInit(i); |
| 4534 | |
| 4535 | if (auto TDK = DeduceTemplateArgumentsFromCallArgument( |
| 4536 | *this, TemplateParamsSt.get(), 0, TemplArg, Init, |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 4537 | Info, Deduced, OriginalCallArgs, /*Decomposed*/ true, |
| 4538 | /*ArgIdx*/ 0, /*TDF*/ 0)) |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4539 | return DeductionFailed(TDK, {DeducedFromInitRange, |
| 4540 | Init->getSourceRange()}); |
| 4541 | |
| 4542 | if (DeducedFromInitRange.isInvalid() && |
| 4543 | Deduced[0].getKind() != TemplateArgument::Null) |
| 4544 | DeducedFromInitRange = Init->getSourceRange(); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4545 | } |
| 4546 | } else { |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4547 | if (!getLangOpts().CPlusPlus && Init->refersToBitField()) { |
| 4548 | Diag(Loc, diag::err_auto_bitfield); |
| 4549 | return DAR_FailedAlreadyDiagnosed; |
| 4550 | } |
| 4551 | |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4552 | if (auto TDK = DeduceTemplateArgumentsFromCallArgument( |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 4553 | *this, TemplateParamsSt.get(), 0, FuncParam, Init, Info, Deduced, |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 4554 | OriginalCallArgs, /*Decomposed*/ false, /*ArgIdx*/ 0, /*TDF*/ 0)) |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4555 | return DeductionFailed(TDK, {}); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4556 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4557 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4558 | // Could be null if somehow 'auto' appears in a non-deduced context. |
Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4559 | if (Deduced[0].getKind() != TemplateArgument::Type) |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4560 | return DeductionFailed(TDK_Incomplete, {}); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4561 | |
Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4562 | QualType DeducedType = Deduced[0].getAsType(); |
| 4563 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4564 | if (InitList) { |
| 4565 | DeducedType = BuildStdInitializerList(DeducedType, Loc); |
| 4566 | if (DeducedType.isNull()) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4567 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4568 | } |
| 4569 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4570 | Result = SubstituteDeducedTypeTransform(*this, DeducedType).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4571 | if (Result.isNull()) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4572 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4573 | |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4574 | // Check that the deduced argument type is compatible with the original |
| 4575 | // argument type per C++ [temp.deduct.call]p4. |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 4576 | QualType DeducedA = InitList ? Deduced[0].getAsType() : Result; |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 4577 | for (const OriginalCallArg &OriginalArg : OriginalCallArgs) { |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 4578 | assert((bool)InitList == OriginalArg.DecomposedParam && |
| 4579 | "decomposed non-init-list in auto deduction?"); |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4580 | if (auto TDK = |
| 4581 | CheckOriginalCallArgDeduction(*this, Info, OriginalArg, DeducedA)) { |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 4582 | Result = QualType(); |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4583 | return DeductionFailed(TDK, {}); |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 4584 | } |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4585 | } |
| 4586 | |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4587 | return DAR_Succeeded; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4588 | } |
| 4589 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 4590 | QualType Sema::SubstAutoType(QualType TypeWithAuto, |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4591 | QualType TypeToReplaceAuto) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4592 | if (TypeToReplaceAuto->isDependentType()) |
Richard Smith | b2997f5 | 2019-05-21 20:10:50 +0000 | [diff] [blame] | 4593 | return SubstituteDeducedTypeTransform( |
| 4594 | *this, DependentAuto{ |
| 4595 | TypeToReplaceAuto->containsUnexpandedParameterPack()}) |
| 4596 | .TransformType(TypeWithAuto); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4597 | return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4598 | .TransformType(TypeWithAuto); |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4599 | } |
| 4600 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4601 | TypeSourceInfo *Sema::SubstAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto, |
| 4602 | QualType TypeToReplaceAuto) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4603 | if (TypeToReplaceAuto->isDependentType()) |
Richard Smith | b2997f5 | 2019-05-21 20:10:50 +0000 | [diff] [blame] | 4604 | return SubstituteDeducedTypeTransform( |
| 4605 | *this, |
| 4606 | DependentAuto{ |
| 4607 | TypeToReplaceAuto->containsUnexpandedParameterPack()}) |
| 4608 | .TransformType(TypeWithAuto); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4609 | return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4610 | .TransformType(TypeWithAuto); |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 4611 | } |
| 4612 | |
Richard Smith | 33c33c3 | 2017-02-04 01:28:01 +0000 | [diff] [blame] | 4613 | QualType Sema::ReplaceAutoType(QualType TypeWithAuto, |
| 4614 | QualType TypeToReplaceAuto) { |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4615 | return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto, |
| 4616 | /*UseTypeSugar*/ false) |
Richard Smith | 33c33c3 | 2017-02-04 01:28:01 +0000 | [diff] [blame] | 4617 | .TransformType(TypeWithAuto); |
| 4618 | } |
| 4619 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4620 | void Sema::DiagnoseAutoDeductionFailure(VarDecl *VDecl, Expr *Init) { |
| 4621 | if (isa<InitListExpr>(Init)) |
| 4622 | Diag(VDecl->getLocation(), |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4623 | VDecl->isInitCapture() |
| 4624 | ? diag::err_init_capture_deduction_failure_from_init_list |
| 4625 | : diag::err_auto_var_deduction_failure_from_init_list) |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4626 | << VDecl->getDeclName() << VDecl->getType() << Init->getSourceRange(); |
| 4627 | else |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4628 | Diag(VDecl->getLocation(), |
| 4629 | VDecl->isInitCapture() ? diag::err_init_capture_deduction_failure |
| 4630 | : diag::err_auto_var_deduction_failure) |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4631 | << VDecl->getDeclName() << VDecl->getType() << Init->getType() |
| 4632 | << Init->getSourceRange(); |
| 4633 | } |
| 4634 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4635 | bool Sema::DeduceReturnType(FunctionDecl *FD, SourceLocation Loc, |
| 4636 | bool Diagnose) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4637 | assert(FD->getReturnType()->isUndeducedType()); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4638 | |
Richard Smith | 50e291e | 2018-01-02 23:52:42 +0000 | [diff] [blame] | 4639 | // For a lambda's conversion operator, deduce any 'auto' or 'decltype(auto)' |
| 4640 | // within the return type from the call operator's type. |
| 4641 | if (isLambdaConversionOperator(FD)) { |
| 4642 | CXXRecordDecl *Lambda = cast<CXXMethodDecl>(FD)->getParent(); |
| 4643 | FunctionDecl *CallOp = Lambda->getLambdaCallOperator(); |
| 4644 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4645 | // For a generic lambda, instantiate the call operator if needed. |
Richard Smith | 50e291e | 2018-01-02 23:52:42 +0000 | [diff] [blame] | 4646 | if (auto *Args = FD->getTemplateSpecializationArgs()) { |
| 4647 | CallOp = InstantiateFunctionDeclaration( |
| 4648 | CallOp->getDescribedFunctionTemplate(), Args, Loc); |
| 4649 | if (!CallOp || CallOp->isInvalidDecl()) |
| 4650 | return true; |
| 4651 | |
| 4652 | // We might need to deduce the return type by instantiating the definition |
| 4653 | // of the operator() function. |
Richard Smith | 26a92d5 | 2019-08-26 18:18:07 +0000 | [diff] [blame] | 4654 | if (CallOp->getReturnType()->isUndeducedType()) { |
| 4655 | runWithSufficientStackSpace(Loc, [&] { |
| 4656 | InstantiateFunctionDefinition(Loc, CallOp); |
| 4657 | }); |
| 4658 | } |
Richard Smith | 50e291e | 2018-01-02 23:52:42 +0000 | [diff] [blame] | 4659 | } |
| 4660 | |
| 4661 | if (CallOp->isInvalidDecl()) |
| 4662 | return true; |
| 4663 | assert(!CallOp->getReturnType()->isUndeducedType() && |
| 4664 | "failed to deduce lambda return type"); |
| 4665 | |
| 4666 | // Build the new return type from scratch. |
| 4667 | QualType RetType = getLambdaConversionFunctionResultType( |
| 4668 | CallOp->getType()->castAs<FunctionProtoType>()); |
| 4669 | if (FD->getReturnType()->getAs<PointerType>()) |
| 4670 | RetType = Context.getPointerType(RetType); |
| 4671 | else { |
| 4672 | assert(FD->getReturnType()->getAs<BlockPointerType>()); |
| 4673 | RetType = Context.getBlockPointerType(RetType); |
| 4674 | } |
| 4675 | Context.adjustDeducedFunctionResultType(FD, RetType); |
| 4676 | return false; |
| 4677 | } |
| 4678 | |
Richard Smith | 26a92d5 | 2019-08-26 18:18:07 +0000 | [diff] [blame] | 4679 | if (FD->getTemplateInstantiationPattern()) { |
| 4680 | runWithSufficientStackSpace(Loc, [&] { |
| 4681 | InstantiateFunctionDefinition(Loc, FD); |
| 4682 | }); |
| 4683 | } |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4684 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4685 | bool StillUndeduced = FD->getReturnType()->isUndeducedType(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4686 | if (StillUndeduced && Diagnose && !FD->isInvalidDecl()) { |
| 4687 | Diag(Loc, diag::err_auto_fn_used_before_defined) << FD; |
| 4688 | Diag(FD->getLocation(), diag::note_callee_decl) << FD; |
| 4689 | } |
| 4690 | |
| 4691 | return StillUndeduced; |
| 4692 | } |
| 4693 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4694 | /// If this is a non-static member function, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 4695 | static void |
| 4696 | AddImplicitObjectParameterType(ASTContext &Context, |
| 4697 | CXXMethodDecl *Method, |
| 4698 | SmallVectorImpl<QualType> &ArgTypes) { |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4699 | // C++11 [temp.func.order]p3: |
| 4700 | // [...] The new parameter is of type "reference to cv A," where cv are |
| 4701 | // the cv-qualifiers of the function template (if any) and A is |
| 4702 | // the class of which the function template is a member. |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4703 | // |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4704 | // The standard doesn't say explicitly, but we pick the appropriate kind of |
| 4705 | // reference type based on [over.match.funcs]p4. |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4706 | QualType ArgTy = Context.getTypeDeclType(Method->getParent()); |
Anastasia Stulova | c61eaa5 | 2019-01-28 11:37:49 +0000 | [diff] [blame] | 4707 | ArgTy = Context.getQualifiedType(ArgTy, Method->getMethodQualifiers()); |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4708 | if (Method->getRefQualifier() == RQ_RValue) |
| 4709 | ArgTy = Context.getRValueReferenceType(ArgTy); |
| 4710 | else |
| 4711 | ArgTy = Context.getLValueReferenceType(ArgTy); |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4712 | ArgTypes.push_back(ArgTy); |
| 4713 | } |
| 4714 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4715 | /// Determine whether the function template \p FT1 is at least as |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4716 | /// specialized as \p FT2. |
| 4717 | static bool isAtLeastAsSpecializedAs(Sema &S, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4718 | SourceLocation Loc, |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4719 | FunctionTemplateDecl *FT1, |
| 4720 | FunctionTemplateDecl *FT2, |
| 4721 | TemplatePartialOrderingContext TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4722 | unsigned NumCallArguments1) { |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4723 | FunctionDecl *FD1 = FT1->getTemplatedDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4724 | FunctionDecl *FD2 = FT2->getTemplatedDecl(); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4725 | const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); |
| 4726 | const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4727 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4728 | assert(Proto1 && Proto2 && "Function templates must have prototypes"); |
| 4729 | TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4730 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4731 | Deduced.resize(TemplateParams->size()); |
| 4732 | |
| 4733 | // C++0x [temp.deduct.partial]p3: |
| 4734 | // The types used to determine the ordering depend on the context in which |
| 4735 | // the partial ordering is done: |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4736 | TemplateDeductionInfo Info(Loc); |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4737 | SmallVector<QualType, 4> Args2; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4738 | switch (TPOC) { |
| 4739 | case TPOC_Call: { |
| 4740 | // - In the context of a function call, the function parameter types are |
| 4741 | // used. |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4742 | CXXMethodDecl *Method1 = dyn_cast<CXXMethodDecl>(FD1); |
| 4743 | CXXMethodDecl *Method2 = dyn_cast<CXXMethodDecl>(FD2); |
Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4744 | |
Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4745 | // C++11 [temp.func.order]p3: |
Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4746 | // [...] If only one of the function templates is a non-static |
| 4747 | // member, that function template is considered to have a new |
| 4748 | // first parameter inserted in its function parameter list. The |
| 4749 | // new parameter is of type "reference to cv A," where cv are |
| 4750 | // the cv-qualifiers of the function template (if any) and A is |
| 4751 | // the class of which the function template is a member. |
| 4752 | // |
Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4753 | // Note that we interpret this to mean "if one of the function |
| 4754 | // templates is a non-static member and the other is a non-member"; |
| 4755 | // otherwise, the ordering rules for static functions against non-static |
| 4756 | // functions don't make any sense. |
| 4757 | // |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4758 | // C++98/03 doesn't have this provision but we've extended DR532 to cover |
| 4759 | // it as wording was broken prior to it. |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4760 | SmallVector<QualType, 4> Args1; |
| 4761 | |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4762 | unsigned NumComparedArguments = NumCallArguments1; |
| 4763 | |
| 4764 | if (!Method2 && Method1 && !Method1->isStatic()) { |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4765 | // Compare 'this' from Method1 against first parameter from Method2. |
| 4766 | AddImplicitObjectParameterType(S.Context, Method1, Args1); |
| 4767 | ++NumComparedArguments; |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4768 | } else if (!Method1 && Method2 && !Method2->isStatic()) { |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4769 | // Compare 'this' from Method2 against first parameter from Method1. |
| 4770 | AddImplicitObjectParameterType(S.Context, Method2, Args2); |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4771 | } |
| 4772 | |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4773 | Args1.insert(Args1.end(), Proto1->param_type_begin(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4774 | Proto1->param_type_end()); |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4775 | Args2.insert(Args2.end(), Proto2->param_type_begin(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4776 | Proto2->param_type_end()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4777 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4778 | // C++ [temp.func.order]p5: |
| 4779 | // The presence of unused ellipsis and default arguments has no effect on |
| 4780 | // the partial ordering of function templates. |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4781 | if (Args1.size() > NumComparedArguments) |
| 4782 | Args1.resize(NumComparedArguments); |
| 4783 | if (Args2.size() > NumComparedArguments) |
| 4784 | Args2.resize(NumComparedArguments); |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4785 | if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(), |
| 4786 | Args1.data(), Args1.size(), Info, Deduced, |
| 4787 | TDF_None, /*PartialOrdering=*/true)) |
| 4788 | return false; |
| 4789 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4790 | break; |
| 4791 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4792 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4793 | case TPOC_Conversion: |
| 4794 | // - In the context of a call to a conversion operator, the return types |
| 4795 | // of the conversion function templates are used. |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4796 | if (DeduceTemplateArgumentsByTypeMatch( |
| 4797 | S, TemplateParams, Proto2->getReturnType(), Proto1->getReturnType(), |
| 4798 | Info, Deduced, TDF_None, |
| 4799 | /*PartialOrdering=*/true)) |
| 4800 | return false; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4801 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4802 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4803 | case TPOC_Other: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4804 | // - In other contexts (14.6.6.2) the function template's function type |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4805 | // is used. |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4806 | if (DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 4807 | FD2->getType(), FD1->getType(), |
| 4808 | Info, Deduced, TDF_None, |
| 4809 | /*PartialOrdering=*/true)) |
| 4810 | return false; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4811 | break; |
| 4812 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4813 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4814 | // C++0x [temp.deduct.partial]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4815 | // In most cases, all template parameters must have values in order for |
| 4816 | // deduction to succeed, but for partial ordering purposes a template |
| 4817 | // parameter may remain without a value provided it is not used in the |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4818 | // types being used for partial ordering. [ Note: a template parameter used |
| 4819 | // in a non-deduced context is considered used. -end note] |
| 4820 | unsigned ArgIdx = 0, NumArgs = Deduced.size(); |
| 4821 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4822 | if (Deduced[ArgIdx].isNull()) |
| 4823 | break; |
| 4824 | |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4825 | // FIXME: We fail to implement [temp.deduct.type]p1 along this path. We need |
| 4826 | // to substitute the deduced arguments back into the template and check that |
| 4827 | // we get the right type. |
Richard Smith | cf82486 | 2016-12-30 04:32:02 +0000 | [diff] [blame] | 4828 | |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4829 | if (ArgIdx == NumArgs) { |
| 4830 | // All template arguments were deduced. FT1 is at least as specialized |
| 4831 | // as FT2. |
| 4832 | return true; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4833 | } |
| 4834 | |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4835 | // Figure out which template parameters were used. |
| 4836 | llvm::SmallBitVector UsedParameters(TemplateParams->size()); |
| 4837 | switch (TPOC) { |
| 4838 | case TPOC_Call: |
| 4839 | for (unsigned I = 0, N = Args2.size(); I != N; ++I) |
| 4840 | ::MarkUsedTemplateParameters(S.Context, Args2[I], false, |
| 4841 | TemplateParams->getDepth(), |
| 4842 | UsedParameters); |
| 4843 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4844 | |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4845 | case TPOC_Conversion: |
| 4846 | ::MarkUsedTemplateParameters(S.Context, Proto2->getReturnType(), false, |
| 4847 | TemplateParams->getDepth(), UsedParameters); |
| 4848 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4849 | |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4850 | case TPOC_Other: |
| 4851 | ::MarkUsedTemplateParameters(S.Context, FD2->getType(), false, |
| 4852 | TemplateParams->getDepth(), |
| 4853 | UsedParameters); |
| 4854 | break; |
Richard Smith | 86a1b13 | 2017-02-16 03:49:44 +0000 | [diff] [blame] | 4855 | } |
| 4856 | |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4857 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4858 | // If this argument had no value deduced but was used in one of the types |
| 4859 | // used for partial ordering, then deduction fails. |
| 4860 | if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) |
| 4861 | return false; |
| 4862 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4863 | return true; |
| 4864 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4865 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4866 | /// Determine whether this a function template whose parameter-type-list |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4867 | /// ends with a function parameter pack. |
| 4868 | static bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) { |
| 4869 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
| 4870 | unsigned NumParams = Function->getNumParams(); |
| 4871 | if (NumParams == 0) |
| 4872 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4873 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4874 | ParmVarDecl *Last = Function->getParamDecl(NumParams - 1); |
| 4875 | if (!Last->isParameterPack()) |
| 4876 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4877 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4878 | // Make sure that no previous parameter is a parameter pack. |
| 4879 | while (--NumParams > 0) { |
| 4880 | if (Function->getParamDecl(NumParams - 1)->isParameterPack()) |
| 4881 | return false; |
| 4882 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4883 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4884 | return true; |
| 4885 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4886 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4887 | /// Returns the more specialized function template according |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4888 | /// to the rules of function template partial ordering (C++ [temp.func.order]). |
| 4889 | /// |
| 4890 | /// \param FT1 the first function template |
| 4891 | /// |
| 4892 | /// \param FT2 the second function template |
| 4893 | /// |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4894 | /// \param TPOC the context in which we are performing partial ordering of |
| 4895 | /// function templates. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4896 | /// |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4897 | /// \param NumCallArguments1 The number of arguments in the call to FT1, used |
| 4898 | /// only when \c TPOC is \c TPOC_Call. |
| 4899 | /// |
| 4900 | /// \param NumCallArguments2 The number of arguments in the call to FT2, used |
| 4901 | /// only when \c TPOC is \c TPOC_Call. |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4902 | /// |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4903 | /// \returns the more specialized function template. If neither |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4904 | /// template is more specialized, returns NULL. |
| 4905 | FunctionTemplateDecl * |
| 4906 | Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, |
| 4907 | FunctionTemplateDecl *FT2, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4908 | SourceLocation Loc, |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4909 | TemplatePartialOrderingContext TPOC, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4910 | unsigned NumCallArguments1, |
| 4911 | unsigned NumCallArguments2) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4912 | bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4913 | NumCallArguments1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4914 | bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4915 | NumCallArguments2); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4916 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4917 | if (Better1 != Better2) // We have a clear winner |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4918 | return Better1 ? FT1 : FT2; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4919 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4920 | if (!Better1 && !Better2) // Neither is better than the other |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4921 | return nullptr; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4922 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4923 | // FIXME: This mimics what GCC implements, but doesn't match up with the |
| 4924 | // proposed resolution for core issue 692. This area needs to be sorted out, |
| 4925 | // but for now we attempt to maintain compatibility. |
| 4926 | bool Variadic1 = isVariadicFunctionTemplate(FT1); |
| 4927 | bool Variadic2 = isVariadicFunctionTemplate(FT2); |
| 4928 | if (Variadic1 != Variadic2) |
| 4929 | return Variadic1? FT2 : FT1; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4930 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4931 | return nullptr; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4932 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4933 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4934 | /// Determine if the two templates are equivalent. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4935 | static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) { |
| 4936 | if (T1 == T2) |
| 4937 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4938 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4939 | if (!T1 || !T2) |
| 4940 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4941 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4942 | return T1->getCanonicalDecl() == T2->getCanonicalDecl(); |
| 4943 | } |
| 4944 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 4945 | /// Retrieve the most specialized of the given function template |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4946 | /// specializations. |
| 4947 | /// |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4948 | /// \param SpecBegin the start iterator of the function template |
| 4949 | /// specializations that we will be comparing. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4950 | /// |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4951 | /// \param SpecEnd the end iterator of the function template |
| 4952 | /// specializations, paired with \p SpecBegin. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4953 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4954 | /// \param Loc the location where the ambiguity or no-specializations |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4955 | /// diagnostic should occur. |
| 4956 | /// |
| 4957 | /// \param NoneDiag partial diagnostic used to diagnose cases where there are |
| 4958 | /// no matching candidates. |
| 4959 | /// |
| 4960 | /// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one |
| 4961 | /// occurs. |
| 4962 | /// |
| 4963 | /// \param CandidateDiag partial diagnostic used for each function template |
| 4964 | /// specialization that is a candidate in the ambiguous ordering. One parameter |
| 4965 | /// in this diagnostic should be unbound, which will correspond to the string |
| 4966 | /// describing the template arguments for the function template specialization. |
| 4967 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4968 | /// \returns the most specialized function template specialization, if |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4969 | /// found. Otherwise, returns SpecEnd. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4970 | UnresolvedSetIterator Sema::getMostSpecialized( |
| 4971 | UnresolvedSetIterator SpecBegin, UnresolvedSetIterator SpecEnd, |
| 4972 | TemplateSpecCandidateSet &FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4973 | SourceLocation Loc, const PartialDiagnostic &NoneDiag, |
| 4974 | const PartialDiagnostic &AmbigDiag, const PartialDiagnostic &CandidateDiag, |
| 4975 | bool Complain, QualType TargetType) { |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4976 | if (SpecBegin == SpecEnd) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4977 | if (Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4978 | Diag(Loc, NoneDiag); |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4979 | FailedCandidates.NoteCandidates(*this, Loc); |
| 4980 | } |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4981 | return SpecEnd; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4982 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4983 | |
| 4984 | if (SpecBegin + 1 == SpecEnd) |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4985 | return SpecBegin; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4986 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4987 | // Find the function template that is better than all of the templates it |
| 4988 | // has been compared to. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4989 | UnresolvedSetIterator Best = SpecBegin; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4990 | FunctionTemplateDecl *BestTemplate |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4991 | = cast<FunctionDecl>(*Best)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4992 | assert(BestTemplate && "Not a function template specialization?"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4993 | for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) { |
| 4994 | FunctionTemplateDecl *Challenger |
| 4995 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4996 | assert(Challenger && "Not a function template specialization?"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4997 | if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4998 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4999 | Challenger)) { |
| 5000 | Best = I; |
| 5001 | BestTemplate = Challenger; |
| 5002 | } |
| 5003 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5004 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5005 | // Make sure that the "best" function template is more specialized than all |
| 5006 | // of the others. |
| 5007 | bool Ambiguous = false; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5008 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 5009 | FunctionTemplateDecl *Challenger |
| 5010 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5011 | if (I != Best && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5012 | !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 5013 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5014 | BestTemplate)) { |
| 5015 | Ambiguous = true; |
| 5016 | break; |
| 5017 | } |
| 5018 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5019 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5020 | if (!Ambiguous) { |
| 5021 | // We found an answer. Return it. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5022 | return Best; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5023 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5024 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5025 | // Diagnose the ambiguity. |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 5026 | if (Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 5027 | Diag(Loc, AmbigDiag); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5028 | |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 5029 | // FIXME: Can we order the candidates in some sane way? |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 5030 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 5031 | PartialDiagnostic PD = CandidateDiag; |
Saleem Abdulrasool | 78704fb | 2016-12-22 04:26:57 +0000 | [diff] [blame] | 5032 | const auto *FD = cast<FunctionDecl>(*I); |
| 5033 | PD << FD << getTemplateArgumentBindingsText( |
| 5034 | FD->getPrimaryTemplate()->getTemplateParameters(), |
| 5035 | *FD->getTemplateSpecializationArgs()); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 5036 | if (!TargetType.isNull()) |
Saleem Abdulrasool | 78704fb | 2016-12-22 04:26:57 +0000 | [diff] [blame] | 5037 | HandleFunctionTypeMismatch(PD, FD->getType(), TargetType); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 5038 | Diag((*I)->getLocation(), PD); |
| 5039 | } |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 5040 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5041 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 5042 | return SpecEnd; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 5043 | } |
| 5044 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 5045 | /// Determine whether one partial specialization, P1, is at least as |
| 5046 | /// specialized than another, P2. |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5047 | /// |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 5048 | /// \tparam TemplateLikeDecl The kind of P2, which must be a |
| 5049 | /// TemplateDecl or {Class,Var}TemplatePartialSpecializationDecl. |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 5050 | /// \param T1 The injected-class-name of P1 (faked for a variable template). |
| 5051 | /// \param T2 The injected-class-name of P2 (faked for a variable template). |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 5052 | template<typename TemplateLikeDecl> |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 5053 | static bool isAtLeastAsSpecializedAs(Sema &S, QualType T1, QualType T2, |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 5054 | TemplateLikeDecl *P2, |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 5055 | TemplateDeductionInfo &Info) { |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5056 | // C++ [temp.class.order]p1: |
| 5057 | // For two class template partial specializations, the first is at least as |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5058 | // specialized as the second if, given the following rewrite to two |
| 5059 | // function templates, the first function template is at least as |
| 5060 | // specialized as the second according to the ordering rules for function |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5061 | // templates (14.6.6.2): |
| 5062 | // - the first function template has the same template parameters as the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5063 | // first partial specialization and has a single function parameter |
| 5064 | // whose type is a class template specialization with the template |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5065 | // arguments of the first partial specialization, and |
| 5066 | // - the second function template has the same template parameters as the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5067 | // second partial specialization and has a single function parameter |
| 5068 | // whose type is a class template specialization with the template |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5069 | // arguments of the second partial specialization. |
| 5070 | // |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 5071 | // Rather than synthesize function templates, we merely perform the |
| 5072 | // equivalent partial ordering by performing deduction directly on |
| 5073 | // the template arguments of the class template partial |
| 5074 | // specializations. This computation is slightly simpler than the |
| 5075 | // general problem of function template partial ordering, because |
| 5076 | // class template partial specializations are more constrained. We |
| 5077 | // know that every template parameter is deducible from the class |
| 5078 | // template partial specialization's template arguments, for |
| 5079 | // example. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5080 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 5081 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 5082 | // Determine whether P1 is at least as specialized as P2. |
| 5083 | Deduced.resize(P2->getTemplateParameters()->size()); |
| 5084 | if (DeduceTemplateArgumentsByTypeMatch(S, P2->getTemplateParameters(), |
| 5085 | T2, T1, Info, Deduced, TDF_None, |
| 5086 | /*PartialOrdering=*/true)) |
| 5087 | return false; |
| 5088 | |
| 5089 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), |
| 5090 | Deduced.end()); |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 5091 | Sema::InstantiatingTemplate Inst(S, Info.getLocation(), P2, DeducedArgs, |
| 5092 | Info); |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 5093 | auto *TST1 = T1->castAs<TemplateSpecializationType>(); |
| 5094 | if (FinishTemplateArgumentDeduction( |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 5095 | S, P2, /*IsPartialOrdering=*/true, |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 5096 | TemplateArgumentList(TemplateArgumentList::OnStack, |
| 5097 | TST1->template_arguments()), |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 5098 | Deduced, Info)) |
| 5099 | return false; |
| 5100 | |
| 5101 | return true; |
| 5102 | } |
| 5103 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5104 | /// Returns the more specialized class template partial specialization |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 5105 | /// according to the rules of partial ordering of class template partial |
| 5106 | /// specializations (C++ [temp.class.order]). |
| 5107 | /// |
| 5108 | /// \param PS1 the first class template partial specialization |
| 5109 | /// |
| 5110 | /// \param PS2 the second class template partial specialization |
| 5111 | /// |
| 5112 | /// \returns the more specialized class template partial specialization. If |
| 5113 | /// neither partial specialization is more specialized, returns NULL. |
| 5114 | ClassTemplatePartialSpecializationDecl * |
| 5115 | Sema::getMoreSpecializedPartialSpecialization( |
| 5116 | ClassTemplatePartialSpecializationDecl *PS1, |
| 5117 | ClassTemplatePartialSpecializationDecl *PS2, |
| 5118 | SourceLocation Loc) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 5119 | QualType PT1 = PS1->getInjectedSpecializationType(); |
| 5120 | QualType PT2 = PS2->getInjectedSpecializationType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5121 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 5122 | TemplateDeductionInfo Info(Loc); |
| 5123 | bool Better1 = isAtLeastAsSpecializedAs(*this, PT1, PT2, PS2, Info); |
| 5124 | bool Better2 = isAtLeastAsSpecializedAs(*this, PT2, PT1, PS1, Info); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5125 | |
| 5126 | if (Better1 == Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5127 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5128 | |
| 5129 | return Better1 ? PS1 : PS2; |
| 5130 | } |
| 5131 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 5132 | bool Sema::isMoreSpecializedThanPrimary( |
| 5133 | ClassTemplatePartialSpecializationDecl *Spec, TemplateDeductionInfo &Info) { |
| 5134 | ClassTemplateDecl *Primary = Spec->getSpecializedTemplate(); |
| 5135 | QualType PrimaryT = Primary->getInjectedClassNameSpecialization(); |
| 5136 | QualType PartialT = Spec->getInjectedSpecializationType(); |
| 5137 | if (!isAtLeastAsSpecializedAs(*this, PartialT, PrimaryT, Primary, Info)) |
| 5138 | return false; |
| 5139 | if (isAtLeastAsSpecializedAs(*this, PrimaryT, PartialT, Spec, Info)) { |
| 5140 | Info.clearSFINAEDiagnostic(); |
| 5141 | return false; |
| 5142 | } |
| 5143 | return true; |
| 5144 | } |
| 5145 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5146 | VarTemplatePartialSpecializationDecl * |
| 5147 | Sema::getMoreSpecializedPartialSpecialization( |
| 5148 | VarTemplatePartialSpecializationDecl *PS1, |
| 5149 | VarTemplatePartialSpecializationDecl *PS2, SourceLocation Loc) { |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 5150 | // Pretend the variable template specializations are class template |
| 5151 | // specializations and form a fake injected class name type for comparison. |
Richard Smith | f04fd0b | 2013-12-12 23:14:16 +0000 | [diff] [blame] | 5152 | assert(PS1->getSpecializedTemplate() == PS2->getSpecializedTemplate() && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5153 | "the partial specializations being compared should specialize" |
| 5154 | " the same template."); |
| 5155 | TemplateName Name(PS1->getSpecializedTemplate()); |
| 5156 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 5157 | QualType PT1 = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 5158 | CanonTemplate, PS1->getTemplateArgs().asArray()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5159 | QualType PT2 = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 5160 | CanonTemplate, PS2->getTemplateArgs().asArray()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 5161 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 5162 | TemplateDeductionInfo Info(Loc); |
| 5163 | bool Better1 = isAtLeastAsSpecializedAs(*this, PT1, PT2, PS2, Info); |
| 5164 | bool Better2 = isAtLeastAsSpecializedAs(*this, PT2, PT1, PS1, Info); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5165 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5166 | if (Better1 == Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5167 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5168 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 5169 | return Better1 ? PS1 : PS2; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 5170 | } |
| 5171 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 5172 | bool Sema::isMoreSpecializedThanPrimary( |
| 5173 | VarTemplatePartialSpecializationDecl *Spec, TemplateDeductionInfo &Info) { |
| 5174 | TemplateDecl *Primary = Spec->getSpecializedTemplate(); |
| 5175 | // FIXME: Cache the injected template arguments rather than recomputing |
| 5176 | // them for each partial specialization. |
| 5177 | SmallVector<TemplateArgument, 8> PrimaryArgs; |
| 5178 | Context.getInjectedTemplateArgs(Primary->getTemplateParameters(), |
| 5179 | PrimaryArgs); |
| 5180 | |
| 5181 | TemplateName CanonTemplate = |
| 5182 | Context.getCanonicalTemplateName(TemplateName(Primary)); |
| 5183 | QualType PrimaryT = Context.getTemplateSpecializationType( |
| 5184 | CanonTemplate, PrimaryArgs); |
| 5185 | QualType PartialT = Context.getTemplateSpecializationType( |
| 5186 | CanonTemplate, Spec->getTemplateArgs().asArray()); |
| 5187 | if (!isAtLeastAsSpecializedAs(*this, PartialT, PrimaryT, Primary, Info)) |
| 5188 | return false; |
| 5189 | if (isAtLeastAsSpecializedAs(*this, PrimaryT, PartialT, Spec, Info)) { |
| 5190 | Info.clearSFINAEDiagnostic(); |
| 5191 | return false; |
| 5192 | } |
| 5193 | return true; |
| 5194 | } |
| 5195 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 5196 | bool Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs( |
| 5197 | TemplateParameterList *P, TemplateDecl *AArg, SourceLocation Loc) { |
| 5198 | // C++1z [temp.arg.template]p4: (DR 150) |
| 5199 | // A template template-parameter P is at least as specialized as a |
| 5200 | // template template-argument A if, given the following rewrite to two |
| 5201 | // function templates... |
| 5202 | |
| 5203 | // Rather than synthesize function templates, we merely perform the |
| 5204 | // equivalent partial ordering by performing deduction directly on |
| 5205 | // the template parameter lists of the template template parameters. |
| 5206 | // |
| 5207 | // Given an invented class template X with the template parameter list of |
| 5208 | // A (including default arguments): |
| 5209 | TemplateName X = Context.getCanonicalTemplateName(TemplateName(AArg)); |
| 5210 | TemplateParameterList *A = AArg->getTemplateParameters(); |
| 5211 | |
| 5212 | // - Each function template has a single function parameter whose type is |
| 5213 | // a specialization of X with template arguments corresponding to the |
| 5214 | // template parameters from the respective function template |
| 5215 | SmallVector<TemplateArgument, 8> AArgs; |
| 5216 | Context.getInjectedTemplateArgs(A, AArgs); |
| 5217 | |
| 5218 | // Check P's arguments against A's parameter list. This will fill in default |
| 5219 | // template arguments as needed. AArgs are already correct by construction. |
| 5220 | // We can't just use CheckTemplateIdType because that will expand alias |
| 5221 | // templates. |
| 5222 | SmallVector<TemplateArgument, 4> PArgs; |
| 5223 | { |
| 5224 | SFINAETrap Trap(*this); |
| 5225 | |
| 5226 | Context.getInjectedTemplateArgs(P, PArgs); |
| 5227 | TemplateArgumentListInfo PArgList(P->getLAngleLoc(), P->getRAngleLoc()); |
| 5228 | for (unsigned I = 0, N = P->size(); I != N; ++I) { |
| 5229 | // Unwrap packs that getInjectedTemplateArgs wrapped around pack |
| 5230 | // expansions, to form an "as written" argument list. |
| 5231 | TemplateArgument Arg = PArgs[I]; |
| 5232 | if (Arg.getKind() == TemplateArgument::Pack) { |
| 5233 | assert(Arg.pack_size() == 1 && Arg.pack_begin()->isPackExpansion()); |
| 5234 | Arg = *Arg.pack_begin(); |
| 5235 | } |
| 5236 | PArgList.addArgument(getTrivialTemplateArgumentLoc( |
| 5237 | Arg, QualType(), P->getParam(I)->getLocation())); |
| 5238 | } |
| 5239 | PArgs.clear(); |
| 5240 | |
| 5241 | // C++1z [temp.arg.template]p3: |
| 5242 | // If the rewrite produces an invalid type, then P is not at least as |
| 5243 | // specialized as A. |
| 5244 | if (CheckTemplateArgumentList(AArg, Loc, PArgList, false, PArgs) || |
| 5245 | Trap.hasErrorOccurred()) |
| 5246 | return false; |
| 5247 | } |
| 5248 | |
| 5249 | QualType AType = Context.getTemplateSpecializationType(X, AArgs); |
| 5250 | QualType PType = Context.getTemplateSpecializationType(X, PArgs); |
| 5251 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 5252 | // ... the function template corresponding to P is at least as specialized |
| 5253 | // as the function template corresponding to A according to the partial |
| 5254 | // ordering rules for function templates. |
| 5255 | TemplateDeductionInfo Info(Loc, A->getDepth()); |
| 5256 | return isAtLeastAsSpecializedAs(*this, PType, AType, AArg, Info); |
| 5257 | } |
| 5258 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5259 | /// Mark the template parameters that are used by the given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5260 | /// expression. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5261 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5262 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5263 | const Expr *E, |
| 5264 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5265 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5266 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 5267 | // We can deduce from a pack expansion. |
| 5268 | if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E)) |
| 5269 | E = Expansion->getPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5270 | |
Richard Smith | 3434900 | 2012-07-09 03:07:20 +0000 | [diff] [blame] | 5271 | // Skip through any implicit casts we added while type-checking, and any |
| 5272 | // substitutions performed by template alias expansion. |
Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 5273 | while (true) { |
Richard Smith | 3434900 | 2012-07-09 03:07:20 +0000 | [diff] [blame] | 5274 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 5275 | E = ICE->getSubExpr(); |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 5276 | else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(E)) |
| 5277 | E = CE->getSubExpr(); |
Richard Smith | 3434900 | 2012-07-09 03:07:20 +0000 | [diff] [blame] | 5278 | else if (const SubstNonTypeTemplateParmExpr *Subst = |
| 5279 | dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 5280 | E = Subst->getReplacement(); |
| 5281 | else |
| 5282 | break; |
| 5283 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5284 | |
| 5285 | // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5286 | // find other occurrences of template parameters. |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 5287 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
Douglas Gregor | 04b1152 | 2010-01-14 18:13:22 +0000 | [diff] [blame] | 5288 | if (!DRE) |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5289 | return; |
| 5290 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5291 | const NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5292 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 5293 | if (!NTTP) |
| 5294 | return; |
| 5295 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5296 | if (NTTP->getDepth() == Depth) |
| 5297 | Used[NTTP->getIndex()] = true; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5298 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5299 | // In C++17 mode, additional arguments may be deduced from the type of a |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5300 | // non-type argument. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5301 | if (Ctx.getLangOpts().CPlusPlus17) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5302 | MarkUsedTemplateParameters(Ctx, NTTP->getType(), OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5303 | } |
| 5304 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5305 | /// Mark the template parameters that are used by the given |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5306 | /// nested name specifier. |
| 5307 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5308 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5309 | NestedNameSpecifier *NNS, |
| 5310 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5311 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5312 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5313 | if (!NNS) |
| 5314 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5315 | |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5316 | MarkUsedTemplateParameters(Ctx, NNS->getPrefix(), OnlyDeduced, Depth, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5317 | Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5318 | MarkUsedTemplateParameters(Ctx, QualType(NNS->getAsType(), 0), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5319 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5320 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5321 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5322 | /// Mark the template parameters that are used by the given |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5323 | /// template name. |
| 5324 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5325 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5326 | TemplateName Name, |
| 5327 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5328 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5329 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5330 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 5331 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5332 | = dyn_cast<TemplateTemplateParmDecl>(Template)) { |
| 5333 | if (TTP->getDepth() == Depth) |
| 5334 | Used[TTP->getIndex()] = true; |
| 5335 | } |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5336 | return; |
| 5337 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5338 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5339 | if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5340 | MarkUsedTemplateParameters(Ctx, QTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5341 | Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5342 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5343 | MarkUsedTemplateParameters(Ctx, DTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5344 | Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5345 | } |
| 5346 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5347 | /// Mark the template parameters that are used by the given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5348 | /// type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5349 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5350 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5351 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5352 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5353 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5354 | if (T.isNull()) |
| 5355 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5356 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5357 | // Non-dependent types have nothing deducible |
| 5358 | if (!T->isDependentType()) |
| 5359 | return; |
| 5360 | |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5361 | T = Ctx.getCanonicalType(T); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5362 | switch (T->getTypeClass()) { |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5363 | case Type::Pointer: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5364 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5365 | cast<PointerType>(T)->getPointeeType(), |
| 5366 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5367 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5368 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5369 | break; |
| 5370 | |
| 5371 | case Type::BlockPointer: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5372 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5373 | cast<BlockPointerType>(T)->getPointeeType(), |
| 5374 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5375 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5376 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5377 | break; |
| 5378 | |
| 5379 | case Type::LValueReference: |
| 5380 | case Type::RValueReference: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5381 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5382 | cast<ReferenceType>(T)->getPointeeType(), |
| 5383 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5384 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5385 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5386 | break; |
| 5387 | |
| 5388 | case Type::MemberPointer: { |
| 5389 | const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5390 | MarkUsedTemplateParameters(Ctx, MemPtr->getPointeeType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5391 | Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5392 | MarkUsedTemplateParameters(Ctx, QualType(MemPtr->getClass(), 0), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5393 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5394 | break; |
| 5395 | } |
| 5396 | |
| 5397 | case Type::DependentSizedArray: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5398 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5399 | cast<DependentSizedArrayType>(T)->getSizeExpr(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5400 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5401 | // Fall through to check the element type |
Galina Kistanova | 3339911 | 2017-06-03 06:35:06 +0000 | [diff] [blame] | 5402 | LLVM_FALLTHROUGH; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5403 | |
| 5404 | case Type::ConstantArray: |
| 5405 | case Type::IncompleteArray: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5406 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5407 | cast<ArrayType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5408 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5409 | break; |
| 5410 | |
| 5411 | case Type::Vector: |
| 5412 | case Type::ExtVector: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5413 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5414 | cast<VectorType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5415 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5416 | break; |
| 5417 | |
Erich Keane | f702b02 | 2018-07-13 19:46:04 +0000 | [diff] [blame] | 5418 | case Type::DependentVector: { |
| 5419 | const auto *VecType = cast<DependentVectorType>(T); |
| 5420 | MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, |
| 5421 | Depth, Used); |
| 5422 | MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, Depth, |
| 5423 | Used); |
| 5424 | break; |
| 5425 | } |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5426 | case Type::DependentSizedExtVector: { |
| 5427 | const DependentSizedExtVectorType *VecType |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 5428 | = cast<DependentSizedExtVectorType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5429 | MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5430 | Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5431 | MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5432 | Depth, Used); |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5433 | break; |
| 5434 | } |
| 5435 | |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 5436 | case Type::DependentAddressSpace: { |
| 5437 | const DependentAddressSpaceType *DependentASType = |
| 5438 | cast<DependentAddressSpaceType>(T); |
| 5439 | MarkUsedTemplateParameters(Ctx, DependentASType->getPointeeType(), |
| 5440 | OnlyDeduced, Depth, Used); |
| 5441 | MarkUsedTemplateParameters(Ctx, |
| 5442 | DependentASType->getAddrSpaceExpr(), |
| 5443 | OnlyDeduced, Depth, Used); |
| 5444 | break; |
| 5445 | } |
| 5446 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5447 | case Type::FunctionProto: { |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 5448 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5449 | MarkUsedTemplateParameters(Ctx, Proto->getReturnType(), OnlyDeduced, Depth, |
| 5450 | Used); |
Richard Smith | c379d1a | 2018-07-12 23:32:39 +0000 | [diff] [blame] | 5451 | for (unsigned I = 0, N = Proto->getNumParams(); I != N; ++I) { |
| 5452 | // C++17 [temp.deduct.type]p5: |
| 5453 | // The non-deduced contexts are: [...] |
| 5454 | // -- A function parameter pack that does not occur at the end of the |
| 5455 | // parameter-declaration-list. |
| 5456 | if (!OnlyDeduced || I + 1 == N || |
| 5457 | !Proto->getParamType(I)->getAs<PackExpansionType>()) { |
| 5458 | MarkUsedTemplateParameters(Ctx, Proto->getParamType(I), OnlyDeduced, |
| 5459 | Depth, Used); |
| 5460 | } else { |
| 5461 | // FIXME: C++17 [temp.deduct.call]p1: |
| 5462 | // When a function parameter pack appears in a non-deduced context, |
| 5463 | // the type of that pack is never deduced. |
| 5464 | // |
| 5465 | // We should also track a set of "never deduced" parameters, and |
| 5466 | // subtract that from the list of deduced parameters after marking. |
| 5467 | } |
| 5468 | } |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 5469 | if (auto *E = Proto->getNoexceptExpr()) |
| 5470 | MarkUsedTemplateParameters(Ctx, E, OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5471 | break; |
| 5472 | } |
| 5473 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5474 | case Type::TemplateTypeParm: { |
| 5475 | const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T); |
| 5476 | if (TTP->getDepth() == Depth) |
| 5477 | Used[TTP->getIndex()] = true; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5478 | break; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5479 | } |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5480 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 5481 | case Type::SubstTemplateTypeParmPack: { |
| 5482 | const SubstTemplateTypeParmPackType *Subst |
| 5483 | = cast<SubstTemplateTypeParmPackType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5484 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 5485 | QualType(Subst->getReplacedParameter(), 0), |
| 5486 | OnlyDeduced, Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5487 | MarkUsedTemplateParameters(Ctx, Subst->getArgumentPack(), |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 5488 | OnlyDeduced, Depth, Used); |
| 5489 | break; |
| 5490 | } |
| 5491 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 5492 | case Type::InjectedClassName: |
| 5493 | T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 5494 | LLVM_FALLTHROUGH; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 5495 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5496 | case Type::TemplateSpecialization: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5497 | const TemplateSpecializationType *Spec |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 5498 | = cast<TemplateSpecializationType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5499 | MarkUsedTemplateParameters(Ctx, Spec->getTemplateName(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5500 | Depth, Used); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5501 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5502 | // C++0x [temp.deduct.type]p9: |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 5503 | // If the template argument list of P contains a pack expansion that is |
| 5504 | // not the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5505 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5506 | if (OnlyDeduced && |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 5507 | hasPackExpansionBeforeEnd(Spec->template_arguments())) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5508 | break; |
| 5509 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5510 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5511 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5512 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5513 | break; |
| 5514 | } |
| 5515 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5516 | case Type::Complex: |
| 5517 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5518 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5519 | cast<ComplexType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5520 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5521 | break; |
| 5522 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5523 | case Type::Atomic: |
| 5524 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5525 | MarkUsedTemplateParameters(Ctx, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5526 | cast<AtomicType>(T)->getValueType(), |
| 5527 | OnlyDeduced, Depth, Used); |
| 5528 | break; |
| 5529 | |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5530 | case Type::DependentName: |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5531 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5532 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5533 | cast<DependentNameType>(T)->getQualifier(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5534 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5535 | break; |
| 5536 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5537 | case Type::DependentTemplateSpecialization: { |
Richard Smith | 50d5b97 | 2015-12-30 20:56:05 +0000 | [diff] [blame] | 5538 | // C++14 [temp.deduct.type]p5: |
| 5539 | // The non-deduced contexts are: |
| 5540 | // -- The nested-name-specifier of a type that was specified using a |
| 5541 | // qualified-id |
| 5542 | // |
| 5543 | // C++14 [temp.deduct.type]p6: |
| 5544 | // When a type name is specified in a way that includes a non-deduced |
| 5545 | // context, all of the types that comprise that type name are also |
| 5546 | // non-deduced. |
| 5547 | if (OnlyDeduced) |
| 5548 | break; |
| 5549 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5550 | const DependentTemplateSpecializationType *Spec |
| 5551 | = cast<DependentTemplateSpecializationType>(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5552 | |
Richard Smith | 50d5b97 | 2015-12-30 20:56:05 +0000 | [diff] [blame] | 5553 | MarkUsedTemplateParameters(Ctx, Spec->getQualifier(), |
| 5554 | OnlyDeduced, Depth, Used); |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5555 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5556 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5557 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5558 | Used); |
| 5559 | break; |
| 5560 | } |
| 5561 | |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5562 | case Type::TypeOf: |
| 5563 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5564 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5565 | cast<TypeOfType>(T)->getUnderlyingType(), |
| 5566 | OnlyDeduced, Depth, Used); |
| 5567 | break; |
| 5568 | |
| 5569 | case Type::TypeOfExpr: |
| 5570 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5571 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5572 | cast<TypeOfExprType>(T)->getUnderlyingExpr(), |
| 5573 | OnlyDeduced, Depth, Used); |
| 5574 | break; |
| 5575 | |
| 5576 | case Type::Decltype: |
| 5577 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5578 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5579 | cast<DecltypeType>(T)->getUnderlyingExpr(), |
| 5580 | OnlyDeduced, Depth, Used); |
| 5581 | break; |
| 5582 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 5583 | case Type::UnaryTransform: |
| 5584 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5585 | MarkUsedTemplateParameters(Ctx, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5586 | cast<UnaryTransformType>(T)->getUnderlyingType(), |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 5587 | OnlyDeduced, Depth, Used); |
| 5588 | break; |
| 5589 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 5590 | case Type::PackExpansion: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5591 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 5592 | cast<PackExpansionType>(T)->getPattern(), |
| 5593 | OnlyDeduced, Depth, Used); |
| 5594 | break; |
| 5595 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5596 | case Type::Auto: |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 5597 | case Type::DeducedTemplateSpecialization: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5598 | MarkUsedTemplateParameters(Ctx, |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 5599 | cast<DeducedType>(T)->getDeducedType(), |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5600 | OnlyDeduced, Depth, Used); |
Adrian Prantl | 4b49085 | 2017-12-19 22:21:48 +0000 | [diff] [blame] | 5601 | break; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5602 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5603 | // None of these types have any template parameters in them. |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5604 | case Type::Builtin: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5605 | case Type::VariableArray: |
| 5606 | case Type::FunctionNoProto: |
| 5607 | case Type::Record: |
| 5608 | case Type::Enum: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5609 | case Type::ObjCInterface: |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5610 | case Type::ObjCObject: |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 5611 | case Type::ObjCObjectPointer: |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5612 | case Type::UnresolvedUsing: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5613 | case Type::Pipe: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5614 | #define TYPE(Class, Base) |
| 5615 | #define ABSTRACT_TYPE(Class, Base) |
| 5616 | #define DEPENDENT_TYPE(Class, Base) |
| 5617 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 5618 | #include "clang/AST/TypeNodes.def" |
| 5619 | break; |
| 5620 | } |
| 5621 | } |
| 5622 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5623 | /// Mark the template parameters that are used by this |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5624 | /// template argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5625 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5626 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5627 | const TemplateArgument &TemplateArg, |
| 5628 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5629 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5630 | llvm::SmallBitVector &Used) { |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5631 | switch (TemplateArg.getKind()) { |
| 5632 | case TemplateArgument::Null: |
| 5633 | case TemplateArgument::Integral: |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5634 | case TemplateArgument::Declaration: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5635 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5636 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5637 | case TemplateArgument::NullPtr: |
| 5638 | MarkUsedTemplateParameters(Ctx, TemplateArg.getNullPtrType(), OnlyDeduced, |
| 5639 | Depth, Used); |
| 5640 | break; |
| 5641 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5642 | case TemplateArgument::Type: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5643 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5644 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5645 | break; |
| 5646 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5647 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 5648 | case TemplateArgument::TemplateExpansion: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5649 | MarkUsedTemplateParameters(Ctx, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5650 | TemplateArg.getAsTemplateOrTemplatePattern(), |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5651 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5652 | break; |
| 5653 | |
| 5654 | case TemplateArgument::Expression: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5655 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsExpr(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5656 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5657 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5658 | |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5659 | case TemplateArgument::Pack: |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 5660 | for (const auto &P : TemplateArg.pack_elements()) |
| 5661 | MarkUsedTemplateParameters(Ctx, P, OnlyDeduced, Depth, Used); |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5662 | break; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5663 | } |
| 5664 | } |
| 5665 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5666 | /// Mark which template parameters can be deduced from a given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5667 | /// template argument list. |
| 5668 | /// |
| 5669 | /// \param TemplateArgs the template argument list from which template |
| 5670 | /// parameters will be deduced. |
| 5671 | /// |
James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 5672 | /// \param Used a bit vector whose elements will be set to \c true |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5673 | /// to indicate when the corresponding template parameter will be |
| 5674 | /// deduced. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5675 | void |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5676 | Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5677 | bool OnlyDeduced, unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5678 | llvm::SmallBitVector &Used) { |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5679 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5680 | // If the template argument list of P contains a pack expansion that is not |
| 5681 | // the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5682 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5683 | if (OnlyDeduced && |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 5684 | hasPackExpansionBeforeEnd(TemplateArgs.asArray())) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5685 | return; |
| 5686 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5687 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5688 | ::MarkUsedTemplateParameters(Context, TemplateArgs[I], OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5689 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5690 | } |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5691 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 5692 | /// Marks all of the template parameters that will be deduced by a |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5693 | /// call to the given function template. |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 5694 | void Sema::MarkDeducedTemplateParameters( |
| 5695 | ASTContext &Ctx, const FunctionTemplateDecl *FunctionTemplate, |
| 5696 | llvm::SmallBitVector &Deduced) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5697 | TemplateParameterList *TemplateParams |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5698 | = FunctionTemplate->getTemplateParameters(); |
| 5699 | Deduced.clear(); |
| 5700 | Deduced.resize(TemplateParams->size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5701 | |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5702 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 5703 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5704 | ::MarkUsedTemplateParameters(Ctx, Function->getParamDecl(I)->getType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5705 | true, TemplateParams->getDepth(), Deduced); |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5706 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5707 | |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 5708 | bool hasDeducibleTemplateParameters(Sema &S, |
| 5709 | FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5710 | QualType T) { |
| 5711 | if (!T->isDependentType()) |
| 5712 | return false; |
| 5713 | |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 5714 | TemplateParameterList *TemplateParams |
| 5715 | = FunctionTemplate->getTemplateParameters(); |
| 5716 | llvm::SmallBitVector Deduced(TemplateParams->size()); |
| 5717 | ::MarkUsedTemplateParameters(S.Context, T, true, TemplateParams->getDepth(), |
| 5718 | Deduced); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5719 | |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5720 | return Deduced.any(); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5721 | } |