Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1 | //===------- SemaTemplateDeduction.cpp - Template Argument Deduction ------===/ |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | //===----------------------------------------------------------------------===/ |
| 8 | // |
| 9 | // This file implements C++ template argument deduction. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===/ |
| 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" |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTLambda.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
| 20 | #include "clang/AST/ExprCXX.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/AST/StmtVisitor.h" |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 22 | #include "clang/AST/TypeOrdering.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "clang/Sema/DeclSpec.h" |
| 24 | #include "clang/Sema/Sema.h" |
| 25 | #include "clang/Sema/Template.h" |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallBitVector.h" |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 27 | #include <algorithm> |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 28 | |
| 29 | namespace clang { |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 30 | using namespace sema; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 31 | /// \brief Various flags that control template argument deduction. |
| 32 | /// |
| 33 | /// These flags can be bitwise-OR'd together. |
| 34 | enum TemplateDeductionFlags { |
| 35 | /// \brief No template argument deduction flags, which indicates the |
| 36 | /// strictest results for template argument deduction (as used for, e.g., |
| 37 | /// matching class template partial specializations). |
| 38 | TDF_None = 0, |
| 39 | /// \brief Within template argument deduction from a function call, we are |
| 40 | /// matching with a parameter type for which the original parameter was |
| 41 | /// a reference. |
| 42 | TDF_ParamWithReferenceType = 0x1, |
| 43 | /// \brief Within template argument deduction from a function call, we |
| 44 | /// are matching in a case where we ignore cv-qualifiers. |
| 45 | TDF_IgnoreQualifiers = 0x02, |
| 46 | /// \brief Within template argument deduction from a function call, |
| 47 | /// we are matching in a case where we can perform template argument |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 48 | /// 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] | 49 | TDF_DerivedClass = 0x04, |
| 50 | /// \brief Allow non-dependent types to differ, e.g., when performing |
| 51 | /// template argument deduction from a function call where conversions |
| 52 | /// may apply. |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 53 | TDF_SkipNonDependent = 0x08, |
| 54 | /// \brief Whether we are performing template argument deduction for |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 55 | /// parameters and arguments in a top-level template argument |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 56 | TDF_TopLevelParameterTypeList = 0x10, |
| 57 | /// \brief Within template argument deduction from overload resolution per |
| 58 | /// C++ [over.over] allow matching function types that are compatible in |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 59 | /// terms of noreturn and default calling convention adjustments, or |
| 60 | /// similarly matching a declared template specialization against a |
| 61 | /// possible template, per C++ [temp.deduct.decl]. In either case, permit |
| 62 | /// deduction where the parameter is a function type that can be converted |
| 63 | /// to the argument type. |
| 64 | TDF_AllowCompatibleFunctionType = 0x20, |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 65 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 66 | } |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 67 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 68 | using namespace clang; |
| 69 | |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 70 | /// \brief Compare two APSInts, extending and switching the sign as |
| 71 | /// necessary to compare their values regardless of underlying type. |
| 72 | static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) { |
| 73 | if (Y.getBitWidth() > X.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 74 | X = X.extend(Y.getBitWidth()); |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 75 | else if (Y.getBitWidth() < X.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 76 | Y = Y.extend(X.getBitWidth()); |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 77 | |
| 78 | // If there is a signedness mismatch, correct it. |
| 79 | if (X.isSigned() != Y.isSigned()) { |
| 80 | // If the signed value is negative, then the values cannot be the same. |
| 81 | if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative())) |
| 82 | return false; |
| 83 | |
| 84 | Y.setIsSigned(true); |
| 85 | X.setIsSigned(true); |
| 86 | } |
| 87 | |
| 88 | return X == Y; |
| 89 | } |
| 90 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 91 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 92 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 93 | TemplateParameterList *TemplateParams, |
| 94 | const TemplateArgument &Param, |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 95 | TemplateArgument Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 96 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 97 | SmallVectorImpl<DeducedTemplateArgument> &Deduced); |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 98 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 99 | static Sema::TemplateDeductionResult |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 100 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 101 | TemplateParameterList *TemplateParams, |
| 102 | QualType Param, |
| 103 | QualType Arg, |
| 104 | TemplateDeductionInfo &Info, |
| 105 | SmallVectorImpl<DeducedTemplateArgument> & |
| 106 | Deduced, |
| 107 | unsigned TDF, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 108 | bool PartialOrdering = false, |
| 109 | bool DeducedFromArrayBound = false); |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 110 | |
| 111 | static Sema::TemplateDeductionResult |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 112 | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 113 | ArrayRef<TemplateArgument> Params, |
| 114 | ArrayRef<TemplateArgument> Args, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 115 | TemplateDeductionInfo &Info, |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 116 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 117 | bool NumberOfArgumentsMustMatch); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 118 | |
Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 119 | static void MarkUsedTemplateParameters(ASTContext &Ctx, |
| 120 | const TemplateArgument &TemplateArg, |
| 121 | bool OnlyDeduced, unsigned Depth, |
| 122 | llvm::SmallBitVector &Used); |
| 123 | |
| 124 | static void MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
| 125 | bool OnlyDeduced, unsigned Level, |
| 126 | llvm::SmallBitVector &Deduced); |
| 127 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 128 | /// \brief If the given expression is of a form that permits the deduction |
| 129 | /// of a non-type template parameter, return the declaration of that |
| 130 | /// non-type template parameter. |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 131 | static NonTypeTemplateParmDecl * |
| 132 | getDeducedParameterFromExpr(TemplateDeductionInfo &Info, Expr *E) { |
Richard Smith | 7ebb07c | 2012-07-08 04:37:51 +0000 | [diff] [blame] | 133 | // If we are within an alias template, the expression may have undergone |
| 134 | // any number of parameter substitutions already. |
| 135 | while (1) { |
| 136 | if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E)) |
| 137 | E = IC->getSubExpr(); |
| 138 | else if (SubstNonTypeTemplateParmExpr *Subst = |
| 139 | dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 140 | E = Subst->getReplacement(); |
| 141 | else |
| 142 | break; |
| 143 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 145 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 146 | if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) |
| 147 | if (NTTP->getDepth() == Info.getDeducedDepth()) |
| 148 | return NTTP; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 150 | return nullptr; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 153 | /// \brief Determine whether two declaration pointers refer to the same |
| 154 | /// declaration. |
| 155 | static bool isSameDeclaration(Decl *X, Decl *Y) { |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 156 | if (NamedDecl *NX = dyn_cast<NamedDecl>(X)) |
| 157 | X = NX->getUnderlyingDecl(); |
| 158 | if (NamedDecl *NY = dyn_cast<NamedDecl>(Y)) |
| 159 | Y = NY->getUnderlyingDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 160 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 161 | return X->getCanonicalDecl() == Y->getCanonicalDecl(); |
| 162 | } |
| 163 | |
| 164 | /// \brief Verify that the given, deduced template arguments are compatible. |
| 165 | /// |
| 166 | /// \returns The deduced template argument, or a NULL template argument if |
| 167 | /// the deduced template arguments were incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 168 | static DeducedTemplateArgument |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 169 | checkDeducedTemplateArguments(ASTContext &Context, |
| 170 | const DeducedTemplateArgument &X, |
| 171 | const DeducedTemplateArgument &Y) { |
| 172 | // We have no deduction for one or both of the arguments; they're compatible. |
| 173 | if (X.isNull()) |
| 174 | return Y; |
| 175 | if (Y.isNull()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 176 | return X; |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 177 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 178 | // If we have two non-type template argument values deduced for the same |
| 179 | // parameter, they must both match the type of the parameter, and thus must |
| 180 | // match each other's type. As we're only keeping one of them, we must check |
| 181 | // for that now. The exception is that if either was deduced from an array |
| 182 | // bound, the type is permitted to differ. |
| 183 | if (!X.wasDeducedFromArrayBound() && !Y.wasDeducedFromArrayBound()) { |
| 184 | QualType XType = X.getNonTypeTemplateArgumentType(); |
| 185 | if (!XType.isNull()) { |
| 186 | QualType YType = Y.getNonTypeTemplateArgumentType(); |
| 187 | if (YType.isNull() || !Context.hasSameType(XType, YType)) |
| 188 | return DeducedTemplateArgument(); |
| 189 | } |
| 190 | } |
| 191 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 192 | switch (X.getKind()) { |
| 193 | case TemplateArgument::Null: |
| 194 | llvm_unreachable("Non-deduced template arguments handled above"); |
| 195 | |
| 196 | case TemplateArgument::Type: |
| 197 | // If two template type arguments have the same type, they're compatible. |
| 198 | if (Y.getKind() == TemplateArgument::Type && |
| 199 | Context.hasSameType(X.getAsType(), Y.getAsType())) |
| 200 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 201 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 202 | // If one of the two arguments was deduced from an array bound, the other |
| 203 | // supersedes it. |
| 204 | if (X.wasDeducedFromArrayBound() != Y.wasDeducedFromArrayBound()) |
| 205 | return X.wasDeducedFromArrayBound() ? Y : X; |
| 206 | |
| 207 | // The arguments are not compatible. |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 208 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 209 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 210 | case TemplateArgument::Integral: |
| 211 | // If we deduced a constant in one case and either a dependent expression or |
| 212 | // declaration in another case, keep the integral constant. |
| 213 | // If both are integral constants with the same value, keep that value. |
| 214 | if (Y.getKind() == TemplateArgument::Expression || |
| 215 | Y.getKind() == TemplateArgument::Declaration || |
| 216 | (Y.getKind() == TemplateArgument::Integral && |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 217 | hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()))) |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 218 | return X.wasDeducedFromArrayBound() ? Y : X; |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 219 | |
| 220 | // All other combinations are incompatible. |
| 221 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 222 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 223 | case TemplateArgument::Template: |
| 224 | if (Y.getKind() == TemplateArgument::Template && |
| 225 | Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate())) |
| 226 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 227 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 228 | // All other combinations are incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 229 | return DeducedTemplateArgument(); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 230 | |
| 231 | case TemplateArgument::TemplateExpansion: |
| 232 | if (Y.getKind() == TemplateArgument::TemplateExpansion && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 233 | Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(), |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 234 | Y.getAsTemplateOrTemplatePattern())) |
| 235 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 236 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 237 | // All other combinations are incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 238 | return DeducedTemplateArgument(); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 239 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 240 | case TemplateArgument::Expression: { |
| 241 | if (Y.getKind() != TemplateArgument::Expression) |
| 242 | return checkDeducedTemplateArguments(Context, Y, X); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 243 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 244 | // Compare the expressions for equality |
| 245 | llvm::FoldingSetNodeID ID1, ID2; |
| 246 | X.getAsExpr()->Profile(ID1, Context, true); |
| 247 | Y.getAsExpr()->Profile(ID2, Context, true); |
| 248 | if (ID1 == ID2) |
| 249 | return X.wasDeducedFromArrayBound() ? Y : X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 250 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 251 | // Differing dependent expressions are incompatible. |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 252 | return DeducedTemplateArgument(); |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 253 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 254 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 255 | case TemplateArgument::Declaration: |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 256 | assert(!X.wasDeducedFromArrayBound()); |
| 257 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 258 | // If we deduced a declaration and a dependent expression, keep the |
| 259 | // declaration. |
| 260 | if (Y.getKind() == TemplateArgument::Expression) |
| 261 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 262 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 263 | // If we deduced a declaration and an integral constant, keep the |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 264 | // integral constant and whichever type did not come from an array |
| 265 | // bound. |
| 266 | if (Y.getKind() == TemplateArgument::Integral) { |
| 267 | if (Y.wasDeducedFromArrayBound()) |
| 268 | return TemplateArgument(Context, Y.getAsIntegral(), |
| 269 | X.getParamTypeForDecl()); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 270 | return Y; |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 271 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 272 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 273 | // If we deduced two declarations, make sure they they refer to the |
| 274 | // same declaration. |
| 275 | if (Y.getKind() == TemplateArgument::Declaration && |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 276 | isSameDeclaration(X.getAsDecl(), Y.getAsDecl())) |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 277 | return X; |
| 278 | |
| 279 | // All other combinations are incompatible. |
| 280 | return DeducedTemplateArgument(); |
| 281 | |
| 282 | case TemplateArgument::NullPtr: |
| 283 | // If we deduced a null pointer and a dependent expression, keep the |
| 284 | // null pointer. |
| 285 | if (Y.getKind() == TemplateArgument::Expression) |
| 286 | return X; |
| 287 | |
| 288 | // If we deduced a null pointer and an integral constant, keep the |
| 289 | // integral constant. |
| 290 | if (Y.getKind() == TemplateArgument::Integral) |
| 291 | return Y; |
| 292 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 293 | // If we deduced two null pointers, they are the same. |
| 294 | if (Y.getKind() == TemplateArgument::NullPtr) |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 295 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 296 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 297 | // All other combinations are incompatible. |
| 298 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 299 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 300 | case TemplateArgument::Pack: |
| 301 | if (Y.getKind() != TemplateArgument::Pack || |
| 302 | X.pack_size() != Y.pack_size()) |
| 303 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 304 | |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 305 | llvm::SmallVector<TemplateArgument, 8> NewPack; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 306 | for (TemplateArgument::pack_iterator XA = X.pack_begin(), |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 307 | XAEnd = X.pack_end(), |
| 308 | YA = Y.pack_begin(); |
| 309 | XA != XAEnd; ++XA, ++YA) { |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 310 | TemplateArgument Merged = checkDeducedTemplateArguments( |
| 311 | Context, DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()), |
| 312 | DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound())); |
| 313 | if (Merged.isNull()) |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 314 | return DeducedTemplateArgument(); |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 315 | NewPack.push_back(Merged); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 316 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 317 | |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 318 | return DeducedTemplateArgument( |
| 319 | TemplateArgument::CreatePackCopy(Context, NewPack), |
| 320 | X.wasDeducedFromArrayBound() && Y.wasDeducedFromArrayBound()); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 321 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 322 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 323 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | /// \brief Deduce the value of the given non-type template parameter |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 327 | /// as the given deduced template argument. All non-type template parameter |
| 328 | /// deduction is funneled through here. |
Benjamin Kramer | 7320b99 | 2016-06-15 14:20:56 +0000 | [diff] [blame] | 329 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 330 | Sema &S, TemplateParameterList *TemplateParams, |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 331 | NonTypeTemplateParmDecl *NTTP, const DeducedTemplateArgument &NewDeduced, |
| 332 | QualType ValueType, TemplateDeductionInfo &Info, |
Benjamin Kramer | 7320b99 | 2016-06-15 14:20:56 +0000 | [diff] [blame] | 333 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 334 | assert(NTTP->getDepth() == Info.getDeducedDepth() && |
| 335 | "deducing non-type template argument with wrong depth"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 336 | |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 337 | DeducedTemplateArgument Result = checkDeducedTemplateArguments( |
| 338 | S.Context, Deduced[NTTP->getIndex()], NewDeduced); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 339 | if (Result.isNull()) { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 340 | Info.Param = NTTP; |
| 341 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 342 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 343 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 344 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 345 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 346 | Deduced[NTTP->getIndex()] = Result; |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 347 | if (!S.getLangOpts().CPlusPlus17) |
Richard Smith | d92eddf | 2016-12-27 06:14:37 +0000 | [diff] [blame] | 348 | return Sema::TDK_Success; |
| 349 | |
Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 350 | if (NTTP->isExpandedParameterPack()) |
| 351 | // FIXME: We may still need to deduce parts of the type here! But we |
| 352 | // don't have any way to find which slice of the type to use, and the |
| 353 | // type stored on the NTTP itself is nonsense. Perhaps the type of an |
| 354 | // expanded NTTP should be a pack expansion type? |
| 355 | return Sema::TDK_Success; |
| 356 | |
Richard Smith | 7bfcc05 | 2017-12-01 21:24:36 +0000 | [diff] [blame] | 357 | // Get the type of the parameter for deduction. If it's a (dependent) array |
| 358 | // or function type, we will not have decayed it yet, so do that now. |
| 359 | QualType ParamType = S.Context.getAdjustedParameterType(NTTP->getType()); |
Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 360 | if (auto *Expansion = dyn_cast<PackExpansionType>(ParamType)) |
| 361 | ParamType = Expansion->getPattern(); |
| 362 | |
Richard Smith | d92eddf | 2016-12-27 06:14:37 +0000 | [diff] [blame] | 363 | // FIXME: It's not clear how deduction of a parameter of reference |
| 364 | // type from an argument (of non-reference type) should be performed. |
| 365 | // For now, we just remove reference types from both sides and let |
| 366 | // the final check for matching types sort out the mess. |
| 367 | return DeduceTemplateArgumentsByTypeMatch( |
Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 368 | S, TemplateParams, ParamType.getNonReferenceType(), |
Richard Smith | d92eddf | 2016-12-27 06:14:37 +0000 | [diff] [blame] | 369 | ValueType.getNonReferenceType(), Info, Deduced, TDF_SkipNonDependent, |
| 370 | /*PartialOrdering=*/false, |
| 371 | /*ArrayBound=*/NewDeduced.wasDeducedFromArrayBound()); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | /// \brief Deduce the value of the given non-type template parameter |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 375 | /// from the given integral constant. |
| 376 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| 377 | Sema &S, TemplateParameterList *TemplateParams, |
| 378 | NonTypeTemplateParmDecl *NTTP, const llvm::APSInt &Value, |
| 379 | QualType ValueType, bool DeducedFromArrayBound, TemplateDeductionInfo &Info, |
| 380 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 381 | return DeduceNonTypeTemplateArgument( |
| 382 | S, TemplateParams, NTTP, |
| 383 | DeducedTemplateArgument(S.Context, Value, ValueType, |
| 384 | DeducedFromArrayBound), |
| 385 | ValueType, Info, Deduced); |
| 386 | } |
| 387 | |
| 388 | /// \brief Deduce the value of the given non-type template parameter |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 389 | /// from the given null pointer template argument type. |
| 390 | static Sema::TemplateDeductionResult DeduceNullPtrTemplateArgument( |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 391 | Sema &S, TemplateParameterList *TemplateParams, |
| 392 | NonTypeTemplateParmDecl *NTTP, QualType NullPtrType, |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 393 | TemplateDeductionInfo &Info, |
| 394 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 395 | Expr *Value = |
| 396 | S.ImpCastExprToType(new (S.Context) CXXNullPtrLiteralExpr( |
| 397 | S.Context.NullPtrTy, NTTP->getLocation()), |
| 398 | NullPtrType, CK_NullToPointer) |
| 399 | .get(); |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 400 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 401 | DeducedTemplateArgument(Value), |
| 402 | Value->getType(), Info, Deduced); |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /// \brief Deduce the value of the given non-type template parameter |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 406 | /// from the given type- or value-dependent expression. |
| 407 | /// |
| 408 | /// \returns true if deduction succeeded, false otherwise. |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 409 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| 410 | Sema &S, TemplateParameterList *TemplateParams, |
| 411 | NonTypeTemplateParmDecl *NTTP, Expr *Value, TemplateDeductionInfo &Info, |
| 412 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 413 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 414 | DeducedTemplateArgument(Value), |
| 415 | Value->getType(), Info, Deduced); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 418 | /// \brief Deduce the value of the given non-type template parameter |
| 419 | /// from the given declaration. |
| 420 | /// |
| 421 | /// \returns true if deduction succeeded, false otherwise. |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 422 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| 423 | Sema &S, TemplateParameterList *TemplateParams, |
| 424 | NonTypeTemplateParmDecl *NTTP, ValueDecl *D, QualType T, |
| 425 | TemplateDeductionInfo &Info, |
| 426 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 427 | D = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 428 | TemplateArgument New(D, T); |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 429 | return DeduceNonTypeTemplateArgument( |
| 430 | S, TemplateParams, NTTP, DeducedTemplateArgument(New), T, Info, Deduced); |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 433 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 434 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 435 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 436 | TemplateName Param, |
| 437 | TemplateName Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 438 | TemplateDeductionInfo &Info, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 439 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 440 | TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 441 | if (!ParamDecl) { |
| 442 | // The parameter type is dependent and is not a template template parameter, |
| 443 | // so there is nothing that we can deduce. |
| 444 | return Sema::TDK_Success; |
| 445 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 446 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 447 | if (TemplateTemplateParmDecl *TempParam |
| 448 | = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 449 | // If we're not deducing at this depth, there's nothing to deduce. |
| 450 | if (TempParam->getDepth() != Info.getDeducedDepth()) |
| 451 | return Sema::TDK_Success; |
| 452 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 453 | DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 454 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 455 | Deduced[TempParam->getIndex()], |
| 456 | NewDeduced); |
| 457 | if (Result.isNull()) { |
| 458 | Info.Param = TempParam; |
| 459 | Info.FirstArg = Deduced[TempParam->getIndex()]; |
| 460 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 461 | return Sema::TDK_Inconsistent; |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 462 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 463 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 464 | Deduced[TempParam->getIndex()] = Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 465 | return Sema::TDK_Success; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 466 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 467 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 468 | // Verify that the two template names are equivalent. |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 469 | if (S.Context.hasSameTemplateName(Param, Arg)) |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 470 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 471 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 472 | // Mismatch of non-dependent template parameter to argument. |
| 473 | Info.FirstArg = TemplateArgument(Param); |
| 474 | Info.SecondArg = TemplateArgument(Arg); |
| 475 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | /// \brief Deduce the template arguments by comparing the template parameter |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 479 | /// type (which is a template-id) with the template argument type. |
| 480 | /// |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 481 | /// \param S the Sema |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 482 | /// |
| 483 | /// \param TemplateParams the template parameters that we are deducing |
| 484 | /// |
| 485 | /// \param Param the parameter type |
| 486 | /// |
| 487 | /// \param Arg the argument type |
| 488 | /// |
| 489 | /// \param Info information about the template argument deduction itself |
| 490 | /// |
| 491 | /// \param Deduced the deduced template arguments |
| 492 | /// |
| 493 | /// \returns the result of template argument deduction so far. Note that a |
| 494 | /// "success" result means that template argument deduction has not yet failed, |
| 495 | /// but it may still fail, later, for other reasons. |
| 496 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 497 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 498 | TemplateParameterList *TemplateParams, |
| 499 | const TemplateSpecializationType *Param, |
| 500 | QualType Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 501 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 502 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 503 | assert(Arg.isCanonical() && "Argument type must be canonical"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 504 | |
Richard Smith | 1337318 | 2018-01-04 01:24:17 +0000 | [diff] [blame] | 505 | // Treat an injected-class-name as its underlying template-id. |
| 506 | if (auto *Injected = dyn_cast<InjectedClassNameType>(Arg)) |
| 507 | Arg = Injected->getInjectedSpecializationType(); |
| 508 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 509 | // Check whether the template argument is a dependent template-id. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | if (const TemplateSpecializationType *SpecArg |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 511 | = dyn_cast<TemplateSpecializationType>(Arg)) { |
| 512 | // Perform template argument deduction for the template name. |
| 513 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 514 | = DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 515 | Param->getTemplateName(), |
| 516 | SpecArg->getTemplateName(), |
| 517 | Info, Deduced)) |
| 518 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 519 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 520 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 521 | // Perform template argument deduction on each template |
Douglas Gregor | d80ea20 | 2010-12-22 18:55:49 +0000 | [diff] [blame] | 522 | // argument. Ignore any missing/extra arguments, since they could be |
| 523 | // filled in by default arguments. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 524 | return DeduceTemplateArguments(S, TemplateParams, |
| 525 | Param->template_arguments(), |
| 526 | SpecArg->template_arguments(), Info, Deduced, |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 527 | /*NumberOfArgumentsMustMatch=*/false); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 528 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 529 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 530 | // If the argument type is a class template specialization, we |
| 531 | // perform template argument deduction using its template |
| 532 | // arguments. |
| 533 | const RecordType *RecordArg = dyn_cast<RecordType>(Arg); |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 534 | if (!RecordArg) { |
| 535 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 536 | Info.SecondArg = TemplateArgument(Arg); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 537 | return Sema::TDK_NonDeducedMismatch; |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 538 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | |
| 540 | ClassTemplateSpecializationDecl *SpecArg |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 541 | = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 542 | if (!SpecArg) { |
| 543 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 544 | Info.SecondArg = TemplateArgument(Arg); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 545 | return Sema::TDK_NonDeducedMismatch; |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 546 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 547 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 548 | // Perform template argument deduction for the template name. |
| 549 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 550 | = DeduceTemplateArguments(S, |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 551 | TemplateParams, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 552 | Param->getTemplateName(), |
| 553 | TemplateName(SpecArg->getSpecializedTemplate()), |
| 554 | Info, Deduced)) |
| 555 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 556 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 557 | // Perform template argument deduction for the template arguments. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 558 | return DeduceTemplateArguments(S, TemplateParams, Param->template_arguments(), |
| 559 | SpecArg->getTemplateArgs().asArray(), Info, |
| 560 | Deduced, /*NumberOfArgumentsMustMatch=*/true); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 561 | } |
| 562 | |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 563 | /// \brief Determines whether the given type is an opaque type that |
| 564 | /// might be more qualified when instantiated. |
| 565 | static bool IsPossiblyOpaquelyQualifiedType(QualType T) { |
| 566 | switch (T->getTypeClass()) { |
| 567 | case Type::TypeOfExpr: |
| 568 | case Type::TypeOf: |
| 569 | case Type::DependentName: |
| 570 | case Type::Decltype: |
| 571 | case Type::UnresolvedUsing: |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 572 | case Type::TemplateTypeParm: |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 573 | return true; |
| 574 | |
| 575 | case Type::ConstantArray: |
| 576 | case Type::IncompleteArray: |
| 577 | case Type::VariableArray: |
| 578 | case Type::DependentSizedArray: |
| 579 | return IsPossiblyOpaquelyQualifiedType( |
| 580 | cast<ArrayType>(T)->getElementType()); |
| 581 | |
| 582 | default: |
| 583 | return false; |
| 584 | } |
| 585 | } |
| 586 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 587 | /// \brief Retrieve the depth and index of a template parameter. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 588 | static std::pair<unsigned, unsigned> |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 589 | getDepthAndIndex(NamedDecl *ND) { |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 590 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND)) |
| 591 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 592 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 593 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND)) |
| 594 | return std::make_pair(NTTP->getDepth(), NTTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 595 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 596 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND); |
| 597 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| 598 | } |
| 599 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 600 | /// \brief Retrieve the depth and index of an unexpanded parameter pack. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 601 | static std::pair<unsigned, unsigned> |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 602 | getDepthAndIndex(UnexpandedParameterPack UPP) { |
| 603 | if (const TemplateTypeParmType *TTP |
| 604 | = UPP.first.dyn_cast<const TemplateTypeParmType *>()) |
| 605 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 606 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 607 | return getDepthAndIndex(UPP.first.get<NamedDecl *>()); |
| 608 | } |
| 609 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 610 | /// \brief Helper function to build a TemplateParameter when we don't |
| 611 | /// know its type statically. |
| 612 | static TemplateParameter makeTemplateParameter(Decl *D) { |
| 613 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) |
| 614 | return TemplateParameter(TTP); |
Craig Topper | 4b482ee | 2013-07-08 04:24:47 +0000 | [diff] [blame] | 615 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 616 | return TemplateParameter(NTTP); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 617 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 618 | return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); |
| 619 | } |
| 620 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 621 | /// A pack that we're currently deducing. |
| 622 | struct clang::DeducedPack { |
| 623 | DeducedPack(unsigned Index) : Index(Index), Outer(nullptr) {} |
Craig Topper | 0a4e1f5 | 2013-07-08 04:44:01 +0000 | [diff] [blame] | 624 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 625 | // The index of the pack. |
| 626 | unsigned Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 627 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 628 | // The old value of the pack before we started deducing it. |
| 629 | DeducedTemplateArgument Saved; |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 630 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 631 | // A deferred value of this pack from an inner deduction, that couldn't be |
| 632 | // deduced because this deduction hadn't happened yet. |
| 633 | DeducedTemplateArgument DeferredDeduction; |
| 634 | |
| 635 | // The new value of the pack. |
| 636 | SmallVector<DeducedTemplateArgument, 4> New; |
| 637 | |
| 638 | // The outer deduction for this pack, if any. |
| 639 | DeducedPack *Outer; |
| 640 | }; |
| 641 | |
Benjamin Kramer | d5748c7 | 2015-03-23 12:31:05 +0000 | [diff] [blame] | 642 | namespace { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 643 | /// A scope in which we're performing pack deduction. |
| 644 | class PackDeductionScope { |
| 645 | public: |
| 646 | PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, |
| 647 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 648 | TemplateDeductionInfo &Info, TemplateArgument Pattern) |
| 649 | : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { |
Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 650 | // Dig out the partially-substituted pack, if there is one. |
| 651 | const TemplateArgument *PartialPackArgs = nullptr; |
| 652 | unsigned NumPartialPackArgs = 0; |
| 653 | std::pair<unsigned, unsigned> PartialPackDepthIndex(-1u, -1u); |
| 654 | if (auto *Scope = S.CurrentInstantiationScope) |
| 655 | if (auto *Partial = Scope->getPartiallySubstitutedPack( |
| 656 | &PartialPackArgs, &NumPartialPackArgs)) |
| 657 | PartialPackDepthIndex = getDepthAndIndex(Partial); |
| 658 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 659 | // Compute the set of template parameter indices that correspond to |
| 660 | // parameter packs expanded by the pack expansion. |
| 661 | { |
| 662 | llvm::SmallBitVector SawIndices(TemplateParams->size()); |
Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 663 | |
| 664 | auto AddPack = [&](unsigned Index) { |
| 665 | if (SawIndices[Index]) |
| 666 | return; |
| 667 | SawIndices[Index] = true; |
| 668 | |
| 669 | // Save the deduced template argument for the parameter pack expanded |
| 670 | // by this pack expansion, then clear out the deduction. |
| 671 | DeducedPack Pack(Index); |
| 672 | Pack.Saved = Deduced[Index]; |
| 673 | Deduced[Index] = TemplateArgument(); |
| 674 | |
| 675 | Packs.push_back(Pack); |
| 676 | }; |
| 677 | |
| 678 | // First look for unexpanded packs in the pattern. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 679 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 680 | S.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
| 681 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 682 | unsigned Depth, Index; |
| 683 | std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); |
Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 684 | if (Depth == Info.getDeducedDepth()) |
| 685 | AddPack(Index); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 686 | } |
Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 687 | assert(!Packs.empty() && "Pack expansion without unexpanded packs?"); |
| 688 | |
| 689 | // This pack expansion will have been partially expanded iff the only |
| 690 | // unexpanded parameter pack within it is the partially-substituted pack. |
| 691 | IsPartiallyExpanded = |
| 692 | Packs.size() == 1 && |
| 693 | PartialPackDepthIndex == |
| 694 | std::make_pair(Info.getDeducedDepth(), Packs.front().Index); |
| 695 | |
| 696 | // Skip over the pack elements that were expanded into separate arguments. |
| 697 | if (IsPartiallyExpanded) |
| 698 | PackElements += NumPartialPackArgs; |
| 699 | |
| 700 | // We can also have deduced template parameters that do not actually |
| 701 | // appear in the pattern, but can be deduced by it (the type of a non-type |
| 702 | // template parameter pack, in particular). These won't have prevented us |
| 703 | // from partially expanding the pack. |
| 704 | llvm::SmallBitVector Used(TemplateParams->size()); |
| 705 | MarkUsedTemplateParameters(S.Context, Pattern, /*OnlyDeduced*/true, |
| 706 | Info.getDeducedDepth(), Used); |
| 707 | for (int Index = Used.find_first(); Index != -1; |
| 708 | Index = Used.find_next(Index)) |
| 709 | if (TemplateParams->getParam(Index)->isParameterPack()) |
| 710 | AddPack(Index); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 711 | } |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 712 | |
| 713 | for (auto &Pack : Packs) { |
| 714 | if (Info.PendingDeducedPacks.size() > Pack.Index) |
| 715 | Pack.Outer = Info.PendingDeducedPacks[Pack.Index]; |
| 716 | else |
| 717 | Info.PendingDeducedPacks.resize(Pack.Index + 1); |
| 718 | Info.PendingDeducedPacks[Pack.Index] = &Pack; |
| 719 | |
Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 720 | if (PartialPackDepthIndex == |
| 721 | std::make_pair(Info.getDeducedDepth(), Pack.Index)) { |
| 722 | Pack.New.append(PartialPackArgs, PartialPackArgs + NumPartialPackArgs); |
| 723 | // We pre-populate the deduced value of the partially-substituted |
| 724 | // pack with the specified value. This is not entirely correct: the |
| 725 | // value is supposed to have been substituted, not deduced, but the |
| 726 | // cases where this is observable require an exact type match anyway. |
| 727 | // |
| 728 | // FIXME: If we could represent a "depth i, index j, pack elem k" |
| 729 | // parameter, we could substitute the partially-substituted pack |
| 730 | // everywhere and avoid this. |
| 731 | if (Pack.New.size() > PackElements) |
| 732 | Deduced[Pack.Index] = Pack.New[PackElements]; |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 733 | } |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 734 | } |
| 735 | } |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 736 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 737 | ~PackDeductionScope() { |
| 738 | for (auto &Pack : Packs) |
| 739 | Info.PendingDeducedPacks[Pack.Index] = Pack.Outer; |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 740 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 741 | |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 742 | /// Determine whether this pack has already been partially expanded into a |
| 743 | /// sequence of (prior) function parameters / template arguments. |
Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 744 | bool isPartiallyExpanded() { return IsPartiallyExpanded; } |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 745 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 746 | /// Move to deducing the next element in each pack that is being deduced. |
| 747 | void nextPackElement() { |
| 748 | // Capture the deduced template arguments for each parameter pack expanded |
| 749 | // by this pack expansion, add them to the list of arguments we've deduced |
| 750 | // for that pack, then clear out the deduced argument. |
| 751 | for (auto &Pack : Packs) { |
| 752 | DeducedTemplateArgument &DeducedArg = Deduced[Pack.Index]; |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 753 | if (!Pack.New.empty() || !DeducedArg.isNull()) { |
| 754 | while (Pack.New.size() < PackElements) |
| 755 | Pack.New.push_back(DeducedTemplateArgument()); |
Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 756 | if (Pack.New.size() == PackElements) |
| 757 | Pack.New.push_back(DeducedArg); |
| 758 | else |
| 759 | Pack.New[PackElements] = DeducedArg; |
| 760 | DeducedArg = Pack.New.size() > PackElements + 1 |
| 761 | ? Pack.New[PackElements + 1] |
| 762 | : DeducedTemplateArgument(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 763 | } |
| 764 | } |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 765 | ++PackElements; |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | /// \brief Finish template argument deduction for a set of argument packs, |
| 769 | /// producing the argument packs and checking for consistency with prior |
| 770 | /// deductions. |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 771 | Sema::TemplateDeductionResult finish() { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 772 | // Build argument packs for each of the parameter packs expanded by this |
| 773 | // pack expansion. |
| 774 | for (auto &Pack : Packs) { |
| 775 | // Put back the old value for this pack. |
| 776 | Deduced[Pack.Index] = Pack.Saved; |
| 777 | |
| 778 | // Build or find a new value for this pack. |
| 779 | DeducedTemplateArgument NewPack; |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 780 | if (PackElements && Pack.New.empty()) { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 781 | if (Pack.DeferredDeduction.isNull()) { |
| 782 | // We were not able to deduce anything for this parameter pack |
| 783 | // (because it only appeared in non-deduced contexts), so just |
| 784 | // restore the saved argument pack. |
| 785 | continue; |
| 786 | } |
| 787 | |
| 788 | NewPack = Pack.DeferredDeduction; |
| 789 | Pack.DeferredDeduction = TemplateArgument(); |
| 790 | } else if (Pack.New.empty()) { |
| 791 | // If we deduced an empty argument pack, create it now. |
| 792 | NewPack = DeducedTemplateArgument(TemplateArgument::getEmptyPack()); |
| 793 | } else { |
| 794 | TemplateArgument *ArgumentPack = |
| 795 | new (S.Context) TemplateArgument[Pack.New.size()]; |
| 796 | std::copy(Pack.New.begin(), Pack.New.end(), ArgumentPack); |
| 797 | NewPack = DeducedTemplateArgument( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 798 | TemplateArgument(llvm::makeArrayRef(ArgumentPack, Pack.New.size())), |
Richard Smith | 7fa88bb | 2017-02-21 07:22:31 +0000 | [diff] [blame] | 799 | // FIXME: This is wrong, it's possible that some pack elements are |
| 800 | // deduced from an array bound and others are not: |
| 801 | // template<typename ...T, T ...V> void g(const T (&...p)[V]); |
| 802 | // g({1, 2, 3}, {{}, {}}); |
| 803 | // ... should deduce T = {int, size_t (from array bound)}. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 804 | Pack.New[0].wasDeducedFromArrayBound()); |
| 805 | } |
| 806 | |
| 807 | // Pick where we're going to put the merged pack. |
| 808 | DeducedTemplateArgument *Loc; |
| 809 | if (Pack.Outer) { |
| 810 | if (Pack.Outer->DeferredDeduction.isNull()) { |
| 811 | // Defer checking this pack until we have a complete pack to compare |
| 812 | // it against. |
| 813 | Pack.Outer->DeferredDeduction = NewPack; |
| 814 | continue; |
| 815 | } |
| 816 | Loc = &Pack.Outer->DeferredDeduction; |
| 817 | } else { |
| 818 | Loc = &Deduced[Pack.Index]; |
| 819 | } |
| 820 | |
| 821 | // Check the new pack matches any previous value. |
| 822 | DeducedTemplateArgument OldPack = *Loc; |
| 823 | DeducedTemplateArgument Result = |
| 824 | checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 825 | |
| 826 | // If we deferred a deduction of this pack, check that one now too. |
| 827 | if (!Result.isNull() && !Pack.DeferredDeduction.isNull()) { |
| 828 | OldPack = Result; |
| 829 | NewPack = Pack.DeferredDeduction; |
| 830 | Result = checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 831 | } |
| 832 | |
| 833 | if (Result.isNull()) { |
| 834 | Info.Param = |
| 835 | makeTemplateParameter(TemplateParams->getParam(Pack.Index)); |
| 836 | Info.FirstArg = OldPack; |
| 837 | Info.SecondArg = NewPack; |
| 838 | return Sema::TDK_Inconsistent; |
| 839 | } |
| 840 | |
| 841 | *Loc = Result; |
| 842 | } |
| 843 | |
| 844 | return Sema::TDK_Success; |
| 845 | } |
| 846 | |
| 847 | private: |
| 848 | Sema &S; |
| 849 | TemplateParameterList *TemplateParams; |
| 850 | SmallVectorImpl<DeducedTemplateArgument> &Deduced; |
| 851 | TemplateDeductionInfo &Info; |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 852 | unsigned PackElements = 0; |
Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 853 | bool IsPartiallyExpanded = false; |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 854 | |
| 855 | SmallVector<DeducedPack, 2> Packs; |
| 856 | }; |
Benjamin Kramer | d5748c7 | 2015-03-23 12:31:05 +0000 | [diff] [blame] | 857 | } // namespace |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 858 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 859 | /// \brief Deduce the template arguments by comparing the list of parameter |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 860 | /// types to the list of argument types, as in the parameter-type-lists of |
| 861 | /// function types (C++ [temp.deduct.type]p10). |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 862 | /// |
| 863 | /// \param S The semantic analysis object within which we are deducing |
| 864 | /// |
| 865 | /// \param TemplateParams The template parameters that we are deducing |
| 866 | /// |
| 867 | /// \param Params The list of parameter types |
| 868 | /// |
| 869 | /// \param NumParams The number of types in \c Params |
| 870 | /// |
| 871 | /// \param Args The list of argument types |
| 872 | /// |
| 873 | /// \param NumArgs The number of types in \c Args |
| 874 | /// |
| 875 | /// \param Info information about the template argument deduction itself |
| 876 | /// |
| 877 | /// \param Deduced the deduced template arguments |
| 878 | /// |
| 879 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
| 880 | /// how template argument deduction is performed. |
| 881 | /// |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 882 | /// \param PartialOrdering If true, we are performing template argument |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 883 | /// deduction for during partial ordering for a call |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 884 | /// (C++0x [temp.deduct.partial]). |
| 885 | /// |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 886 | /// \returns the result of template argument deduction so far. Note that a |
| 887 | /// "success" result means that template argument deduction has not yet failed, |
| 888 | /// but it may still fail, later, for other reasons. |
| 889 | static Sema::TemplateDeductionResult |
| 890 | DeduceTemplateArguments(Sema &S, |
| 891 | TemplateParameterList *TemplateParams, |
| 892 | const QualType *Params, unsigned NumParams, |
| 893 | const QualType *Args, unsigned NumArgs, |
| 894 | TemplateDeductionInfo &Info, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 895 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 896 | unsigned TDF, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 897 | bool PartialOrdering = false) { |
Douglas Gregor | 86bea35 | 2011-01-05 23:23:17 +0000 | [diff] [blame] | 898 | // Fast-path check to see if we have too many/too few arguments. |
| 899 | if (NumParams != NumArgs && |
| 900 | !(NumParams && isa<PackExpansionType>(Params[NumParams - 1])) && |
| 901 | !(NumArgs && isa<PackExpansionType>(Args[NumArgs - 1]))) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 902 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 903 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 904 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 905 | // Similarly, if P has a form that contains (T), then each parameter type |
| 906 | // Pi of the respective parameter-type- list of P is compared with the |
| 907 | // corresponding parameter type Ai of the corresponding parameter-type-list |
| 908 | // of A. [...] |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 909 | unsigned ArgIdx = 0, ParamIdx = 0; |
| 910 | for (; ParamIdx != NumParams; ++ParamIdx) { |
| 911 | // Check argument types. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 912 | const PackExpansionType *Expansion |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 913 | = dyn_cast<PackExpansionType>(Params[ParamIdx]); |
| 914 | if (!Expansion) { |
| 915 | // Simple case: compare the parameter and argument types at this point. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 916 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 917 | // Make sure we have an argument. |
| 918 | if (ArgIdx >= NumArgs) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 919 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 920 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 921 | if (isa<PackExpansionType>(Args[ArgIdx])) { |
| 922 | // C++0x [temp.deduct.type]p22: |
| 923 | // If the original function parameter associated with A is a function |
| 924 | // parameter pack and the function parameter associated with P is not |
| 925 | // a function parameter pack, then template argument deduction fails. |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 926 | return Sema::TDK_MiscellaneousDeductionFailure; |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 927 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 928 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 929 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 930 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 931 | Params[ParamIdx], Args[ArgIdx], |
| 932 | Info, Deduced, TDF, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 933 | PartialOrdering)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 934 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 935 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 936 | ++ArgIdx; |
| 937 | continue; |
| 938 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 939 | |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 940 | // C++0x [temp.deduct.type]p5: |
| 941 | // The non-deduced contexts are: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 942 | // - A function parameter pack that does not occur at the end of the |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 943 | // parameter-declaration-clause. |
| 944 | if (ParamIdx + 1 < NumParams) |
| 945 | return Sema::TDK_Success; |
| 946 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 947 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 948 | // If the parameter-declaration corresponding to Pi is a function |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 949 | // parameter pack, then the type of its declarator- id is compared with |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 950 | // each remaining parameter type in the parameter-type-list of A. Each |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 951 | // comparison deduces template arguments for subsequent positions in the |
| 952 | // template parameter packs expanded by the function parameter pack. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 953 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 954 | QualType Pattern = Expansion->getPattern(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 955 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 956 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 957 | for (; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 958 | // Deduce template arguments from the pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 959 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 960 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, Pattern, |
| 961 | Args[ArgIdx], Info, Deduced, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 962 | TDF, PartialOrdering)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 963 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 964 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 965 | PackScope.nextPackElement(); |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 966 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 967 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 968 | // Build argument packs for each of the parameter packs expanded by this |
| 969 | // pack expansion. |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 970 | if (auto Result = PackScope.finish()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 971 | return Result; |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 972 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 973 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 974 | // Make sure we don't have any extra arguments. |
| 975 | if (ArgIdx < NumArgs) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 976 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 977 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 978 | return Sema::TDK_Success; |
| 979 | } |
| 980 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 981 | /// \brief Determine whether the parameter has qualifiers that are either |
| 982 | /// inconsistent with or a superset of the argument's qualifiers. |
| 983 | static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType, |
| 984 | QualType ArgType) { |
| 985 | Qualifiers ParamQs = ParamType.getQualifiers(); |
| 986 | Qualifiers ArgQs = ArgType.getQualifiers(); |
| 987 | |
| 988 | if (ParamQs == ArgQs) |
| 989 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 990 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 991 | // Mismatched (but not missing) Objective-C GC attributes. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 992 | if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() && |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 993 | ParamQs.hasObjCGCAttr()) |
| 994 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 995 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 996 | // Mismatched (but not missing) address spaces. |
| 997 | if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() && |
| 998 | ParamQs.hasAddressSpace()) |
| 999 | return true; |
| 1000 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1001 | // Mismatched (but not missing) Objective-C lifetime qualifiers. |
| 1002 | if (ParamQs.getObjCLifetime() != ArgQs.getObjCLifetime() && |
| 1003 | ParamQs.hasObjCLifetime()) |
| 1004 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1005 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1006 | // CVR qualifier superset. |
| 1007 | return (ParamQs.getCVRQualifiers() != ArgQs.getCVRQualifiers()) && |
| 1008 | ((ParamQs.getCVRQualifiers() | ArgQs.getCVRQualifiers()) |
| 1009 | == ParamQs.getCVRQualifiers()); |
| 1010 | } |
| 1011 | |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1012 | /// \brief Compare types for equality with respect to possibly compatible |
| 1013 | /// function types (noreturn adjustment, implicit calling conventions). If any |
| 1014 | /// of parameter and argument is not a function, just perform type comparison. |
| 1015 | /// |
| 1016 | /// \param Param the template parameter type. |
| 1017 | /// |
| 1018 | /// \param Arg the argument type. |
| 1019 | bool Sema::isSameOrCompatibleFunctionType(CanQualType Param, |
| 1020 | CanQualType Arg) { |
| 1021 | const FunctionType *ParamFunction = Param->getAs<FunctionType>(), |
| 1022 | *ArgFunction = Arg->getAs<FunctionType>(); |
| 1023 | |
| 1024 | // Just compare if not functions. |
| 1025 | if (!ParamFunction || !ArgFunction) |
| 1026 | return Param == Arg; |
| 1027 | |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 1028 | // Noreturn and noexcept adjustment. |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1029 | QualType AdjustedParam; |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 1030 | if (IsFunctionConversion(Param, Arg, AdjustedParam)) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1031 | return Arg == Context.getCanonicalType(AdjustedParam); |
| 1032 | |
| 1033 | // FIXME: Compatible calling conventions. |
| 1034 | |
| 1035 | return Param == Arg; |
| 1036 | } |
| 1037 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1038 | /// Get the index of the first template parameter that was originally from the |
| 1039 | /// innermost template-parameter-list. This is 0 except when we concatenate |
| 1040 | /// the template parameter lists of a class template and a constructor template |
| 1041 | /// when forming an implicit deduction guide. |
| 1042 | static unsigned getFirstInnerIndex(FunctionTemplateDecl *FTD) { |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1043 | auto *Guide = dyn_cast<CXXDeductionGuideDecl>(FTD->getTemplatedDecl()); |
| 1044 | if (!Guide || !Guide->isImplicit()) |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1045 | return 0; |
Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1046 | return Guide->getDeducedTemplate()->getTemplateParameters()->size(); |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | /// Determine whether a type denotes a forwarding reference. |
| 1050 | static bool isForwardingReference(QualType Param, unsigned FirstInnerIndex) { |
| 1051 | // C++1z [temp.deduct.call]p3: |
| 1052 | // A forwarding reference is an rvalue reference to a cv-unqualified |
| 1053 | // template parameter that does not represent a template parameter of a |
| 1054 | // class template. |
| 1055 | if (auto *ParamRef = Param->getAs<RValueReferenceType>()) { |
| 1056 | if (ParamRef->getPointeeType().getQualifiers()) |
| 1057 | return false; |
| 1058 | auto *TypeParm = ParamRef->getPointeeType()->getAs<TemplateTypeParmType>(); |
| 1059 | return TypeParm && TypeParm->getIndex() >= FirstInnerIndex; |
| 1060 | } |
| 1061 | return false; |
| 1062 | } |
| 1063 | |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1064 | /// \brief Deduce the template arguments by comparing the parameter type and |
| 1065 | /// the argument type (C++ [temp.deduct.type]). |
| 1066 | /// |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1067 | /// \param S the semantic analysis object within which we are deducing |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1068 | /// |
| 1069 | /// \param TemplateParams the template parameters that we are deducing |
| 1070 | /// |
| 1071 | /// \param ParamIn the parameter type |
| 1072 | /// |
| 1073 | /// \param ArgIn the argument type |
| 1074 | /// |
| 1075 | /// \param Info information about the template argument deduction itself |
| 1076 | /// |
| 1077 | /// \param Deduced the deduced template arguments |
| 1078 | /// |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1079 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1080 | /// how template argument deduction is performed. |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1081 | /// |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1082 | /// \param PartialOrdering Whether we're performing template argument deduction |
| 1083 | /// in the context of partial ordering (C++0x [temp.deduct.partial]). |
| 1084 | /// |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1085 | /// \returns the result of template argument deduction so far. Note that a |
| 1086 | /// "success" result means that template argument deduction has not yet failed, |
| 1087 | /// but it may still fail, later, for other reasons. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1088 | static Sema::TemplateDeductionResult |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1089 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 1090 | TemplateParameterList *TemplateParams, |
| 1091 | QualType ParamIn, QualType ArgIn, |
| 1092 | TemplateDeductionInfo &Info, |
| 1093 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 1094 | unsigned TDF, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1095 | bool PartialOrdering, |
| 1096 | bool DeducedFromArrayBound) { |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1097 | // We only want to look at the canonical types, since typedefs and |
| 1098 | // sugar are not part of template argument deduction. |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1099 | QualType Param = S.Context.getCanonicalType(ParamIn); |
| 1100 | QualType Arg = S.Context.getCanonicalType(ArgIn); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1101 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1102 | // If the argument type is a pack expansion, look at its pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1103 | // This isn't explicitly called out |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1104 | if (const PackExpansionType *ArgExpansion |
| 1105 | = dyn_cast<PackExpansionType>(Arg)) |
| 1106 | Arg = ArgExpansion->getPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1107 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1108 | if (PartialOrdering) { |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1109 | // C++11 [temp.deduct.partial]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1110 | // Before the partial ordering is done, certain transformations are |
| 1111 | // performed on the types used for partial ordering: |
| 1112 | // - 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] | 1113 | const ReferenceType *ParamRef = Param->getAs<ReferenceType>(); |
| 1114 | if (ParamRef) |
| 1115 | Param = ParamRef->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1116 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1117 | // - If A is a reference type, A is replaced by the type referred to. |
| 1118 | const ReferenceType *ArgRef = Arg->getAs<ReferenceType>(); |
| 1119 | if (ArgRef) |
| 1120 | Arg = ArgRef->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1121 | |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1122 | if (ParamRef && ArgRef && S.Context.hasSameUnqualifiedType(Param, Arg)) { |
| 1123 | // C++11 [temp.deduct.partial]p9: |
| 1124 | // If, for a given type, deduction succeeds in both directions (i.e., |
| 1125 | // the types are identical after the transformations above) and both |
| 1126 | // P and A were reference types [...]: |
| 1127 | // - if [one type] was an lvalue reference and [the other type] was |
| 1128 | // not, [the other type] is not considered to be at least as |
| 1129 | // specialized as [the first type] |
| 1130 | // - if [one type] is more cv-qualified than [the other type], |
| 1131 | // [the other type] is not considered to be at least as specialized |
| 1132 | // as [the first type] |
| 1133 | // Objective-C ARC adds: |
| 1134 | // - [one type] has non-trivial lifetime, [the other type] has |
| 1135 | // __unsafe_unretained lifetime, and the types are otherwise |
| 1136 | // identical |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1137 | // |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1138 | // A is "considered to be at least as specialized" as P iff deduction |
| 1139 | // succeeds, so we model this as a deduction failure. Note that |
| 1140 | // [the first type] is P and [the other type] is A here; the standard |
| 1141 | // gets this backwards. |
Douglas Gregor | 85894a8 | 2011-04-30 17:07:52 +0000 | [diff] [blame] | 1142 | Qualifiers ParamQuals = Param.getQualifiers(); |
| 1143 | Qualifiers ArgQuals = Arg.getQualifiers(); |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1144 | if ((ParamRef->isLValueReferenceType() && |
| 1145 | !ArgRef->isLValueReferenceType()) || |
| 1146 | ParamQuals.isStrictSupersetOf(ArgQuals) || |
| 1147 | (ParamQuals.hasNonTrivialObjCLifetime() && |
| 1148 | ArgQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone && |
| 1149 | ParamQuals.withoutObjCLifetime() == |
| 1150 | ArgQuals.withoutObjCLifetime())) { |
| 1151 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1152 | Info.SecondArg = TemplateArgument(ArgIn); |
| 1153 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 6beabee | 2014-01-02 19:42:02 +0000 | [diff] [blame] | 1154 | } |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1155 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1156 | |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1157 | // C++11 [temp.deduct.partial]p7: |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1158 | // Remove any top-level cv-qualifiers: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1159 | // - 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] | 1160 | // version of P. |
| 1161 | Param = Param.getUnqualifiedType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1162 | // - 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] | 1163 | // version of A. |
| 1164 | Arg = Arg.getUnqualifiedType(); |
| 1165 | } else { |
| 1166 | // C++0x [temp.deduct.call]p4 bullet 1: |
| 1167 | // - If the original P is a reference type, the deduced A (i.e., the type |
| 1168 | // referred to by the reference) can be more cv-qualified than the |
| 1169 | // transformed A. |
| 1170 | if (TDF & TDF_ParamWithReferenceType) { |
| 1171 | Qualifiers Quals; |
| 1172 | QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals); |
| 1173 | Quals.setCVRQualifiers(Quals.getCVRQualifiers() & |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 1174 | Arg.getCVRQualifiers()); |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1175 | Param = S.Context.getQualifiedType(UnqualParam, Quals); |
| 1176 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1177 | |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1178 | if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) { |
| 1179 | // C++0x [temp.deduct.type]p10: |
| 1180 | // If P and A are function types that originated from deduction when |
| 1181 | // taking the address of a function template (14.8.2.2) or when deducing |
| 1182 | // 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] | 1183 | // 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] | 1184 | // respectively, Pi is adjusted if it is a forwarding reference and Ai |
| 1185 | // is an lvalue reference, in |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1186 | // 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] | 1187 | // type (i.e., T&& is changed to simply T). [ Note: As a result, when |
| 1188 | // 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] | 1189 | // deduced as X&. - end note ] |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1190 | TDF &= ~TDF_TopLevelParameterTypeList; |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1191 | if (isForwardingReference(Param, 0) && Arg->isLValueReferenceType()) |
| 1192 | Param = Param->getPointeeType(); |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1193 | } |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1194 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1195 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1196 | // C++ [temp.deduct.type]p9: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1197 | // A template type argument T, a template template argument TT or a |
| 1198 | // 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] | 1199 | // the following forms: |
| 1200 | // |
| 1201 | // T |
| 1202 | // cv-list T |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1203 | if (const TemplateTypeParmType *TemplateTypeParm |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1204 | = Param->getAs<TemplateTypeParmType>()) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1205 | // Just skip any attempts to deduce from a placeholder type or a parameter |
| 1206 | // at a different depth. |
| 1207 | if (Arg->isPlaceholderType() || |
| 1208 | Info.getDeducedDepth() != TemplateTypeParm->getDepth()) |
Douglas Gregor | 4ea5dec | 2011-09-22 15:57:07 +0000 | [diff] [blame] | 1209 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1210 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1211 | unsigned Index = TemplateTypeParm->getIndex(); |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1212 | bool RecanonicalizeArg = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1213 | |
Douglas Gregor | 6045482 | 2009-07-22 20:02:25 +0000 | [diff] [blame] | 1214 | // If the argument type is an array type, move the qualifiers up to the |
| 1215 | // 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] | 1216 | if (isa<ArrayType>(Arg)) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1217 | Qualifiers Quals; |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1218 | Arg = S.Context.getUnqualifiedArrayType(Arg, Quals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1219 | if (Quals) { |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1220 | Arg = S.Context.getQualifiedType(Arg, Quals); |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1221 | RecanonicalizeArg = true; |
| 1222 | } |
| 1223 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1224 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1225 | // The argument type can not be less qualified than the parameter |
| 1226 | // type. |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1227 | if (!(TDF & TDF_IgnoreQualifiers) && |
| 1228 | hasInconsistentOrSupersetQualifiersOf(Param, Arg)) { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1229 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1230 | Info.FirstArg = TemplateArgument(Param); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1231 | Info.SecondArg = TemplateArgument(Arg); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1232 | return Sema::TDK_Underqualified; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1233 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1234 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1235 | assert(TemplateTypeParm->getDepth() == Info.getDeducedDepth() && |
| 1236 | "saw template type parameter with wrong depth"); |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1237 | assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function"); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1238 | QualType DeducedType = Arg; |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1239 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1240 | // Remove any qualifiers on the parameter from the deduced type. |
| 1241 | // We checked the qualifiers for consistency above. |
| 1242 | Qualifiers DeducedQs = DeducedType.getQualifiers(); |
| 1243 | Qualifiers ParamQs = Param.getQualifiers(); |
| 1244 | DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers()); |
| 1245 | if (ParamQs.hasObjCGCAttr()) |
| 1246 | DeducedQs.removeObjCGCAttr(); |
| 1247 | if (ParamQs.hasAddressSpace()) |
| 1248 | DeducedQs.removeAddressSpace(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1249 | if (ParamQs.hasObjCLifetime()) |
| 1250 | DeducedQs.removeObjCLifetime(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1251 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1252 | // Objective-C ARC: |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1253 | // If template deduction would produce a lifetime qualifier on a type |
| 1254 | // that is not a lifetime type, template argument deduction fails. |
| 1255 | if (ParamQs.hasObjCLifetime() && !DeducedType->isObjCLifetimeType() && |
| 1256 | !DeducedType->isDependentType()) { |
| 1257 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1258 | Info.FirstArg = TemplateArgument(Param); |
| 1259 | Info.SecondArg = TemplateArgument(Arg); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1260 | return Sema::TDK_Underqualified; |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1261 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1262 | |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1263 | // Objective-C ARC: |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1264 | // If template deduction would produce an argument type with lifetime type |
| 1265 | // but no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1266 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1267 | DeducedType->isObjCLifetimeType() && |
| 1268 | !DeducedQs.hasObjCLifetime()) |
| 1269 | DeducedQs.setObjCLifetime(Qualifiers::OCL_Strong); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1270 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1271 | DeducedType = S.Context.getQualifiedType(DeducedType.getUnqualifiedType(), |
| 1272 | DeducedQs); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1273 | |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1274 | if (RecanonicalizeArg) |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1275 | DeducedType = S.Context.getCanonicalType(DeducedType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1276 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1277 | DeducedTemplateArgument NewDeduced(DeducedType, DeducedFromArrayBound); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1278 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1279 | Deduced[Index], |
| 1280 | NewDeduced); |
| 1281 | if (Result.isNull()) { |
| 1282 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1283 | Info.FirstArg = Deduced[Index]; |
| 1284 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1285 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1286 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1287 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1288 | Deduced[Index] = Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1289 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1292 | // Set up the template argument deduction information for a failure. |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1293 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1294 | Info.SecondArg = TemplateArgument(ArgIn); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1295 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 1296 | // If the parameter is an already-substituted template parameter |
| 1297 | // pack, do nothing: we don't know which of its arguments to look |
| 1298 | // at, so we have to wait until all of the parameter packs in this |
| 1299 | // expansion have arguments. |
| 1300 | if (isa<SubstTemplateTypeParmPackType>(Param)) |
| 1301 | return Sema::TDK_Success; |
| 1302 | |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1303 | // Check the cv-qualifiers on the parameter and argument types. |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1304 | CanQualType CanParam = S.Context.getCanonicalType(Param); |
| 1305 | CanQualType CanArg = S.Context.getCanonicalType(Arg); |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1306 | if (!(TDF & TDF_IgnoreQualifiers)) { |
| 1307 | if (TDF & TDF_ParamWithReferenceType) { |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1308 | if (hasInconsistentOrSupersetQualifiersOf(Param, Arg)) |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1309 | return Sema::TDK_NonDeducedMismatch; |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 1310 | } else if (!IsPossiblyOpaquelyQualifiedType(Param)) { |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1311 | if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1312 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1313 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1314 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1315 | // If the parameter type is not dependent, there is nothing to deduce. |
| 1316 | if (!Param->isDependentType()) { |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1317 | if (!(TDF & TDF_SkipNonDependent)) { |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 1318 | bool NonDeduced = |
| 1319 | (TDF & TDF_AllowCompatibleFunctionType) |
| 1320 | ? !S.isSameOrCompatibleFunctionType(CanParam, CanArg) |
| 1321 | : Param != Arg; |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1322 | if (NonDeduced) { |
| 1323 | return Sema::TDK_NonDeducedMismatch; |
| 1324 | } |
| 1325 | } |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1326 | return Sema::TDK_Success; |
| 1327 | } |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1328 | } else if (!Param->isDependentType()) { |
| 1329 | CanQualType ParamUnqualType = CanParam.getUnqualifiedType(), |
| 1330 | ArgUnqualType = CanArg.getUnqualifiedType(); |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 1331 | bool Success = |
| 1332 | (TDF & TDF_AllowCompatibleFunctionType) |
| 1333 | ? S.isSameOrCompatibleFunctionType(ParamUnqualType, ArgUnqualType) |
| 1334 | : ParamUnqualType == ArgUnqualType; |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1335 | if (Success) |
| 1336 | return Sema::TDK_Success; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1337 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1338 | |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1339 | switch (Param->getTypeClass()) { |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1340 | // Non-canonical types cannot appear here. |
| 1341 | #define NON_CANONICAL_TYPE(Class, Base) \ |
| 1342 | case Type::Class: llvm_unreachable("deducing non-canonical type: " #Class); |
| 1343 | #define TYPE(Class, Base) |
| 1344 | #include "clang/AST/TypeNodes.def" |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1345 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1346 | case Type::TemplateTypeParm: |
| 1347 | case Type::SubstTemplateTypeParmPack: |
| 1348 | llvm_unreachable("Type nodes handled above"); |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1349 | |
| 1350 | // These types cannot be dependent, so simply check whether the types are |
| 1351 | // the same. |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1352 | case Type::Builtin: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1353 | case Type::VariableArray: |
| 1354 | case Type::Vector: |
| 1355 | case Type::FunctionNoProto: |
| 1356 | case Type::Record: |
| 1357 | case Type::Enum: |
| 1358 | case Type::ObjCObject: |
| 1359 | case Type::ObjCInterface: |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1360 | case Type::ObjCObjectPointer: { |
| 1361 | if (TDF & TDF_SkipNonDependent) |
| 1362 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1363 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1364 | if (TDF & TDF_IgnoreQualifiers) { |
| 1365 | Param = Param.getUnqualifiedType(); |
| 1366 | Arg = Arg.getUnqualifiedType(); |
| 1367 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1368 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1369 | return Param == Arg? Sema::TDK_Success : Sema::TDK_NonDeducedMismatch; |
| 1370 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1371 | |
| 1372 | // _Complex T [placeholder extension] |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1373 | case Type::Complex: |
| 1374 | if (const ComplexType *ComplexArg = Arg->getAs<ComplexType>()) |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1375 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1376 | cast<ComplexType>(Param)->getElementType(), |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1377 | ComplexArg->getElementType(), |
| 1378 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1379 | |
| 1380 | return Sema::TDK_NonDeducedMismatch; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1381 | |
| 1382 | // _Atomic T [extension] |
| 1383 | case Type::Atomic: |
| 1384 | if (const AtomicType *AtomicArg = Arg->getAs<AtomicType>()) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1385 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1386 | cast<AtomicType>(Param)->getValueType(), |
| 1387 | AtomicArg->getValueType(), |
| 1388 | Info, Deduced, TDF); |
| 1389 | |
| 1390 | return Sema::TDK_NonDeducedMismatch; |
| 1391 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1392 | // T * |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1393 | case Type::Pointer: { |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1394 | QualType PointeeType; |
| 1395 | if (const PointerType *PointerArg = Arg->getAs<PointerType>()) { |
| 1396 | PointeeType = PointerArg->getPointeeType(); |
| 1397 | } else if (const ObjCObjectPointerType *PointerArg |
| 1398 | = Arg->getAs<ObjCObjectPointerType>()) { |
| 1399 | PointeeType = PointerArg->getPointeeType(); |
| 1400 | } else { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1401 | return Sema::TDK_NonDeducedMismatch; |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1402 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1403 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1404 | unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1405 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1406 | cast<PointerType>(Param)->getPointeeType(), |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1407 | PointeeType, |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1408 | Info, Deduced, SubTDF); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1409 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1410 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1411 | // T & |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1412 | case Type::LValueReference: { |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 1413 | const LValueReferenceType *ReferenceArg = |
| 1414 | Arg->getAs<LValueReferenceType>(); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1415 | if (!ReferenceArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1416 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1417 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1418 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1419 | cast<LValueReferenceType>(Param)->getPointeeType(), |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1420 | ReferenceArg->getPointeeType(), Info, Deduced, 0); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1421 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1422 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1423 | // T && [C++0x] |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1424 | case Type::RValueReference: { |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 1425 | const RValueReferenceType *ReferenceArg = |
| 1426 | Arg->getAs<RValueReferenceType>(); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1427 | if (!ReferenceArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1428 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1430 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1431 | cast<RValueReferenceType>(Param)->getPointeeType(), |
| 1432 | ReferenceArg->getPointeeType(), |
| 1433 | Info, Deduced, 0); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1434 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1435 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1436 | // T [] (implied, but not stated explicitly) |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1437 | case Type::IncompleteArray: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1438 | const IncompleteArrayType *IncompleteArrayArg = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1439 | S.Context.getAsIncompleteArrayType(Arg); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1440 | if (!IncompleteArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1441 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1442 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1443 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1444 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1445 | S.Context.getAsIncompleteArrayType(Param)->getElementType(), |
| 1446 | IncompleteArrayArg->getElementType(), |
| 1447 | Info, Deduced, SubTDF); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1448 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1449 | |
| 1450 | // T [integer-constant] |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1451 | case Type::ConstantArray: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1452 | const ConstantArrayType *ConstantArrayArg = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1453 | S.Context.getAsConstantArrayType(Arg); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1454 | if (!ConstantArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1455 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1456 | |
| 1457 | const ConstantArrayType *ConstantArrayParm = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1458 | S.Context.getAsConstantArrayType(Param); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1459 | if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1460 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1461 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1462 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1463 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1464 | ConstantArrayParm->getElementType(), |
| 1465 | ConstantArrayArg->getElementType(), |
| 1466 | Info, Deduced, SubTDF); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1467 | } |
| 1468 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1469 | // type [i] |
| 1470 | case Type::DependentSizedArray: { |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1471 | const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1472 | if (!ArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1473 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1474 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1475 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
| 1476 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1477 | // Check the element type of the arrays |
| 1478 | const DependentSizedArrayType *DependentArrayParm |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1479 | = S.Context.getAsDependentSizedArrayType(Param); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1480 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1481 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1482 | DependentArrayParm->getElementType(), |
| 1483 | ArrayArg->getElementType(), |
| 1484 | Info, Deduced, SubTDF)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1485 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1486 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1487 | // Determine the array bound is something we can deduce. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1488 | NonTypeTemplateParmDecl *NTTP |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1489 | = getDeducedParameterFromExpr(Info, DependentArrayParm->getSizeExpr()); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1490 | if (!NTTP) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1491 | return Sema::TDK_Success; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1492 | |
| 1493 | // We can perform template argument deduction for the given non-type |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1494 | // template parameter. |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1495 | assert(NTTP->getDepth() == Info.getDeducedDepth() && |
| 1496 | "saw non-type template parameter with wrong depth"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1497 | if (const ConstantArrayType *ConstantArrayArg |
Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1498 | = dyn_cast<ConstantArrayType>(ArrayArg)) { |
| 1499 | llvm::APSInt Size(ConstantArrayArg->getSize()); |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1500 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, Size, |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1501 | S.Context.getSizeType(), |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1502 | /*ArrayBound=*/true, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1503 | Info, Deduced); |
Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1504 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1505 | if (const DependentSizedArrayType *DependentArrayArg |
| 1506 | = dyn_cast<DependentSizedArrayType>(ArrayArg)) |
Douglas Gregor | 7a49ead | 2010-12-22 23:15:38 +0000 | [diff] [blame] | 1507 | if (DependentArrayArg->getSizeExpr()) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1508 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
Douglas Gregor | 7a49ead | 2010-12-22 23:15:38 +0000 | [diff] [blame] | 1509 | DependentArrayArg->getSizeExpr(), |
| 1510 | Info, Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1511 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1512 | // Incomplete type does not match a dependently-sized array type |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1513 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1514 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1515 | |
| 1516 | // type(*)(T) |
| 1517 | // T(*)() |
| 1518 | // T(*)(T) |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1519 | case Type::FunctionProto: { |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1520 | unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1521 | const FunctionProtoType *FunctionProtoArg = |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1522 | dyn_cast<FunctionProtoType>(Arg); |
| 1523 | if (!FunctionProtoArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1524 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1525 | |
| 1526 | const FunctionProtoType *FunctionProtoParam = |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1527 | cast<FunctionProtoType>(Param); |
Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1528 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1529 | if (FunctionProtoParam->getTypeQuals() |
Douglas Gregor | 54e462a | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1530 | != FunctionProtoArg->getTypeQuals() || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1531 | FunctionProtoParam->getRefQualifier() |
Douglas Gregor | 54e462a | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1532 | != FunctionProtoArg->getRefQualifier() || |
| 1533 | FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1534 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1535 | |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1536 | // Check return types. |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 1537 | if (auto Result = DeduceTemplateArgumentsByTypeMatch( |
| 1538 | S, TemplateParams, FunctionProtoParam->getReturnType(), |
| 1539 | FunctionProtoArg->getReturnType(), Info, Deduced, 0)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1540 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1541 | |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 1542 | // Check parameter types. |
| 1543 | if (auto Result = DeduceTemplateArguments( |
| 1544 | S, TemplateParams, FunctionProtoParam->param_type_begin(), |
| 1545 | FunctionProtoParam->getNumParams(), |
| 1546 | FunctionProtoArg->param_type_begin(), |
| 1547 | FunctionProtoArg->getNumParams(), Info, Deduced, SubTDF)) |
| 1548 | return Result; |
| 1549 | |
| 1550 | if (TDF & TDF_AllowCompatibleFunctionType) |
| 1551 | return Sema::TDK_Success; |
| 1552 | |
| 1553 | // FIXME: Per core-2016/10/1019 (no corresponding core issue yet), permit |
| 1554 | // deducing through the noexcept-specifier if it's part of the canonical |
| 1555 | // type. libstdc++ relies on this. |
| 1556 | Expr *NoexceptExpr = FunctionProtoParam->getNoexceptExpr(); |
| 1557 | if (NonTypeTemplateParmDecl *NTTP = |
| 1558 | NoexceptExpr ? getDeducedParameterFromExpr(Info, NoexceptExpr) |
| 1559 | : nullptr) { |
| 1560 | assert(NTTP->getDepth() == Info.getDeducedDepth() && |
| 1561 | "saw non-type template parameter with wrong depth"); |
| 1562 | |
| 1563 | llvm::APSInt Noexcept(1); |
| 1564 | switch (FunctionProtoArg->canThrow(S.Context)) { |
| 1565 | case CT_Cannot: |
| 1566 | Noexcept = 1; |
| 1567 | LLVM_FALLTHROUGH; |
| 1568 | |
| 1569 | case CT_Can: |
| 1570 | // We give E in noexcept(E) the "deduced from array bound" treatment. |
| 1571 | // FIXME: Should we? |
| 1572 | return DeduceNonTypeTemplateArgument( |
| 1573 | S, TemplateParams, NTTP, Noexcept, S.Context.BoolTy, |
| 1574 | /*ArrayBound*/true, Info, Deduced); |
| 1575 | |
| 1576 | case CT_Dependent: |
| 1577 | if (Expr *ArgNoexceptExpr = FunctionProtoArg->getNoexceptExpr()) |
| 1578 | return DeduceNonTypeTemplateArgument( |
| 1579 | S, TemplateParams, NTTP, ArgNoexceptExpr, Info, Deduced); |
| 1580 | // Can't deduce anything from throw(T...). |
| 1581 | break; |
| 1582 | } |
| 1583 | } |
| 1584 | // FIXME: Detect non-deduced exception specification mismatches? |
| 1585 | |
| 1586 | return Sema::TDK_Success; |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1587 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1588 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1589 | case Type::InjectedClassName: { |
| 1590 | // Treat a template's injected-class-name as if the template |
| 1591 | // specialization type had been used. |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1592 | Param = cast<InjectedClassNameType>(Param) |
| 1593 | ->getInjectedSpecializationType(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1594 | assert(isa<TemplateSpecializationType>(Param) && |
| 1595 | "injected class name is not a template specialization type"); |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 1596 | LLVM_FALLTHROUGH; |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1599 | // template-name<T> (where template-name refers to a class template) |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1600 | // template-name<i> |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1601 | // TT<T> |
| 1602 | // TT<i> |
| 1603 | // TT<> |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1604 | case Type::TemplateSpecialization: { |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1605 | const TemplateSpecializationType *SpecParam = |
| 1606 | cast<TemplateSpecializationType>(Param); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1607 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1608 | // When Arg cannot be a derived class, we can just try to deduce template |
| 1609 | // arguments from the template-id. |
| 1610 | const RecordType *RecordT = Arg->getAs<RecordType>(); |
| 1611 | if (!(TDF & TDF_DerivedClass) || !RecordT) |
| 1612 | return DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg, Info, |
| 1613 | Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1614 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1615 | SmallVector<DeducedTemplateArgument, 8> DeducedOrig(Deduced.begin(), |
| 1616 | Deduced.end()); |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1617 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1618 | Sema::TemplateDeductionResult Result = DeduceTemplateArguments( |
| 1619 | S, TemplateParams, SpecParam, Arg, Info, Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1620 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1621 | if (Result == Sema::TDK_Success) |
| 1622 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1623 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1624 | // We cannot inspect base classes as part of deduction when the type |
| 1625 | // is incomplete, so either instantiate any templates necessary to |
| 1626 | // complete the type, or skip over it if it cannot be completed. |
| 1627 | if (!S.isCompleteType(Info.getLocation(), Arg)) |
| 1628 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1629 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1630 | // C++14 [temp.deduct.call] p4b3: |
| 1631 | // If P is a class and P has the form simple-template-id, then the |
| 1632 | // transformed A can be a derived class of the deduced A. Likewise if |
| 1633 | // P is a pointer to a class of the form simple-template-id, the |
| 1634 | // transformed A can be a pointer to a derived class pointed to by the |
| 1635 | // deduced A. |
| 1636 | // |
| 1637 | // These alternatives are considered only if type deduction would |
| 1638 | // otherwise fail. If they yield more than one possible deduced A, the |
| 1639 | // type deduction fails. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1640 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1641 | // Reset the incorrectly deduced argument from above. |
| 1642 | Deduced = DeducedOrig; |
| 1643 | |
| 1644 | // Use data recursion to crawl through the list of base classes. |
| 1645 | // Visited contains the set of nodes we have already visited, while |
| 1646 | // ToVisit is our stack of records that we still need to visit. |
| 1647 | llvm::SmallPtrSet<const RecordType *, 8> Visited; |
| 1648 | SmallVector<const RecordType *, 8> ToVisit; |
| 1649 | ToVisit.push_back(RecordT); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1650 | bool Successful = false; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1651 | SmallVector<DeducedTemplateArgument, 8> SuccessfulDeduced; |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1652 | while (!ToVisit.empty()) { |
| 1653 | // Retrieve the next class in the inheritance hierarchy. |
| 1654 | const RecordType *NextT = ToVisit.pop_back_val(); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1655 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1656 | // If we have already seen this type, skip it. |
| 1657 | if (!Visited.insert(NextT).second) |
| 1658 | continue; |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1659 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1660 | // If this is a base class, try to perform template argument |
| 1661 | // deduction from it. |
| 1662 | if (NextT != RecordT) { |
| 1663 | TemplateDeductionInfo BaseInfo(Info.getLocation()); |
| 1664 | Sema::TemplateDeductionResult BaseResult = |
| 1665 | DeduceTemplateArguments(S, TemplateParams, SpecParam, |
| 1666 | QualType(NextT, 0), BaseInfo, Deduced); |
| 1667 | |
| 1668 | // If template argument deduction for this base was successful, |
| 1669 | // note that we had some success. Otherwise, ignore any deductions |
| 1670 | // from this base class. |
| 1671 | if (BaseResult == Sema::TDK_Success) { |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1672 | // If we've already seen some success, then deduction fails due to |
| 1673 | // an ambiguity (temp.deduct.call p5). |
| 1674 | if (Successful) |
| 1675 | return Sema::TDK_MiscellaneousDeductionFailure; |
| 1676 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1677 | Successful = true; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1678 | std::swap(SuccessfulDeduced, Deduced); |
| 1679 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1680 | Info.Param = BaseInfo.Param; |
| 1681 | Info.FirstArg = BaseInfo.FirstArg; |
| 1682 | Info.SecondArg = BaseInfo.SecondArg; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | Deduced = DeducedOrig; |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1686 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1687 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1688 | // Visit base classes |
| 1689 | CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl()); |
| 1690 | for (const auto &Base : Next->bases()) { |
| 1691 | assert(Base.getType()->isRecordType() && |
| 1692 | "Base class that isn't a record?"); |
| 1693 | ToVisit.push_back(Base.getType()->getAs<RecordType>()); |
| 1694 | } |
| 1695 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1696 | |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1697 | if (Successful) { |
| 1698 | std::swap(SuccessfulDeduced, Deduced); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1699 | return Sema::TDK_Success; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1700 | } |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1701 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1702 | return Result; |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1705 | // T type::* |
| 1706 | // T T::* |
| 1707 | // T (type::*)() |
| 1708 | // type (T::*)() |
| 1709 | // type (type::*)(T) |
| 1710 | // type (T::*)(T) |
| 1711 | // T (type::*)(T) |
| 1712 | // T (T::*)() |
| 1713 | // T (T::*)(T) |
| 1714 | case Type::MemberPointer: { |
| 1715 | const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); |
| 1716 | const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); |
| 1717 | if (!MemPtrArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1718 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1719 | |
David Majnemer | a381cda | 2015-11-30 20:34:28 +0000 | [diff] [blame] | 1720 | QualType ParamPointeeType = MemPtrParam->getPointeeType(); |
| 1721 | if (ParamPointeeType->isFunctionType()) |
| 1722 | S.adjustMemberFunctionCC(ParamPointeeType, /*IsStatic=*/true, |
| 1723 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
| 1724 | QualType ArgPointeeType = MemPtrArg->getPointeeType(); |
| 1725 | if (ArgPointeeType->isFunctionType()) |
| 1726 | S.adjustMemberFunctionCC(ArgPointeeType, /*IsStatic=*/true, |
| 1727 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
| 1728 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1729 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1730 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
David Majnemer | a381cda | 2015-11-30 20:34:28 +0000 | [diff] [blame] | 1731 | ParamPointeeType, |
| 1732 | ArgPointeeType, |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1733 | Info, Deduced, |
| 1734 | TDF & TDF_IgnoreQualifiers)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1735 | return Result; |
| 1736 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1737 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1738 | QualType(MemPtrParam->getClass(), 0), |
| 1739 | QualType(MemPtrArg->getClass(), 0), |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1740 | Info, Deduced, |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1741 | TDF & TDF_IgnoreQualifiers); |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
Anders Carlsson | 15f1dd1 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1744 | // (clang extension) |
| 1745 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1746 | // type(^)(T) |
| 1747 | // T(^)() |
| 1748 | // T(^)(T) |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1749 | case Type::BlockPointer: { |
| 1750 | const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); |
| 1751 | const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1752 | |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1753 | if (!BlockPtrArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1754 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1755 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1756 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1757 | BlockPtrParam->getPointeeType(), |
| 1758 | BlockPtrArg->getPointeeType(), |
| 1759 | Info, Deduced, 0); |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1762 | // (clang extension) |
| 1763 | // |
| 1764 | // T __attribute__(((ext_vector_type(<integral constant>)))) |
| 1765 | case Type::ExtVector: { |
| 1766 | const ExtVectorType *VectorParam = cast<ExtVectorType>(Param); |
| 1767 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1768 | // Make sure that the vectors have the same number of elements. |
| 1769 | if (VectorParam->getNumElements() != VectorArg->getNumElements()) |
| 1770 | return Sema::TDK_NonDeducedMismatch; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1771 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1772 | // Perform deduction on the element types. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1773 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1774 | VectorParam->getElementType(), |
| 1775 | VectorArg->getElementType(), |
| 1776 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1777 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1778 | |
| 1779 | if (const DependentSizedExtVectorType *VectorArg |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1780 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1781 | // We can't check the number of elements, since the argument has a |
| 1782 | // dependent number of elements. This can only occur during partial |
| 1783 | // ordering. |
| 1784 | |
| 1785 | // Perform deduction on the element types. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1786 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1787 | VectorParam->getElementType(), |
| 1788 | VectorArg->getElementType(), |
| 1789 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1790 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1791 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1792 | return Sema::TDK_NonDeducedMismatch; |
| 1793 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1794 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1795 | // (clang extension) |
| 1796 | // |
| 1797 | // T __attribute__(((ext_vector_type(N)))) |
| 1798 | case Type::DependentSizedExtVector: { |
| 1799 | const DependentSizedExtVectorType *VectorParam |
| 1800 | = cast<DependentSizedExtVectorType>(Param); |
| 1801 | |
| 1802 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1803 | // Perform deduction on the element types. |
| 1804 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1805 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1806 | VectorParam->getElementType(), |
| 1807 | VectorArg->getElementType(), |
| 1808 | Info, Deduced, TDF)) |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1809 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1810 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1811 | // Perform deduction on the vector size, if we can. |
| 1812 | NonTypeTemplateParmDecl *NTTP |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1813 | = getDeducedParameterFromExpr(Info, VectorParam->getSizeExpr()); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1814 | if (!NTTP) |
| 1815 | return Sema::TDK_Success; |
| 1816 | |
| 1817 | llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); |
| 1818 | ArgSize = VectorArg->getNumElements(); |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1819 | // Note that we use the "array bound" rules here; just like in that |
| 1820 | // case, we don't have any particular type for the vector size, but |
| 1821 | // we can provide one if necessary. |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1822 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1823 | S.Context.IntTy, true, Info, |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 1824 | Deduced); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1825 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1826 | |
| 1827 | if (const DependentSizedExtVectorType *VectorArg |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1828 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1829 | // Perform deduction on the element types. |
| 1830 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1831 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1832 | VectorParam->getElementType(), |
| 1833 | VectorArg->getElementType(), |
| 1834 | Info, Deduced, TDF)) |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1835 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1836 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1837 | // Perform deduction on the vector size, if we can. |
| 1838 | NonTypeTemplateParmDecl *NTTP |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1839 | = getDeducedParameterFromExpr(Info, VectorParam->getSizeExpr()); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1840 | if (!NTTP) |
| 1841 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1842 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1843 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 1844 | VectorArg->getSizeExpr(), |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1845 | Info, Deduced); |
| 1846 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1847 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1848 | return Sema::TDK_NonDeducedMismatch; |
| 1849 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1850 | |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 1851 | // (clang extension) |
| 1852 | // |
| 1853 | // T __attribute__(((address_space(N)))) |
| 1854 | case Type::DependentAddressSpace: { |
| 1855 | const DependentAddressSpaceType *AddressSpaceParam = |
| 1856 | cast<DependentAddressSpaceType>(Param); |
| 1857 | |
| 1858 | if (const DependentAddressSpaceType *AddressSpaceArg = |
| 1859 | dyn_cast<DependentAddressSpaceType>(Arg)) { |
| 1860 | // Perform deduction on the pointer type. |
| 1861 | if (Sema::TemplateDeductionResult Result = |
| 1862 | DeduceTemplateArgumentsByTypeMatch( |
| 1863 | S, TemplateParams, AddressSpaceParam->getPointeeType(), |
| 1864 | AddressSpaceArg->getPointeeType(), Info, Deduced, TDF)) |
| 1865 | return Result; |
| 1866 | |
| 1867 | // Perform deduction on the address space, if we can. |
| 1868 | NonTypeTemplateParmDecl *NTTP = getDeducedParameterFromExpr( |
| 1869 | Info, AddressSpaceParam->getAddrSpaceExpr()); |
| 1870 | if (!NTTP) |
| 1871 | return Sema::TDK_Success; |
| 1872 | |
| 1873 | return DeduceNonTypeTemplateArgument( |
| 1874 | S, TemplateParams, NTTP, AddressSpaceArg->getAddrSpaceExpr(), Info, |
| 1875 | Deduced); |
| 1876 | } |
| 1877 | |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 1878 | if (isTargetAddressSpace(Arg.getAddressSpace())) { |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 1879 | llvm::APSInt ArgAddressSpace(S.Context.getTypeSize(S.Context.IntTy), |
| 1880 | false); |
Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 1881 | ArgAddressSpace = toTargetAddressSpace(Arg.getAddressSpace()); |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 1882 | |
| 1883 | // Perform deduction on the pointer types. |
| 1884 | if (Sema::TemplateDeductionResult Result = |
| 1885 | DeduceTemplateArgumentsByTypeMatch( |
| 1886 | S, TemplateParams, AddressSpaceParam->getPointeeType(), |
| 1887 | S.Context.removeAddrSpaceQualType(Arg), Info, Deduced, TDF)) |
| 1888 | return Result; |
| 1889 | |
| 1890 | // Perform deduction on the address space, if we can. |
| 1891 | NonTypeTemplateParmDecl *NTTP = getDeducedParameterFromExpr( |
| 1892 | Info, AddressSpaceParam->getAddrSpaceExpr()); |
| 1893 | if (!NTTP) |
| 1894 | return Sema::TDK_Success; |
| 1895 | |
| 1896 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 1897 | ArgAddressSpace, S.Context.IntTy, |
| 1898 | true, Info, Deduced); |
| 1899 | } |
| 1900 | |
| 1901 | return Sema::TDK_NonDeducedMismatch; |
| 1902 | } |
| 1903 | |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1904 | case Type::TypeOfExpr: |
| 1905 | case Type::TypeOf: |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 1906 | case Type::DependentName: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1907 | case Type::UnresolvedUsing: |
| 1908 | case Type::Decltype: |
| 1909 | case Type::UnaryTransform: |
| 1910 | case Type::Auto: |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1911 | case Type::DeducedTemplateSpecialization: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1912 | case Type::DependentTemplateSpecialization: |
| 1913 | case Type::PackExpansion: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 1914 | case Type::Pipe: |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1915 | // No template argument deduction for these types |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1916 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1919 | llvm_unreachable("Invalid Type Class!"); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1922 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1923 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1924 | TemplateParameterList *TemplateParams, |
| 1925 | const TemplateArgument &Param, |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1926 | TemplateArgument Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1927 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 1928 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1929 | // If the template argument is a pack expansion, perform template argument |
| 1930 | // deduction against the pattern of that expansion. This only occurs during |
| 1931 | // partial ordering. |
| 1932 | if (Arg.isPackExpansion()) |
| 1933 | Arg = Arg.getPackExpansionPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1934 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1935 | switch (Param.getKind()) { |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1936 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1937 | llvm_unreachable("Null template argument in parameter list"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1938 | |
| 1939 | case TemplateArgument::Type: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1940 | if (Arg.getKind() == TemplateArgument::Type) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1941 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1942 | Param.getAsType(), |
| 1943 | Arg.getAsType(), |
| 1944 | Info, Deduced, 0); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1945 | Info.FirstArg = Param; |
| 1946 | Info.SecondArg = Arg; |
| 1947 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1948 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1949 | case TemplateArgument::Template: |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1950 | if (Arg.getKind() == TemplateArgument::Template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1951 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1952 | Param.getAsTemplate(), |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1953 | Arg.getAsTemplate(), Info, Deduced); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1954 | Info.FirstArg = Param; |
| 1955 | Info.SecondArg = Arg; |
| 1956 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1957 | |
| 1958 | case TemplateArgument::TemplateExpansion: |
| 1959 | llvm_unreachable("caller should handle pack expansions"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1960 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1961 | case TemplateArgument::Declaration: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1962 | if (Arg.getKind() == TemplateArgument::Declaration && |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 1963 | isSameDeclaration(Param.getAsDecl(), Arg.getAsDecl())) |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 1964 | return Sema::TDK_Success; |
| 1965 | |
| 1966 | Info.FirstArg = Param; |
| 1967 | Info.SecondArg = Arg; |
| 1968 | return Sema::TDK_NonDeducedMismatch; |
| 1969 | |
| 1970 | case TemplateArgument::NullPtr: |
| 1971 | if (Arg.getKind() == TemplateArgument::NullPtr && |
| 1972 | S.Context.hasSameType(Param.getNullPtrType(), Arg.getNullPtrType())) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1973 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1974 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1975 | Info.FirstArg = Param; |
| 1976 | Info.SecondArg = Arg; |
| 1977 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1978 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1979 | case TemplateArgument::Integral: |
| 1980 | if (Arg.getKind() == TemplateArgument::Integral) { |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1981 | if (hasSameExtendedValue(Param.getAsIntegral(), Arg.getAsIntegral())) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1982 | return Sema::TDK_Success; |
| 1983 | |
| 1984 | Info.FirstArg = Param; |
| 1985 | Info.SecondArg = Arg; |
| 1986 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1987 | } |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1988 | |
| 1989 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 1990 | Info.FirstArg = Param; |
| 1991 | Info.SecondArg = Arg; |
| 1992 | return Sema::TDK_NonDeducedMismatch; |
| 1993 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1994 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1995 | Info.FirstArg = Param; |
| 1996 | Info.SecondArg = Arg; |
| 1997 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1998 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1999 | case TemplateArgument::Expression: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2000 | if (NonTypeTemplateParmDecl *NTTP |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2001 | = getDeducedParameterFromExpr(Info, Param.getAsExpr())) { |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2002 | if (Arg.getKind() == TemplateArgument::Integral) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 2003 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 2004 | Arg.getAsIntegral(), |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 2005 | Arg.getIntegralType(), |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2006 | /*ArrayBound=*/false, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2007 | Info, Deduced); |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 2008 | if (Arg.getKind() == TemplateArgument::NullPtr) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 2009 | return DeduceNullPtrTemplateArgument(S, TemplateParams, NTTP, |
| 2010 | Arg.getNullPtrType(), |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 2011 | Info, Deduced); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2012 | if (Arg.getKind() == TemplateArgument::Expression) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 2013 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 2014 | Arg.getAsExpr(), Info, Deduced); |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 2015 | if (Arg.getKind() == TemplateArgument::Declaration) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 2016 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 2017 | Arg.getAsDecl(), |
| 2018 | Arg.getParamTypeForDecl(), |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 2019 | Info, Deduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2020 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2021 | Info.FirstArg = Param; |
| 2022 | Info.SecondArg = Arg; |
| 2023 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2024 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2025 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2026 | // Can't deduce anything, but that's okay. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2027 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2028 | } |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 2029 | case TemplateArgument::Pack: |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2030 | llvm_unreachable("Argument packs should be expanded by the caller!"); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2031 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2032 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2033 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2036 | /// \brief Determine whether there is a template argument to be used for |
| 2037 | /// deduction. |
| 2038 | /// |
| 2039 | /// This routine "expands" argument packs in-place, overriding its input |
| 2040 | /// parameters so that \c Args[ArgIdx] will be the available template argument. |
| 2041 | /// |
| 2042 | /// \returns true if there is another template argument (which will be at |
| 2043 | /// \c Args[ArgIdx]), false otherwise. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2044 | static bool hasTemplateArgumentForDeduction(ArrayRef<TemplateArgument> &Args, |
| 2045 | unsigned &ArgIdx) { |
| 2046 | if (ArgIdx == Args.size()) |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2047 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2048 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2049 | const TemplateArgument &Arg = Args[ArgIdx]; |
| 2050 | if (Arg.getKind() != TemplateArgument::Pack) |
| 2051 | return true; |
| 2052 | |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2053 | assert(ArgIdx == Args.size() - 1 && "Pack not at the end of argument list?"); |
| 2054 | Args = Arg.pack_elements(); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2055 | ArgIdx = 0; |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2056 | return ArgIdx < Args.size(); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 2059 | /// \brief Determine whether the given set of template arguments has a pack |
| 2060 | /// expansion that is not the last template argument. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2061 | static bool hasPackExpansionBeforeEnd(ArrayRef<TemplateArgument> Args) { |
| 2062 | bool FoundPackExpansion = false; |
| 2063 | for (const auto &A : Args) { |
| 2064 | if (FoundPackExpansion) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 2065 | return true; |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2066 | |
| 2067 | if (A.getKind() == TemplateArgument::Pack) |
| 2068 | return hasPackExpansionBeforeEnd(A.pack_elements()); |
| 2069 | |
| 2070 | if (A.isPackExpansion()) |
| 2071 | FoundPackExpansion = true; |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 2072 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2073 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 2074 | return false; |
| 2075 | } |
| 2076 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2077 | static Sema::TemplateDeductionResult |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 2078 | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2079 | ArrayRef<TemplateArgument> Params, |
| 2080 | ArrayRef<TemplateArgument> Args, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2081 | TemplateDeductionInfo &Info, |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 2082 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2083 | bool NumberOfArgumentsMustMatch) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2084 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2085 | // If the template argument list of P contains a pack expansion that is not |
| 2086 | // the last template argument, the entire template argument list is a |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2087 | // non-deduced context. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2088 | if (hasPackExpansionBeforeEnd(Params)) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 2089 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2090 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2091 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2092 | // If P has a form that contains <T> or <i>, then each argument Pi of the |
| 2093 | // respective template argument list P is compared with the corresponding |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2094 | // argument Ai of the corresponding template argument list of A. |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2095 | unsigned ArgIdx = 0, ParamIdx = 0; |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2096 | for (; hasTemplateArgumentForDeduction(Params, ParamIdx); ++ParamIdx) { |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2097 | if (!Params[ParamIdx].isPackExpansion()) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2098 | // The simple case: deduce template arguments by matching Pi and Ai. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2099 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2100 | // Check whether we have enough arguments. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2101 | if (!hasTemplateArgumentForDeduction(Args, ArgIdx)) |
Richard Smith | ec7176e | 2017-01-05 02:31:32 +0000 | [diff] [blame] | 2102 | return NumberOfArgumentsMustMatch |
| 2103 | ? Sema::TDK_MiscellaneousDeductionFailure |
| 2104 | : Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2105 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 2106 | // C++1z [temp.deduct.type]p9: |
| 2107 | // During partial ordering, if Ai was originally a pack expansion [and] |
| 2108 | // Pi is not a pack expansion, template argument deduction fails. |
| 2109 | if (Args[ArgIdx].isPackExpansion()) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 2110 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2111 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2112 | // Perform deduction for this Pi/Ai pair. |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2113 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 2114 | = DeduceTemplateArguments(S, TemplateParams, |
| 2115 | Params[ParamIdx], Args[ArgIdx], |
| 2116 | Info, Deduced)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2117 | return Result; |
| 2118 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2119 | // Move to the next argument. |
| 2120 | ++ArgIdx; |
| 2121 | continue; |
| 2122 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2123 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2124 | // The parameter is a pack expansion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2125 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2126 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2127 | // If Pi is a pack expansion, then the pattern of Pi is compared with |
| 2128 | // each remaining argument in the template argument list of A. Each |
| 2129 | // comparison deduces template arguments for subsequent positions in the |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2130 | // template parameter packs expanded by Pi. |
| 2131 | TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2132 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2133 | // FIXME: If there are no remaining arguments, we can bail out early |
| 2134 | // and set any deduced parameter packs to an empty argument pack. |
| 2135 | // The latter part of this is a (minor) correctness issue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2136 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 2137 | // Prepare to deduce the packs within the pattern. |
| 2138 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2139 | |
| 2140 | // Keep track of the deduced template arguments for each parameter pack |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2141 | // expanded by this pack expansion (the outer index) and for each |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2142 | // template argument (the inner SmallVectors). |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2143 | for (; hasTemplateArgumentForDeduction(Args, ArgIdx); ++ArgIdx) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2144 | // Deduce template arguments from the pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2145 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2146 | = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx], |
| 2147 | Info, Deduced)) |
| 2148 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2149 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 2150 | PackScope.nextPackElement(); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2151 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2152 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2153 | // Build argument packs for each of the parameter packs expanded by this |
| 2154 | // pack expansion. |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 2155 | if (auto Result = PackScope.finish()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2156 | return Result; |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2157 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2158 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2159 | return Sema::TDK_Success; |
| 2160 | } |
| 2161 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2162 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2163 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2164 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2165 | const TemplateArgumentList &ParamList, |
| 2166 | const TemplateArgumentList &ArgList, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2167 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2168 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2169 | return DeduceTemplateArguments(S, TemplateParams, ParamList.asArray(), |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 2170 | ArgList.asArray(), Info, Deduced, |
| 2171 | /*NumberOfArgumentsMustMatch*/false); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2174 | /// \brief Determine whether two template arguments are the same. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2175 | static bool isSameTemplateArg(ASTContext &Context, |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2176 | TemplateArgument X, |
| 2177 | const TemplateArgument &Y, |
| 2178 | bool PackExpansionMatchesPack = false) { |
| 2179 | // If we're checking deduced arguments (X) against original arguments (Y), |
| 2180 | // we will have flattened packs to non-expansions in X. |
| 2181 | if (PackExpansionMatchesPack && X.isPackExpansion() && !Y.isPackExpansion()) |
| 2182 | X = X.getPackExpansionPattern(); |
| 2183 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2184 | if (X.getKind() != Y.getKind()) |
| 2185 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2186 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2187 | switch (X.getKind()) { |
| 2188 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2189 | llvm_unreachable("Comparing NULL template argument"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2190 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2191 | case TemplateArgument::Type: |
| 2192 | return Context.getCanonicalType(X.getAsType()) == |
| 2193 | Context.getCanonicalType(Y.getAsType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2194 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2195 | case TemplateArgument::Declaration: |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 2196 | return isSameDeclaration(X.getAsDecl(), Y.getAsDecl()); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2197 | |
| 2198 | case TemplateArgument::NullPtr: |
| 2199 | return Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2200 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2201 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2202 | case TemplateArgument::TemplateExpansion: |
| 2203 | return Context.getCanonicalTemplateName( |
| 2204 | X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() == |
| 2205 | Context.getCanonicalTemplateName( |
| 2206 | Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2207 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2208 | case TemplateArgument::Integral: |
Richard Smith | 993f203 | 2016-12-25 20:21:12 +0000 | [diff] [blame] | 2209 | return hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2210 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2211 | case TemplateArgument::Expression: { |
| 2212 | llvm::FoldingSetNodeID XID, YID; |
| 2213 | X.getAsExpr()->Profile(XID, Context, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2214 | Y.getAsExpr()->Profile(YID, Context, true); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2215 | return XID == YID; |
| 2216 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2217 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2218 | case TemplateArgument::Pack: |
| 2219 | if (X.pack_size() != Y.pack_size()) |
| 2220 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2221 | |
| 2222 | for (TemplateArgument::pack_iterator XP = X.pack_begin(), |
| 2223 | XPEnd = X.pack_end(), |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2224 | YP = Y.pack_begin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2225 | XP != XPEnd; ++XP, ++YP) |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2226 | if (!isSameTemplateArg(Context, *XP, *YP, PackExpansionMatchesPack)) |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2227 | return false; |
| 2228 | |
| 2229 | return true; |
| 2230 | } |
| 2231 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2232 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2233 | } |
| 2234 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2235 | /// \brief Allocate a TemplateArgumentLoc where all locations have |
| 2236 | /// been initialized to the given location. |
| 2237 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2238 | /// \param Arg The template argument we are producing template argument |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2239 | /// location information for. |
| 2240 | /// |
| 2241 | /// \param NTTPType For a declaration template argument, the type of |
| 2242 | /// the non-type template parameter that corresponds to this template |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2243 | /// argument. Can be null if no type sugar is available to add to the |
| 2244 | /// type from the template argument. |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2245 | /// |
| 2246 | /// \param Loc The source location to use for the resulting template |
| 2247 | /// argument. |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2248 | TemplateArgumentLoc |
| 2249 | Sema::getTrivialTemplateArgumentLoc(const TemplateArgument &Arg, |
| 2250 | QualType NTTPType, SourceLocation Loc) { |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2251 | switch (Arg.getKind()) { |
| 2252 | case TemplateArgument::Null: |
| 2253 | llvm_unreachable("Can't get a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2254 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2255 | case TemplateArgument::Type: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2256 | return TemplateArgumentLoc( |
| 2257 | Arg, Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2258 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2259 | case TemplateArgument::Declaration: { |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2260 | if (NTTPType.isNull()) |
| 2261 | NTTPType = Arg.getParamTypeForDecl(); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2262 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
| 2263 | .getAs<Expr>(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2264 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2265 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2266 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2267 | case TemplateArgument::NullPtr: { |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2268 | if (NTTPType.isNull()) |
| 2269 | NTTPType = Arg.getNullPtrType(); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2270 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
| 2271 | .getAs<Expr>(); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2272 | return TemplateArgumentLoc(TemplateArgument(NTTPType, /*isNullPtr*/true), |
| 2273 | E); |
| 2274 | } |
| 2275 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2276 | case TemplateArgument::Integral: { |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2277 | Expr *E = |
| 2278 | BuildExpressionFromIntegralTemplateArgument(Arg, Loc).getAs<Expr>(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2279 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2280 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2281 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2282 | case TemplateArgument::Template: |
| 2283 | case TemplateArgument::TemplateExpansion: { |
| 2284 | NestedNameSpecifierLocBuilder Builder; |
| 2285 | TemplateName Template = Arg.getAsTemplate(); |
| 2286 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2287 | Builder.MakeTrivial(Context, DTN->getQualifier(), Loc); |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 2288 | else if (QualifiedTemplateName *QTN = |
| 2289 | Template.getAsQualifiedTemplateName()) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2290 | Builder.MakeTrivial(Context, QTN->getQualifier(), Loc); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2291 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2292 | if (Arg.getKind() == TemplateArgument::Template) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2293 | return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(Context), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2294 | Loc); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2295 | |
| 2296 | return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(Context), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2297 | Loc, Loc); |
| 2298 | } |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2299 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2300 | case TemplateArgument::Expression: |
| 2301 | return TemplateArgumentLoc(Arg, Arg.getAsExpr()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2302 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2303 | case TemplateArgument::Pack: |
| 2304 | return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo()); |
| 2305 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2306 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2307 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2308 | } |
| 2309 | |
| 2310 | |
| 2311 | /// \brief Convert the given deduced template argument and add it to the set of |
| 2312 | /// fully-converted template arguments. |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2313 | static bool |
| 2314 | ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param, |
| 2315 | DeducedTemplateArgument Arg, |
| 2316 | NamedDecl *Template, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2317 | TemplateDeductionInfo &Info, |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2318 | bool IsDeduced, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2319 | SmallVectorImpl<TemplateArgument> &Output) { |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2320 | auto ConvertArg = [&](DeducedTemplateArgument Arg, |
| 2321 | unsigned ArgumentPackIndex) { |
| 2322 | // Convert the deduced template argument into a template |
| 2323 | // argument that we can check, almost as if the user had written |
| 2324 | // the template argument explicitly. |
| 2325 | TemplateArgumentLoc ArgLoc = |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2326 | S.getTrivialTemplateArgumentLoc(Arg, QualType(), Info.getLocation()); |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2327 | |
| 2328 | // Check the template argument, converting it as necessary. |
| 2329 | return S.CheckTemplateArgument( |
| 2330 | Param, ArgLoc, Template, Template->getLocation(), |
| 2331 | Template->getSourceRange().getEnd(), ArgumentPackIndex, Output, |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2332 | IsDeduced |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2333 | ? (Arg.wasDeducedFromArrayBound() ? Sema::CTAK_DeducedFromArrayBound |
| 2334 | : Sema::CTAK_Deduced) |
| 2335 | : Sema::CTAK_Specified); |
| 2336 | }; |
| 2337 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2338 | if (Arg.getKind() == TemplateArgument::Pack) { |
| 2339 | // This is a template argument pack, so check each of its arguments against |
| 2340 | // the template parameter. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2341 | SmallVector<TemplateArgument, 2> PackedArgsBuilder; |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 2342 | for (const auto &P : Arg.pack_elements()) { |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2343 | // When converting the deduced template argument, append it to the |
| 2344 | // general output list. We need to do this so that the template argument |
| 2345 | // checking logic has all of the prior template arguments available. |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 2346 | DeducedTemplateArgument InnerArg(P); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2347 | InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound()); |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2348 | assert(InnerArg.getKind() != TemplateArgument::Pack && |
| 2349 | "deduced nested pack"); |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 2350 | if (P.isNull()) { |
| 2351 | // We deduced arguments for some elements of this pack, but not for |
| 2352 | // all of them. This happens if we get a conditionally-non-deduced |
| 2353 | // context in a pack expansion (such as an overload set in one of the |
| 2354 | // arguments). |
| 2355 | S.Diag(Param->getLocation(), |
| 2356 | diag::err_template_arg_deduced_incomplete_pack) |
| 2357 | << Arg << Param; |
| 2358 | return true; |
| 2359 | } |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2360 | if (ConvertArg(InnerArg, PackedArgsBuilder.size())) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2361 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2362 | |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2363 | // Move the converted template argument into our argument pack. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 2364 | PackedArgsBuilder.push_back(Output.pop_back_val()); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2365 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2366 | |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2367 | // 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] | 2368 | // itself, in case that substitution fails. |
| 2369 | if (PackedArgsBuilder.empty()) { |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2370 | LocalInstantiationScope Scope(S); |
Richard Smith | e824775 | 2016-12-22 07:24:39 +0000 | [diff] [blame] | 2371 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Output); |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2372 | MultiLevelTemplateArgumentList Args(TemplateArgs); |
| 2373 | |
| 2374 | if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2375 | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
| 2376 | NTTP, Output, |
| 2377 | Template->getSourceRange()); |
Simon Pilgrim | 6f3e1ea | 2016-12-26 18:11:49 +0000 | [diff] [blame] | 2378 | if (Inst.isInvalid() || |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2379 | S.SubstType(NTTP->getType(), Args, NTTP->getLocation(), |
| 2380 | NTTP->getDeclName()).isNull()) |
| 2381 | return true; |
| 2382 | } else if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 2383 | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
| 2384 | TTP, Output, |
| 2385 | Template->getSourceRange()); |
| 2386 | if (Inst.isInvalid() || !S.SubstDecl(TTP, S.CurContext, Args)) |
| 2387 | return true; |
| 2388 | } |
| 2389 | // For type parameters, no substitution is ever required. |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2390 | } |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2391 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2392 | // Create the resulting argument pack. |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 2393 | Output.push_back( |
| 2394 | TemplateArgument::CreatePackCopy(S.Context, PackedArgsBuilder)); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2395 | return false; |
| 2396 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2397 | |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2398 | return ConvertArg(Arg, 0); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2399 | } |
| 2400 | |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2401 | // FIXME: This should not be a template, but |
| 2402 | // ClassTemplatePartialSpecializationDecl sadly does not derive from |
| 2403 | // TemplateDecl. |
| 2404 | template<typename TemplateDeclT> |
| 2405 | static Sema::TemplateDeductionResult ConvertDeducedTemplateArguments( |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2406 | Sema &S, TemplateDeclT *Template, bool IsDeduced, |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2407 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2408 | TemplateDeductionInfo &Info, SmallVectorImpl<TemplateArgument> &Builder, |
| 2409 | LocalInstantiationScope *CurrentInstantiationScope = nullptr, |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 2410 | unsigned NumAlreadyConverted = 0, bool PartialOverloading = false) { |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2411 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| 2412 | |
| 2413 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 2414 | NamedDecl *Param = TemplateParams->getParam(I); |
| 2415 | |
| 2416 | if (!Deduced[I].isNull()) { |
| 2417 | if (I < NumAlreadyConverted) { |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2418 | // We may have had explicitly-specified template arguments for a |
| 2419 | // template parameter pack (that may or may not have been extended |
| 2420 | // via additional deduced arguments). |
Richard Smith | 9c0c986 | 2017-01-05 20:27:28 +0000 | [diff] [blame] | 2421 | if (Param->isParameterPack() && CurrentInstantiationScope && |
| 2422 | CurrentInstantiationScope->getPartiallySubstitutedPack() == Param) { |
| 2423 | // Forget the partially-substituted pack; its substitution is now |
| 2424 | // complete. |
| 2425 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2426 | // We still need to check the argument in case it was extended by |
| 2427 | // deduction. |
| 2428 | } else { |
| 2429 | // We have already fully type-checked and converted this |
| 2430 | // argument, because it was explicitly-specified. Just record the |
| 2431 | // presence of this argument. |
| 2432 | Builder.push_back(Deduced[I]); |
| 2433 | continue; |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2434 | } |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2435 | } |
| 2436 | |
Richard Smith | 9c0c986 | 2017-01-05 20:27:28 +0000 | [diff] [blame] | 2437 | // We may have deduced this argument, so it still needs to be |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2438 | // checked and converted. |
| 2439 | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Template, Info, |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2440 | IsDeduced, Builder)) { |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2441 | Info.Param = makeTemplateParameter(Param); |
| 2442 | // FIXME: These template arguments are temporary. Free them! |
| 2443 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2444 | return Sema::TDK_SubstitutionFailure; |
| 2445 | } |
| 2446 | |
| 2447 | continue; |
| 2448 | } |
| 2449 | |
| 2450 | // C++0x [temp.arg.explicit]p3: |
| 2451 | // A trailing template parameter pack (14.5.3) not otherwise deduced will |
| 2452 | // be deduced to an empty sequence of template arguments. |
| 2453 | // FIXME: Where did the word "trailing" come from? |
| 2454 | if (Param->isTemplateParameterPack()) { |
| 2455 | // We may have had explicitly-specified template arguments for this |
| 2456 | // template parameter pack. If so, our empty deduction extends the |
| 2457 | // explicitly-specified set (C++0x [temp.arg.explicit]p9). |
| 2458 | const TemplateArgument *ExplicitArgs; |
| 2459 | unsigned NumExplicitArgs; |
| 2460 | if (CurrentInstantiationScope && |
| 2461 | CurrentInstantiationScope->getPartiallySubstitutedPack( |
| 2462 | &ExplicitArgs, &NumExplicitArgs) == Param) { |
| 2463 | Builder.push_back(TemplateArgument( |
| 2464 | llvm::makeArrayRef(ExplicitArgs, NumExplicitArgs))); |
| 2465 | |
| 2466 | // Forget the partially-substituted pack; its substitution is now |
| 2467 | // complete. |
| 2468 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2469 | } else { |
| 2470 | // Go through the motions of checking the empty argument pack against |
| 2471 | // the parameter pack. |
| 2472 | DeducedTemplateArgument DeducedPack(TemplateArgument::getEmptyPack()); |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2473 | if (ConvertDeducedTemplateArgument(S, Param, DeducedPack, Template, |
| 2474 | Info, IsDeduced, Builder)) { |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2475 | Info.Param = makeTemplateParameter(Param); |
| 2476 | // FIXME: These template arguments are temporary. Free them! |
| 2477 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2478 | return Sema::TDK_SubstitutionFailure; |
| 2479 | } |
| 2480 | } |
| 2481 | continue; |
| 2482 | } |
| 2483 | |
| 2484 | // Substitute into the default template argument, if available. |
| 2485 | bool HasDefaultArg = false; |
| 2486 | TemplateDecl *TD = dyn_cast<TemplateDecl>(Template); |
| 2487 | if (!TD) { |
Richard Smith | f8ba3fd | 2017-06-02 22:53:06 +0000 | [diff] [blame] | 2488 | assert(isa<ClassTemplatePartialSpecializationDecl>(Template) || |
| 2489 | isa<VarTemplatePartialSpecializationDecl>(Template)); |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2490 | return Sema::TDK_Incomplete; |
| 2491 | } |
| 2492 | |
| 2493 | TemplateArgumentLoc DefArg = S.SubstDefaultTemplateArgumentIfAvailable( |
| 2494 | TD, TD->getLocation(), TD->getSourceRange().getEnd(), Param, Builder, |
| 2495 | HasDefaultArg); |
| 2496 | |
| 2497 | // If there was no default argument, deduction is incomplete. |
| 2498 | if (DefArg.getArgument().isNull()) { |
| 2499 | Info.Param = makeTemplateParameter( |
| 2500 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
| 2501 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 2502 | if (PartialOverloading) break; |
| 2503 | |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2504 | return HasDefaultArg ? Sema::TDK_SubstitutionFailure |
| 2505 | : Sema::TDK_Incomplete; |
| 2506 | } |
| 2507 | |
| 2508 | // Check whether we can actually use the default argument. |
| 2509 | if (S.CheckTemplateArgument(Param, DefArg, TD, TD->getLocation(), |
| 2510 | TD->getSourceRange().getEnd(), 0, Builder, |
| 2511 | Sema::CTAK_Specified)) { |
| 2512 | Info.Param = makeTemplateParameter( |
| 2513 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
| 2514 | // FIXME: These template arguments are temporary. Free them! |
| 2515 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2516 | return Sema::TDK_SubstitutionFailure; |
| 2517 | } |
| 2518 | |
| 2519 | // If we get here, we successfully used the default template argument. |
| 2520 | } |
| 2521 | |
| 2522 | return Sema::TDK_Success; |
| 2523 | } |
| 2524 | |
Benjamin Kramer | 357c9e1 | 2017-02-11 12:21:17 +0000 | [diff] [blame] | 2525 | static DeclContext *getAsDeclContextOrEnclosing(Decl *D) { |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2526 | if (auto *DC = dyn_cast<DeclContext>(D)) |
| 2527 | return DC; |
| 2528 | return D->getDeclContext(); |
| 2529 | } |
| 2530 | |
| 2531 | template<typename T> struct IsPartialSpecialization { |
| 2532 | static constexpr bool value = false; |
| 2533 | }; |
| 2534 | template<> |
| 2535 | struct IsPartialSpecialization<ClassTemplatePartialSpecializationDecl> { |
| 2536 | static constexpr bool value = true; |
| 2537 | }; |
| 2538 | template<> |
| 2539 | struct IsPartialSpecialization<VarTemplatePartialSpecializationDecl> { |
| 2540 | static constexpr bool value = true; |
| 2541 | }; |
| 2542 | |
| 2543 | /// Complete template argument deduction for a partial specialization. |
| 2544 | template <typename T> |
| 2545 | static typename std::enable_if<IsPartialSpecialization<T>::value, |
| 2546 | Sema::TemplateDeductionResult>::type |
| 2547 | FinishTemplateArgumentDeduction( |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2548 | Sema &S, T *Partial, bool IsPartialOrdering, |
| 2549 | const TemplateArgumentList &TemplateArgs, |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2550 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2551 | TemplateDeductionInfo &Info) { |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2552 | // Unevaluated SFINAE context. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2553 | EnterExpressionEvaluationContext Unevaluated( |
| 2554 | S, Sema::ExpressionEvaluationContext::Unevaluated); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2555 | Sema::SFINAETrap Trap(S); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2556 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2557 | Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Partial)); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2558 | |
| 2559 | // C++ [temp.deduct.type]p2: |
| 2560 | // [...] or if any template argument remains neither deduced nor |
| 2561 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2562 | SmallVector<TemplateArgument, 4> Builder; |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2563 | if (auto Result = ConvertDeducedTemplateArguments( |
| 2564 | S, Partial, IsPartialOrdering, Deduced, Info, Builder)) |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2565 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2566 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2567 | // Form the template argument list from the deduced template arguments. |
| 2568 | TemplateArgumentList *DeducedArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2569 | = TemplateArgumentList::CreateCopy(S.Context, Builder); |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2570 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2571 | Info.reset(DeducedArgumentList); |
| 2572 | |
| 2573 | // Substitute the deduced template arguments into the template |
| 2574 | // arguments of the class template partial specialization, and |
| 2575 | // verify that the instantiated template arguments are both valid |
| 2576 | // and are equivalent to the template arguments originally provided |
| 2577 | // to the class template. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2578 | LocalInstantiationScope InstScope(S); |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2579 | auto *Template = Partial->getSpecializedTemplate(); |
| 2580 | const ASTTemplateArgumentListInfo *PartialTemplArgInfo = |
| 2581 | Partial->getTemplateArgsAsWritten(); |
| 2582 | const TemplateArgumentLoc *PartialTemplateArgs = |
| 2583 | PartialTemplArgInfo->getTemplateArgs(); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2584 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2585 | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, |
| 2586 | PartialTemplArgInfo->RAngleLoc); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2587 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2588 | if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs, |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2589 | InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { |
| 2590 | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; |
| 2591 | if (ParamIdx >= Partial->getTemplateParameters()->size()) |
| 2592 | ParamIdx = Partial->getTemplateParameters()->size() - 1; |
| 2593 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2594 | Decl *Param = const_cast<NamedDecl *>( |
| 2595 | Partial->getTemplateParameters()->getParam(ParamIdx)); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2596 | Info.Param = makeTemplateParameter(Param); |
| 2597 | Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); |
| 2598 | return Sema::TDK_SubstitutionFailure; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2599 | } |
| 2600 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2601 | SmallVector<TemplateArgument, 4> ConvertedInstArgs; |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2602 | if (S.CheckTemplateArgumentList(Template, Partial->getLocation(), InstArgs, |
| 2603 | false, ConvertedInstArgs)) |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2604 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2605 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2606 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2607 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2608 | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2609 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 2610 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2611 | Info.FirstArg = TemplateArgs[I]; |
| 2612 | Info.SecondArg = InstArg; |
| 2613 | return Sema::TDK_NonDeducedMismatch; |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | if (Trap.hasErrorOccurred()) |
| 2618 | return Sema::TDK_SubstitutionFailure; |
| 2619 | |
| 2620 | return Sema::TDK_Success; |
| 2621 | } |
| 2622 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2623 | /// Complete template argument deduction for a class or variable template, |
| 2624 | /// when partial ordering against a partial specialization. |
| 2625 | // FIXME: Factor out duplication with partial specialization version above. |
Benjamin Kramer | 357c9e1 | 2017-02-11 12:21:17 +0000 | [diff] [blame] | 2626 | static Sema::TemplateDeductionResult FinishTemplateArgumentDeduction( |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2627 | Sema &S, TemplateDecl *Template, bool PartialOrdering, |
| 2628 | const TemplateArgumentList &TemplateArgs, |
| 2629 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2630 | TemplateDeductionInfo &Info) { |
| 2631 | // Unevaluated SFINAE context. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2632 | EnterExpressionEvaluationContext Unevaluated( |
| 2633 | S, Sema::ExpressionEvaluationContext::Unevaluated); |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2634 | Sema::SFINAETrap Trap(S); |
| 2635 | |
| 2636 | Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Template)); |
| 2637 | |
| 2638 | // C++ [temp.deduct.type]p2: |
| 2639 | // [...] or if any template argument remains neither deduced nor |
| 2640 | // explicitly specified, template argument deduction fails. |
| 2641 | SmallVector<TemplateArgument, 4> Builder; |
| 2642 | if (auto Result = ConvertDeducedTemplateArguments( |
| 2643 | S, Template, /*IsDeduced*/PartialOrdering, Deduced, Info, Builder)) |
| 2644 | return Result; |
| 2645 | |
| 2646 | // Check that we produced the correct argument list. |
| 2647 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| 2648 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
| 2649 | TemplateArgument InstArg = Builder[I]; |
| 2650 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg, |
| 2651 | /*PackExpansionMatchesPack*/true)) { |
| 2652 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
| 2653 | Info.FirstArg = TemplateArgs[I]; |
| 2654 | Info.SecondArg = InstArg; |
| 2655 | return Sema::TDK_NonDeducedMismatch; |
| 2656 | } |
| 2657 | } |
| 2658 | |
| 2659 | if (Trap.hasErrorOccurred()) |
| 2660 | return Sema::TDK_SubstitutionFailure; |
| 2661 | |
| 2662 | return Sema::TDK_Success; |
| 2663 | } |
| 2664 | |
| 2665 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2666 | /// \brief Perform template argument deduction to determine whether |
Larisse Voufo | 833b05a | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 2667 | /// the given template arguments match the given class template |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2668 | /// partial specialization per C++ [temp.class.spec.match]. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2669 | Sema::TemplateDeductionResult |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2670 | Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2671 | const TemplateArgumentList &TemplateArgs, |
| 2672 | TemplateDeductionInfo &Info) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 2673 | if (Partial->isInvalidDecl()) |
| 2674 | return TDK_Invalid; |
| 2675 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2676 | // C++ [temp.class.spec.match]p2: |
| 2677 | // A partial specialization matches a given actual template |
| 2678 | // argument list if the template arguments of the partial |
| 2679 | // specialization can be deduced from the actual template argument |
| 2680 | // list (14.8.2). |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2681 | |
| 2682 | // Unevaluated SFINAE context. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2683 | EnterExpressionEvaluationContext Unevaluated( |
| 2684 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2685 | SFINAETrap Trap(*this); |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2686 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2687 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2688 | Deduced.resize(Partial->getTemplateParameters()->size()); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2689 | if (TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2690 | = ::DeduceTemplateArguments(*this, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2691 | Partial->getTemplateParameters(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2692 | Partial->getTemplateArgs(), |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2693 | TemplateArgs, Info, Deduced)) |
| 2694 | return Result; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 2695 | |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2696 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2697 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2698 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2699 | if (Inst.isInvalid()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2700 | return TDK_InstantiationDepth; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2701 | |
Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2702 | if (Trap.hasErrorOccurred()) |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2703 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2704 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2705 | return ::FinishTemplateArgumentDeduction( |
| 2706 | *this, Partial, /*PartialOrdering=*/false, TemplateArgs, Deduced, Info); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2707 | } |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2708 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2709 | /// \brief Perform template argument deduction to determine whether |
| 2710 | /// the given template arguments match the given variable template |
| 2711 | /// partial specialization per C++ [temp.class.spec.match]. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2712 | Sema::TemplateDeductionResult |
| 2713 | Sema::DeduceTemplateArguments(VarTemplatePartialSpecializationDecl *Partial, |
| 2714 | const TemplateArgumentList &TemplateArgs, |
| 2715 | TemplateDeductionInfo &Info) { |
| 2716 | if (Partial->isInvalidDecl()) |
| 2717 | return TDK_Invalid; |
| 2718 | |
| 2719 | // C++ [temp.class.spec.match]p2: |
| 2720 | // A partial specialization matches a given actual template |
| 2721 | // argument list if the template arguments of the partial |
| 2722 | // specialization can be deduced from the actual template argument |
| 2723 | // list (14.8.2). |
| 2724 | |
| 2725 | // Unevaluated SFINAE context. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2726 | EnterExpressionEvaluationContext Unevaluated( |
| 2727 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2728 | SFINAETrap Trap(*this); |
| 2729 | |
| 2730 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 2731 | Deduced.resize(Partial->getTemplateParameters()->size()); |
| 2732 | if (TemplateDeductionResult Result = ::DeduceTemplateArguments( |
| 2733 | *this, Partial->getTemplateParameters(), Partial->getTemplateArgs(), |
| 2734 | TemplateArgs, Info, Deduced)) |
| 2735 | return Result; |
| 2736 | |
| 2737 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2738 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2739 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2740 | if (Inst.isInvalid()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2741 | return TDK_InstantiationDepth; |
| 2742 | |
| 2743 | if (Trap.hasErrorOccurred()) |
| 2744 | return Sema::TDK_SubstitutionFailure; |
| 2745 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2746 | return ::FinishTemplateArgumentDeduction( |
| 2747 | *this, Partial, /*PartialOrdering=*/false, TemplateArgs, Deduced, Info); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2748 | } |
| 2749 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2750 | /// \brief Determine whether the given type T is a simple-template-id type. |
| 2751 | static bool isSimpleTemplateIdType(QualType T) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2752 | if (const TemplateSpecializationType *Spec |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2753 | = T->getAs<TemplateSpecializationType>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2754 | return Spec->getTemplateName().getAsTemplateDecl() != nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2755 | |
Richard Smith | 1363e8f | 2017-09-07 07:22:36 +0000 | [diff] [blame] | 2756 | // C++17 [temp.local]p2: |
| 2757 | // the injected-class-name [...] is equivalent to the template-name followed |
| 2758 | // by the template-arguments of the class template specialization or partial |
| 2759 | // specialization enclosed in <> |
| 2760 | // ... which means it's equivalent to a simple-template-id. |
| 2761 | // |
| 2762 | // This only arises during class template argument deduction for a copy |
| 2763 | // deduction candidate, where it permits slicing. |
| 2764 | if (T->getAs<InjectedClassNameType>()) |
| 2765 | return true; |
| 2766 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2767 | return false; |
| 2768 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2769 | |
| 2770 | /// \brief Substitute the explicitly-provided template arguments into the |
| 2771 | /// given function template according to C++ [temp.arg.explicit]. |
| 2772 | /// |
| 2773 | /// \param FunctionTemplate the function template into which the explicit |
| 2774 | /// template arguments will be substituted. |
| 2775 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2776 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2777 | /// arguments. |
| 2778 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2779 | /// \param Deduced the deduced template arguments, which will be populated |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2780 | /// with the converted and checked explicit template arguments. |
| 2781 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2782 | /// \param ParamTypes will be populated with the instantiated function |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2783 | /// parameters. |
| 2784 | /// |
| 2785 | /// \param FunctionType if non-NULL, the result type of the function template |
| 2786 | /// will also be instantiated and the pointed-to value will be updated with |
| 2787 | /// the instantiated function type. |
| 2788 | /// |
| 2789 | /// \param Info if substitution fails for any reason, this object will be |
| 2790 | /// populated with more information about the failure. |
| 2791 | /// |
| 2792 | /// \returns TDK_Success if substitution was successful, or some failure |
| 2793 | /// condition. |
| 2794 | Sema::TemplateDeductionResult |
| 2795 | Sema::SubstituteExplicitTemplateArguments( |
| 2796 | FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2797 | TemplateArgumentListInfo &ExplicitTemplateArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2798 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2799 | SmallVectorImpl<QualType> &ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2800 | QualType *FunctionType, |
| 2801 | TemplateDeductionInfo &Info) { |
| 2802 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 2803 | TemplateParameterList *TemplateParams |
| 2804 | = FunctionTemplate->getTemplateParameters(); |
| 2805 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2806 | if (ExplicitTemplateArgs.size() == 0) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2807 | // No arguments to substitute; just copy over the parameter types and |
| 2808 | // fill in the function type. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2809 | for (auto P : Function->parameters()) |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 2810 | ParamTypes.push_back(P->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2811 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2812 | if (FunctionType) |
| 2813 | *FunctionType = Function->getType(); |
| 2814 | return TDK_Success; |
| 2815 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2816 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2817 | // Unevaluated SFINAE context. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2818 | EnterExpressionEvaluationContext Unevaluated( |
| 2819 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2820 | SFINAETrap Trap(*this); |
| 2821 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2822 | // C++ [temp.arg.explicit]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2823 | // Template arguments that are present shall be specified in the |
| 2824 | // declaration order of their corresponding template-parameters. The |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2825 | // template argument list shall not specify more template-arguments than |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2826 | // there are corresponding template-parameters. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2827 | SmallVector<TemplateArgument, 4> Builder; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2828 | |
| 2829 | // Enter a new template instantiation context where we check the |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2830 | // explicitly-specified template arguments against this function template, |
| 2831 | // and then substitute them into the function parameter types. |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 2832 | SmallVector<TemplateArgument, 4> DeducedArgs; |
Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 2833 | InstantiatingTemplate Inst( |
| 2834 | *this, Info.getLocation(), FunctionTemplate, DeducedArgs, |
| 2835 | CodeSynthesisContext::ExplicitTemplateArgumentSubstitution, Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2836 | if (Inst.isInvalid()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2837 | return TDK_InstantiationDepth; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2838 | |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 2839 | if (CheckTemplateArgumentList(FunctionTemplate, SourceLocation(), |
| 2840 | ExplicitTemplateArgs, true, Builder, false) || |
| 2841 | Trap.hasErrorOccurred()) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2842 | unsigned Index = Builder.size(); |
Douglas Gregor | 62c281a | 2010-05-09 01:26:06 +0000 | [diff] [blame] | 2843 | if (Index >= TemplateParams->size()) |
| 2844 | Index = TemplateParams->size() - 1; |
| 2845 | Info.Param = makeTemplateParameter(TemplateParams->getParam(Index)); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2846 | return TDK_InvalidExplicitArguments; |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 2847 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2848 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2849 | // Form the template argument list from the explicitly-specified |
| 2850 | // template arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2851 | TemplateArgumentList *ExplicitArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2852 | = TemplateArgumentList::CreateCopy(Context, Builder); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2853 | Info.reset(ExplicitArgumentList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2854 | |
John McCall | 036855a | 2010-10-12 19:40:14 +0000 | [diff] [blame] | 2855 | // Template argument deduction and the final substitution should be |
| 2856 | // done in the context of the templated declaration. Explicit |
| 2857 | // argument substitution, on the other hand, needs to happen in the |
| 2858 | // calling context. |
| 2859 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
| 2860 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2861 | // If we deduced template arguments for a template parameter pack, |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2862 | // note that the template argument pack is partially substituted and record |
| 2863 | // the explicit template arguments. They'll be used as part of deduction |
| 2864 | // for this template parameter pack. |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2865 | for (unsigned I = 0, N = Builder.size(); I != N; ++I) { |
| 2866 | const TemplateArgument &Arg = Builder[I]; |
| 2867 | if (Arg.getKind() == TemplateArgument::Pack) { |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2868 | CurrentInstantiationScope->SetPartiallySubstitutedPack( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2869 | TemplateParams->getParam(I), |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2870 | Arg.pack_begin(), |
| 2871 | Arg.pack_size()); |
| 2872 | break; |
| 2873 | } |
| 2874 | } |
| 2875 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2876 | const FunctionProtoType *Proto |
| 2877 | = Function->getType()->getAs<FunctionProtoType>(); |
| 2878 | assert(Proto && "Function template does not have a prototype?"); |
| 2879 | |
Richard Smith | 70b1304 | 2015-01-09 01:19:56 +0000 | [diff] [blame] | 2880 | // Isolate our substituted parameters from our caller. |
| 2881 | LocalInstantiationScope InstScope(*this, /*MergeWithOuterScope*/true); |
| 2882 | |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2883 | ExtParameterInfoBuilder ExtParamInfos; |
| 2884 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2885 | // Instantiate the types of each of the function parameters given the |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2886 | // explicitly-specified template arguments. If the function has a trailing |
| 2887 | // return type, substitute it after the arguments to ensure we substitute |
| 2888 | // in lexical order. |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2889 | if (Proto->hasTrailingReturn()) { |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2890 | if (SubstParmTypes(Function->getLocation(), Function->parameters(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2891 | Proto->getExtParameterInfosOrNull(), |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2892 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2893 | ParamTypes, /*params*/ nullptr, ExtParamInfos)) |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2894 | return TDK_SubstitutionFailure; |
| 2895 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2896 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2897 | // Instantiate the return type. |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2898 | QualType ResultType; |
| 2899 | { |
| 2900 | // C++11 [expr.prim.general]p3: |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2901 | // If a declaration declares a member function or member function |
| 2902 | // 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] | 2903 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2904 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2905 | // declarator. |
| 2906 | unsigned ThisTypeQuals = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2907 | CXXRecordDecl *ThisContext = nullptr; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2908 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
| 2909 | ThisContext = Method->getParent(); |
| 2910 | ThisTypeQuals = Method->getTypeQualifiers(); |
| 2911 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2912 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2913 | CXXThisScopeRAII ThisScope(*this, ThisContext, ThisTypeQuals, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2914 | getLangOpts().CPlusPlus11); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2915 | |
| 2916 | ResultType = |
| 2917 | SubstType(Proto->getReturnType(), |
| 2918 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 2919 | Function->getTypeSpecStartLoc(), Function->getDeclName()); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2920 | if (ResultType.isNull() || Trap.hasErrorOccurred()) |
| 2921 | return TDK_SubstitutionFailure; |
| 2922 | } |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2923 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2924 | // Instantiate the types of each of the function parameters given the |
| 2925 | // explicitly-specified template arguments if we didn't do so earlier. |
| 2926 | if (!Proto->hasTrailingReturn() && |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2927 | SubstParmTypes(Function->getLocation(), Function->parameters(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2928 | Proto->getExtParameterInfosOrNull(), |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2929 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2930 | ParamTypes, /*params*/ nullptr, ExtParamInfos)) |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2931 | return TDK_SubstitutionFailure; |
| 2932 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2933 | if (FunctionType) { |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2934 | auto EPI = Proto->getExtProtoInfo(); |
| 2935 | EPI.ExtParameterInfos = ExtParamInfos.getPointerOrNull(ParamTypes.size()); |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 2936 | |
| 2937 | // In C++1z onwards, exception specifications are part of the function type, |
| 2938 | // so substitution into the type must also substitute into the exception |
| 2939 | // specification. |
| 2940 | SmallVector<QualType, 4> ExceptionStorage; |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 2941 | if (getLangOpts().CPlusPlus17 && |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 2942 | SubstExceptionSpec( |
| 2943 | Function->getLocation(), EPI.ExceptionSpec, ExceptionStorage, |
| 2944 | MultiLevelTemplateArgumentList(*ExplicitArgumentList))) |
| 2945 | return TDK_SubstitutionFailure; |
| 2946 | |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2947 | *FunctionType = BuildFunctionType(ResultType, ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2948 | Function->getLocation(), |
Eli Friedman | d8725a9 | 2010-08-05 02:54:05 +0000 | [diff] [blame] | 2949 | Function->getDeclName(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2950 | EPI); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2951 | if (FunctionType->isNull() || Trap.hasErrorOccurred()) |
| 2952 | return TDK_SubstitutionFailure; |
| 2953 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2954 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2955 | // C++ [temp.arg.explicit]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2956 | // Trailing template arguments that can be deduced (14.8.2) may be |
| 2957 | // omitted from the list of explicit template-arguments. If all of the |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2958 | // template arguments can be deduced, they may all be omitted; in this |
| 2959 | // case, the empty template argument list <> itself may also be omitted. |
| 2960 | // |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2961 | // Take all of the explicitly-specified arguments and put them into |
| 2962 | // the set of deduced template arguments. Explicitly-specified |
| 2963 | // parameter packs, however, will be set to NULL since the deduction |
| 2964 | // mechanisms handle explicitly-specified argument packs directly. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2965 | Deduced.reserve(TemplateParams->size()); |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2966 | for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) { |
| 2967 | const TemplateArgument &Arg = ExplicitArgumentList->get(I); |
| 2968 | if (Arg.getKind() == TemplateArgument::Pack) |
| 2969 | Deduced.push_back(DeducedTemplateArgument()); |
| 2970 | else |
| 2971 | Deduced.push_back(Arg); |
| 2972 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2973 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2974 | return TDK_Success; |
| 2975 | } |
| 2976 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2977 | /// \brief Check whether the deduced argument type for a call to a function |
| 2978 | /// template matches the actual argument type per C++ [temp.deduct.call]p4. |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 2979 | static Sema::TemplateDeductionResult |
| 2980 | CheckOriginalCallArgDeduction(Sema &S, TemplateDeductionInfo &Info, |
| 2981 | Sema::OriginalCallArg OriginalArg, |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2982 | QualType DeducedA) { |
| 2983 | ASTContext &Context = S.Context; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2984 | |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 2985 | auto Failed = [&]() -> Sema::TemplateDeductionResult { |
| 2986 | Info.FirstArg = TemplateArgument(DeducedA); |
| 2987 | Info.SecondArg = TemplateArgument(OriginalArg.OriginalArgType); |
| 2988 | Info.CallArgIndex = OriginalArg.ArgIdx; |
| 2989 | return OriginalArg.DecomposedParam ? Sema::TDK_DeducedMismatchNested |
| 2990 | : Sema::TDK_DeducedMismatch; |
| 2991 | }; |
| 2992 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2993 | QualType A = OriginalArg.OriginalArgType; |
| 2994 | QualType OriginalParamType = OriginalArg.OriginalParamType; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2995 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2996 | // Check for type equality (top-level cv-qualifiers are ignored). |
| 2997 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 2998 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2999 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3000 | // Strip off references on the argument types; they aren't needed for |
| 3001 | // the following checks. |
| 3002 | if (const ReferenceType *DeducedARef = DeducedA->getAs<ReferenceType>()) |
| 3003 | DeducedA = DeducedARef->getPointeeType(); |
| 3004 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
| 3005 | A = ARef->getPointeeType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3006 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3007 | // C++ [temp.deduct.call]p4: |
| 3008 | // [...] However, there are three cases that allow a difference: |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3009 | // - If the original P is a reference type, the deduced A (i.e., the |
| 3010 | // type referred to by the reference) can be more cv-qualified than |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3011 | // the transformed A. |
| 3012 | if (const ReferenceType *OriginalParamRef |
| 3013 | = OriginalParamType->getAs<ReferenceType>()) { |
| 3014 | // We don't want to keep the reference around any more. |
| 3015 | OriginalParamType = OriginalParamRef->getPointeeType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3016 | |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 3017 | // FIXME: Resolve core issue (no number yet): if the original P is a |
| 3018 | // reference type and the transformed A is function type "noexcept F", |
| 3019 | // the deduced A can be F. |
| 3020 | QualType Tmp; |
| 3021 | if (A->isFunctionType() && S.IsFunctionConversion(A, DeducedA, Tmp)) |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3022 | return Sema::TDK_Success; |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 3023 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3024 | Qualifiers AQuals = A.getQualifiers(); |
| 3025 | Qualifiers DeducedAQuals = DeducedA.getQualifiers(); |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 3026 | |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 3027 | // Under Objective-C++ ARC, the deduced type may have implicitly |
| 3028 | // been given strong or (when dealing with a const reference) |
| 3029 | // unsafe_unretained lifetime. If so, update the original |
| 3030 | // qualifiers to include this lifetime. |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 3031 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 3032 | ((DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_Strong && |
| 3033 | AQuals.getObjCLifetime() == Qualifiers::OCL_None) || |
| 3034 | (DeducedAQuals.hasConst() && |
| 3035 | DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone))) { |
| 3036 | AQuals.setObjCLifetime(DeducedAQuals.getObjCLifetime()); |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 3037 | } |
| 3038 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3039 | if (AQuals == DeducedAQuals) { |
| 3040 | // Qualifiers match; there's nothing to do. |
| 3041 | } else if (!DeducedAQuals.compatiblyIncludes(AQuals)) { |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3042 | return Failed(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3043 | } else { |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3044 | // Qualifiers are compatible, so have the argument type adopt the |
| 3045 | // deduced argument type's qualifiers as if we had performed the |
| 3046 | // qualification conversion. |
| 3047 | A = Context.getQualifiedType(A.getUnqualifiedType(), DeducedAQuals); |
| 3048 | } |
| 3049 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3050 | |
| 3051 | // - The transformed A can be another pointer or pointer to member |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3052 | // type that can be converted to the deduced A via a function pointer |
| 3053 | // conversion and/or a qualification conversion. |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 3054 | // |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 3055 | // Also allow conversions which merely strip __attribute__((noreturn)) from |
| 3056 | // function types (recursively). |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3057 | bool ObjCLifetimeConversion = false; |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 3058 | QualType ResultTy; |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3059 | if ((A->isAnyPointerType() || A->isMemberPointerType()) && |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 3060 | (S.IsQualificationConversion(A, DeducedA, false, |
| 3061 | ObjCLifetimeConversion) || |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3062 | S.IsFunctionConversion(A, DeducedA, ResultTy))) |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3063 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3064 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3065 | // - 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] | 3066 | // transformed A can be a derived class of the deduced A. [...] |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3067 | // [...] Likewise, if P is a pointer to a class of the form |
| 3068 | // simple-template-id, the transformed A can be a pointer to a |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3069 | // derived class pointed to by the deduced A. |
| 3070 | if (const PointerType *OriginalParamPtr |
| 3071 | = OriginalParamType->getAs<PointerType>()) { |
| 3072 | if (const PointerType *DeducedAPtr = DeducedA->getAs<PointerType>()) { |
| 3073 | if (const PointerType *APtr = A->getAs<PointerType>()) { |
| 3074 | if (A->getPointeeType()->isRecordType()) { |
| 3075 | OriginalParamType = OriginalParamPtr->getPointeeType(); |
| 3076 | DeducedA = DeducedAPtr->getPointeeType(); |
| 3077 | A = APtr->getPointeeType(); |
| 3078 | } |
| 3079 | } |
| 3080 | } |
| 3081 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3082 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3083 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3084 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3085 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3086 | if (A->isRecordType() && isSimpleTemplateIdType(OriginalParamType) && |
Richard Smith | 148bc6a | 2018-02-20 23:47:12 +0000 | [diff] [blame^] | 3087 | S.IsDerivedFrom(Info.getLocation(), A, DeducedA)) |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3088 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3089 | |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3090 | return Failed(); |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3091 | } |
| 3092 | |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3093 | /// Find the pack index for a particular parameter index in an instantiation of |
| 3094 | /// a function template with specific arguments. |
| 3095 | /// |
| 3096 | /// \return The pack index for whichever pack produced this parameter, or -1 |
| 3097 | /// if this was not produced by a parameter. Intended to be used as the |
| 3098 | /// ArgumentPackSubstitutionIndex for further substitutions. |
| 3099 | // FIXME: We should track this in OriginalCallArgs so we don't need to |
| 3100 | // reconstruct it here. |
| 3101 | static unsigned getPackIndexForParam(Sema &S, |
| 3102 | FunctionTemplateDecl *FunctionTemplate, |
| 3103 | const MultiLevelTemplateArgumentList &Args, |
| 3104 | unsigned ParamIdx) { |
| 3105 | unsigned Idx = 0; |
| 3106 | for (auto *PD : FunctionTemplate->getTemplatedDecl()->parameters()) { |
| 3107 | if (PD->isParameterPack()) { |
| 3108 | unsigned NumExpansions = |
| 3109 | S.getNumArgumentsInExpansion(PD->getType(), Args).getValueOr(1); |
| 3110 | if (Idx + NumExpansions > ParamIdx) |
| 3111 | return ParamIdx - Idx; |
| 3112 | Idx += NumExpansions; |
| 3113 | } else { |
| 3114 | if (Idx == ParamIdx) |
| 3115 | return -1; // Not a pack expansion |
| 3116 | ++Idx; |
| 3117 | } |
| 3118 | } |
| 3119 | |
| 3120 | llvm_unreachable("parameter index would not be produced from template"); |
| 3121 | } |
| 3122 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3123 | /// \brief Finish template argument deduction for a function template, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3124 | /// checking the deduced template arguments for completeness and forming |
| 3125 | /// the function template specialization. |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3126 | /// |
| 3127 | /// \param OriginalCallArgs If non-NULL, the original call arguments against |
| 3128 | /// which the deduced argument types should be compared. |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3129 | Sema::TemplateDeductionResult Sema::FinishTemplateArgumentDeduction( |
| 3130 | FunctionTemplateDecl *FunctionTemplate, |
| 3131 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3132 | unsigned NumExplicitlySpecified, FunctionDecl *&Specialization, |
| 3133 | TemplateDeductionInfo &Info, |
| 3134 | SmallVectorImpl<OriginalCallArg> const *OriginalCallArgs, |
| 3135 | bool PartialOverloading, llvm::function_ref<bool()> CheckNonDependent) { |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3136 | // Unevaluated SFINAE context. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 3137 | EnterExpressionEvaluationContext Unevaluated( |
| 3138 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3139 | SFINAETrap Trap(*this); |
| 3140 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3141 | // Enter a new template instantiation context while we instantiate the |
| 3142 | // actual function declaration. |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3143 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 3144 | InstantiatingTemplate Inst( |
| 3145 | *this, Info.getLocation(), FunctionTemplate, DeducedArgs, |
| 3146 | CodeSynthesisContext::DeducedTemplateArgumentSubstitution, Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3147 | if (Inst.isInvalid()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3148 | return TDK_InstantiationDepth; |
| 3149 | |
John McCall | e23b871 | 2010-04-29 01:18:58 +0000 | [diff] [blame] | 3150 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
John McCall | 80e58cd | 2010-04-29 00:35:03 +0000 | [diff] [blame] | 3151 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3152 | // C++ [temp.deduct.type]p2: |
| 3153 | // [...] or if any template argument remains neither deduced nor |
| 3154 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3155 | SmallVector<TemplateArgument, 4> Builder; |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 3156 | if (auto Result = ConvertDeducedTemplateArguments( |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 3157 | *this, FunctionTemplate, /*IsDeduced*/true, Deduced, Info, Builder, |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 3158 | CurrentInstantiationScope, NumExplicitlySpecified, |
| 3159 | PartialOverloading)) |
| 3160 | return Result; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3161 | |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3162 | // C++ [temp.deduct.call]p10: [DR1391] |
| 3163 | // If deduction succeeds for all parameters that contain |
| 3164 | // template-parameters that participate in template argument deduction, |
| 3165 | // and all template arguments are explicitly specified, deduced, or |
| 3166 | // obtained from default template arguments, remaining parameters are then |
| 3167 | // compared with the corresponding arguments. For each remaining parameter |
| 3168 | // P with a type that was non-dependent before substitution of any |
| 3169 | // explicitly-specified template arguments, if the corresponding argument |
| 3170 | // A cannot be implicitly converted to P, deduction fails. |
| 3171 | if (CheckNonDependent()) |
| 3172 | return TDK_NonDependentConversionFailure; |
| 3173 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3174 | // Form the template argument list from the deduced template arguments. |
| 3175 | TemplateArgumentList *DeducedArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3176 | = TemplateArgumentList::CreateCopy(Context, Builder); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3177 | Info.reset(DeducedArgumentList); |
| 3178 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3179 | // Substitute the deduced template arguments into the function template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3180 | // declaration to produce the function template specialization. |
Douglas Gregor | 1614237 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 3181 | DeclContext *Owner = FunctionTemplate->getDeclContext(); |
| 3182 | if (FunctionTemplate->getFriendObjectKind()) |
| 3183 | Owner = FunctionTemplate->getLexicalDeclContext(); |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3184 | MultiLevelTemplateArgumentList SubstArgs(*DeducedArgumentList); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3185 | Specialization = cast_or_null<FunctionDecl>( |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3186 | SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner, SubstArgs)); |
Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 3187 | if (!Specialization || Specialization->isInvalidDecl()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3188 | return TDK_SubstitutionFailure; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3189 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3190 | assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == |
Douglas Gregor | 31fae89 | 2009-09-15 18:26:13 +0000 | [diff] [blame] | 3191 | FunctionTemplate->getCanonicalDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3192 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3193 | // If the template argument list is owned by the function template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3194 | // specialization, release it. |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 3195 | if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList && |
| 3196 | !Trap.hasErrorOccurred()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3197 | Info.take(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3198 | |
Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 3199 | // There may have been an error that did not prevent us from constructing a |
| 3200 | // declaration. Mark the declaration invalid and return with a substitution |
| 3201 | // failure. |
| 3202 | if (Trap.hasErrorOccurred()) { |
| 3203 | Specialization->setInvalidDecl(true); |
| 3204 | return TDK_SubstitutionFailure; |
| 3205 | } |
| 3206 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3207 | if (OriginalCallArgs) { |
| 3208 | // C++ [temp.deduct.call]p4: |
| 3209 | // In general, the deduction process attempts to find template argument |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3210 | // 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] | 3211 | // is transformed as described above). [...] |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3212 | llvm::SmallDenseMap<std::pair<unsigned, QualType>, QualType> DeducedATypes; |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3213 | for (unsigned I = 0, N = OriginalCallArgs->size(); I != N; ++I) { |
| 3214 | OriginalCallArg OriginalArg = (*OriginalCallArgs)[I]; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3215 | |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3216 | auto ParamIdx = OriginalArg.ArgIdx; |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3217 | if (ParamIdx >= Specialization->getNumParams()) |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3218 | // FIXME: This presumably means a pack ended up smaller than we |
| 3219 | // expected while deducing. Should this not result in deduction |
| 3220 | // failure? Can it even happen? |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3221 | continue; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3222 | |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3223 | QualType DeducedA; |
| 3224 | if (!OriginalArg.DecomposedParam) { |
| 3225 | // P is one of the function parameters, just look up its substituted |
| 3226 | // type. |
| 3227 | DeducedA = Specialization->getParamDecl(ParamIdx)->getType(); |
| 3228 | } else { |
| 3229 | // P is a decomposed element of a parameter corresponding to a |
| 3230 | // braced-init-list argument. Substitute back into P to find the |
| 3231 | // deduced A. |
| 3232 | QualType &CacheEntry = |
| 3233 | DeducedATypes[{ParamIdx, OriginalArg.OriginalParamType}]; |
| 3234 | if (CacheEntry.isNull()) { |
| 3235 | ArgumentPackSubstitutionIndexRAII PackIndex( |
| 3236 | *this, getPackIndexForParam(*this, FunctionTemplate, SubstArgs, |
| 3237 | ParamIdx)); |
| 3238 | CacheEntry = |
| 3239 | SubstType(OriginalArg.OriginalParamType, SubstArgs, |
| 3240 | Specialization->getTypeSpecStartLoc(), |
| 3241 | Specialization->getDeclName()); |
| 3242 | } |
| 3243 | DeducedA = CacheEntry; |
| 3244 | } |
| 3245 | |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3246 | if (auto TDK = |
| 3247 | CheckOriginalCallArgDeduction(*this, Info, OriginalArg, DeducedA)) |
| 3248 | return TDK; |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3249 | } |
| 3250 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3251 | |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3252 | // If we suppressed any diagnostics while performing template argument |
| 3253 | // deduction, and if we haven't already instantiated this declaration, |
| 3254 | // keep track of these diagnostics. They'll be emitted if this specialization |
| 3255 | // is actually used. |
| 3256 | if (Info.diag_begin() != Info.diag_end()) { |
Craig Topper | 79be4cd | 2013-07-05 04:33:53 +0000 | [diff] [blame] | 3257 | SuppressedDiagnosticsMap::iterator |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3258 | Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl()); |
| 3259 | if (Pos == SuppressedDiagnostics.end()) |
| 3260 | SuppressedDiagnostics[Specialization->getCanonicalDecl()] |
| 3261 | .append(Info.diag_begin(), Info.diag_end()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3262 | } |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3263 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3264 | return TDK_Success; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3265 | } |
| 3266 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3267 | /// Gets the type of a function for template-argument-deducton |
| 3268 | /// purposes when it's considered as part of an overload set. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3269 | static QualType GetTypeOfFunction(Sema &S, const OverloadExpr::FindResult &R, |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3270 | FunctionDecl *Fn) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3271 | // We may need to deduce the return type of the function now. |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3272 | if (S.getLangOpts().CPlusPlus14 && Fn->getReturnType()->isUndeducedType() && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3273 | S.DeduceReturnType(Fn, R.Expression->getExprLoc(), /*Diagnose*/ false)) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3274 | return QualType(); |
| 3275 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3276 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3277 | if (Method->isInstance()) { |
| 3278 | // An instance method that's referenced in a form that doesn't |
| 3279 | // look like a member pointer is just invalid. |
| 3280 | if (!R.HasFormOfMemberPointer) return QualType(); |
| 3281 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3282 | return S.Context.getMemberPointerType(Fn->getType(), |
| 3283 | S.Context.getTypeDeclType(Method->getParent()).getTypePtr()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | if (!R.IsAddressOfOperand) return Fn->getType(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3287 | return S.Context.getPointerType(Fn->getType()); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3288 | } |
| 3289 | |
| 3290 | /// Apply the deduction rules for overload sets. |
| 3291 | /// |
| 3292 | /// \return the null type if this argument should be treated as an |
| 3293 | /// undeduced context |
| 3294 | static QualType |
| 3295 | ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams, |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3296 | Expr *Arg, QualType ParamType, |
| 3297 | bool ParamWasReference) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3298 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3299 | OverloadExpr::FindResult R = OverloadExpr::find(Arg); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3300 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3301 | OverloadExpr *Ovl = R.Expression; |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3302 | |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3303 | // C++0x [temp.deduct.call]p4 |
| 3304 | unsigned TDF = 0; |
| 3305 | if (ParamWasReference) |
| 3306 | TDF |= TDF_ParamWithReferenceType; |
| 3307 | if (R.IsAddressOfOperand) |
| 3308 | TDF |= TDF_IgnoreQualifiers; |
| 3309 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3310 | // C++0x [temp.deduct.call]p6: |
| 3311 | // When P is a function type, pointer to function type, or pointer |
| 3312 | // to member function type: |
| 3313 | |
| 3314 | if (!ParamType->isFunctionType() && |
| 3315 | !ParamType->isFunctionPointerType() && |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3316 | !ParamType->isMemberFunctionPointerType()) { |
| 3317 | if (Ovl->hasExplicitTemplateArgs()) { |
| 3318 | // But we can still look for an explicit specialization. |
| 3319 | if (FunctionDecl *ExplicitSpec |
| 3320 | = S.ResolveSingleFunctionTemplateSpecialization(Ovl)) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3321 | return GetTypeOfFunction(S, R, ExplicitSpec); |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3322 | } |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3323 | |
George Burgess IV | cc2f355 | 2016-03-19 21:51:45 +0000 | [diff] [blame] | 3324 | DeclAccessPair DAP; |
| 3325 | if (FunctionDecl *Viable = |
| 3326 | S.resolveAddressOfOnlyViableOverloadCandidate(Arg, DAP)) |
| 3327 | return GetTypeOfFunction(S, R, Viable); |
| 3328 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3329 | return QualType(); |
| 3330 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3331 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3332 | // Gather the explicit template arguments, if any. |
| 3333 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 3334 | if (Ovl->hasExplicitTemplateArgs()) |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 3335 | Ovl->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3336 | QualType Match; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 3337 | for (UnresolvedSetIterator I = Ovl->decls_begin(), |
| 3338 | E = Ovl->decls_end(); I != E; ++I) { |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3339 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 3340 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3341 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) { |
| 3342 | // - If the argument is an overload set containing one or more |
| 3343 | // function templates, the parameter is treated as a |
| 3344 | // non-deduced context. |
| 3345 | if (!Ovl->hasExplicitTemplateArgs()) |
| 3346 | return QualType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3347 | |
| 3348 | // Otherwise, see if we can resolve a function type |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3349 | FunctionDecl *Specialization = nullptr; |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3350 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3351 | if (S.DeduceTemplateArguments(FunTmpl, &ExplicitTemplateArgs, |
| 3352 | Specialization, Info)) |
| 3353 | continue; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3354 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3355 | D = Specialization; |
| 3356 | } |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3357 | |
| 3358 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3359 | QualType ArgType = GetTypeOfFunction(S, R, Fn); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3360 | if (ArgType.isNull()) continue; |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3361 | |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3362 | // Function-to-pointer conversion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3363 | if (!ParamWasReference && ParamType->isPointerType() && |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3364 | ArgType->isFunctionType()) |
| 3365 | ArgType = S.Context.getPointerType(ArgType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3366 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3367 | // - If the argument is an overload set (not containing function |
| 3368 | // templates), trial argument deduction is attempted using each |
| 3369 | // of the members of the set. If deduction succeeds for only one |
| 3370 | // of the overload set members, that member is used as the |
| 3371 | // argument value for the deduction. If deduction succeeds for |
| 3372 | // more than one member of the overload set the parameter is |
| 3373 | // treated as a non-deduced context. |
| 3374 | |
| 3375 | // We do all of this in a fresh context per C++0x [temp.deduct.type]p2: |
| 3376 | // Type deduction is done independently for each P/A pair, and |
| 3377 | // the deduced template argument values are then combined. |
| 3378 | // So we do not reject deductions which were made elsewhere. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3379 | SmallVector<DeducedTemplateArgument, 8> |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3380 | Deduced(TemplateParams->size()); |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3381 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3382 | Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3383 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
| 3384 | ArgType, Info, Deduced, TDF); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3385 | if (Result) continue; |
| 3386 | if (!Match.isNull()) return QualType(); |
| 3387 | Match = ArgType; |
| 3388 | } |
| 3389 | |
| 3390 | return Match; |
| 3391 | } |
| 3392 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3393 | /// \brief Perform the adjustments to the parameter and argument types |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3394 | /// described in C++ [temp.deduct.call]. |
| 3395 | /// |
| 3396 | /// \returns true if the caller should not attempt to perform any template |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3397 | /// argument deduction based on this P/A pair because the argument is an |
| 3398 | /// overloaded function set that could not be resolved. |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3399 | static bool AdjustFunctionParmAndArgTypesForDeduction( |
| 3400 | Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, |
| 3401 | QualType &ParamType, QualType &ArgType, Expr *Arg, unsigned &TDF) { |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3402 | // C++0x [temp.deduct.call]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3403 | // 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] | 3404 | // are ignored for type deduction. |
Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3405 | if (ParamType.hasQualifiers()) |
| 3406 | ParamType = ParamType.getUnqualifiedType(); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3407 | |
| 3408 | // [...] If P is a reference type, the type referred to by P is |
| 3409 | // used for type deduction. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3410 | const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>(); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3411 | if (ParamRefType) |
| 3412 | ParamType = ParamRefType->getPointeeType(); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3413 | |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3414 | // Overload sets usually make this parameter an undeduced context, |
| 3415 | // but there are sometimes special circumstances. Typically |
| 3416 | // involving a template-id-expr. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3417 | if (ArgType == S.Context.OverloadTy) { |
| 3418 | ArgType = ResolveOverloadForDeduction(S, TemplateParams, |
| 3419 | Arg, ParamType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3420 | ParamRefType != nullptr); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3421 | if (ArgType.isNull()) |
| 3422 | return true; |
| 3423 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3424 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3425 | if (ParamRefType) { |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3426 | // If the argument has incomplete array type, try to complete its type. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3427 | if (ArgType->isIncompleteArrayType()) { |
| 3428 | S.completeExprArrayBound(Arg); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3429 | ArgType = Arg->getType(); |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3430 | } |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3431 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3432 | // C++1z [temp.deduct.call]p3: |
| 3433 | // If P is a forwarding reference and the argument is an lvalue, the type |
| 3434 | // "lvalue reference to A" is used in place of A for type deduction. |
| 3435 | if (isForwardingReference(QualType(ParamRefType, 0), FirstInnerIndex) && |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3436 | Arg->isLValue()) |
| 3437 | ArgType = S.Context.getLValueReferenceType(ArgType); |
| 3438 | } else { |
| 3439 | // C++ [temp.deduct.call]p2: |
| 3440 | // If P is not a reference type: |
| 3441 | // - If A is an array type, the pointer type produced by the |
| 3442 | // array-to-pointer standard conversion (4.2) is used in place of |
| 3443 | // A for type deduction; otherwise, |
| 3444 | if (ArgType->isArrayType()) |
| 3445 | ArgType = S.Context.getArrayDecayedType(ArgType); |
| 3446 | // - If A is a function type, the pointer type produced by the |
| 3447 | // function-to-pointer standard conversion (4.3) is used in place |
| 3448 | // of A for type deduction; otherwise, |
| 3449 | else if (ArgType->isFunctionType()) |
| 3450 | ArgType = S.Context.getPointerType(ArgType); |
| 3451 | else { |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3452 | // - 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] | 3453 | // type are ignored for type deduction. |
Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3454 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3455 | } |
| 3456 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3457 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3458 | // C++0x [temp.deduct.call]p4: |
| 3459 | // In general, the deduction process attempts to find template argument |
| 3460 | // values that will make the deduced A identical to A (after the type A |
| 3461 | // is transformed as described above). [...] |
| 3462 | TDF = TDF_SkipNonDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3463 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3464 | // - If the original P is a reference type, the deduced A (i.e., the |
| 3465 | // type referred to by the reference) can be more cv-qualified than |
| 3466 | // the transformed A. |
| 3467 | if (ParamRefType) |
| 3468 | TDF |= TDF_ParamWithReferenceType; |
| 3469 | // - The transformed A can be another pointer or pointer to member |
| 3470 | // type that can be converted to the deduced A via a qualification |
| 3471 | // conversion (4.4). |
| 3472 | if (ArgType->isPointerType() || ArgType->isMemberPointerType() || |
| 3473 | ArgType->isObjCObjectPointerType()) |
| 3474 | TDF |= TDF_IgnoreQualifiers; |
| 3475 | // - If P is a class and P has the form simple-template-id, then the |
| 3476 | // transformed A can be a derived class of the deduced A. Likewise, |
| 3477 | // if P is a pointer to a class of the form simple-template-id, the |
| 3478 | // transformed A can be a pointer to a derived class pointed to by |
| 3479 | // the deduced A. |
| 3480 | if (isSimpleTemplateIdType(ParamType) || |
| 3481 | (isa<PointerType>(ParamType) && |
| 3482 | isSimpleTemplateIdType( |
| 3483 | ParamType->getAs<PointerType>()->getPointeeType()))) |
| 3484 | TDF |= TDF_DerivedClass; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3485 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3486 | return false; |
| 3487 | } |
| 3488 | |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 3489 | static bool |
| 3490 | hasDeducibleTemplateParameters(Sema &S, FunctionTemplateDecl *FunctionTemplate, |
| 3491 | QualType T); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3492 | |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3493 | static Sema::TemplateDeductionResult DeduceTemplateArgumentsFromCallArgument( |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3494 | Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, |
| 3495 | QualType ParamType, Expr *Arg, TemplateDeductionInfo &Info, |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3496 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3497 | SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3498 | bool DecomposedParam, unsigned ArgIdx, unsigned TDF); |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3499 | |
| 3500 | /// \brief Attempt template argument deduction from an initializer list |
| 3501 | /// deemed to be an argument in a function call. |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3502 | static Sema::TemplateDeductionResult DeduceFromInitializerList( |
| 3503 | Sema &S, TemplateParameterList *TemplateParams, QualType AdjustedParamType, |
| 3504 | InitListExpr *ILE, TemplateDeductionInfo &Info, |
| 3505 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3506 | SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, unsigned ArgIdx, |
| 3507 | unsigned TDF) { |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3508 | // C++ [temp.deduct.call]p1: (CWG 1591) |
| 3509 | // If removing references and cv-qualifiers from P gives |
| 3510 | // std::initializer_list<P0> or P0[N] for some P0 and N and the argument is |
| 3511 | // a non-empty initializer list, then deduction is performed instead for |
| 3512 | // each element of the initializer list, taking P0 as a function template |
| 3513 | // parameter type and the initializer element as its argument |
| 3514 | // |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3515 | // We've already removed references and cv-qualifiers here. |
Richard Smith | 9c5534c | 2017-01-05 04:16:30 +0000 | [diff] [blame] | 3516 | if (!ILE->getNumInits()) |
| 3517 | return Sema::TDK_Success; |
| 3518 | |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3519 | QualType ElTy; |
| 3520 | auto *ArrTy = S.Context.getAsArrayType(AdjustedParamType); |
| 3521 | if (ArrTy) |
| 3522 | ElTy = ArrTy->getElementType(); |
| 3523 | else if (!S.isStdInitializerList(AdjustedParamType, &ElTy)) { |
| 3524 | // Otherwise, an initializer list argument causes the parameter to be |
| 3525 | // considered a non-deduced context |
| 3526 | return Sema::TDK_Success; |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3527 | } |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3528 | |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3529 | // Deduction only needs to be done for dependent types. |
| 3530 | if (ElTy->isDependentType()) { |
| 3531 | for (Expr *E : ILE->inits()) { |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3532 | if (auto Result = DeduceTemplateArgumentsFromCallArgument( |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3533 | S, TemplateParams, 0, ElTy, E, Info, Deduced, OriginalCallArgs, true, |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3534 | ArgIdx, TDF)) |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3535 | return Result; |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3536 | } |
| 3537 | } |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3538 | |
| 3539 | // in the P0[N] case, if N is a non-type template parameter, N is deduced |
| 3540 | // from the length of the initializer list. |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3541 | if (auto *DependentArrTy = dyn_cast_or_null<DependentSizedArrayType>(ArrTy)) { |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3542 | // Determine the array bound is something we can deduce. |
| 3543 | if (NonTypeTemplateParmDecl *NTTP = |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3544 | getDeducedParameterFromExpr(Info, DependentArrTy->getSizeExpr())) { |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3545 | // We can perform template argument deduction for the given non-type |
| 3546 | // template parameter. |
Richard Smith | 7fa88bb | 2017-02-21 07:22:31 +0000 | [diff] [blame] | 3547 | // C++ [temp.deduct.type]p13: |
| 3548 | // The type of N in the type T[N] is std::size_t. |
| 3549 | QualType T = S.Context.getSizeType(); |
| 3550 | llvm::APInt Size(S.Context.getIntWidth(T), ILE->getNumInits()); |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3551 | if (auto Result = DeduceNonTypeTemplateArgument( |
Richard Smith | 7fa88bb | 2017-02-21 07:22:31 +0000 | [diff] [blame] | 3552 | S, TemplateParams, NTTP, llvm::APSInt(Size), T, |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3553 | /*ArrayBound=*/true, Info, Deduced)) |
| 3554 | return Result; |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3555 | } |
| 3556 | } |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3557 | |
| 3558 | return Sema::TDK_Success; |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3559 | } |
| 3560 | |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3561 | /// \brief Perform template argument deduction per [temp.deduct.call] for a |
| 3562 | /// single parameter / argument pair. |
| 3563 | static Sema::TemplateDeductionResult DeduceTemplateArgumentsFromCallArgument( |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3564 | Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, |
| 3565 | QualType ParamType, Expr *Arg, TemplateDeductionInfo &Info, |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3566 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3567 | SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3568 | bool DecomposedParam, unsigned ArgIdx, unsigned TDF) { |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3569 | QualType ArgType = Arg->getType(); |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3570 | QualType OrigParamType = ParamType; |
| 3571 | |
| 3572 | // If P is a reference type [...] |
| 3573 | // If P is a cv-qualified type [...] |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3574 | if (AdjustFunctionParmAndArgTypesForDeduction( |
| 3575 | S, TemplateParams, FirstInnerIndex, ParamType, ArgType, Arg, TDF)) |
Richard Smith | 363ae81 | 2017-01-04 22:03:59 +0000 | [diff] [blame] | 3576 | return Sema::TDK_Success; |
| 3577 | |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3578 | // If [...] the argument is a non-empty initializer list [...] |
| 3579 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) |
| 3580 | return DeduceFromInitializerList(S, TemplateParams, ParamType, ILE, Info, |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3581 | Deduced, OriginalCallArgs, ArgIdx, TDF); |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3582 | |
| 3583 | // [...] the deduction process attempts to find template argument values |
| 3584 | // that will make the deduced A identical to A |
| 3585 | // |
| 3586 | // Keep track of the argument type and corresponding parameter index, |
| 3587 | // so we can check for compatibility between the deduced A and A. |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3588 | OriginalCallArgs.push_back( |
| 3589 | Sema::OriginalCallArg(OrigParamType, DecomposedParam, ArgIdx, ArgType)); |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3590 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3591 | ArgType, Info, Deduced, TDF); |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3592 | } |
| 3593 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3594 | /// \brief Perform template argument deduction from a function call |
| 3595 | /// (C++ [temp.deduct.call]). |
| 3596 | /// |
| 3597 | /// \param FunctionTemplate the function template for which we are performing |
| 3598 | /// template argument deduction. |
| 3599 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3600 | /// \param ExplicitTemplateArgs the explicit template arguments provided |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 3601 | /// for this call. |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3602 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3603 | /// \param Args the function call arguments |
| 3604 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3605 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3606 | /// this will be set to the function template specialization produced by |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3607 | /// template argument deduction. |
| 3608 | /// |
| 3609 | /// \param Info the argument will be updated to provide additional information |
| 3610 | /// about template argument deduction. |
| 3611 | /// |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3612 | /// \param CheckNonDependent A callback to invoke to check conversions for |
| 3613 | /// non-dependent parameters, between deduction and substitution, per DR1391. |
| 3614 | /// If this returns true, substitution will be skipped and we return |
| 3615 | /// TDK_NonDependentConversionFailure. The callback is passed the parameter |
| 3616 | /// types (after substituting explicit template arguments). |
| 3617 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3618 | /// \returns the result of template argument deduction. |
Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 3619 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3620 | FunctionTemplateDecl *FunctionTemplate, |
| 3621 | TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3622 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3623 | bool PartialOverloading, |
| 3624 | llvm::function_ref<bool(ArrayRef<QualType>)> CheckNonDependent) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3625 | if (FunctionTemplate->isInvalidDecl()) |
| 3626 | return TDK_Invalid; |
| 3627 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3628 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3629 | unsigned NumParams = Function->getNumParams(); |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3630 | |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3631 | unsigned FirstInnerIndex = getFirstInnerIndex(FunctionTemplate); |
| 3632 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3633 | // C++ [temp.deduct.call]p1: |
| 3634 | // Template argument deduction is done by comparing each function template |
| 3635 | // parameter type (call it P) with the type of the corresponding argument |
| 3636 | // of the call (call it A) as described below. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3637 | if (Args.size() < Function->getMinRequiredArguments() && !PartialOverloading) |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3638 | return TDK_TooFewArguments; |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3639 | else if (TooManyArguments(NumParams, Args.size(), PartialOverloading)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3640 | const FunctionProtoType *Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3641 | = Function->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3642 | if (Proto->isTemplateVariadic()) |
| 3643 | /* Do nothing */; |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3644 | else if (!Proto->isVariadic()) |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3645 | return TDK_TooManyArguments; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3646 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3647 | |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3648 | // The types of the parameters from which we will perform template argument |
| 3649 | // deduction. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3650 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3651 | TemplateParameterList *TemplateParams |
| 3652 | = FunctionTemplate->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3653 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3654 | SmallVector<QualType, 8> ParamTypes; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3655 | unsigned NumExplicitlySpecified = 0; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3656 | if (ExplicitTemplateArgs) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3657 | TemplateDeductionResult Result = |
| 3658 | SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3659 | *ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3660 | Deduced, |
| 3661 | ParamTypes, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3662 | nullptr, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3663 | Info); |
| 3664 | if (Result) |
| 3665 | return Result; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3666 | |
| 3667 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3668 | } else { |
| 3669 | // Just fill in the parameter types from the function declaration. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3670 | for (unsigned I = 0; I != NumParams; ++I) |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3671 | ParamTypes.push_back(Function->getParamDecl(I)->getType()); |
| 3672 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3673 | |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3674 | SmallVector<OriginalCallArg, 8> OriginalCallArgs; |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3675 | |
| 3676 | // Deduce an argument of type ParamType from an expression with index ArgIdx. |
| 3677 | auto DeduceCallArgument = [&](QualType ParamType, unsigned ArgIdx) { |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3678 | // C++ [demp.deduct.call]p1: (DR1391) |
| 3679 | // Template argument deduction is done by comparing each function template |
| 3680 | // parameter that contains template-parameters that participate in |
| 3681 | // template argument deduction ... |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 3682 | if (!hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3683 | return Sema::TDK_Success; |
| 3684 | |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3685 | // ... with the type of the corresponding argument |
| 3686 | return DeduceTemplateArgumentsFromCallArgument( |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3687 | *this, TemplateParams, FirstInnerIndex, ParamType, Args[ArgIdx], Info, Deduced, |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3688 | OriginalCallArgs, /*Decomposed*/false, ArgIdx, /*TDF*/ 0); |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3689 | }; |
| 3690 | |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3691 | // Deduce template arguments from the function parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3692 | Deduced.resize(TemplateParams->size()); |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3693 | SmallVector<QualType, 8> ParamTypesForArgChecking; |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3694 | for (unsigned ParamIdx = 0, NumParamTypes = ParamTypes.size(), ArgIdx = 0; |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3695 | ParamIdx != NumParamTypes; ++ParamIdx) { |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3696 | QualType ParamType = ParamTypes[ParamIdx]; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3697 | |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3698 | const PackExpansionType *ParamExpansion = |
| 3699 | dyn_cast<PackExpansionType>(ParamType); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3700 | if (!ParamExpansion) { |
| 3701 | // Simple case: matching a function parameter to a function argument. |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3702 | if (ArgIdx >= Args.size()) |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3703 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3704 | |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3705 | ParamTypesForArgChecking.push_back(ParamType); |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3706 | if (auto Result = DeduceCallArgument(ParamType, ArgIdx++)) |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3707 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3708 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3709 | continue; |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3710 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3711 | |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3712 | QualType ParamPattern = ParamExpansion->getPattern(); |
| 3713 | PackDeductionScope PackScope(*this, TemplateParams, Deduced, Info, |
| 3714 | ParamPattern); |
| 3715 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3716 | // C++0x [temp.deduct.call]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3717 | // For a function parameter pack that occurs at the end of the |
| 3718 | // parameter-declaration-list, the type A of each remaining argument of |
| 3719 | // the call is compared with the type P of the declarator-id of the |
| 3720 | // function parameter pack. Each comparison deduces template arguments |
| 3721 | // for subsequent positions in the template parameter packs expanded by |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3722 | // the function parameter pack. When a function parameter pack appears |
| 3723 | // in a non-deduced context [not at the end of the list], the type of |
| 3724 | // that parameter pack is never deduced. |
| 3725 | // |
| 3726 | // FIXME: The above rule allows the size of the parameter pack to change |
| 3727 | // after we skip it (in the non-deduced case). That makes no sense, so |
| 3728 | // we instead notionally deduce the pack against N arguments, where N is |
| 3729 | // the length of the explicitly-specified pack if it's expanded by the |
| 3730 | // parameter pack and 0 otherwise, and we treat each deduction as a |
| 3731 | // non-deduced context. |
| 3732 | if (ParamIdx + 1 == NumParamTypes) { |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3733 | for (; ArgIdx < Args.size(); PackScope.nextPackElement(), ++ArgIdx) { |
| 3734 | ParamTypesForArgChecking.push_back(ParamPattern); |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3735 | if (auto Result = DeduceCallArgument(ParamPattern, ArgIdx)) |
| 3736 | return Result; |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3737 | } |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3738 | } else { |
| 3739 | // If the parameter type contains an explicitly-specified pack that we |
| 3740 | // could not expand, skip the number of parameters notionally created |
| 3741 | // by the expansion. |
| 3742 | Optional<unsigned> NumExpansions = ParamExpansion->getNumExpansions(); |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3743 | if (NumExpansions && !PackScope.isPartiallyExpanded()) { |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3744 | for (unsigned I = 0; I != *NumExpansions && ArgIdx < Args.size(); |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3745 | ++I, ++ArgIdx) { |
| 3746 | ParamTypesForArgChecking.push_back(ParamPattern); |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3747 | // FIXME: Should we add OriginalCallArgs for these? What if the |
| 3748 | // corresponding argument is a list? |
| 3749 | PackScope.nextPackElement(); |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3750 | } |
| 3751 | } |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3752 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3753 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3754 | // Build argument packs for each of the parameter packs expanded by this |
| 3755 | // pack expansion. |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 3756 | if (auto Result = PackScope.finish()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3757 | return Result; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3758 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3759 | |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3760 | return FinishTemplateArgumentDeduction( |
| 3761 | FunctionTemplate, Deduced, NumExplicitlySpecified, Specialization, Info, |
| 3762 | &OriginalCallArgs, PartialOverloading, |
| 3763 | [&]() { return CheckNonDependent(ParamTypesForArgChecking); }); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3764 | } |
| 3765 | |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3766 | QualType Sema::adjustCCAndNoReturn(QualType ArgFunctionType, |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3767 | QualType FunctionType, |
| 3768 | bool AdjustExceptionSpec) { |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3769 | if (ArgFunctionType.isNull()) |
| 3770 | return ArgFunctionType; |
| 3771 | |
| 3772 | const FunctionProtoType *FunctionTypeP = |
| 3773 | FunctionType->castAs<FunctionProtoType>(); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3774 | const FunctionProtoType *ArgFunctionTypeP = |
| 3775 | ArgFunctionType->getAs<FunctionProtoType>(); |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3776 | |
| 3777 | FunctionProtoType::ExtProtoInfo EPI = ArgFunctionTypeP->getExtProtoInfo(); |
| 3778 | bool Rebuild = false; |
| 3779 | |
| 3780 | CallingConv CC = FunctionTypeP->getCallConv(); |
| 3781 | if (EPI.ExtInfo.getCC() != CC) { |
| 3782 | EPI.ExtInfo = EPI.ExtInfo.withCallingConv(CC); |
| 3783 | Rebuild = true; |
| 3784 | } |
| 3785 | |
| 3786 | bool NoReturn = FunctionTypeP->getNoReturnAttr(); |
| 3787 | if (EPI.ExtInfo.getNoReturn() != NoReturn) { |
| 3788 | EPI.ExtInfo = EPI.ExtInfo.withNoReturn(NoReturn); |
| 3789 | Rebuild = true; |
| 3790 | } |
| 3791 | |
| 3792 | if (AdjustExceptionSpec && (FunctionTypeP->hasExceptionSpec() || |
| 3793 | ArgFunctionTypeP->hasExceptionSpec())) { |
| 3794 | EPI.ExceptionSpec = FunctionTypeP->getExtProtoInfo().ExceptionSpec; |
| 3795 | Rebuild = true; |
| 3796 | } |
| 3797 | |
| 3798 | if (!Rebuild) |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3799 | return ArgFunctionType; |
| 3800 | |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3801 | return Context.getFunctionType(ArgFunctionTypeP->getReturnType(), |
| 3802 | ArgFunctionTypeP->getParamTypes(), EPI); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3803 | } |
| 3804 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3805 | /// \brief Deduce template arguments when taking the address of a function |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3806 | /// template (C++ [temp.deduct.funcaddr]) or matching a specialization to |
| 3807 | /// a template. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3808 | /// |
| 3809 | /// \param FunctionTemplate the function template for which we are performing |
| 3810 | /// template argument deduction. |
| 3811 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3812 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3813 | /// arguments. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3814 | /// |
| 3815 | /// \param ArgFunctionType the function type that will be used as the |
| 3816 | /// "argument" type (A) when performing template argument deduction from the |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3817 | /// function template's function type. This type may be NULL, if there is no |
| 3818 | /// argument type to compare against, in C++0x [temp.arg.explicit]p3. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3819 | /// |
| 3820 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3821 | /// this will be set to the function template specialization produced by |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3822 | /// template argument deduction. |
| 3823 | /// |
| 3824 | /// \param Info the argument will be updated to provide additional information |
| 3825 | /// about template argument deduction. |
| 3826 | /// |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3827 | /// \param IsAddressOfFunction If \c true, we are deducing as part of taking |
| 3828 | /// the address of a function template per [temp.deduct.funcaddr] and |
| 3829 | /// [over.over]. If \c false, we are looking up a function template |
| 3830 | /// specialization based on its signature, per [temp.deduct.decl]. |
| 3831 | /// |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3832 | /// \returns the result of template argument deduction. |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3833 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3834 | FunctionTemplateDecl *FunctionTemplate, |
| 3835 | TemplateArgumentListInfo *ExplicitTemplateArgs, QualType ArgFunctionType, |
| 3836 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 3837 | bool IsAddressOfFunction) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3838 | if (FunctionTemplate->isInvalidDecl()) |
| 3839 | return TDK_Invalid; |
| 3840 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3841 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 3842 | TemplateParameterList *TemplateParams |
| 3843 | = FunctionTemplate->getTemplateParameters(); |
| 3844 | QualType FunctionType = Function->getType(); |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3845 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3846 | // Substitute any explicit template arguments. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3847 | LocalInstantiationScope InstScope(*this); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3848 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3849 | unsigned NumExplicitlySpecified = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3850 | SmallVector<QualType, 4> ParamTypes; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3851 | if (ExplicitTemplateArgs) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3852 | if (TemplateDeductionResult Result |
| 3853 | = SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3854 | *ExplicitTemplateArgs, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3855 | Deduced, ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3856 | &FunctionType, Info)) |
| 3857 | return Result; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3858 | |
| 3859 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3860 | } |
| 3861 | |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 3862 | // When taking the address of a function, we require convertibility of |
| 3863 | // the resulting function type. Otherwise, we allow arbitrary mismatches |
| 3864 | // of calling convention and noreturn. |
| 3865 | if (!IsAddressOfFunction) |
| 3866 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, FunctionType, |
| 3867 | /*AdjustExceptionSpec*/false); |
| 3868 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3869 | // Unevaluated SFINAE context. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 3870 | EnterExpressionEvaluationContext Unevaluated( |
| 3871 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3872 | SFINAETrap Trap(*this); |
| 3873 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3874 | Deduced.resize(TemplateParams->size()); |
| 3875 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3876 | // 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] | 3877 | // type so that we treat it as a non-deduced context in what follows. If we |
| 3878 | // are looking up by signature, the signature type should also have a deduced |
| 3879 | // return type, which we instead expect to exactly match. |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3880 | bool HasDeducedReturnType = false; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3881 | if (getLangOpts().CPlusPlus14 && IsAddressOfFunction && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3882 | Function->getReturnType()->getContainedAutoType()) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3883 | FunctionType = SubstAutoType(FunctionType, Context.DependentTy); |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3884 | HasDeducedReturnType = true; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3885 | } |
| 3886 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3887 | if (!ArgFunctionType.isNull()) { |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 3888 | unsigned TDF = |
| 3889 | TDF_TopLevelParameterTypeList | TDF_AllowCompatibleFunctionType; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3890 | // Deduce template arguments from the function type. |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3891 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3892 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3893 | FunctionType, ArgFunctionType, |
| 3894 | Info, Deduced, TDF)) |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3895 | return Result; |
| 3896 | } |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3897 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3898 | if (TemplateDeductionResult Result |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3899 | = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
| 3900 | NumExplicitlySpecified, |
| 3901 | Specialization, Info)) |
| 3902 | return Result; |
| 3903 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3904 | // If the function has a deduced return type, deduce it now, so we can check |
| 3905 | // that the deduced function type matches the requested type. |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3906 | if (HasDeducedReturnType && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3907 | Specialization->getReturnType()->isUndeducedType() && |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3908 | DeduceReturnType(Specialization, Info.getLocation(), false)) |
| 3909 | return TDK_MiscellaneousDeductionFailure; |
| 3910 | |
Richard Smith | 9095e5b | 2016-11-01 01:31:23 +0000 | [diff] [blame] | 3911 | // If the function has a dependent exception specification, resolve it now, |
| 3912 | // so we can check that the exception specification matches. |
| 3913 | auto *SpecializationFPT = |
| 3914 | Specialization->getType()->castAs<FunctionProtoType>(); |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3915 | if (getLangOpts().CPlusPlus17 && |
Richard Smith | 9095e5b | 2016-11-01 01:31:23 +0000 | [diff] [blame] | 3916 | isUnresolvedExceptionSpec(SpecializationFPT->getExceptionSpecType()) && |
| 3917 | !ResolveExceptionSpec(Info.getLocation(), SpecializationFPT)) |
| 3918 | return TDK_MiscellaneousDeductionFailure; |
| 3919 | |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 3920 | // Adjust the exception specification of the argument to match the |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3921 | // substituted and resolved type we just formed. (Calling convention and |
| 3922 | // noreturn can't be dependent, so we don't actually need this for them |
| 3923 | // right now.) |
| 3924 | QualType SpecializationType = Specialization->getType(); |
| 3925 | if (!IsAddressOfFunction) |
| 3926 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, SpecializationType, |
| 3927 | /*AdjustExceptionSpec*/true); |
| 3928 | |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3929 | // 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] | 3930 | // specialization with respect to arguments of compatible pointer to function |
| 3931 | // types, template argument deduction fails. |
| 3932 | if (!ArgFunctionType.isNull()) { |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3933 | if (IsAddressOfFunction && |
| 3934 | !isSameOrCompatibleFunctionType( |
| 3935 | Context.getCanonicalType(SpecializationType), |
| 3936 | Context.getCanonicalType(ArgFunctionType))) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3937 | return TDK_MiscellaneousDeductionFailure; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3938 | |
| 3939 | if (!IsAddressOfFunction && |
| 3940 | !Context.hasSameType(SpecializationType, ArgFunctionType)) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3941 | return TDK_MiscellaneousDeductionFailure; |
| 3942 | } |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3943 | |
| 3944 | return TDK_Success; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3945 | } |
| 3946 | |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3947 | /// \brief Deduce template arguments for a templated conversion |
| 3948 | /// function (C++ [temp.deduct.conv]) and, if successful, produce a |
| 3949 | /// conversion function template specialization. |
| 3950 | Sema::TemplateDeductionResult |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3951 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *ConversionTemplate, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3952 | QualType ToType, |
| 3953 | CXXConversionDecl *&Specialization, |
| 3954 | TemplateDeductionInfo &Info) { |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3955 | if (ConversionTemplate->isInvalidDecl()) |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3956 | return TDK_Invalid; |
| 3957 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3958 | CXXConversionDecl *ConversionGeneric |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3959 | = cast<CXXConversionDecl>(ConversionTemplate->getTemplatedDecl()); |
| 3960 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3961 | QualType FromType = ConversionGeneric->getConversionType(); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3962 | |
| 3963 | // Canonicalize the types for deduction. |
| 3964 | QualType P = Context.getCanonicalType(FromType); |
| 3965 | QualType A = Context.getCanonicalType(ToType); |
| 3966 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3967 | // C++0x [temp.deduct.conv]p2: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3968 | // If P is a reference type, the type referred to by P is used for |
| 3969 | // type deduction. |
| 3970 | if (const ReferenceType *PRef = P->getAs<ReferenceType>()) |
| 3971 | P = PRef->getPointeeType(); |
| 3972 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3973 | // C++0x [temp.deduct.conv]p4: |
| 3974 | // [...] 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] | 3975 | // for type deduction. |
| 3976 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3977 | A = ARef->getPointeeType().getUnqualifiedType(); |
| 3978 | // C++ [temp.deduct.conv]p3: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3979 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3980 | // If A is not a reference type: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3981 | else { |
| 3982 | assert(!A->isReferenceType() && "Reference types were handled above"); |
| 3983 | |
| 3984 | // - If P is an array type, the pointer type produced by the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3985 | // array-to-pointer standard conversion (4.2) is used in place |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3986 | // of P for type deduction; otherwise, |
| 3987 | if (P->isArrayType()) |
| 3988 | P = Context.getArrayDecayedType(P); |
| 3989 | // - If P is a function type, the pointer type produced by the |
| 3990 | // function-to-pointer standard conversion (4.3) is used in |
| 3991 | // place of P for type deduction; otherwise, |
| 3992 | else if (P->isFunctionType()) |
| 3993 | P = Context.getPointerType(P); |
| 3994 | // - 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] | 3995 | // P's type are ignored for type deduction. |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3996 | else |
| 3997 | P = P.getUnqualifiedType(); |
| 3998 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3999 | // C++0x [temp.deduct.conv]p4: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4000 | // 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] | 4001 | // 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] | 4002 | // referred to by A is used for type deduction. |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4003 | A = A.getUnqualifiedType(); |
| 4004 | } |
| 4005 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 4006 | // Unevaluated SFINAE context. |
Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4007 | EnterExpressionEvaluationContext Unevaluated( |
| 4008 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4009 | SFINAETrap Trap(*this); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4010 | |
| 4011 | // C++ [temp.deduct.conv]p1: |
| 4012 | // Template argument deduction is done by comparing the return |
| 4013 | // type of the template conversion function (call it P) with the |
| 4014 | // type that is required as the result of the conversion (call it |
| 4015 | // A) as described in 14.8.2.4. |
| 4016 | TemplateParameterList *TemplateParams |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 4017 | = ConversionTemplate->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4018 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4019 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4020 | |
| 4021 | // C++0x [temp.deduct.conv]p4: |
| 4022 | // In general, the deduction process attempts to find template |
| 4023 | // argument values that will make the deduced A identical to |
| 4024 | // A. However, there are two cases that allow a difference: |
| 4025 | unsigned TDF = 0; |
| 4026 | // - If the original A is a reference type, A can be more |
| 4027 | // cv-qualified than the deduced A (i.e., the type referred to |
| 4028 | // by the reference) |
| 4029 | if (ToType->isReferenceType()) |
| 4030 | TDF |= TDF_ParamWithReferenceType; |
| 4031 | // - The deduced A can be another pointer or pointer to member |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4032 | // type that can be converted to A via a qualification |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4033 | // conversion. |
| 4034 | // |
| 4035 | // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when |
| 4036 | // both P and A are pointers or member pointers. In this case, we |
| 4037 | // just ignore cv-qualifiers completely). |
| 4038 | if ((P->isPointerType() && A->isPointerType()) || |
Douglas Gregor | 9f05ed5 | 2011-08-30 00:37:54 +0000 | [diff] [blame] | 4039 | (P->isMemberPointerType() && A->isMemberPointerType())) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4040 | TDF |= TDF_IgnoreQualifiers; |
| 4041 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4042 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 4043 | P, A, Info, Deduced, TDF)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4044 | return Result; |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 4045 | |
| 4046 | // Create an Instantiation Scope for finalizing the operator. |
| 4047 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4048 | // Finish template argument deduction. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4049 | FunctionDecl *ConversionSpecialized = nullptr; |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 4050 | TemplateDeductionResult Result |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 4051 | = FinishTemplateArgumentDeduction(ConversionTemplate, Deduced, 0, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 4052 | ConversionSpecialized, Info); |
| 4053 | Specialization = cast_or_null<CXXConversionDecl>(ConversionSpecialized); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4054 | return Result; |
| 4055 | } |
| 4056 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4057 | /// \brief Deduce template arguments for a function template when there is |
| 4058 | /// nothing to deduce against (C++0x [temp.arg.explicit]p3). |
| 4059 | /// |
| 4060 | /// \param FunctionTemplate the function template for which we are performing |
| 4061 | /// template argument deduction. |
| 4062 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 4063 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4064 | /// arguments. |
| 4065 | /// |
| 4066 | /// \param Specialization if template argument deduction was successful, |
| 4067 | /// this will be set to the function template specialization produced by |
| 4068 | /// template argument deduction. |
| 4069 | /// |
| 4070 | /// \param Info the argument will be updated to provide additional information |
| 4071 | /// about template argument deduction. |
| 4072 | /// |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4073 | /// \param IsAddressOfFunction If \c true, we are deducing as part of taking |
| 4074 | /// the address of a function template in a context where we do not have a |
| 4075 | /// target type, per [over.over]. If \c false, we are looking up a function |
| 4076 | /// template specialization based on its signature, which only happens when |
| 4077 | /// deducing a function parameter type from an argument that is a template-id |
| 4078 | /// naming a function template specialization. |
| 4079 | /// |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4080 | /// \returns the result of template argument deduction. |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4081 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 4082 | FunctionTemplateDecl *FunctionTemplate, |
| 4083 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 4084 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 4085 | bool IsAddressOfFunction) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4086 | return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 4087 | QualType(), Specialization, Info, |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4088 | IsAddressOfFunction); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4089 | } |
| 4090 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4091 | namespace { |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4092 | /// Substitute the 'auto' specifier or deduced template specialization type |
| 4093 | /// specifier within a type for a given replacement type. |
| 4094 | class SubstituteDeducedTypeTransform : |
| 4095 | public TreeTransform<SubstituteDeducedTypeTransform> { |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4096 | QualType Replacement; |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4097 | bool UseTypeSugar; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4098 | public: |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4099 | SubstituteDeducedTypeTransform(Sema &SemaRef, QualType Replacement, |
| 4100 | bool UseTypeSugar = true) |
| 4101 | : TreeTransform<SubstituteDeducedTypeTransform>(SemaRef), |
| 4102 | Replacement(Replacement), UseTypeSugar(UseTypeSugar) {} |
| 4103 | |
| 4104 | QualType TransformDesugared(TypeLocBuilder &TLB, DeducedTypeLoc TL) { |
| 4105 | assert(isa<TemplateTypeParmType>(Replacement) && |
| 4106 | "unexpected unsugared replacement kind"); |
| 4107 | QualType Result = Replacement; |
| 4108 | TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result); |
| 4109 | NewTL.setNameLoc(TL.getNameLoc()); |
| 4110 | return Result; |
| 4111 | } |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 4112 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4113 | QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) { |
| 4114 | // If we're building the type pattern to deduce against, don't wrap the |
| 4115 | // substituted type in an AutoType. Certain template deduction rules |
| 4116 | // apply only when a template type parameter appears directly (and not if |
| 4117 | // the parameter is found through desugaring). For instance: |
| 4118 | // auto &&lref = lvalue; |
| 4119 | // must transform into "rvalue reference to T" not "rvalue reference to |
| 4120 | // 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] | 4121 | // |
| 4122 | // FIXME: Is this still necessary? |
| 4123 | if (!UseTypeSugar) |
| 4124 | return TransformDesugared(TLB, TL); |
| 4125 | |
| 4126 | QualType Result = SemaRef.Context.getAutoType( |
| 4127 | Replacement, TL.getTypePtr()->getKeyword(), Replacement.isNull()); |
| 4128 | auto NewTL = TLB.push<AutoTypeLoc>(Result); |
| 4129 | NewTL.setNameLoc(TL.getNameLoc()); |
| 4130 | return Result; |
| 4131 | } |
| 4132 | |
| 4133 | QualType TransformDeducedTemplateSpecializationType( |
| 4134 | TypeLocBuilder &TLB, DeducedTemplateSpecializationTypeLoc TL) { |
| 4135 | if (!UseTypeSugar) |
| 4136 | return TransformDesugared(TLB, TL); |
| 4137 | |
| 4138 | QualType Result = SemaRef.Context.getDeducedTemplateSpecializationType( |
| 4139 | TL.getTypePtr()->getTemplateName(), |
| 4140 | Replacement, Replacement.isNull()); |
| 4141 | auto NewTL = TLB.push<DeducedTemplateSpecializationTypeLoc>(Result); |
| 4142 | NewTL.setNameLoc(TL.getNameLoc()); |
| 4143 | return Result; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4144 | } |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 4145 | |
| 4146 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 4147 | // Lambdas never need to be transformed. |
| 4148 | return E; |
| 4149 | } |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4150 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4151 | QualType Apply(TypeLoc TL) { |
| 4152 | // Create some scratch storage for the transformed type locations. |
| 4153 | // FIXME: We're just going to throw this information away. Don't build it. |
| 4154 | TypeLocBuilder TLB; |
| 4155 | TLB.reserve(TL.getFullDataSize()); |
| 4156 | return TransformType(TLB, TL); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4157 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4158 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4159 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4160 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4161 | Sema::DeduceAutoResult |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4162 | Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *&Init, QualType &Result, |
| 4163 | Optional<unsigned> DependentDeductionDepth) { |
| 4164 | return DeduceAutoType(Type->getTypeLoc(), Init, Result, |
| 4165 | DependentDeductionDepth); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4166 | } |
| 4167 | |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4168 | /// Attempt to produce an informative diagostic explaining why auto deduction |
| 4169 | /// failed. |
| 4170 | /// \return \c true if diagnosed, \c false if not. |
| 4171 | static bool diagnoseAutoDeductionFailure(Sema &S, |
| 4172 | Sema::TemplateDeductionResult TDK, |
| 4173 | TemplateDeductionInfo &Info, |
| 4174 | ArrayRef<SourceRange> Ranges) { |
| 4175 | switch (TDK) { |
| 4176 | case Sema::TDK_Inconsistent: { |
| 4177 | // Inconsistent deduction means we were deducing from an initializer list. |
| 4178 | auto D = S.Diag(Info.getLocation(), diag::err_auto_inconsistent_deduction); |
| 4179 | D << Info.FirstArg << Info.SecondArg; |
| 4180 | for (auto R : Ranges) |
| 4181 | D << R; |
| 4182 | return true; |
| 4183 | } |
| 4184 | |
| 4185 | // FIXME: Are there other cases for which a custom diagnostic is more useful |
| 4186 | // than the basic "types don't match" diagnostic? |
| 4187 | |
| 4188 | default: |
| 4189 | return false; |
| 4190 | } |
| 4191 | } |
| 4192 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4193 | /// \brief 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] | 4194 | /// |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4195 | /// Note that this is done even if the initializer is dependent. (This is |
| 4196 | /// necessary to support partial ordering of templates using 'auto'.) |
| 4197 | /// A dependent type will be produced when deducing from a dependent type. |
| 4198 | /// |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4199 | /// \param Type the type pattern using the auto type-specifier. |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4200 | /// \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] | 4201 | /// \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] | 4202 | /// deduced type. |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4203 | /// \param DependentDeductionDepth Set if we should permit deduction in |
| 4204 | /// dependent cases. This is necessary for template partial ordering with |
| 4205 | /// 'auto' template parameters. The value specified is the template |
| 4206 | /// parameter depth at which we should perform 'auto' deduction. |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4207 | Sema::DeduceAutoResult |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4208 | Sema::DeduceAutoType(TypeLoc Type, Expr *&Init, QualType &Result, |
| 4209 | Optional<unsigned> DependentDeductionDepth) { |
John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4210 | if (Init->getType()->isNonOverloadPlaceholderType()) { |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4211 | ExprResult NonPlaceholder = CheckPlaceholderExpr(Init); |
| 4212 | if (NonPlaceholder.isInvalid()) |
| 4213 | return DAR_FailedAlreadyDiagnosed; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4214 | Init = NonPlaceholder.get(); |
John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4215 | } |
| 4216 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4217 | if (!DependentDeductionDepth && |
| 4218 | (Type.getType()->isDependentType() || Init->isTypeDependent())) { |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4219 | Result = SubstituteDeducedTypeTransform(*this, QualType()).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4220 | assert(!Result.isNull() && "substituting DependentTy can't fail"); |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4221 | return DAR_Succeeded; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4222 | } |
| 4223 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4224 | // Find the depth of template parameter to synthesize. |
| 4225 | unsigned Depth = DependentDeductionDepth.getValueOr(0); |
| 4226 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4227 | // If this is a 'decltype(auto)' specifier, do the decltype dance. |
| 4228 | // Since 'decltype(auto)' can only occur at the top of the type, we |
| 4229 | // don't need to go digging for it. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4230 | if (const AutoType *AT = Type.getType()->getAs<AutoType>()) { |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4231 | if (AT->isDecltypeAuto()) { |
| 4232 | if (isa<InitListExpr>(Init)) { |
| 4233 | Diag(Init->getLocStart(), diag::err_decltype_auto_initializer_list); |
| 4234 | return DAR_FailedAlreadyDiagnosed; |
| 4235 | } |
| 4236 | |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 4237 | QualType Deduced = BuildDecltypeType(Init, Init->getLocStart(), false); |
David Majnemer | 3c20ab2 | 2015-07-01 00:29:28 +0000 | [diff] [blame] | 4238 | if (Deduced.isNull()) |
| 4239 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4240 | // FIXME: Support a non-canonical deduced type for 'auto'. |
| 4241 | Deduced = Context.getCanonicalType(Deduced); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4242 | Result = SubstituteDeducedTypeTransform(*this, Deduced).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4243 | if (Result.isNull()) |
| 4244 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4245 | return DAR_Succeeded; |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4246 | } else if (!getLangOpts().CPlusPlus) { |
| 4247 | if (isa<InitListExpr>(Init)) { |
| 4248 | Diag(Init->getLocStart(), diag::err_auto_init_list_from_c); |
| 4249 | return DAR_FailedAlreadyDiagnosed; |
| 4250 | } |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4251 | } |
| 4252 | } |
| 4253 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4254 | SourceLocation Loc = Init->getExprLoc(); |
| 4255 | |
| 4256 | LocalInstantiationScope InstScope(*this); |
| 4257 | |
| 4258 | // Build template<class TemplParam> void Func(FuncParam); |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4259 | TemplateTypeParmDecl *TemplParam = TemplateTypeParmDecl::Create( |
| 4260 | Context, nullptr, SourceLocation(), Loc, Depth, 0, nullptr, false, false); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4261 | QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0); |
| 4262 | NamedDecl *TemplParamPtr = TemplParam; |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 4263 | FixedSizeTemplateParameterListStorage<1, false> TemplateParamsSt( |
| 4264 | Loc, Loc, TemplParamPtr, Loc, nullptr); |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 4265 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4266 | QualType FuncParam = |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4267 | SubstituteDeducedTypeTransform(*this, TemplArg, /*UseTypeSugar*/false) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4268 | .Apply(Type); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4269 | assert(!FuncParam.isNull() && |
| 4270 | "substituting template parameter for 'auto' failed"); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4271 | |
| 4272 | // Deduce type of TemplParam in Func(Init) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4273 | SmallVector<DeducedTemplateArgument, 1> Deduced; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4274 | Deduced.resize(1); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4275 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4276 | TemplateDeductionInfo Info(Loc, Depth); |
| 4277 | |
| 4278 | // If deduction failed, don't diagnose if the initializer is dependent; it |
| 4279 | // might acquire a matching type in the instantiation. |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4280 | auto DeductionFailed = [&](TemplateDeductionResult TDK, |
| 4281 | ArrayRef<SourceRange> Ranges) -> DeduceAutoResult { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4282 | if (Init->isTypeDependent()) { |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4283 | Result = SubstituteDeducedTypeTransform(*this, QualType()).Apply(Type); |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4284 | assert(!Result.isNull() && "substituting DependentTy can't fail"); |
| 4285 | return DAR_Succeeded; |
| 4286 | } |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4287 | if (diagnoseAutoDeductionFailure(*this, TDK, Info, Ranges)) |
| 4288 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4289 | return DAR_Failed; |
| 4290 | }; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4291 | |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 4292 | SmallVector<OriginalCallArg, 4> OriginalCallArgs; |
| 4293 | |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 4294 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4295 | if (InitList) { |
Richard Smith | c8a32e5 | 2017-01-05 23:12:16 +0000 | [diff] [blame] | 4296 | // Notionally, we substitute std::initializer_list<T> for 'auto' and deduce |
| 4297 | // against that. Such deduction only succeeds if removing cv-qualifiers and |
| 4298 | // references results in std::initializer_list<T>. |
| 4299 | if (!Type.getType().getNonReferenceType()->getAs<AutoType>()) |
| 4300 | return DAR_Failed; |
| 4301 | |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4302 | SourceRange DeducedFromInitRange; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4303 | for (unsigned i = 0, e = InitList->getNumInits(); i < e; ++i) { |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4304 | Expr *Init = InitList->getInit(i); |
| 4305 | |
| 4306 | if (auto TDK = DeduceTemplateArgumentsFromCallArgument( |
| 4307 | *this, TemplateParamsSt.get(), 0, TemplArg, Init, |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 4308 | Info, Deduced, OriginalCallArgs, /*Decomposed*/ true, |
| 4309 | /*ArgIdx*/ 0, /*TDF*/ 0)) |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4310 | return DeductionFailed(TDK, {DeducedFromInitRange, |
| 4311 | Init->getSourceRange()}); |
| 4312 | |
| 4313 | if (DeducedFromInitRange.isInvalid() && |
| 4314 | Deduced[0].getKind() != TemplateArgument::Null) |
| 4315 | DeducedFromInitRange = Init->getSourceRange(); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4316 | } |
| 4317 | } else { |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4318 | if (!getLangOpts().CPlusPlus && Init->refersToBitField()) { |
| 4319 | Diag(Loc, diag::err_auto_bitfield); |
| 4320 | return DAR_FailedAlreadyDiagnosed; |
| 4321 | } |
| 4322 | |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4323 | if (auto TDK = DeduceTemplateArgumentsFromCallArgument( |
Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 4324 | *this, TemplateParamsSt.get(), 0, FuncParam, Init, Info, Deduced, |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 4325 | OriginalCallArgs, /*Decomposed*/ false, /*ArgIdx*/ 0, /*TDF*/ 0)) |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4326 | return DeductionFailed(TDK, {}); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4327 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4328 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4329 | // Could be null if somehow 'auto' appears in a non-deduced context. |
Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4330 | if (Deduced[0].getKind() != TemplateArgument::Type) |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4331 | return DeductionFailed(TDK_Incomplete, {}); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4332 | |
Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4333 | QualType DeducedType = Deduced[0].getAsType(); |
| 4334 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4335 | if (InitList) { |
| 4336 | DeducedType = BuildStdInitializerList(DeducedType, Loc); |
| 4337 | if (DeducedType.isNull()) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4338 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4339 | } |
| 4340 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4341 | Result = SubstituteDeducedTypeTransform(*this, DeducedType).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4342 | if (Result.isNull()) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4343 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4344 | |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4345 | // Check that the deduced argument type is compatible with the original |
| 4346 | // argument type per C++ [temp.deduct.call]p4. |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 4347 | QualType DeducedA = InitList ? Deduced[0].getAsType() : Result; |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 4348 | for (const OriginalCallArg &OriginalArg : OriginalCallArgs) { |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 4349 | assert((bool)InitList == OriginalArg.DecomposedParam && |
| 4350 | "decomposed non-init-list in auto deduction?"); |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4351 | if (auto TDK = |
| 4352 | CheckOriginalCallArgDeduction(*this, Info, OriginalArg, DeducedA)) { |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 4353 | Result = QualType(); |
Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4354 | return DeductionFailed(TDK, {}); |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 4355 | } |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4356 | } |
| 4357 | |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4358 | return DAR_Succeeded; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4359 | } |
| 4360 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 4361 | QualType Sema::SubstAutoType(QualType TypeWithAuto, |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4362 | QualType TypeToReplaceAuto) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4363 | if (TypeToReplaceAuto->isDependentType()) |
| 4364 | TypeToReplaceAuto = QualType(); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4365 | return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4366 | .TransformType(TypeWithAuto); |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4367 | } |
| 4368 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4369 | TypeSourceInfo *Sema::SubstAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto, |
| 4370 | QualType TypeToReplaceAuto) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4371 | if (TypeToReplaceAuto->isDependentType()) |
| 4372 | TypeToReplaceAuto = QualType(); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4373 | return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4374 | .TransformType(TypeWithAuto); |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 4375 | } |
| 4376 | |
Richard Smith | 33c33c3 | 2017-02-04 01:28:01 +0000 | [diff] [blame] | 4377 | QualType Sema::ReplaceAutoType(QualType TypeWithAuto, |
| 4378 | QualType TypeToReplaceAuto) { |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4379 | return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto, |
| 4380 | /*UseTypeSugar*/ false) |
Richard Smith | 33c33c3 | 2017-02-04 01:28:01 +0000 | [diff] [blame] | 4381 | .TransformType(TypeWithAuto); |
| 4382 | } |
| 4383 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4384 | void Sema::DiagnoseAutoDeductionFailure(VarDecl *VDecl, Expr *Init) { |
| 4385 | if (isa<InitListExpr>(Init)) |
| 4386 | Diag(VDecl->getLocation(), |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4387 | VDecl->isInitCapture() |
| 4388 | ? diag::err_init_capture_deduction_failure_from_init_list |
| 4389 | : diag::err_auto_var_deduction_failure_from_init_list) |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4390 | << VDecl->getDeclName() << VDecl->getType() << Init->getSourceRange(); |
| 4391 | else |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4392 | Diag(VDecl->getLocation(), |
| 4393 | VDecl->isInitCapture() ? diag::err_init_capture_deduction_failure |
| 4394 | : diag::err_auto_var_deduction_failure) |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4395 | << VDecl->getDeclName() << VDecl->getType() << Init->getType() |
| 4396 | << Init->getSourceRange(); |
| 4397 | } |
| 4398 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4399 | bool Sema::DeduceReturnType(FunctionDecl *FD, SourceLocation Loc, |
| 4400 | bool Diagnose) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4401 | assert(FD->getReturnType()->isUndeducedType()); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4402 | |
Richard Smith | 50e291e | 2018-01-02 23:52:42 +0000 | [diff] [blame] | 4403 | // For a lambda's conversion operator, deduce any 'auto' or 'decltype(auto)' |
| 4404 | // within the return type from the call operator's type. |
| 4405 | if (isLambdaConversionOperator(FD)) { |
| 4406 | CXXRecordDecl *Lambda = cast<CXXMethodDecl>(FD)->getParent(); |
| 4407 | FunctionDecl *CallOp = Lambda->getLambdaCallOperator(); |
| 4408 | |
| 4409 | // For a generic lambda, instantiate the call operator if needed. |
| 4410 | if (auto *Args = FD->getTemplateSpecializationArgs()) { |
| 4411 | CallOp = InstantiateFunctionDeclaration( |
| 4412 | CallOp->getDescribedFunctionTemplate(), Args, Loc); |
| 4413 | if (!CallOp || CallOp->isInvalidDecl()) |
| 4414 | return true; |
| 4415 | |
| 4416 | // We might need to deduce the return type by instantiating the definition |
| 4417 | // of the operator() function. |
| 4418 | if (CallOp->getReturnType()->isUndeducedType()) |
| 4419 | InstantiateFunctionDefinition(Loc, CallOp); |
| 4420 | } |
| 4421 | |
| 4422 | if (CallOp->isInvalidDecl()) |
| 4423 | return true; |
| 4424 | assert(!CallOp->getReturnType()->isUndeducedType() && |
| 4425 | "failed to deduce lambda return type"); |
| 4426 | |
| 4427 | // Build the new return type from scratch. |
| 4428 | QualType RetType = getLambdaConversionFunctionResultType( |
| 4429 | CallOp->getType()->castAs<FunctionProtoType>()); |
| 4430 | if (FD->getReturnType()->getAs<PointerType>()) |
| 4431 | RetType = Context.getPointerType(RetType); |
| 4432 | else { |
| 4433 | assert(FD->getReturnType()->getAs<BlockPointerType>()); |
| 4434 | RetType = Context.getBlockPointerType(RetType); |
| 4435 | } |
| 4436 | Context.adjustDeducedFunctionResultType(FD, RetType); |
| 4437 | return false; |
| 4438 | } |
| 4439 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4440 | if (FD->getTemplateInstantiationPattern()) |
| 4441 | InstantiateFunctionDefinition(Loc, FD); |
| 4442 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4443 | bool StillUndeduced = FD->getReturnType()->isUndeducedType(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4444 | if (StillUndeduced && Diagnose && !FD->isInvalidDecl()) { |
| 4445 | Diag(Loc, diag::err_auto_fn_used_before_defined) << FD; |
| 4446 | Diag(FD->getLocation(), diag::note_callee_decl) << FD; |
| 4447 | } |
| 4448 | |
| 4449 | return StillUndeduced; |
| 4450 | } |
| 4451 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4452 | /// \brief If this is a non-static member function, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 4453 | static void |
| 4454 | AddImplicitObjectParameterType(ASTContext &Context, |
| 4455 | CXXMethodDecl *Method, |
| 4456 | SmallVectorImpl<QualType> &ArgTypes) { |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4457 | // C++11 [temp.func.order]p3: |
| 4458 | // [...] The new parameter is of type "reference to cv A," where cv are |
| 4459 | // the cv-qualifiers of the function template (if any) and A is |
| 4460 | // the class of which the function template is a member. |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4461 | // |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4462 | // The standard doesn't say explicitly, but we pick the appropriate kind of |
| 4463 | // reference type based on [over.match.funcs]p4. |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4464 | QualType ArgTy = Context.getTypeDeclType(Method->getParent()); |
| 4465 | ArgTy = Context.getQualifiedType(ArgTy, |
| 4466 | Qualifiers::fromCVRMask(Method->getTypeQualifiers())); |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4467 | if (Method->getRefQualifier() == RQ_RValue) |
| 4468 | ArgTy = Context.getRValueReferenceType(ArgTy); |
| 4469 | else |
| 4470 | ArgTy = Context.getLValueReferenceType(ArgTy); |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4471 | ArgTypes.push_back(ArgTy); |
| 4472 | } |
| 4473 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4474 | /// \brief Determine whether the function template \p FT1 is at least as |
| 4475 | /// specialized as \p FT2. |
| 4476 | static bool isAtLeastAsSpecializedAs(Sema &S, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4477 | SourceLocation Loc, |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4478 | FunctionTemplateDecl *FT1, |
| 4479 | FunctionTemplateDecl *FT2, |
| 4480 | TemplatePartialOrderingContext TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4481 | unsigned NumCallArguments1) { |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4482 | FunctionDecl *FD1 = FT1->getTemplatedDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4483 | FunctionDecl *FD2 = FT2->getTemplatedDecl(); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4484 | const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); |
| 4485 | const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4486 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4487 | assert(Proto1 && Proto2 && "Function templates must have prototypes"); |
| 4488 | TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4489 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4490 | Deduced.resize(TemplateParams->size()); |
| 4491 | |
| 4492 | // C++0x [temp.deduct.partial]p3: |
| 4493 | // The types used to determine the ordering depend on the context in which |
| 4494 | // the partial ordering is done: |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4495 | TemplateDeductionInfo Info(Loc); |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4496 | SmallVector<QualType, 4> Args2; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4497 | switch (TPOC) { |
| 4498 | case TPOC_Call: { |
| 4499 | // - In the context of a function call, the function parameter types are |
| 4500 | // used. |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4501 | CXXMethodDecl *Method1 = dyn_cast<CXXMethodDecl>(FD1); |
| 4502 | CXXMethodDecl *Method2 = dyn_cast<CXXMethodDecl>(FD2); |
Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4503 | |
Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4504 | // C++11 [temp.func.order]p3: |
Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4505 | // [...] If only one of the function templates is a non-static |
| 4506 | // member, that function template is considered to have a new |
| 4507 | // first parameter inserted in its function parameter list. The |
| 4508 | // new parameter is of type "reference to cv A," where cv are |
| 4509 | // the cv-qualifiers of the function template (if any) and A is |
| 4510 | // the class of which the function template is a member. |
| 4511 | // |
Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4512 | // Note that we interpret this to mean "if one of the function |
| 4513 | // templates is a non-static member and the other is a non-member"; |
| 4514 | // otherwise, the ordering rules for static functions against non-static |
| 4515 | // functions don't make any sense. |
| 4516 | // |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4517 | // C++98/03 doesn't have this provision but we've extended DR532 to cover |
| 4518 | // it as wording was broken prior to it. |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4519 | SmallVector<QualType, 4> Args1; |
| 4520 | |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4521 | unsigned NumComparedArguments = NumCallArguments1; |
| 4522 | |
| 4523 | if (!Method2 && Method1 && !Method1->isStatic()) { |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4524 | // Compare 'this' from Method1 against first parameter from Method2. |
| 4525 | AddImplicitObjectParameterType(S.Context, Method1, Args1); |
| 4526 | ++NumComparedArguments; |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4527 | } else if (!Method1 && Method2 && !Method2->isStatic()) { |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4528 | // Compare 'this' from Method2 against first parameter from Method1. |
| 4529 | AddImplicitObjectParameterType(S.Context, Method2, Args2); |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4530 | } |
| 4531 | |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4532 | Args1.insert(Args1.end(), Proto1->param_type_begin(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4533 | Proto1->param_type_end()); |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4534 | Args2.insert(Args2.end(), Proto2->param_type_begin(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4535 | Proto2->param_type_end()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4536 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4537 | // C++ [temp.func.order]p5: |
| 4538 | // The presence of unused ellipsis and default arguments has no effect on |
| 4539 | // the partial ordering of function templates. |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4540 | if (Args1.size() > NumComparedArguments) |
| 4541 | Args1.resize(NumComparedArguments); |
| 4542 | if (Args2.size() > NumComparedArguments) |
| 4543 | Args2.resize(NumComparedArguments); |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4544 | if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(), |
| 4545 | Args1.data(), Args1.size(), Info, Deduced, |
| 4546 | TDF_None, /*PartialOrdering=*/true)) |
| 4547 | return false; |
| 4548 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4549 | break; |
| 4550 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4551 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4552 | case TPOC_Conversion: |
| 4553 | // - In the context of a call to a conversion operator, the return types |
| 4554 | // of the conversion function templates are used. |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4555 | if (DeduceTemplateArgumentsByTypeMatch( |
| 4556 | S, TemplateParams, Proto2->getReturnType(), Proto1->getReturnType(), |
| 4557 | Info, Deduced, TDF_None, |
| 4558 | /*PartialOrdering=*/true)) |
| 4559 | return false; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4560 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4561 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4562 | case TPOC_Other: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4563 | // - 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] | 4564 | // is used. |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4565 | if (DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 4566 | FD2->getType(), FD1->getType(), |
| 4567 | Info, Deduced, TDF_None, |
| 4568 | /*PartialOrdering=*/true)) |
| 4569 | return false; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4570 | break; |
| 4571 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4572 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4573 | // C++0x [temp.deduct.partial]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4574 | // In most cases, all template parameters must have values in order for |
| 4575 | // deduction to succeed, but for partial ordering purposes a template |
| 4576 | // 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] | 4577 | // types being used for partial ordering. [ Note: a template parameter used |
| 4578 | // in a non-deduced context is considered used. -end note] |
| 4579 | unsigned ArgIdx = 0, NumArgs = Deduced.size(); |
| 4580 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4581 | if (Deduced[ArgIdx].isNull()) |
| 4582 | break; |
| 4583 | |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4584 | // FIXME: We fail to implement [temp.deduct.type]p1 along this path. We need |
| 4585 | // to substitute the deduced arguments back into the template and check that |
| 4586 | // we get the right type. |
Richard Smith | cf82486 | 2016-12-30 04:32:02 +0000 | [diff] [blame] | 4587 | |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4588 | if (ArgIdx == NumArgs) { |
| 4589 | // All template arguments were deduced. FT1 is at least as specialized |
| 4590 | // as FT2. |
| 4591 | return true; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4592 | } |
| 4593 | |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4594 | // Figure out which template parameters were used. |
| 4595 | llvm::SmallBitVector UsedParameters(TemplateParams->size()); |
| 4596 | switch (TPOC) { |
| 4597 | case TPOC_Call: |
| 4598 | for (unsigned I = 0, N = Args2.size(); I != N; ++I) |
| 4599 | ::MarkUsedTemplateParameters(S.Context, Args2[I], false, |
| 4600 | TemplateParams->getDepth(), |
| 4601 | UsedParameters); |
| 4602 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4603 | |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4604 | case TPOC_Conversion: |
| 4605 | ::MarkUsedTemplateParameters(S.Context, Proto2->getReturnType(), false, |
| 4606 | TemplateParams->getDepth(), UsedParameters); |
| 4607 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4608 | |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4609 | case TPOC_Other: |
| 4610 | ::MarkUsedTemplateParameters(S.Context, FD2->getType(), false, |
| 4611 | TemplateParams->getDepth(), |
| 4612 | UsedParameters); |
| 4613 | break; |
Richard Smith | 86a1b13 | 2017-02-16 03:49:44 +0000 | [diff] [blame] | 4614 | } |
| 4615 | |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4616 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4617 | // If this argument had no value deduced but was used in one of the types |
| 4618 | // used for partial ordering, then deduction fails. |
| 4619 | if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) |
| 4620 | return false; |
| 4621 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4622 | return true; |
| 4623 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4624 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4625 | /// \brief Determine whether this a function template whose parameter-type-list |
| 4626 | /// ends with a function parameter pack. |
| 4627 | static bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) { |
| 4628 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
| 4629 | unsigned NumParams = Function->getNumParams(); |
| 4630 | if (NumParams == 0) |
| 4631 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4632 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4633 | ParmVarDecl *Last = Function->getParamDecl(NumParams - 1); |
| 4634 | if (!Last->isParameterPack()) |
| 4635 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4636 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4637 | // Make sure that no previous parameter is a parameter pack. |
| 4638 | while (--NumParams > 0) { |
| 4639 | if (Function->getParamDecl(NumParams - 1)->isParameterPack()) |
| 4640 | return false; |
| 4641 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4642 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4643 | return true; |
| 4644 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4645 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4646 | /// \brief Returns the more specialized function template according |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4647 | /// to the rules of function template partial ordering (C++ [temp.func.order]). |
| 4648 | /// |
| 4649 | /// \param FT1 the first function template |
| 4650 | /// |
| 4651 | /// \param FT2 the second function template |
| 4652 | /// |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4653 | /// \param TPOC the context in which we are performing partial ordering of |
| 4654 | /// function templates. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4655 | /// |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4656 | /// \param NumCallArguments1 The number of arguments in the call to FT1, used |
| 4657 | /// only when \c TPOC is \c TPOC_Call. |
| 4658 | /// |
| 4659 | /// \param NumCallArguments2 The number of arguments in the call to FT2, used |
| 4660 | /// only when \c TPOC is \c TPOC_Call. |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4661 | /// |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4662 | /// \returns the more specialized function template. If neither |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4663 | /// template is more specialized, returns NULL. |
| 4664 | FunctionTemplateDecl * |
| 4665 | Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, |
| 4666 | FunctionTemplateDecl *FT2, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4667 | SourceLocation Loc, |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4668 | TemplatePartialOrderingContext TPOC, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4669 | unsigned NumCallArguments1, |
| 4670 | unsigned NumCallArguments2) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4671 | bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4672 | NumCallArguments1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4673 | bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4674 | NumCallArguments2); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4675 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4676 | if (Better1 != Better2) // We have a clear winner |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4677 | return Better1 ? FT1 : FT2; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4678 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4679 | if (!Better1 && !Better2) // Neither is better than the other |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4680 | return nullptr; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4681 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4682 | // FIXME: This mimics what GCC implements, but doesn't match up with the |
| 4683 | // proposed resolution for core issue 692. This area needs to be sorted out, |
| 4684 | // but for now we attempt to maintain compatibility. |
| 4685 | bool Variadic1 = isVariadicFunctionTemplate(FT1); |
| 4686 | bool Variadic2 = isVariadicFunctionTemplate(FT2); |
| 4687 | if (Variadic1 != Variadic2) |
| 4688 | return Variadic1? FT2 : FT1; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4689 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4690 | return nullptr; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4691 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4692 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4693 | /// \brief Determine if the two templates are equivalent. |
| 4694 | static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) { |
| 4695 | if (T1 == T2) |
| 4696 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4697 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4698 | if (!T1 || !T2) |
| 4699 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4700 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4701 | return T1->getCanonicalDecl() == T2->getCanonicalDecl(); |
| 4702 | } |
| 4703 | |
| 4704 | /// \brief Retrieve the most specialized of the given function template |
| 4705 | /// specializations. |
| 4706 | /// |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4707 | /// \param SpecBegin the start iterator of the function template |
| 4708 | /// specializations that we will be comparing. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4709 | /// |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4710 | /// \param SpecEnd the end iterator of the function template |
| 4711 | /// specializations, paired with \p SpecBegin. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4712 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4713 | /// \param Loc the location where the ambiguity or no-specializations |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4714 | /// diagnostic should occur. |
| 4715 | /// |
| 4716 | /// \param NoneDiag partial diagnostic used to diagnose cases where there are |
| 4717 | /// no matching candidates. |
| 4718 | /// |
| 4719 | /// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one |
| 4720 | /// occurs. |
| 4721 | /// |
| 4722 | /// \param CandidateDiag partial diagnostic used for each function template |
| 4723 | /// specialization that is a candidate in the ambiguous ordering. One parameter |
| 4724 | /// in this diagnostic should be unbound, which will correspond to the string |
| 4725 | /// describing the template arguments for the function template specialization. |
| 4726 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4727 | /// \returns the most specialized function template specialization, if |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4728 | /// found. Otherwise, returns SpecEnd. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4729 | UnresolvedSetIterator Sema::getMostSpecialized( |
| 4730 | UnresolvedSetIterator SpecBegin, UnresolvedSetIterator SpecEnd, |
| 4731 | TemplateSpecCandidateSet &FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4732 | SourceLocation Loc, const PartialDiagnostic &NoneDiag, |
| 4733 | const PartialDiagnostic &AmbigDiag, const PartialDiagnostic &CandidateDiag, |
| 4734 | bool Complain, QualType TargetType) { |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4735 | if (SpecBegin == SpecEnd) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4736 | if (Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4737 | Diag(Loc, NoneDiag); |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4738 | FailedCandidates.NoteCandidates(*this, Loc); |
| 4739 | } |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4740 | return SpecEnd; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4741 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4742 | |
| 4743 | if (SpecBegin + 1 == SpecEnd) |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4744 | return SpecBegin; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4745 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4746 | // Find the function template that is better than all of the templates it |
| 4747 | // has been compared to. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4748 | UnresolvedSetIterator Best = SpecBegin; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4749 | FunctionTemplateDecl *BestTemplate |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4750 | = cast<FunctionDecl>(*Best)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4751 | assert(BestTemplate && "Not a function template specialization?"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4752 | for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) { |
| 4753 | FunctionTemplateDecl *Challenger |
| 4754 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4755 | assert(Challenger && "Not a function template specialization?"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4756 | if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4757 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4758 | Challenger)) { |
| 4759 | Best = I; |
| 4760 | BestTemplate = Challenger; |
| 4761 | } |
| 4762 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4763 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4764 | // Make sure that the "best" function template is more specialized than all |
| 4765 | // of the others. |
| 4766 | bool Ambiguous = false; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4767 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4768 | FunctionTemplateDecl *Challenger |
| 4769 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4770 | if (I != Best && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4771 | !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4772 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4773 | BestTemplate)) { |
| 4774 | Ambiguous = true; |
| 4775 | break; |
| 4776 | } |
| 4777 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4778 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4779 | if (!Ambiguous) { |
| 4780 | // We found an answer. Return it. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4781 | return Best; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4782 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4783 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4784 | // Diagnose the ambiguity. |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4785 | if (Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4786 | Diag(Loc, AmbigDiag); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4787 | |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4788 | // FIXME: Can we order the candidates in some sane way? |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4789 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4790 | PartialDiagnostic PD = CandidateDiag; |
Saleem Abdulrasool | 78704fb | 2016-12-22 04:26:57 +0000 | [diff] [blame] | 4791 | const auto *FD = cast<FunctionDecl>(*I); |
| 4792 | PD << FD << getTemplateArgumentBindingsText( |
| 4793 | FD->getPrimaryTemplate()->getTemplateParameters(), |
| 4794 | *FD->getTemplateSpecializationArgs()); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4795 | if (!TargetType.isNull()) |
Saleem Abdulrasool | 78704fb | 2016-12-22 04:26:57 +0000 | [diff] [blame] | 4796 | HandleFunctionTypeMismatch(PD, FD->getType(), TargetType); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4797 | Diag((*I)->getLocation(), PD); |
| 4798 | } |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4799 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4800 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4801 | return SpecEnd; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4802 | } |
| 4803 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4804 | /// Determine whether one partial specialization, P1, is at least as |
| 4805 | /// specialized than another, P2. |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4806 | /// |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 4807 | /// \tparam TemplateLikeDecl The kind of P2, which must be a |
| 4808 | /// TemplateDecl or {Class,Var}TemplatePartialSpecializationDecl. |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4809 | /// \param T1 The injected-class-name of P1 (faked for a variable template). |
| 4810 | /// \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] | 4811 | template<typename TemplateLikeDecl> |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4812 | static bool isAtLeastAsSpecializedAs(Sema &S, QualType T1, QualType T2, |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 4813 | TemplateLikeDecl *P2, |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 4814 | TemplateDeductionInfo &Info) { |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4815 | // C++ [temp.class.order]p1: |
| 4816 | // For two class template partial specializations, the first is at least as |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4817 | // specialized as the second if, given the following rewrite to two |
| 4818 | // function templates, the first function template is at least as |
| 4819 | // specialized as the second according to the ordering rules for function |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4820 | // templates (14.6.6.2): |
| 4821 | // - the first function template has the same template parameters as the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4822 | // first partial specialization and has a single function parameter |
| 4823 | // whose type is a class template specialization with the template |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4824 | // arguments of the first partial specialization, and |
| 4825 | // - the second function template has the same template parameters as the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4826 | // second partial specialization and has a single function parameter |
| 4827 | // whose type is a class template specialization with the template |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4828 | // arguments of the second partial specialization. |
| 4829 | // |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 4830 | // Rather than synthesize function templates, we merely perform the |
| 4831 | // equivalent partial ordering by performing deduction directly on |
| 4832 | // the template arguments of the class template partial |
| 4833 | // specializations. This computation is slightly simpler than the |
| 4834 | // general problem of function template partial ordering, because |
| 4835 | // class template partial specializations are more constrained. We |
| 4836 | // know that every template parameter is deducible from the class |
| 4837 | // template partial specialization's template arguments, for |
| 4838 | // example. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4839 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4840 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4841 | // Determine whether P1 is at least as specialized as P2. |
| 4842 | Deduced.resize(P2->getTemplateParameters()->size()); |
| 4843 | if (DeduceTemplateArgumentsByTypeMatch(S, P2->getTemplateParameters(), |
| 4844 | T2, T1, Info, Deduced, TDF_None, |
| 4845 | /*PartialOrdering=*/true)) |
| 4846 | return false; |
| 4847 | |
| 4848 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), |
| 4849 | Deduced.end()); |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 4850 | Sema::InstantiatingTemplate Inst(S, Info.getLocation(), P2, DeducedArgs, |
| 4851 | Info); |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4852 | auto *TST1 = T1->castAs<TemplateSpecializationType>(); |
| 4853 | if (FinishTemplateArgumentDeduction( |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4854 | S, P2, /*PartialOrdering=*/true, |
| 4855 | TemplateArgumentList(TemplateArgumentList::OnStack, |
| 4856 | TST1->template_arguments()), |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4857 | Deduced, Info)) |
| 4858 | return false; |
| 4859 | |
| 4860 | return true; |
| 4861 | } |
| 4862 | |
| 4863 | /// \brief Returns the more specialized class template partial specialization |
| 4864 | /// according to the rules of partial ordering of class template partial |
| 4865 | /// specializations (C++ [temp.class.order]). |
| 4866 | /// |
| 4867 | /// \param PS1 the first class template partial specialization |
| 4868 | /// |
| 4869 | /// \param PS2 the second class template partial specialization |
| 4870 | /// |
| 4871 | /// \returns the more specialized class template partial specialization. If |
| 4872 | /// neither partial specialization is more specialized, returns NULL. |
| 4873 | ClassTemplatePartialSpecializationDecl * |
| 4874 | Sema::getMoreSpecializedPartialSpecialization( |
| 4875 | ClassTemplatePartialSpecializationDecl *PS1, |
| 4876 | ClassTemplatePartialSpecializationDecl *PS2, |
| 4877 | SourceLocation Loc) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4878 | QualType PT1 = PS1->getInjectedSpecializationType(); |
| 4879 | QualType PT2 = PS2->getInjectedSpecializationType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4880 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 4881 | TemplateDeductionInfo Info(Loc); |
| 4882 | bool Better1 = isAtLeastAsSpecializedAs(*this, PT1, PT2, PS2, Info); |
| 4883 | bool Better2 = isAtLeastAsSpecializedAs(*this, PT2, PT1, PS1, Info); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4884 | |
| 4885 | if (Better1 == Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4886 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4887 | |
| 4888 | return Better1 ? PS1 : PS2; |
| 4889 | } |
| 4890 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 4891 | bool Sema::isMoreSpecializedThanPrimary( |
| 4892 | ClassTemplatePartialSpecializationDecl *Spec, TemplateDeductionInfo &Info) { |
| 4893 | ClassTemplateDecl *Primary = Spec->getSpecializedTemplate(); |
| 4894 | QualType PrimaryT = Primary->getInjectedClassNameSpecialization(); |
| 4895 | QualType PartialT = Spec->getInjectedSpecializationType(); |
| 4896 | if (!isAtLeastAsSpecializedAs(*this, PartialT, PrimaryT, Primary, Info)) |
| 4897 | return false; |
| 4898 | if (isAtLeastAsSpecializedAs(*this, PrimaryT, PartialT, Spec, Info)) { |
| 4899 | Info.clearSFINAEDiagnostic(); |
| 4900 | return false; |
| 4901 | } |
| 4902 | return true; |
| 4903 | } |
| 4904 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4905 | VarTemplatePartialSpecializationDecl * |
| 4906 | Sema::getMoreSpecializedPartialSpecialization( |
| 4907 | VarTemplatePartialSpecializationDecl *PS1, |
| 4908 | VarTemplatePartialSpecializationDecl *PS2, SourceLocation Loc) { |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4909 | // Pretend the variable template specializations are class template |
| 4910 | // specializations and form a fake injected class name type for comparison. |
Richard Smith | f04fd0b | 2013-12-12 23:14:16 +0000 | [diff] [blame] | 4911 | assert(PS1->getSpecializedTemplate() == PS2->getSpecializedTemplate() && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4912 | "the partial specializations being compared should specialize" |
| 4913 | " the same template."); |
| 4914 | TemplateName Name(PS1->getSpecializedTemplate()); |
| 4915 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4916 | QualType PT1 = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4917 | CanonTemplate, PS1->getTemplateArgs().asArray()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4918 | QualType PT2 = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4919 | CanonTemplate, PS2->getTemplateArgs().asArray()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4920 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 4921 | TemplateDeductionInfo Info(Loc); |
| 4922 | bool Better1 = isAtLeastAsSpecializedAs(*this, PT1, PT2, PS2, Info); |
| 4923 | bool Better2 = isAtLeastAsSpecializedAs(*this, PT2, PT1, PS1, Info); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4924 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4925 | if (Better1 == Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4926 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4927 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4928 | return Better1 ? PS1 : PS2; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4929 | } |
| 4930 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 4931 | bool Sema::isMoreSpecializedThanPrimary( |
| 4932 | VarTemplatePartialSpecializationDecl *Spec, TemplateDeductionInfo &Info) { |
| 4933 | TemplateDecl *Primary = Spec->getSpecializedTemplate(); |
| 4934 | // FIXME: Cache the injected template arguments rather than recomputing |
| 4935 | // them for each partial specialization. |
| 4936 | SmallVector<TemplateArgument, 8> PrimaryArgs; |
| 4937 | Context.getInjectedTemplateArgs(Primary->getTemplateParameters(), |
| 4938 | PrimaryArgs); |
| 4939 | |
| 4940 | TemplateName CanonTemplate = |
| 4941 | Context.getCanonicalTemplateName(TemplateName(Primary)); |
| 4942 | QualType PrimaryT = Context.getTemplateSpecializationType( |
| 4943 | CanonTemplate, PrimaryArgs); |
| 4944 | QualType PartialT = Context.getTemplateSpecializationType( |
| 4945 | CanonTemplate, Spec->getTemplateArgs().asArray()); |
| 4946 | if (!isAtLeastAsSpecializedAs(*this, PartialT, PrimaryT, Primary, Info)) |
| 4947 | return false; |
| 4948 | if (isAtLeastAsSpecializedAs(*this, PrimaryT, PartialT, Spec, Info)) { |
| 4949 | Info.clearSFINAEDiagnostic(); |
| 4950 | return false; |
| 4951 | } |
| 4952 | return true; |
| 4953 | } |
| 4954 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 4955 | bool Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs( |
| 4956 | TemplateParameterList *P, TemplateDecl *AArg, SourceLocation Loc) { |
| 4957 | // C++1z [temp.arg.template]p4: (DR 150) |
| 4958 | // A template template-parameter P is at least as specialized as a |
| 4959 | // template template-argument A if, given the following rewrite to two |
| 4960 | // function templates... |
| 4961 | |
| 4962 | // Rather than synthesize function templates, we merely perform the |
| 4963 | // equivalent partial ordering by performing deduction directly on |
| 4964 | // the template parameter lists of the template template parameters. |
| 4965 | // |
| 4966 | // Given an invented class template X with the template parameter list of |
| 4967 | // A (including default arguments): |
| 4968 | TemplateName X = Context.getCanonicalTemplateName(TemplateName(AArg)); |
| 4969 | TemplateParameterList *A = AArg->getTemplateParameters(); |
| 4970 | |
| 4971 | // - Each function template has a single function parameter whose type is |
| 4972 | // a specialization of X with template arguments corresponding to the |
| 4973 | // template parameters from the respective function template |
| 4974 | SmallVector<TemplateArgument, 8> AArgs; |
| 4975 | Context.getInjectedTemplateArgs(A, AArgs); |
| 4976 | |
| 4977 | // Check P's arguments against A's parameter list. This will fill in default |
| 4978 | // template arguments as needed. AArgs are already correct by construction. |
| 4979 | // We can't just use CheckTemplateIdType because that will expand alias |
| 4980 | // templates. |
| 4981 | SmallVector<TemplateArgument, 4> PArgs; |
| 4982 | { |
| 4983 | SFINAETrap Trap(*this); |
| 4984 | |
| 4985 | Context.getInjectedTemplateArgs(P, PArgs); |
| 4986 | TemplateArgumentListInfo PArgList(P->getLAngleLoc(), P->getRAngleLoc()); |
| 4987 | for (unsigned I = 0, N = P->size(); I != N; ++I) { |
| 4988 | // Unwrap packs that getInjectedTemplateArgs wrapped around pack |
| 4989 | // expansions, to form an "as written" argument list. |
| 4990 | TemplateArgument Arg = PArgs[I]; |
| 4991 | if (Arg.getKind() == TemplateArgument::Pack) { |
| 4992 | assert(Arg.pack_size() == 1 && Arg.pack_begin()->isPackExpansion()); |
| 4993 | Arg = *Arg.pack_begin(); |
| 4994 | } |
| 4995 | PArgList.addArgument(getTrivialTemplateArgumentLoc( |
| 4996 | Arg, QualType(), P->getParam(I)->getLocation())); |
| 4997 | } |
| 4998 | PArgs.clear(); |
| 4999 | |
| 5000 | // C++1z [temp.arg.template]p3: |
| 5001 | // If the rewrite produces an invalid type, then P is not at least as |
| 5002 | // specialized as A. |
| 5003 | if (CheckTemplateArgumentList(AArg, Loc, PArgList, false, PArgs) || |
| 5004 | Trap.hasErrorOccurred()) |
| 5005 | return false; |
| 5006 | } |
| 5007 | |
| 5008 | QualType AType = Context.getTemplateSpecializationType(X, AArgs); |
| 5009 | QualType PType = Context.getTemplateSpecializationType(X, PArgs); |
| 5010 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 5011 | // ... the function template corresponding to P is at least as specialized |
| 5012 | // as the function template corresponding to A according to the partial |
| 5013 | // ordering rules for function templates. |
| 5014 | TemplateDeductionInfo Info(Loc, A->getDepth()); |
| 5015 | return isAtLeastAsSpecializedAs(*this, PType, AType, AArg, Info); |
| 5016 | } |
| 5017 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5018 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5019 | /// expression. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5020 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5021 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5022 | const Expr *E, |
| 5023 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5024 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5025 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 5026 | // We can deduce from a pack expansion. |
| 5027 | if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E)) |
| 5028 | E = Expansion->getPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5029 | |
Richard Smith | 3434900 | 2012-07-09 03:07:20 +0000 | [diff] [blame] | 5030 | // Skip through any implicit casts we added while type-checking, and any |
| 5031 | // substitutions performed by template alias expansion. |
| 5032 | while (1) { |
| 5033 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 5034 | E = ICE->getSubExpr(); |
| 5035 | else if (const SubstNonTypeTemplateParmExpr *Subst = |
| 5036 | dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 5037 | E = Subst->getReplacement(); |
| 5038 | else |
| 5039 | break; |
| 5040 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5041 | |
| 5042 | // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5043 | // find other occurrences of template parameters. |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 5044 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
Douglas Gregor | 04b1152 | 2010-01-14 18:13:22 +0000 | [diff] [blame] | 5045 | if (!DRE) |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5046 | return; |
| 5047 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5048 | const NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5049 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 5050 | if (!NTTP) |
| 5051 | return; |
| 5052 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5053 | if (NTTP->getDepth() == Depth) |
| 5054 | Used[NTTP->getIndex()] = true; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5055 | |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5056 | // 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] | 5057 | // non-type argument. |
Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5058 | if (Ctx.getLangOpts().CPlusPlus17) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5059 | MarkUsedTemplateParameters(Ctx, NTTP->getType(), OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5060 | } |
| 5061 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5062 | /// \brief Mark the template parameters that are used by the given |
| 5063 | /// nested name specifier. |
| 5064 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5065 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5066 | NestedNameSpecifier *NNS, |
| 5067 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5068 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5069 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5070 | if (!NNS) |
| 5071 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5072 | |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5073 | MarkUsedTemplateParameters(Ctx, NNS->getPrefix(), OnlyDeduced, Depth, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5074 | Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5075 | MarkUsedTemplateParameters(Ctx, QualType(NNS->getAsType(), 0), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5076 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5077 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5078 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5079 | /// \brief Mark the template parameters that are used by the given |
| 5080 | /// template name. |
| 5081 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5082 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5083 | TemplateName Name, |
| 5084 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5085 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5086 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5087 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 5088 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5089 | = dyn_cast<TemplateTemplateParmDecl>(Template)) { |
| 5090 | if (TTP->getDepth() == Depth) |
| 5091 | Used[TTP->getIndex()] = true; |
| 5092 | } |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5093 | return; |
| 5094 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5095 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5096 | if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5097 | MarkUsedTemplateParameters(Ctx, QTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5098 | Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5099 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5100 | MarkUsedTemplateParameters(Ctx, DTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5101 | Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5102 | } |
| 5103 | |
| 5104 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5105 | /// type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5106 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5107 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5108 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5109 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5110 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5111 | if (T.isNull()) |
| 5112 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5113 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5114 | // Non-dependent types have nothing deducible |
| 5115 | if (!T->isDependentType()) |
| 5116 | return; |
| 5117 | |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5118 | T = Ctx.getCanonicalType(T); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5119 | switch (T->getTypeClass()) { |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5120 | case Type::Pointer: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5121 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5122 | cast<PointerType>(T)->getPointeeType(), |
| 5123 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5124 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5125 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5126 | break; |
| 5127 | |
| 5128 | case Type::BlockPointer: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5129 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5130 | cast<BlockPointerType>(T)->getPointeeType(), |
| 5131 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5132 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5133 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5134 | break; |
| 5135 | |
| 5136 | case Type::LValueReference: |
| 5137 | case Type::RValueReference: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5138 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5139 | cast<ReferenceType>(T)->getPointeeType(), |
| 5140 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5141 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5142 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5143 | break; |
| 5144 | |
| 5145 | case Type::MemberPointer: { |
| 5146 | const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5147 | MarkUsedTemplateParameters(Ctx, MemPtr->getPointeeType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5148 | Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5149 | MarkUsedTemplateParameters(Ctx, QualType(MemPtr->getClass(), 0), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5150 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5151 | break; |
| 5152 | } |
| 5153 | |
| 5154 | case Type::DependentSizedArray: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5155 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5156 | cast<DependentSizedArrayType>(T)->getSizeExpr(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5157 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5158 | // Fall through to check the element type |
Galina Kistanova | 3339911 | 2017-06-03 06:35:06 +0000 | [diff] [blame] | 5159 | LLVM_FALLTHROUGH; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5160 | |
| 5161 | case Type::ConstantArray: |
| 5162 | case Type::IncompleteArray: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5163 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5164 | cast<ArrayType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5165 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5166 | break; |
| 5167 | |
| 5168 | case Type::Vector: |
| 5169 | case Type::ExtVector: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5170 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5171 | cast<VectorType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5172 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5173 | break; |
| 5174 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5175 | case Type::DependentSizedExtVector: { |
| 5176 | const DependentSizedExtVectorType *VecType |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 5177 | = cast<DependentSizedExtVectorType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5178 | MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5179 | Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5180 | MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5181 | Depth, Used); |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5182 | break; |
| 5183 | } |
| 5184 | |
Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 5185 | case Type::DependentAddressSpace: { |
| 5186 | const DependentAddressSpaceType *DependentASType = |
| 5187 | cast<DependentAddressSpaceType>(T); |
| 5188 | MarkUsedTemplateParameters(Ctx, DependentASType->getPointeeType(), |
| 5189 | OnlyDeduced, Depth, Used); |
| 5190 | MarkUsedTemplateParameters(Ctx, |
| 5191 | DependentASType->getAddrSpaceExpr(), |
| 5192 | OnlyDeduced, Depth, Used); |
| 5193 | break; |
| 5194 | } |
| 5195 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5196 | case Type::FunctionProto: { |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 5197 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5198 | MarkUsedTemplateParameters(Ctx, Proto->getReturnType(), OnlyDeduced, Depth, |
| 5199 | Used); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 5200 | for (unsigned I = 0, N = Proto->getNumParams(); I != N; ++I) |
| 5201 | MarkUsedTemplateParameters(Ctx, Proto->getParamType(I), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5202 | Depth, Used); |
Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 5203 | if (auto *E = Proto->getNoexceptExpr()) |
| 5204 | MarkUsedTemplateParameters(Ctx, E, OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5205 | break; |
| 5206 | } |
| 5207 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5208 | case Type::TemplateTypeParm: { |
| 5209 | const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T); |
| 5210 | if (TTP->getDepth() == Depth) |
| 5211 | Used[TTP->getIndex()] = true; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5212 | break; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5213 | } |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5214 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 5215 | case Type::SubstTemplateTypeParmPack: { |
| 5216 | const SubstTemplateTypeParmPackType *Subst |
| 5217 | = cast<SubstTemplateTypeParmPackType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5218 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 5219 | QualType(Subst->getReplacedParameter(), 0), |
| 5220 | OnlyDeduced, Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5221 | MarkUsedTemplateParameters(Ctx, Subst->getArgumentPack(), |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 5222 | OnlyDeduced, Depth, Used); |
| 5223 | break; |
| 5224 | } |
| 5225 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 5226 | case Type::InjectedClassName: |
| 5227 | T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); |
Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 5228 | LLVM_FALLTHROUGH; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 5229 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5230 | case Type::TemplateSpecialization: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5231 | const TemplateSpecializationType *Spec |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 5232 | = cast<TemplateSpecializationType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5233 | MarkUsedTemplateParameters(Ctx, Spec->getTemplateName(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5234 | Depth, Used); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5235 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5236 | // C++0x [temp.deduct.type]p9: |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 5237 | // If the template argument list of P contains a pack expansion that is |
| 5238 | // not the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5239 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5240 | if (OnlyDeduced && |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 5241 | hasPackExpansionBeforeEnd(Spec->template_arguments())) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5242 | break; |
| 5243 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5244 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5245 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5246 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5247 | break; |
| 5248 | } |
| 5249 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5250 | case Type::Complex: |
| 5251 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5252 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5253 | cast<ComplexType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5254 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5255 | break; |
| 5256 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5257 | case Type::Atomic: |
| 5258 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5259 | MarkUsedTemplateParameters(Ctx, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5260 | cast<AtomicType>(T)->getValueType(), |
| 5261 | OnlyDeduced, Depth, Used); |
| 5262 | break; |
| 5263 | |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5264 | case Type::DependentName: |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5265 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5266 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5267 | cast<DependentNameType>(T)->getQualifier(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5268 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5269 | break; |
| 5270 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5271 | case Type::DependentTemplateSpecialization: { |
Richard Smith | 50d5b97 | 2015-12-30 20:56:05 +0000 | [diff] [blame] | 5272 | // C++14 [temp.deduct.type]p5: |
| 5273 | // The non-deduced contexts are: |
| 5274 | // -- The nested-name-specifier of a type that was specified using a |
| 5275 | // qualified-id |
| 5276 | // |
| 5277 | // C++14 [temp.deduct.type]p6: |
| 5278 | // When a type name is specified in a way that includes a non-deduced |
| 5279 | // context, all of the types that comprise that type name are also |
| 5280 | // non-deduced. |
| 5281 | if (OnlyDeduced) |
| 5282 | break; |
| 5283 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5284 | const DependentTemplateSpecializationType *Spec |
| 5285 | = cast<DependentTemplateSpecializationType>(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5286 | |
Richard Smith | 50d5b97 | 2015-12-30 20:56:05 +0000 | [diff] [blame] | 5287 | MarkUsedTemplateParameters(Ctx, Spec->getQualifier(), |
| 5288 | OnlyDeduced, Depth, Used); |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5289 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5290 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5291 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5292 | Used); |
| 5293 | break; |
| 5294 | } |
| 5295 | |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5296 | case Type::TypeOf: |
| 5297 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5298 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5299 | cast<TypeOfType>(T)->getUnderlyingType(), |
| 5300 | OnlyDeduced, Depth, Used); |
| 5301 | break; |
| 5302 | |
| 5303 | case Type::TypeOfExpr: |
| 5304 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5305 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5306 | cast<TypeOfExprType>(T)->getUnderlyingExpr(), |
| 5307 | OnlyDeduced, Depth, Used); |
| 5308 | break; |
| 5309 | |
| 5310 | case Type::Decltype: |
| 5311 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5312 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5313 | cast<DecltypeType>(T)->getUnderlyingExpr(), |
| 5314 | OnlyDeduced, Depth, Used); |
| 5315 | break; |
| 5316 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 5317 | case Type::UnaryTransform: |
| 5318 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5319 | MarkUsedTemplateParameters(Ctx, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5320 | cast<UnaryTransformType>(T)->getUnderlyingType(), |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 5321 | OnlyDeduced, Depth, Used); |
| 5322 | break; |
| 5323 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 5324 | case Type::PackExpansion: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5325 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 5326 | cast<PackExpansionType>(T)->getPattern(), |
| 5327 | OnlyDeduced, Depth, Used); |
| 5328 | break; |
| 5329 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5330 | case Type::Auto: |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 5331 | case Type::DeducedTemplateSpecialization: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5332 | MarkUsedTemplateParameters(Ctx, |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 5333 | cast<DeducedType>(T)->getDeducedType(), |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5334 | OnlyDeduced, Depth, Used); |
Adrian Prantl | 4b49085 | 2017-12-19 22:21:48 +0000 | [diff] [blame] | 5335 | break; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5336 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5337 | // None of these types have any template parameters in them. |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5338 | case Type::Builtin: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5339 | case Type::VariableArray: |
| 5340 | case Type::FunctionNoProto: |
| 5341 | case Type::Record: |
| 5342 | case Type::Enum: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5343 | case Type::ObjCInterface: |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5344 | case Type::ObjCObject: |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 5345 | case Type::ObjCObjectPointer: |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5346 | case Type::UnresolvedUsing: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5347 | case Type::Pipe: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5348 | #define TYPE(Class, Base) |
| 5349 | #define ABSTRACT_TYPE(Class, Base) |
| 5350 | #define DEPENDENT_TYPE(Class, Base) |
| 5351 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 5352 | #include "clang/AST/TypeNodes.def" |
| 5353 | break; |
| 5354 | } |
| 5355 | } |
| 5356 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5357 | /// \brief Mark the template parameters that are used by this |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5358 | /// template argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5359 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5360 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5361 | const TemplateArgument &TemplateArg, |
| 5362 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5363 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5364 | llvm::SmallBitVector &Used) { |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5365 | switch (TemplateArg.getKind()) { |
| 5366 | case TemplateArgument::Null: |
| 5367 | case TemplateArgument::Integral: |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5368 | case TemplateArgument::Declaration: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5369 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5370 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5371 | case TemplateArgument::NullPtr: |
| 5372 | MarkUsedTemplateParameters(Ctx, TemplateArg.getNullPtrType(), OnlyDeduced, |
| 5373 | Depth, Used); |
| 5374 | break; |
| 5375 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5376 | case TemplateArgument::Type: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5377 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5378 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5379 | break; |
| 5380 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5381 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 5382 | case TemplateArgument::TemplateExpansion: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5383 | MarkUsedTemplateParameters(Ctx, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5384 | TemplateArg.getAsTemplateOrTemplatePattern(), |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5385 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5386 | break; |
| 5387 | |
| 5388 | case TemplateArgument::Expression: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5389 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsExpr(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5390 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5391 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5392 | |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5393 | case TemplateArgument::Pack: |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 5394 | for (const auto &P : TemplateArg.pack_elements()) |
| 5395 | MarkUsedTemplateParameters(Ctx, P, OnlyDeduced, Depth, Used); |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5396 | break; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5397 | } |
| 5398 | } |
| 5399 | |
James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 5400 | /// \brief Mark which template parameters can be deduced from a given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5401 | /// template argument list. |
| 5402 | /// |
| 5403 | /// \param TemplateArgs the template argument list from which template |
| 5404 | /// parameters will be deduced. |
| 5405 | /// |
James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 5406 | /// \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] | 5407 | /// to indicate when the corresponding template parameter will be |
| 5408 | /// deduced. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5409 | void |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5410 | Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5411 | bool OnlyDeduced, unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5412 | llvm::SmallBitVector &Used) { |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5413 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5414 | // If the template argument list of P contains a pack expansion that is not |
| 5415 | // the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5416 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5417 | if (OnlyDeduced && |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 5418 | hasPackExpansionBeforeEnd(TemplateArgs.asArray())) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5419 | return; |
| 5420 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5421 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5422 | ::MarkUsedTemplateParameters(Context, TemplateArgs[I], OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5423 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5424 | } |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5425 | |
| 5426 | /// \brief Marks all of the template parameters that will be deduced by a |
| 5427 | /// call to the given function template. |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 5428 | void Sema::MarkDeducedTemplateParameters( |
| 5429 | ASTContext &Ctx, const FunctionTemplateDecl *FunctionTemplate, |
| 5430 | llvm::SmallBitVector &Deduced) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5431 | TemplateParameterList *TemplateParams |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5432 | = FunctionTemplate->getTemplateParameters(); |
| 5433 | Deduced.clear(); |
| 5434 | Deduced.resize(TemplateParams->size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5435 | |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5436 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 5437 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5438 | ::MarkUsedTemplateParameters(Ctx, Function->getParamDecl(I)->getType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5439 | true, TemplateParams->getDepth(), Deduced); |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5440 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5441 | |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 5442 | bool hasDeducibleTemplateParameters(Sema &S, |
| 5443 | FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5444 | QualType T) { |
| 5445 | if (!T->isDependentType()) |
| 5446 | return false; |
| 5447 | |
Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 5448 | TemplateParameterList *TemplateParams |
| 5449 | = FunctionTemplate->getTemplateParameters(); |
| 5450 | llvm::SmallBitVector Deduced(TemplateParams->size()); |
| 5451 | ::MarkUsedTemplateParameters(S.Context, T, true, TemplateParams->getDepth(), |
| 5452 | Deduced); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5453 | |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5454 | return Deduced.any(); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5455 | } |