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 |
| 59 | /// terms of noreturn and default calling convention adjustments. |
| 60 | TDF_InOverloadResolution = 0x20 |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 61 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 62 | } |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 63 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 64 | using namespace clang; |
| 65 | |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 66 | /// \brief Compare two APSInts, extending and switching the sign as |
| 67 | /// necessary to compare their values regardless of underlying type. |
| 68 | static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) { |
| 69 | if (Y.getBitWidth() > X.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 70 | X = X.extend(Y.getBitWidth()); |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 71 | else if (Y.getBitWidth() < X.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 72 | Y = Y.extend(X.getBitWidth()); |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 73 | |
| 74 | // If there is a signedness mismatch, correct it. |
| 75 | if (X.isSigned() != Y.isSigned()) { |
| 76 | // If the signed value is negative, then the values cannot be the same. |
| 77 | if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative())) |
| 78 | return false; |
| 79 | |
| 80 | Y.setIsSigned(true); |
| 81 | X.setIsSigned(true); |
| 82 | } |
| 83 | |
| 84 | return X == Y; |
| 85 | } |
| 86 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 87 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 88 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 89 | TemplateParameterList *TemplateParams, |
| 90 | const TemplateArgument &Param, |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 91 | TemplateArgument Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 92 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 93 | SmallVectorImpl<DeducedTemplateArgument> &Deduced); |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 94 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 95 | static Sema::TemplateDeductionResult |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 96 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 97 | TemplateParameterList *TemplateParams, |
| 98 | QualType Param, |
| 99 | QualType Arg, |
| 100 | TemplateDeductionInfo &Info, |
| 101 | SmallVectorImpl<DeducedTemplateArgument> & |
| 102 | Deduced, |
| 103 | unsigned TDF, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 104 | bool PartialOrdering = false, |
| 105 | bool DeducedFromArrayBound = false); |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 106 | |
| 107 | static Sema::TemplateDeductionResult |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 108 | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 109 | ArrayRef<TemplateArgument> Params, |
| 110 | ArrayRef<TemplateArgument> Args, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 111 | TemplateDeductionInfo &Info, |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 112 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 113 | bool NumberOfArgumentsMustMatch); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 114 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 115 | /// \brief If the given expression is of a form that permits the deduction |
| 116 | /// of a non-type template parameter, return the declaration of that |
| 117 | /// non-type template parameter. |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 118 | static NonTypeTemplateParmDecl * |
| 119 | getDeducedParameterFromExpr(TemplateDeductionInfo &Info, Expr *E) { |
Richard Smith | 7ebb07c | 2012-07-08 04:37:51 +0000 | [diff] [blame] | 120 | // If we are within an alias template, the expression may have undergone |
| 121 | // any number of parameter substitutions already. |
| 122 | while (1) { |
| 123 | if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E)) |
| 124 | E = IC->getSubExpr(); |
| 125 | else if (SubstNonTypeTemplateParmExpr *Subst = |
| 126 | dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 127 | E = Subst->getReplacement(); |
| 128 | else |
| 129 | break; |
| 130 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 132 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 133 | if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) |
| 134 | if (NTTP->getDepth() == Info.getDeducedDepth()) |
| 135 | return NTTP; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 137 | return nullptr; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 140 | /// \brief Determine whether two declaration pointers refer to the same |
| 141 | /// declaration. |
| 142 | static bool isSameDeclaration(Decl *X, Decl *Y) { |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 143 | if (NamedDecl *NX = dyn_cast<NamedDecl>(X)) |
| 144 | X = NX->getUnderlyingDecl(); |
| 145 | if (NamedDecl *NY = dyn_cast<NamedDecl>(Y)) |
| 146 | Y = NY->getUnderlyingDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 147 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 148 | return X->getCanonicalDecl() == Y->getCanonicalDecl(); |
| 149 | } |
| 150 | |
| 151 | /// \brief Verify that the given, deduced template arguments are compatible. |
| 152 | /// |
| 153 | /// \returns The deduced template argument, or a NULL template argument if |
| 154 | /// the deduced template arguments were incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 155 | static DeducedTemplateArgument |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 156 | checkDeducedTemplateArguments(ASTContext &Context, |
| 157 | const DeducedTemplateArgument &X, |
| 158 | const DeducedTemplateArgument &Y) { |
| 159 | // We have no deduction for one or both of the arguments; they're compatible. |
| 160 | if (X.isNull()) |
| 161 | return Y; |
| 162 | if (Y.isNull()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 163 | return X; |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 164 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 165 | // If we have two non-type template argument values deduced for the same |
| 166 | // parameter, they must both match the type of the parameter, and thus must |
| 167 | // match each other's type. As we're only keeping one of them, we must check |
| 168 | // for that now. The exception is that if either was deduced from an array |
| 169 | // bound, the type is permitted to differ. |
| 170 | if (!X.wasDeducedFromArrayBound() && !Y.wasDeducedFromArrayBound()) { |
| 171 | QualType XType = X.getNonTypeTemplateArgumentType(); |
| 172 | if (!XType.isNull()) { |
| 173 | QualType YType = Y.getNonTypeTemplateArgumentType(); |
| 174 | if (YType.isNull() || !Context.hasSameType(XType, YType)) |
| 175 | return DeducedTemplateArgument(); |
| 176 | } |
| 177 | } |
| 178 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 179 | switch (X.getKind()) { |
| 180 | case TemplateArgument::Null: |
| 181 | llvm_unreachable("Non-deduced template arguments handled above"); |
| 182 | |
| 183 | case TemplateArgument::Type: |
| 184 | // If two template type arguments have the same type, they're compatible. |
| 185 | if (Y.getKind() == TemplateArgument::Type && |
| 186 | Context.hasSameType(X.getAsType(), Y.getAsType())) |
| 187 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 188 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 189 | // If one of the two arguments was deduced from an array bound, the other |
| 190 | // supersedes it. |
| 191 | if (X.wasDeducedFromArrayBound() != Y.wasDeducedFromArrayBound()) |
| 192 | return X.wasDeducedFromArrayBound() ? Y : X; |
| 193 | |
| 194 | // The arguments are not compatible. |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 195 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 196 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 197 | case TemplateArgument::Integral: |
| 198 | // If we deduced a constant in one case and either a dependent expression or |
| 199 | // declaration in another case, keep the integral constant. |
| 200 | // If both are integral constants with the same value, keep that value. |
| 201 | if (Y.getKind() == TemplateArgument::Expression || |
| 202 | Y.getKind() == TemplateArgument::Declaration || |
| 203 | (Y.getKind() == TemplateArgument::Integral && |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 204 | hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()))) |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 205 | return X.wasDeducedFromArrayBound() ? Y : X; |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 206 | |
| 207 | // All other combinations are incompatible. |
| 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::Template: |
| 211 | if (Y.getKind() == TemplateArgument::Template && |
| 212 | Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate())) |
| 213 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 214 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 215 | // All other combinations are incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 216 | return DeducedTemplateArgument(); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 217 | |
| 218 | case TemplateArgument::TemplateExpansion: |
| 219 | if (Y.getKind() == TemplateArgument::TemplateExpansion && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 220 | Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(), |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 221 | Y.getAsTemplateOrTemplatePattern())) |
| 222 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 223 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 224 | // All other combinations are incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 225 | return DeducedTemplateArgument(); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 226 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 227 | case TemplateArgument::Expression: { |
| 228 | if (Y.getKind() != TemplateArgument::Expression) |
| 229 | return checkDeducedTemplateArguments(Context, Y, X); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 230 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 231 | // Compare the expressions for equality |
| 232 | llvm::FoldingSetNodeID ID1, ID2; |
| 233 | X.getAsExpr()->Profile(ID1, Context, true); |
| 234 | Y.getAsExpr()->Profile(ID2, Context, true); |
| 235 | if (ID1 == ID2) |
| 236 | return X.wasDeducedFromArrayBound() ? Y : X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 237 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 238 | // Differing dependent expressions are incompatible. |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 239 | return DeducedTemplateArgument(); |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 240 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 241 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 242 | case TemplateArgument::Declaration: |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 243 | assert(!X.wasDeducedFromArrayBound()); |
| 244 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 245 | // If we deduced a declaration and a dependent expression, keep the |
| 246 | // declaration. |
| 247 | if (Y.getKind() == TemplateArgument::Expression) |
| 248 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 249 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 250 | // If we deduced a declaration and an integral constant, keep the |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 251 | // integral constant and whichever type did not come from an array |
| 252 | // bound. |
| 253 | if (Y.getKind() == TemplateArgument::Integral) { |
| 254 | if (Y.wasDeducedFromArrayBound()) |
| 255 | return TemplateArgument(Context, Y.getAsIntegral(), |
| 256 | X.getParamTypeForDecl()); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 257 | return Y; |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 258 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 259 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 260 | // If we deduced two declarations, make sure they they refer to the |
| 261 | // same declaration. |
| 262 | if (Y.getKind() == TemplateArgument::Declaration && |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 263 | isSameDeclaration(X.getAsDecl(), Y.getAsDecl())) |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 264 | return X; |
| 265 | |
| 266 | // All other combinations are incompatible. |
| 267 | return DeducedTemplateArgument(); |
| 268 | |
| 269 | case TemplateArgument::NullPtr: |
| 270 | // If we deduced a null pointer and a dependent expression, keep the |
| 271 | // null pointer. |
| 272 | if (Y.getKind() == TemplateArgument::Expression) |
| 273 | return X; |
| 274 | |
| 275 | // If we deduced a null pointer and an integral constant, keep the |
| 276 | // integral constant. |
| 277 | if (Y.getKind() == TemplateArgument::Integral) |
| 278 | return Y; |
| 279 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 280 | // If we deduced two null pointers, they are the same. |
| 281 | if (Y.getKind() == TemplateArgument::NullPtr) |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 282 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 283 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 284 | // All other combinations are incompatible. |
| 285 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 286 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 287 | case TemplateArgument::Pack: |
| 288 | if (Y.getKind() != TemplateArgument::Pack || |
| 289 | X.pack_size() != Y.pack_size()) |
| 290 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 291 | |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 292 | llvm::SmallVector<TemplateArgument, 8> NewPack; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 293 | for (TemplateArgument::pack_iterator XA = X.pack_begin(), |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 294 | XAEnd = X.pack_end(), |
| 295 | YA = Y.pack_begin(); |
| 296 | XA != XAEnd; ++XA, ++YA) { |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 297 | TemplateArgument Merged = checkDeducedTemplateArguments( |
| 298 | Context, DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()), |
| 299 | DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound())); |
| 300 | if (Merged.isNull()) |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 301 | return DeducedTemplateArgument(); |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 302 | NewPack.push_back(Merged); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 303 | } |
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 | return DeducedTemplateArgument( |
| 306 | TemplateArgument::CreatePackCopy(Context, NewPack), |
| 307 | X.wasDeducedFromArrayBound() && Y.wasDeducedFromArrayBound()); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 308 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 309 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 310 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 313 | /// \brief Deduce the value of the given non-type template parameter |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 314 | /// as the given deduced template argument. All non-type template parameter |
| 315 | /// deduction is funneled through here. |
Benjamin Kramer | 7320b99 | 2016-06-15 14:20:56 +0000 | [diff] [blame] | 316 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 317 | Sema &S, TemplateParameterList *TemplateParams, |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 318 | NonTypeTemplateParmDecl *NTTP, const DeducedTemplateArgument &NewDeduced, |
| 319 | QualType ValueType, TemplateDeductionInfo &Info, |
Benjamin Kramer | 7320b99 | 2016-06-15 14:20:56 +0000 | [diff] [blame] | 320 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 321 | assert(NTTP->getDepth() == Info.getDeducedDepth() && |
| 322 | "deducing non-type template argument with wrong depth"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 324 | DeducedTemplateArgument Result = checkDeducedTemplateArguments( |
| 325 | S.Context, Deduced[NTTP->getIndex()], NewDeduced); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 326 | if (Result.isNull()) { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 327 | Info.Param = NTTP; |
| 328 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 329 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 330 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 331 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 332 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 333 | Deduced[NTTP->getIndex()] = Result; |
Richard Smith | d92eddf | 2016-12-27 06:14:37 +0000 | [diff] [blame] | 334 | if (!S.getLangOpts().CPlusPlus1z) |
| 335 | return Sema::TDK_Success; |
| 336 | |
| 337 | // FIXME: It's not clear how deduction of a parameter of reference |
| 338 | // type from an argument (of non-reference type) should be performed. |
| 339 | // For now, we just remove reference types from both sides and let |
| 340 | // the final check for matching types sort out the mess. |
| 341 | return DeduceTemplateArgumentsByTypeMatch( |
| 342 | S, TemplateParams, NTTP->getType().getNonReferenceType(), |
| 343 | ValueType.getNonReferenceType(), Info, Deduced, TDF_SkipNonDependent, |
| 344 | /*PartialOrdering=*/false, |
| 345 | /*ArrayBound=*/NewDeduced.wasDeducedFromArrayBound()); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | /// \brief Deduce the value of the given non-type template parameter |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 349 | /// from the given integral constant. |
| 350 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| 351 | Sema &S, TemplateParameterList *TemplateParams, |
| 352 | NonTypeTemplateParmDecl *NTTP, const llvm::APSInt &Value, |
| 353 | QualType ValueType, bool DeducedFromArrayBound, TemplateDeductionInfo &Info, |
| 354 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 355 | return DeduceNonTypeTemplateArgument( |
| 356 | S, TemplateParams, NTTP, |
| 357 | DeducedTemplateArgument(S.Context, Value, ValueType, |
| 358 | DeducedFromArrayBound), |
| 359 | ValueType, Info, Deduced); |
| 360 | } |
| 361 | |
| 362 | /// \brief Deduce the value of the given non-type template parameter |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 363 | /// from the given null pointer template argument type. |
| 364 | static Sema::TemplateDeductionResult DeduceNullPtrTemplateArgument( |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 365 | Sema &S, TemplateParameterList *TemplateParams, |
| 366 | NonTypeTemplateParmDecl *NTTP, QualType NullPtrType, |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 367 | TemplateDeductionInfo &Info, |
| 368 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 369 | Expr *Value = |
| 370 | S.ImpCastExprToType(new (S.Context) CXXNullPtrLiteralExpr( |
| 371 | S.Context.NullPtrTy, NTTP->getLocation()), |
| 372 | NullPtrType, CK_NullToPointer) |
| 373 | .get(); |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 374 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 375 | DeducedTemplateArgument(Value), |
| 376 | Value->getType(), Info, Deduced); |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /// \brief Deduce the value of the given non-type template parameter |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 380 | /// from the given type- or value-dependent expression. |
| 381 | /// |
| 382 | /// \returns true if deduction succeeded, false otherwise. |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 383 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| 384 | Sema &S, TemplateParameterList *TemplateParams, |
| 385 | NonTypeTemplateParmDecl *NTTP, Expr *Value, TemplateDeductionInfo &Info, |
| 386 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 387 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 388 | DeducedTemplateArgument(Value), |
| 389 | Value->getType(), Info, Deduced); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 392 | /// \brief Deduce the value of the given non-type template parameter |
| 393 | /// from the given declaration. |
| 394 | /// |
| 395 | /// \returns true if deduction succeeded, false otherwise. |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 396 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| 397 | Sema &S, TemplateParameterList *TemplateParams, |
| 398 | NonTypeTemplateParmDecl *NTTP, ValueDecl *D, QualType T, |
| 399 | TemplateDeductionInfo &Info, |
| 400 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 401 | D = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 402 | TemplateArgument New(D, T); |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 403 | return DeduceNonTypeTemplateArgument( |
| 404 | S, TemplateParams, NTTP, DeducedTemplateArgument(New), T, Info, Deduced); |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 407 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 408 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 409 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 410 | TemplateName Param, |
| 411 | TemplateName Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 412 | TemplateDeductionInfo &Info, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 413 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 414 | TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 415 | if (!ParamDecl) { |
| 416 | // The parameter type is dependent and is not a template template parameter, |
| 417 | // so there is nothing that we can deduce. |
| 418 | return Sema::TDK_Success; |
| 419 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 420 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 421 | if (TemplateTemplateParmDecl *TempParam |
| 422 | = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 423 | // If we're not deducing at this depth, there's nothing to deduce. |
| 424 | if (TempParam->getDepth() != Info.getDeducedDepth()) |
| 425 | return Sema::TDK_Success; |
| 426 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 427 | DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 428 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 429 | Deduced[TempParam->getIndex()], |
| 430 | NewDeduced); |
| 431 | if (Result.isNull()) { |
| 432 | Info.Param = TempParam; |
| 433 | Info.FirstArg = Deduced[TempParam->getIndex()]; |
| 434 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 435 | return Sema::TDK_Inconsistent; |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 436 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 437 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 438 | Deduced[TempParam->getIndex()] = Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 439 | return Sema::TDK_Success; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 440 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 441 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 442 | // Verify that the two template names are equivalent. |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 443 | if (S.Context.hasSameTemplateName(Param, Arg)) |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 444 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 445 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 446 | // Mismatch of non-dependent template parameter to argument. |
| 447 | Info.FirstArg = TemplateArgument(Param); |
| 448 | Info.SecondArg = TemplateArgument(Arg); |
| 449 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 452 | /// \brief Deduce the template arguments by comparing the template parameter |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 453 | /// type (which is a template-id) with the template argument type. |
| 454 | /// |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 455 | /// \param S the Sema |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 456 | /// |
| 457 | /// \param TemplateParams the template parameters that we are deducing |
| 458 | /// |
| 459 | /// \param Param the parameter type |
| 460 | /// |
| 461 | /// \param Arg the argument type |
| 462 | /// |
| 463 | /// \param Info information about the template argument deduction itself |
| 464 | /// |
| 465 | /// \param Deduced the deduced template arguments |
| 466 | /// |
| 467 | /// \returns the result of template argument deduction so far. Note that a |
| 468 | /// "success" result means that template argument deduction has not yet failed, |
| 469 | /// but it may still fail, later, for other reasons. |
| 470 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 471 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 472 | TemplateParameterList *TemplateParams, |
| 473 | const TemplateSpecializationType *Param, |
| 474 | QualType Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 475 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 476 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 477 | assert(Arg.isCanonical() && "Argument type must be canonical"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 479 | // Check whether the template argument is a dependent template-id. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | if (const TemplateSpecializationType *SpecArg |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 481 | = dyn_cast<TemplateSpecializationType>(Arg)) { |
| 482 | // Perform template argument deduction for the template name. |
| 483 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 484 | = DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 485 | Param->getTemplateName(), |
| 486 | SpecArg->getTemplateName(), |
| 487 | Info, Deduced)) |
| 488 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 491 | // Perform template argument deduction on each template |
Douglas Gregor | d80ea20 | 2010-12-22 18:55:49 +0000 | [diff] [blame] | 492 | // argument. Ignore any missing/extra arguments, since they could be |
| 493 | // filled in by default arguments. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 494 | return DeduceTemplateArguments(S, TemplateParams, |
| 495 | Param->template_arguments(), |
| 496 | SpecArg->template_arguments(), Info, Deduced, |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 497 | /*NumberOfArgumentsMustMatch=*/false); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 498 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 499 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 500 | // If the argument type is a class template specialization, we |
| 501 | // perform template argument deduction using its template |
| 502 | // arguments. |
| 503 | const RecordType *RecordArg = dyn_cast<RecordType>(Arg); |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 504 | if (!RecordArg) { |
| 505 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 506 | Info.SecondArg = TemplateArgument(Arg); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 507 | return Sema::TDK_NonDeducedMismatch; |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 508 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 509 | |
| 510 | ClassTemplateSpecializationDecl *SpecArg |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 511 | = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 512 | if (!SpecArg) { |
| 513 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 514 | Info.SecondArg = TemplateArgument(Arg); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 515 | return Sema::TDK_NonDeducedMismatch; |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 516 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 518 | // Perform template argument deduction for the template name. |
| 519 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 520 | = DeduceTemplateArguments(S, |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 521 | TemplateParams, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 522 | Param->getTemplateName(), |
| 523 | TemplateName(SpecArg->getSpecializedTemplate()), |
| 524 | Info, Deduced)) |
| 525 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 526 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 527 | // Perform template argument deduction for the template arguments. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 528 | return DeduceTemplateArguments(S, TemplateParams, Param->template_arguments(), |
| 529 | SpecArg->getTemplateArgs().asArray(), Info, |
| 530 | Deduced, /*NumberOfArgumentsMustMatch=*/true); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 531 | } |
| 532 | |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 533 | /// \brief Determines whether the given type is an opaque type that |
| 534 | /// might be more qualified when instantiated. |
| 535 | static bool IsPossiblyOpaquelyQualifiedType(QualType T) { |
| 536 | switch (T->getTypeClass()) { |
| 537 | case Type::TypeOfExpr: |
| 538 | case Type::TypeOf: |
| 539 | case Type::DependentName: |
| 540 | case Type::Decltype: |
| 541 | case Type::UnresolvedUsing: |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 542 | case Type::TemplateTypeParm: |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 543 | return true; |
| 544 | |
| 545 | case Type::ConstantArray: |
| 546 | case Type::IncompleteArray: |
| 547 | case Type::VariableArray: |
| 548 | case Type::DependentSizedArray: |
| 549 | return IsPossiblyOpaquelyQualifiedType( |
| 550 | cast<ArrayType>(T)->getElementType()); |
| 551 | |
| 552 | default: |
| 553 | return false; |
| 554 | } |
| 555 | } |
| 556 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 557 | /// \brief Retrieve the depth and index of a template parameter. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 558 | static std::pair<unsigned, unsigned> |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 559 | getDepthAndIndex(NamedDecl *ND) { |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 560 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND)) |
| 561 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 562 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 563 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND)) |
| 564 | return std::make_pair(NTTP->getDepth(), NTTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 565 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 566 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND); |
| 567 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| 568 | } |
| 569 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 570 | /// \brief Retrieve the depth and index of an unexpanded parameter pack. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 571 | static std::pair<unsigned, unsigned> |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 572 | getDepthAndIndex(UnexpandedParameterPack UPP) { |
| 573 | if (const TemplateTypeParmType *TTP |
| 574 | = UPP.first.dyn_cast<const TemplateTypeParmType *>()) |
| 575 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 576 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 577 | return getDepthAndIndex(UPP.first.get<NamedDecl *>()); |
| 578 | } |
| 579 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 580 | /// \brief Helper function to build a TemplateParameter when we don't |
| 581 | /// know its type statically. |
| 582 | static TemplateParameter makeTemplateParameter(Decl *D) { |
| 583 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) |
| 584 | return TemplateParameter(TTP); |
Craig Topper | 4b482ee | 2013-07-08 04:24:47 +0000 | [diff] [blame] | 585 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 586 | return TemplateParameter(NTTP); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 587 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 588 | return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); |
| 589 | } |
| 590 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 591 | /// A pack that we're currently deducing. |
| 592 | struct clang::DeducedPack { |
| 593 | DeducedPack(unsigned Index) : Index(Index), Outer(nullptr) {} |
Craig Topper | 0a4e1f5 | 2013-07-08 04:44:01 +0000 | [diff] [blame] | 594 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 595 | // The index of the pack. |
| 596 | unsigned Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 597 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 598 | // The old value of the pack before we started deducing it. |
| 599 | DeducedTemplateArgument Saved; |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 600 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 601 | // A deferred value of this pack from an inner deduction, that couldn't be |
| 602 | // deduced because this deduction hadn't happened yet. |
| 603 | DeducedTemplateArgument DeferredDeduction; |
| 604 | |
| 605 | // The new value of the pack. |
| 606 | SmallVector<DeducedTemplateArgument, 4> New; |
| 607 | |
| 608 | // The outer deduction for this pack, if any. |
| 609 | DeducedPack *Outer; |
| 610 | }; |
| 611 | |
Benjamin Kramer | d5748c7 | 2015-03-23 12:31:05 +0000 | [diff] [blame] | 612 | namespace { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 613 | /// A scope in which we're performing pack deduction. |
| 614 | class PackDeductionScope { |
| 615 | public: |
| 616 | PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, |
| 617 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 618 | TemplateDeductionInfo &Info, TemplateArgument Pattern) |
| 619 | : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { |
| 620 | // Compute the set of template parameter indices that correspond to |
| 621 | // parameter packs expanded by the pack expansion. |
| 622 | { |
| 623 | llvm::SmallBitVector SawIndices(TemplateParams->size()); |
| 624 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 625 | S.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
| 626 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 627 | unsigned Depth, Index; |
| 628 | std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 629 | if (Depth == Info.getDeducedDepth() && !SawIndices[Index]) { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 630 | SawIndices[Index] = true; |
| 631 | |
| 632 | // Save the deduced template argument for the parameter pack expanded |
| 633 | // by this pack expansion, then clear out the deduction. |
| 634 | DeducedPack Pack(Index); |
| 635 | Pack.Saved = Deduced[Index]; |
| 636 | Deduced[Index] = TemplateArgument(); |
| 637 | |
| 638 | Packs.push_back(Pack); |
| 639 | } |
| 640 | } |
| 641 | } |
| 642 | assert(!Packs.empty() && "Pack expansion without unexpanded packs?"); |
| 643 | |
| 644 | for (auto &Pack : Packs) { |
| 645 | if (Info.PendingDeducedPacks.size() > Pack.Index) |
| 646 | Pack.Outer = Info.PendingDeducedPacks[Pack.Index]; |
| 647 | else |
| 648 | Info.PendingDeducedPacks.resize(Pack.Index + 1); |
| 649 | Info.PendingDeducedPacks[Pack.Index] = &Pack; |
| 650 | |
| 651 | if (S.CurrentInstantiationScope) { |
| 652 | // If the template argument pack was explicitly specified, add that to |
| 653 | // the set of deduced arguments. |
| 654 | const TemplateArgument *ExplicitArgs; |
| 655 | unsigned NumExplicitArgs; |
| 656 | NamedDecl *PartiallySubstitutedPack = |
| 657 | S.CurrentInstantiationScope->getPartiallySubstitutedPack( |
| 658 | &ExplicitArgs, &NumExplicitArgs); |
| 659 | if (PartiallySubstitutedPack && |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 660 | getDepthAndIndex(PartiallySubstitutedPack) == |
| 661 | std::make_pair(Info.getDeducedDepth(), Pack.Index)) |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 662 | Pack.New.append(ExplicitArgs, ExplicitArgs + NumExplicitArgs); |
| 663 | } |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 664 | } |
| 665 | } |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 666 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 667 | ~PackDeductionScope() { |
| 668 | for (auto &Pack : Packs) |
| 669 | Info.PendingDeducedPacks[Pack.Index] = Pack.Outer; |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 670 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 671 | |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 672 | /// Determine whether this pack has already been partially expanded into a |
| 673 | /// sequence of (prior) function parameters / template arguments. |
| 674 | bool isPartiallyExpanded() { |
| 675 | if (Packs.size() != 1 || !S.CurrentInstantiationScope) |
| 676 | return false; |
| 677 | |
| 678 | auto *PartiallySubstitutedPack = |
| 679 | S.CurrentInstantiationScope->getPartiallySubstitutedPack(); |
| 680 | return PartiallySubstitutedPack && |
| 681 | getDepthAndIndex(PartiallySubstitutedPack) == |
| 682 | std::make_pair(Info.getDeducedDepth(), Packs.front().Index); |
| 683 | } |
| 684 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 685 | /// Move to deducing the next element in each pack that is being deduced. |
| 686 | void nextPackElement() { |
| 687 | // Capture the deduced template arguments for each parameter pack expanded |
| 688 | // by this pack expansion, add them to the list of arguments we've deduced |
| 689 | // for that pack, then clear out the deduced argument. |
| 690 | for (auto &Pack : Packs) { |
| 691 | DeducedTemplateArgument &DeducedArg = Deduced[Pack.Index]; |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 692 | if (!Pack.New.empty() || !DeducedArg.isNull()) { |
| 693 | while (Pack.New.size() < PackElements) |
| 694 | Pack.New.push_back(DeducedTemplateArgument()); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 695 | Pack.New.push_back(DeducedArg); |
| 696 | DeducedArg = DeducedTemplateArgument(); |
| 697 | } |
| 698 | } |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 699 | ++PackElements; |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | /// \brief Finish template argument deduction for a set of argument packs, |
| 703 | /// producing the argument packs and checking for consistency with prior |
| 704 | /// deductions. |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 705 | Sema::TemplateDeductionResult finish() { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 706 | // Build argument packs for each of the parameter packs expanded by this |
| 707 | // pack expansion. |
| 708 | for (auto &Pack : Packs) { |
| 709 | // Put back the old value for this pack. |
| 710 | Deduced[Pack.Index] = Pack.Saved; |
| 711 | |
| 712 | // Build or find a new value for this pack. |
| 713 | DeducedTemplateArgument NewPack; |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 714 | if (PackElements && Pack.New.empty()) { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 715 | if (Pack.DeferredDeduction.isNull()) { |
| 716 | // We were not able to deduce anything for this parameter pack |
| 717 | // (because it only appeared in non-deduced contexts), so just |
| 718 | // restore the saved argument pack. |
| 719 | continue; |
| 720 | } |
| 721 | |
| 722 | NewPack = Pack.DeferredDeduction; |
| 723 | Pack.DeferredDeduction = TemplateArgument(); |
| 724 | } else if (Pack.New.empty()) { |
| 725 | // If we deduced an empty argument pack, create it now. |
| 726 | NewPack = DeducedTemplateArgument(TemplateArgument::getEmptyPack()); |
| 727 | } else { |
| 728 | TemplateArgument *ArgumentPack = |
| 729 | new (S.Context) TemplateArgument[Pack.New.size()]; |
| 730 | std::copy(Pack.New.begin(), Pack.New.end(), ArgumentPack); |
| 731 | NewPack = DeducedTemplateArgument( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 732 | TemplateArgument(llvm::makeArrayRef(ArgumentPack, Pack.New.size())), |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 733 | Pack.New[0].wasDeducedFromArrayBound()); |
| 734 | } |
| 735 | |
| 736 | // Pick where we're going to put the merged pack. |
| 737 | DeducedTemplateArgument *Loc; |
| 738 | if (Pack.Outer) { |
| 739 | if (Pack.Outer->DeferredDeduction.isNull()) { |
| 740 | // Defer checking this pack until we have a complete pack to compare |
| 741 | // it against. |
| 742 | Pack.Outer->DeferredDeduction = NewPack; |
| 743 | continue; |
| 744 | } |
| 745 | Loc = &Pack.Outer->DeferredDeduction; |
| 746 | } else { |
| 747 | Loc = &Deduced[Pack.Index]; |
| 748 | } |
| 749 | |
| 750 | // Check the new pack matches any previous value. |
| 751 | DeducedTemplateArgument OldPack = *Loc; |
| 752 | DeducedTemplateArgument Result = |
| 753 | checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 754 | |
| 755 | // If we deferred a deduction of this pack, check that one now too. |
| 756 | if (!Result.isNull() && !Pack.DeferredDeduction.isNull()) { |
| 757 | OldPack = Result; |
| 758 | NewPack = Pack.DeferredDeduction; |
| 759 | Result = checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 760 | } |
| 761 | |
| 762 | if (Result.isNull()) { |
| 763 | Info.Param = |
| 764 | makeTemplateParameter(TemplateParams->getParam(Pack.Index)); |
| 765 | Info.FirstArg = OldPack; |
| 766 | Info.SecondArg = NewPack; |
| 767 | return Sema::TDK_Inconsistent; |
| 768 | } |
| 769 | |
| 770 | *Loc = Result; |
| 771 | } |
| 772 | |
| 773 | return Sema::TDK_Success; |
| 774 | } |
| 775 | |
| 776 | private: |
| 777 | Sema &S; |
| 778 | TemplateParameterList *TemplateParams; |
| 779 | SmallVectorImpl<DeducedTemplateArgument> &Deduced; |
| 780 | TemplateDeductionInfo &Info; |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 781 | unsigned PackElements = 0; |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 782 | |
| 783 | SmallVector<DeducedPack, 2> Packs; |
| 784 | }; |
Benjamin Kramer | d5748c7 | 2015-03-23 12:31:05 +0000 | [diff] [blame] | 785 | } // namespace |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 786 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 787 | /// \brief Deduce the template arguments by comparing the list of parameter |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 788 | /// types to the list of argument types, as in the parameter-type-lists of |
| 789 | /// function types (C++ [temp.deduct.type]p10). |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 790 | /// |
| 791 | /// \param S The semantic analysis object within which we are deducing |
| 792 | /// |
| 793 | /// \param TemplateParams The template parameters that we are deducing |
| 794 | /// |
| 795 | /// \param Params The list of parameter types |
| 796 | /// |
| 797 | /// \param NumParams The number of types in \c Params |
| 798 | /// |
| 799 | /// \param Args The list of argument types |
| 800 | /// |
| 801 | /// \param NumArgs The number of types in \c Args |
| 802 | /// |
| 803 | /// \param Info information about the template argument deduction itself |
| 804 | /// |
| 805 | /// \param Deduced the deduced template arguments |
| 806 | /// |
| 807 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
| 808 | /// how template argument deduction is performed. |
| 809 | /// |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 810 | /// \param PartialOrdering If true, we are performing template argument |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 811 | /// deduction for during partial ordering for a call |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 812 | /// (C++0x [temp.deduct.partial]). |
| 813 | /// |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 814 | /// \returns the result of template argument deduction so far. Note that a |
| 815 | /// "success" result means that template argument deduction has not yet failed, |
| 816 | /// but it may still fail, later, for other reasons. |
| 817 | static Sema::TemplateDeductionResult |
| 818 | DeduceTemplateArguments(Sema &S, |
| 819 | TemplateParameterList *TemplateParams, |
| 820 | const QualType *Params, unsigned NumParams, |
| 821 | const QualType *Args, unsigned NumArgs, |
| 822 | TemplateDeductionInfo &Info, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 823 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 824 | unsigned TDF, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 825 | bool PartialOrdering = false) { |
Douglas Gregor | 86bea35 | 2011-01-05 23:23:17 +0000 | [diff] [blame] | 826 | // Fast-path check to see if we have too many/too few arguments. |
| 827 | if (NumParams != NumArgs && |
| 828 | !(NumParams && isa<PackExpansionType>(Params[NumParams - 1])) && |
| 829 | !(NumArgs && isa<PackExpansionType>(Args[NumArgs - 1]))) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 830 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 831 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 832 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 833 | // Similarly, if P has a form that contains (T), then each parameter type |
| 834 | // Pi of the respective parameter-type- list of P is compared with the |
| 835 | // corresponding parameter type Ai of the corresponding parameter-type-list |
| 836 | // of A. [...] |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 837 | unsigned ArgIdx = 0, ParamIdx = 0; |
| 838 | for (; ParamIdx != NumParams; ++ParamIdx) { |
| 839 | // Check argument types. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 840 | const PackExpansionType *Expansion |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 841 | = dyn_cast<PackExpansionType>(Params[ParamIdx]); |
| 842 | if (!Expansion) { |
| 843 | // Simple case: compare the parameter and argument types at this point. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 844 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 845 | // Make sure we have an argument. |
| 846 | if (ArgIdx >= NumArgs) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 847 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 848 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 849 | if (isa<PackExpansionType>(Args[ArgIdx])) { |
| 850 | // C++0x [temp.deduct.type]p22: |
| 851 | // If the original function parameter associated with A is a function |
| 852 | // parameter pack and the function parameter associated with P is not |
| 853 | // a function parameter pack, then template argument deduction fails. |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 854 | return Sema::TDK_MiscellaneousDeductionFailure; |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 855 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 856 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 857 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 858 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 859 | Params[ParamIdx], Args[ArgIdx], |
| 860 | Info, Deduced, TDF, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 861 | PartialOrdering)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 862 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 863 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 864 | ++ArgIdx; |
| 865 | continue; |
| 866 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 867 | |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 868 | // C++0x [temp.deduct.type]p5: |
| 869 | // The non-deduced contexts are: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 870 | // - 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] | 871 | // parameter-declaration-clause. |
| 872 | if (ParamIdx + 1 < NumParams) |
| 873 | return Sema::TDK_Success; |
| 874 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 875 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 876 | // If the parameter-declaration corresponding to Pi is a function |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 877 | // parameter pack, then the type of its declarator- id is compared with |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 878 | // each remaining parameter type in the parameter-type-list of A. Each |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 879 | // comparison deduces template arguments for subsequent positions in the |
| 880 | // template parameter packs expanded by the function parameter pack. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 881 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 882 | QualType Pattern = Expansion->getPattern(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 883 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 884 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 885 | for (; ArgIdx < NumArgs; ++ArgIdx) { |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 886 | // Deduce template arguments from the pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 887 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 888 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, Pattern, |
| 889 | Args[ArgIdx], Info, Deduced, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 890 | TDF, PartialOrdering)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 891 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 892 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 893 | PackScope.nextPackElement(); |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 894 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 895 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 896 | // Build argument packs for each of the parameter packs expanded by this |
| 897 | // pack expansion. |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 898 | if (auto Result = PackScope.finish()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 899 | return Result; |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 900 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 901 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 902 | // Make sure we don't have any extra arguments. |
| 903 | if (ArgIdx < NumArgs) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 904 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 905 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 906 | return Sema::TDK_Success; |
| 907 | } |
| 908 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 909 | /// \brief Determine whether the parameter has qualifiers that are either |
| 910 | /// inconsistent with or a superset of the argument's qualifiers. |
| 911 | static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType, |
| 912 | QualType ArgType) { |
| 913 | Qualifiers ParamQs = ParamType.getQualifiers(); |
| 914 | Qualifiers ArgQs = ArgType.getQualifiers(); |
| 915 | |
| 916 | if (ParamQs == ArgQs) |
| 917 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 918 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 919 | // Mismatched (but not missing) Objective-C GC attributes. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 920 | if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() && |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 921 | ParamQs.hasObjCGCAttr()) |
| 922 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 923 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 924 | // Mismatched (but not missing) address spaces. |
| 925 | if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() && |
| 926 | ParamQs.hasAddressSpace()) |
| 927 | return true; |
| 928 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 929 | // Mismatched (but not missing) Objective-C lifetime qualifiers. |
| 930 | if (ParamQs.getObjCLifetime() != ArgQs.getObjCLifetime() && |
| 931 | ParamQs.hasObjCLifetime()) |
| 932 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 933 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 934 | // CVR qualifier superset. |
| 935 | return (ParamQs.getCVRQualifiers() != ArgQs.getCVRQualifiers()) && |
| 936 | ((ParamQs.getCVRQualifiers() | ArgQs.getCVRQualifiers()) |
| 937 | == ParamQs.getCVRQualifiers()); |
| 938 | } |
| 939 | |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 940 | /// \brief Compare types for equality with respect to possibly compatible |
| 941 | /// function types (noreturn adjustment, implicit calling conventions). If any |
| 942 | /// of parameter and argument is not a function, just perform type comparison. |
| 943 | /// |
| 944 | /// \param Param the template parameter type. |
| 945 | /// |
| 946 | /// \param Arg the argument type. |
| 947 | bool Sema::isSameOrCompatibleFunctionType(CanQualType Param, |
| 948 | CanQualType Arg) { |
| 949 | const FunctionType *ParamFunction = Param->getAs<FunctionType>(), |
| 950 | *ArgFunction = Arg->getAs<FunctionType>(); |
| 951 | |
| 952 | // Just compare if not functions. |
| 953 | if (!ParamFunction || !ArgFunction) |
| 954 | return Param == Arg; |
| 955 | |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 956 | // Noreturn and noexcept adjustment. |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 957 | QualType AdjustedParam; |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 958 | if (IsFunctionConversion(Param, Arg, AdjustedParam)) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 959 | return Arg == Context.getCanonicalType(AdjustedParam); |
| 960 | |
| 961 | // FIXME: Compatible calling conventions. |
| 962 | |
| 963 | return Param == Arg; |
| 964 | } |
| 965 | |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 966 | /// \brief Deduce the template arguments by comparing the parameter type and |
| 967 | /// the argument type (C++ [temp.deduct.type]). |
| 968 | /// |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 969 | /// \param S the semantic analysis object within which we are deducing |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 970 | /// |
| 971 | /// \param TemplateParams the template parameters that we are deducing |
| 972 | /// |
| 973 | /// \param ParamIn the parameter type |
| 974 | /// |
| 975 | /// \param ArgIn the argument type |
| 976 | /// |
| 977 | /// \param Info information about the template argument deduction itself |
| 978 | /// |
| 979 | /// \param Deduced the deduced template arguments |
| 980 | /// |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 981 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 982 | /// how template argument deduction is performed. |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 983 | /// |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 984 | /// \param PartialOrdering Whether we're performing template argument deduction |
| 985 | /// in the context of partial ordering (C++0x [temp.deduct.partial]). |
| 986 | /// |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 987 | /// \returns the result of template argument deduction so far. Note that a |
| 988 | /// "success" result means that template argument deduction has not yet failed, |
| 989 | /// but it may still fail, later, for other reasons. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 990 | static Sema::TemplateDeductionResult |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 991 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 992 | TemplateParameterList *TemplateParams, |
| 993 | QualType ParamIn, QualType ArgIn, |
| 994 | TemplateDeductionInfo &Info, |
| 995 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 996 | unsigned TDF, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 997 | bool PartialOrdering, |
| 998 | bool DeducedFromArrayBound) { |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 999 | // We only want to look at the canonical types, since typedefs and |
| 1000 | // sugar are not part of template argument deduction. |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1001 | QualType Param = S.Context.getCanonicalType(ParamIn); |
| 1002 | QualType Arg = S.Context.getCanonicalType(ArgIn); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1003 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1004 | // If the argument type is a pack expansion, look at its pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1005 | // This isn't explicitly called out |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1006 | if (const PackExpansionType *ArgExpansion |
| 1007 | = dyn_cast<PackExpansionType>(Arg)) |
| 1008 | Arg = ArgExpansion->getPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1009 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1010 | if (PartialOrdering) { |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1011 | // C++11 [temp.deduct.partial]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1012 | // Before the partial ordering is done, certain transformations are |
| 1013 | // performed on the types used for partial ordering: |
| 1014 | // - 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] | 1015 | const ReferenceType *ParamRef = Param->getAs<ReferenceType>(); |
| 1016 | if (ParamRef) |
| 1017 | Param = ParamRef->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1018 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1019 | // - If A is a reference type, A is replaced by the type referred to. |
| 1020 | const ReferenceType *ArgRef = Arg->getAs<ReferenceType>(); |
| 1021 | if (ArgRef) |
| 1022 | Arg = ArgRef->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1023 | |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1024 | if (ParamRef && ArgRef && S.Context.hasSameUnqualifiedType(Param, Arg)) { |
| 1025 | // C++11 [temp.deduct.partial]p9: |
| 1026 | // If, for a given type, deduction succeeds in both directions (i.e., |
| 1027 | // the types are identical after the transformations above) and both |
| 1028 | // P and A were reference types [...]: |
| 1029 | // - if [one type] was an lvalue reference and [the other type] was |
| 1030 | // not, [the other type] is not considered to be at least as |
| 1031 | // specialized as [the first type] |
| 1032 | // - if [one type] is more cv-qualified than [the other type], |
| 1033 | // [the other type] is not considered to be at least as specialized |
| 1034 | // as [the first type] |
| 1035 | // Objective-C ARC adds: |
| 1036 | // - [one type] has non-trivial lifetime, [the other type] has |
| 1037 | // __unsafe_unretained lifetime, and the types are otherwise |
| 1038 | // identical |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1039 | // |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1040 | // A is "considered to be at least as specialized" as P iff deduction |
| 1041 | // succeeds, so we model this as a deduction failure. Note that |
| 1042 | // [the first type] is P and [the other type] is A here; the standard |
| 1043 | // gets this backwards. |
Douglas Gregor | 85894a8 | 2011-04-30 17:07:52 +0000 | [diff] [blame] | 1044 | Qualifiers ParamQuals = Param.getQualifiers(); |
| 1045 | Qualifiers ArgQuals = Arg.getQualifiers(); |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1046 | if ((ParamRef->isLValueReferenceType() && |
| 1047 | !ArgRef->isLValueReferenceType()) || |
| 1048 | ParamQuals.isStrictSupersetOf(ArgQuals) || |
| 1049 | (ParamQuals.hasNonTrivialObjCLifetime() && |
| 1050 | ArgQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone && |
| 1051 | ParamQuals.withoutObjCLifetime() == |
| 1052 | ArgQuals.withoutObjCLifetime())) { |
| 1053 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1054 | Info.SecondArg = TemplateArgument(ArgIn); |
| 1055 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 6beabee | 2014-01-02 19:42:02 +0000 | [diff] [blame] | 1056 | } |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1057 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1058 | |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1059 | // C++11 [temp.deduct.partial]p7: |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1060 | // Remove any top-level cv-qualifiers: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1061 | // - 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] | 1062 | // version of P. |
| 1063 | Param = Param.getUnqualifiedType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1064 | // - 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] | 1065 | // version of A. |
| 1066 | Arg = Arg.getUnqualifiedType(); |
| 1067 | } else { |
| 1068 | // C++0x [temp.deduct.call]p4 bullet 1: |
| 1069 | // - If the original P is a reference type, the deduced A (i.e., the type |
| 1070 | // referred to by the reference) can be more cv-qualified than the |
| 1071 | // transformed A. |
| 1072 | if (TDF & TDF_ParamWithReferenceType) { |
| 1073 | Qualifiers Quals; |
| 1074 | QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals); |
| 1075 | Quals.setCVRQualifiers(Quals.getCVRQualifiers() & |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 1076 | Arg.getCVRQualifiers()); |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1077 | Param = S.Context.getQualifiedType(UnqualParam, Quals); |
| 1078 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1079 | |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1080 | if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) { |
| 1081 | // C++0x [temp.deduct.type]p10: |
| 1082 | // If P and A are function types that originated from deduction when |
| 1083 | // taking the address of a function template (14.8.2.2) or when deducing |
| 1084 | // 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] | 1085 | // Ai are parameters of the top-level parameter-type-list of P and A, |
| 1086 | // respectively, Pi is adjusted if it is an rvalue reference to a |
| 1087 | // cv-unqualified template parameter and Ai is an lvalue reference, in |
| 1088 | // 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] | 1089 | // type (i.e., T&& is changed to simply T). [ Note: As a result, when |
| 1090 | // 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] | 1091 | // deduced as X&. - end note ] |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1092 | TDF &= ~TDF_TopLevelParameterTypeList; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1093 | |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1094 | if (const RValueReferenceType *ParamRef |
| 1095 | = Param->getAs<RValueReferenceType>()) { |
| 1096 | if (isa<TemplateTypeParmType>(ParamRef->getPointeeType()) && |
| 1097 | !ParamRef->getPointeeType().getQualifiers()) |
| 1098 | if (Arg->isLValueReferenceType()) |
| 1099 | Param = ParamRef->getPointeeType(); |
| 1100 | } |
| 1101 | } |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1102 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1103 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1104 | // C++ [temp.deduct.type]p9: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1105 | // A template type argument T, a template template argument TT or a |
| 1106 | // 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] | 1107 | // the following forms: |
| 1108 | // |
| 1109 | // T |
| 1110 | // cv-list T |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1111 | if (const TemplateTypeParmType *TemplateTypeParm |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1112 | = Param->getAs<TemplateTypeParmType>()) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1113 | // Just skip any attempts to deduce from a placeholder type or a parameter |
| 1114 | // at a different depth. |
| 1115 | if (Arg->isPlaceholderType() || |
| 1116 | Info.getDeducedDepth() != TemplateTypeParm->getDepth()) |
Douglas Gregor | 4ea5dec | 2011-09-22 15:57:07 +0000 | [diff] [blame] | 1117 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1118 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1119 | unsigned Index = TemplateTypeParm->getIndex(); |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1120 | bool RecanonicalizeArg = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1121 | |
Douglas Gregor | 6045482 | 2009-07-22 20:02:25 +0000 | [diff] [blame] | 1122 | // If the argument type is an array type, move the qualifiers up to the |
| 1123 | // 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] | 1124 | if (isa<ArrayType>(Arg)) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1125 | Qualifiers Quals; |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1126 | Arg = S.Context.getUnqualifiedArrayType(Arg, Quals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1127 | if (Quals) { |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1128 | Arg = S.Context.getQualifiedType(Arg, Quals); |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1129 | RecanonicalizeArg = true; |
| 1130 | } |
| 1131 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1132 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1133 | // The argument type can not be less qualified than the parameter |
| 1134 | // type. |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1135 | if (!(TDF & TDF_IgnoreQualifiers) && |
| 1136 | hasInconsistentOrSupersetQualifiersOf(Param, Arg)) { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1137 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1138 | Info.FirstArg = TemplateArgument(Param); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1139 | Info.SecondArg = TemplateArgument(Arg); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1140 | return Sema::TDK_Underqualified; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1141 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1142 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1143 | assert(TemplateTypeParm->getDepth() == Info.getDeducedDepth() && |
| 1144 | "saw template type parameter with wrong depth"); |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1145 | assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function"); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1146 | QualType DeducedType = Arg; |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1147 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1148 | // Remove any qualifiers on the parameter from the deduced type. |
| 1149 | // We checked the qualifiers for consistency above. |
| 1150 | Qualifiers DeducedQs = DeducedType.getQualifiers(); |
| 1151 | Qualifiers ParamQs = Param.getQualifiers(); |
| 1152 | DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers()); |
| 1153 | if (ParamQs.hasObjCGCAttr()) |
| 1154 | DeducedQs.removeObjCGCAttr(); |
| 1155 | if (ParamQs.hasAddressSpace()) |
| 1156 | DeducedQs.removeAddressSpace(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1157 | if (ParamQs.hasObjCLifetime()) |
| 1158 | DeducedQs.removeObjCLifetime(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1159 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1160 | // Objective-C ARC: |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1161 | // If template deduction would produce a lifetime qualifier on a type |
| 1162 | // that is not a lifetime type, template argument deduction fails. |
| 1163 | if (ParamQs.hasObjCLifetime() && !DeducedType->isObjCLifetimeType() && |
| 1164 | !DeducedType->isDependentType()) { |
| 1165 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1166 | Info.FirstArg = TemplateArgument(Param); |
| 1167 | Info.SecondArg = TemplateArgument(Arg); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1168 | return Sema::TDK_Underqualified; |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1169 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1170 | |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1171 | // Objective-C ARC: |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1172 | // If template deduction would produce an argument type with lifetime type |
| 1173 | // but no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1174 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1175 | DeducedType->isObjCLifetimeType() && |
| 1176 | !DeducedQs.hasObjCLifetime()) |
| 1177 | DeducedQs.setObjCLifetime(Qualifiers::OCL_Strong); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1178 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1179 | DeducedType = S.Context.getQualifiedType(DeducedType.getUnqualifiedType(), |
| 1180 | DeducedQs); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1181 | |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1182 | if (RecanonicalizeArg) |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1183 | DeducedType = S.Context.getCanonicalType(DeducedType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1184 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1185 | DeducedTemplateArgument NewDeduced(DeducedType, DeducedFromArrayBound); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1186 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1187 | Deduced[Index], |
| 1188 | NewDeduced); |
| 1189 | if (Result.isNull()) { |
| 1190 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1191 | Info.FirstArg = Deduced[Index]; |
| 1192 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1193 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1194 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1195 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1196 | Deduced[Index] = Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1197 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1200 | // Set up the template argument deduction information for a failure. |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1201 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1202 | Info.SecondArg = TemplateArgument(ArgIn); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1203 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 1204 | // If the parameter is an already-substituted template parameter |
| 1205 | // pack, do nothing: we don't know which of its arguments to look |
| 1206 | // at, so we have to wait until all of the parameter packs in this |
| 1207 | // expansion have arguments. |
| 1208 | if (isa<SubstTemplateTypeParmPackType>(Param)) |
| 1209 | return Sema::TDK_Success; |
| 1210 | |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1211 | // Check the cv-qualifiers on the parameter and argument types. |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1212 | CanQualType CanParam = S.Context.getCanonicalType(Param); |
| 1213 | CanQualType CanArg = S.Context.getCanonicalType(Arg); |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1214 | if (!(TDF & TDF_IgnoreQualifiers)) { |
| 1215 | if (TDF & TDF_ParamWithReferenceType) { |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1216 | if (hasInconsistentOrSupersetQualifiersOf(Param, Arg)) |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1217 | return Sema::TDK_NonDeducedMismatch; |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 1218 | } else if (!IsPossiblyOpaquelyQualifiedType(Param)) { |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1219 | if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1220 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1221 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1222 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1223 | // If the parameter type is not dependent, there is nothing to deduce. |
| 1224 | if (!Param->isDependentType()) { |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1225 | if (!(TDF & TDF_SkipNonDependent)) { |
| 1226 | bool NonDeduced = (TDF & TDF_InOverloadResolution)? |
| 1227 | !S.isSameOrCompatibleFunctionType(CanParam, CanArg) : |
| 1228 | Param != Arg; |
| 1229 | if (NonDeduced) { |
| 1230 | return Sema::TDK_NonDeducedMismatch; |
| 1231 | } |
| 1232 | } |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1233 | return Sema::TDK_Success; |
| 1234 | } |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1235 | } else if (!Param->isDependentType()) { |
| 1236 | CanQualType ParamUnqualType = CanParam.getUnqualifiedType(), |
| 1237 | ArgUnqualType = CanArg.getUnqualifiedType(); |
| 1238 | bool Success = (TDF & TDF_InOverloadResolution)? |
| 1239 | S.isSameOrCompatibleFunctionType(ParamUnqualType, |
| 1240 | ArgUnqualType) : |
| 1241 | ParamUnqualType == ArgUnqualType; |
| 1242 | if (Success) |
| 1243 | return Sema::TDK_Success; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1244 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1245 | |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1246 | switch (Param->getTypeClass()) { |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1247 | // Non-canonical types cannot appear here. |
| 1248 | #define NON_CANONICAL_TYPE(Class, Base) \ |
| 1249 | case Type::Class: llvm_unreachable("deducing non-canonical type: " #Class); |
| 1250 | #define TYPE(Class, Base) |
| 1251 | #include "clang/AST/TypeNodes.def" |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1252 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1253 | case Type::TemplateTypeParm: |
| 1254 | case Type::SubstTemplateTypeParmPack: |
| 1255 | llvm_unreachable("Type nodes handled above"); |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1256 | |
| 1257 | // These types cannot be dependent, so simply check whether the types are |
| 1258 | // the same. |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1259 | case Type::Builtin: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1260 | case Type::VariableArray: |
| 1261 | case Type::Vector: |
| 1262 | case Type::FunctionNoProto: |
| 1263 | case Type::Record: |
| 1264 | case Type::Enum: |
| 1265 | case Type::ObjCObject: |
| 1266 | case Type::ObjCInterface: |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1267 | case Type::ObjCObjectPointer: { |
| 1268 | if (TDF & TDF_SkipNonDependent) |
| 1269 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1270 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1271 | if (TDF & TDF_IgnoreQualifiers) { |
| 1272 | Param = Param.getUnqualifiedType(); |
| 1273 | Arg = Arg.getUnqualifiedType(); |
| 1274 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1275 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1276 | return Param == Arg? Sema::TDK_Success : Sema::TDK_NonDeducedMismatch; |
| 1277 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1278 | |
| 1279 | // _Complex T [placeholder extension] |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1280 | case Type::Complex: |
| 1281 | if (const ComplexType *ComplexArg = Arg->getAs<ComplexType>()) |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1282 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1283 | cast<ComplexType>(Param)->getElementType(), |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1284 | ComplexArg->getElementType(), |
| 1285 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1286 | |
| 1287 | return Sema::TDK_NonDeducedMismatch; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1288 | |
| 1289 | // _Atomic T [extension] |
| 1290 | case Type::Atomic: |
| 1291 | if (const AtomicType *AtomicArg = Arg->getAs<AtomicType>()) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1292 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1293 | cast<AtomicType>(Param)->getValueType(), |
| 1294 | AtomicArg->getValueType(), |
| 1295 | Info, Deduced, TDF); |
| 1296 | |
| 1297 | return Sema::TDK_NonDeducedMismatch; |
| 1298 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1299 | // T * |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1300 | case Type::Pointer: { |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1301 | QualType PointeeType; |
| 1302 | if (const PointerType *PointerArg = Arg->getAs<PointerType>()) { |
| 1303 | PointeeType = PointerArg->getPointeeType(); |
| 1304 | } else if (const ObjCObjectPointerType *PointerArg |
| 1305 | = Arg->getAs<ObjCObjectPointerType>()) { |
| 1306 | PointeeType = PointerArg->getPointeeType(); |
| 1307 | } else { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1308 | return Sema::TDK_NonDeducedMismatch; |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1309 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1310 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1311 | unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1312 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1313 | cast<PointerType>(Param)->getPointeeType(), |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1314 | PointeeType, |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1315 | Info, Deduced, SubTDF); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1316 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1317 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1318 | // T & |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1319 | case Type::LValueReference: { |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 1320 | const LValueReferenceType *ReferenceArg = |
| 1321 | Arg->getAs<LValueReferenceType>(); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1322 | if (!ReferenceArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1323 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1324 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1325 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1326 | cast<LValueReferenceType>(Param)->getPointeeType(), |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1327 | ReferenceArg->getPointeeType(), Info, Deduced, 0); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1328 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1329 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1330 | // T && [C++0x] |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1331 | case Type::RValueReference: { |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 1332 | const RValueReferenceType *ReferenceArg = |
| 1333 | Arg->getAs<RValueReferenceType>(); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1334 | if (!ReferenceArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1335 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1336 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1337 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1338 | cast<RValueReferenceType>(Param)->getPointeeType(), |
| 1339 | ReferenceArg->getPointeeType(), |
| 1340 | Info, Deduced, 0); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1341 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1342 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1343 | // T [] (implied, but not stated explicitly) |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1344 | case Type::IncompleteArray: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1345 | const IncompleteArrayType *IncompleteArrayArg = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1346 | S.Context.getAsIncompleteArrayType(Arg); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1347 | if (!IncompleteArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1348 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1349 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1350 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1351 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1352 | S.Context.getAsIncompleteArrayType(Param)->getElementType(), |
| 1353 | IncompleteArrayArg->getElementType(), |
| 1354 | Info, Deduced, SubTDF); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1355 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1356 | |
| 1357 | // T [integer-constant] |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1358 | case Type::ConstantArray: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1359 | const ConstantArrayType *ConstantArrayArg = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1360 | S.Context.getAsConstantArrayType(Arg); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1361 | if (!ConstantArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1362 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1363 | |
| 1364 | const ConstantArrayType *ConstantArrayParm = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1365 | S.Context.getAsConstantArrayType(Param); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1366 | if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1367 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1368 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1369 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1370 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1371 | ConstantArrayParm->getElementType(), |
| 1372 | ConstantArrayArg->getElementType(), |
| 1373 | Info, Deduced, SubTDF); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1376 | // type [i] |
| 1377 | case Type::DependentSizedArray: { |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1378 | const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1379 | if (!ArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1380 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1381 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1382 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
| 1383 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1384 | // Check the element type of the arrays |
| 1385 | const DependentSizedArrayType *DependentArrayParm |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1386 | = S.Context.getAsDependentSizedArrayType(Param); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1387 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1388 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1389 | DependentArrayParm->getElementType(), |
| 1390 | ArrayArg->getElementType(), |
| 1391 | Info, Deduced, SubTDF)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1392 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1393 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1394 | // Determine the array bound is something we can deduce. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1395 | NonTypeTemplateParmDecl *NTTP |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1396 | = getDeducedParameterFromExpr(Info, DependentArrayParm->getSizeExpr()); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1397 | if (!NTTP) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1398 | return Sema::TDK_Success; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1399 | |
| 1400 | // We can perform template argument deduction for the given non-type |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1401 | // template parameter. |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1402 | assert(NTTP->getDepth() == Info.getDeducedDepth() && |
| 1403 | "saw non-type template parameter with wrong depth"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | if (const ConstantArrayType *ConstantArrayArg |
Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1405 | = dyn_cast<ConstantArrayType>(ArrayArg)) { |
| 1406 | llvm::APSInt Size(ConstantArrayArg->getSize()); |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1407 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, Size, |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1408 | S.Context.getSizeType(), |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1409 | /*ArrayBound=*/true, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1410 | Info, Deduced); |
Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1411 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1412 | if (const DependentSizedArrayType *DependentArrayArg |
| 1413 | = dyn_cast<DependentSizedArrayType>(ArrayArg)) |
Douglas Gregor | 7a49ead | 2010-12-22 23:15:38 +0000 | [diff] [blame] | 1414 | if (DependentArrayArg->getSizeExpr()) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1415 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
Douglas Gregor | 7a49ead | 2010-12-22 23:15:38 +0000 | [diff] [blame] | 1416 | DependentArrayArg->getSizeExpr(), |
| 1417 | Info, Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1419 | // Incomplete type does not match a dependently-sized array type |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1420 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1421 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1422 | |
| 1423 | // type(*)(T) |
| 1424 | // T(*)() |
| 1425 | // T(*)(T) |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1426 | case Type::FunctionProto: { |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1427 | unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | const FunctionProtoType *FunctionProtoArg = |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1429 | dyn_cast<FunctionProtoType>(Arg); |
| 1430 | if (!FunctionProtoArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1431 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1432 | |
| 1433 | const FunctionProtoType *FunctionProtoParam = |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1434 | cast<FunctionProtoType>(Param); |
Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1435 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1436 | if (FunctionProtoParam->getTypeQuals() |
Douglas Gregor | 54e462a | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1437 | != FunctionProtoArg->getTypeQuals() || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1438 | FunctionProtoParam->getRefQualifier() |
Douglas Gregor | 54e462a | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1439 | != FunctionProtoArg->getRefQualifier() || |
| 1440 | FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1441 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1442 | |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1443 | // Check return types. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1444 | if (Sema::TemplateDeductionResult Result = |
| 1445 | DeduceTemplateArgumentsByTypeMatch( |
| 1446 | S, TemplateParams, FunctionProtoParam->getReturnType(), |
| 1447 | FunctionProtoArg->getReturnType(), Info, Deduced, 0)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1448 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1449 | |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1450 | return DeduceTemplateArguments( |
| 1451 | S, TemplateParams, FunctionProtoParam->param_type_begin(), |
| 1452 | FunctionProtoParam->getNumParams(), |
| 1453 | FunctionProtoArg->param_type_begin(), |
| 1454 | FunctionProtoArg->getNumParams(), Info, Deduced, SubTDF); |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1455 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1456 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1457 | case Type::InjectedClassName: { |
| 1458 | // Treat a template's injected-class-name as if the template |
| 1459 | // specialization type had been used. |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1460 | Param = cast<InjectedClassNameType>(Param) |
| 1461 | ->getInjectedSpecializationType(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1462 | assert(isa<TemplateSpecializationType>(Param) && |
| 1463 | "injected class name is not a template specialization type"); |
| 1464 | // fall through |
| 1465 | } |
| 1466 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1467 | // template-name<T> (where template-name refers to a class template) |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1468 | // template-name<i> |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1469 | // TT<T> |
| 1470 | // TT<i> |
| 1471 | // TT<> |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1472 | case Type::TemplateSpecialization: { |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1473 | const TemplateSpecializationType *SpecParam = |
| 1474 | cast<TemplateSpecializationType>(Param); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1475 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1476 | // When Arg cannot be a derived class, we can just try to deduce template |
| 1477 | // arguments from the template-id. |
| 1478 | const RecordType *RecordT = Arg->getAs<RecordType>(); |
| 1479 | if (!(TDF & TDF_DerivedClass) || !RecordT) |
| 1480 | return DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg, Info, |
| 1481 | Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1483 | SmallVector<DeducedTemplateArgument, 8> DeducedOrig(Deduced.begin(), |
| 1484 | Deduced.end()); |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1485 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1486 | Sema::TemplateDeductionResult Result = DeduceTemplateArguments( |
| 1487 | S, TemplateParams, SpecParam, Arg, Info, Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1488 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1489 | if (Result == Sema::TDK_Success) |
| 1490 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1491 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1492 | // We cannot inspect base classes as part of deduction when the type |
| 1493 | // is incomplete, so either instantiate any templates necessary to |
| 1494 | // complete the type, or skip over it if it cannot be completed. |
| 1495 | if (!S.isCompleteType(Info.getLocation(), Arg)) |
| 1496 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1497 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1498 | // C++14 [temp.deduct.call] p4b3: |
| 1499 | // If P is a class and P has the form simple-template-id, then the |
| 1500 | // transformed A can be a derived class of the deduced A. Likewise if |
| 1501 | // P is a pointer to a class of the form simple-template-id, the |
| 1502 | // transformed A can be a pointer to a derived class pointed to by the |
| 1503 | // deduced A. |
| 1504 | // |
| 1505 | // These alternatives are considered only if type deduction would |
| 1506 | // otherwise fail. If they yield more than one possible deduced A, the |
| 1507 | // type deduction fails. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1508 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1509 | // Reset the incorrectly deduced argument from above. |
| 1510 | Deduced = DeducedOrig; |
| 1511 | |
| 1512 | // Use data recursion to crawl through the list of base classes. |
| 1513 | // Visited contains the set of nodes we have already visited, while |
| 1514 | // ToVisit is our stack of records that we still need to visit. |
| 1515 | llvm::SmallPtrSet<const RecordType *, 8> Visited; |
| 1516 | SmallVector<const RecordType *, 8> ToVisit; |
| 1517 | ToVisit.push_back(RecordT); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1518 | bool Successful = false; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1519 | SmallVector<DeducedTemplateArgument, 8> SuccessfulDeduced; |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1520 | while (!ToVisit.empty()) { |
| 1521 | // Retrieve the next class in the inheritance hierarchy. |
| 1522 | const RecordType *NextT = ToVisit.pop_back_val(); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1523 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1524 | // If we have already seen this type, skip it. |
| 1525 | if (!Visited.insert(NextT).second) |
| 1526 | continue; |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1527 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1528 | // If this is a base class, try to perform template argument |
| 1529 | // deduction from it. |
| 1530 | if (NextT != RecordT) { |
| 1531 | TemplateDeductionInfo BaseInfo(Info.getLocation()); |
| 1532 | Sema::TemplateDeductionResult BaseResult = |
| 1533 | DeduceTemplateArguments(S, TemplateParams, SpecParam, |
| 1534 | QualType(NextT, 0), BaseInfo, Deduced); |
| 1535 | |
| 1536 | // If template argument deduction for this base was successful, |
| 1537 | // note that we had some success. Otherwise, ignore any deductions |
| 1538 | // from this base class. |
| 1539 | if (BaseResult == Sema::TDK_Success) { |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1540 | // If we've already seen some success, then deduction fails due to |
| 1541 | // an ambiguity (temp.deduct.call p5). |
| 1542 | if (Successful) |
| 1543 | return Sema::TDK_MiscellaneousDeductionFailure; |
| 1544 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1545 | Successful = true; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1546 | std::swap(SuccessfulDeduced, Deduced); |
| 1547 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1548 | Info.Param = BaseInfo.Param; |
| 1549 | Info.FirstArg = BaseInfo.FirstArg; |
| 1550 | Info.SecondArg = BaseInfo.SecondArg; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | Deduced = DeducedOrig; |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1554 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1556 | // Visit base classes |
| 1557 | CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl()); |
| 1558 | for (const auto &Base : Next->bases()) { |
| 1559 | assert(Base.getType()->isRecordType() && |
| 1560 | "Base class that isn't a record?"); |
| 1561 | ToVisit.push_back(Base.getType()->getAs<RecordType>()); |
| 1562 | } |
| 1563 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1564 | |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1565 | if (Successful) { |
| 1566 | std::swap(SuccessfulDeduced, Deduced); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1567 | return Sema::TDK_Success; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1568 | } |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1569 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1570 | return Result; |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1573 | // T type::* |
| 1574 | // T T::* |
| 1575 | // T (type::*)() |
| 1576 | // type (T::*)() |
| 1577 | // type (type::*)(T) |
| 1578 | // type (T::*)(T) |
| 1579 | // T (type::*)(T) |
| 1580 | // T (T::*)() |
| 1581 | // T (T::*)(T) |
| 1582 | case Type::MemberPointer: { |
| 1583 | const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); |
| 1584 | const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); |
| 1585 | if (!MemPtrArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1586 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1587 | |
David Majnemer | a381cda | 2015-11-30 20:34:28 +0000 | [diff] [blame] | 1588 | QualType ParamPointeeType = MemPtrParam->getPointeeType(); |
| 1589 | if (ParamPointeeType->isFunctionType()) |
| 1590 | S.adjustMemberFunctionCC(ParamPointeeType, /*IsStatic=*/true, |
| 1591 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
| 1592 | QualType ArgPointeeType = MemPtrArg->getPointeeType(); |
| 1593 | if (ArgPointeeType->isFunctionType()) |
| 1594 | S.adjustMemberFunctionCC(ArgPointeeType, /*IsStatic=*/true, |
| 1595 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
| 1596 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1597 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1598 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
David Majnemer | a381cda | 2015-11-30 20:34:28 +0000 | [diff] [blame] | 1599 | ParamPointeeType, |
| 1600 | ArgPointeeType, |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1601 | Info, Deduced, |
| 1602 | TDF & TDF_IgnoreQualifiers)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1603 | return Result; |
| 1604 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1605 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1606 | QualType(MemPtrParam->getClass(), 0), |
| 1607 | QualType(MemPtrArg->getClass(), 0), |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1608 | Info, Deduced, |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1609 | TDF & TDF_IgnoreQualifiers); |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
Anders Carlsson | 15f1dd1 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1612 | // (clang extension) |
| 1613 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1614 | // type(^)(T) |
| 1615 | // T(^)() |
| 1616 | // T(^)(T) |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1617 | case Type::BlockPointer: { |
| 1618 | const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); |
| 1619 | const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1620 | |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1621 | if (!BlockPtrArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1622 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1623 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1624 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1625 | BlockPtrParam->getPointeeType(), |
| 1626 | BlockPtrArg->getPointeeType(), |
| 1627 | Info, Deduced, 0); |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1630 | // (clang extension) |
| 1631 | // |
| 1632 | // T __attribute__(((ext_vector_type(<integral constant>)))) |
| 1633 | case Type::ExtVector: { |
| 1634 | const ExtVectorType *VectorParam = cast<ExtVectorType>(Param); |
| 1635 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1636 | // Make sure that the vectors have the same number of elements. |
| 1637 | if (VectorParam->getNumElements() != VectorArg->getNumElements()) |
| 1638 | return Sema::TDK_NonDeducedMismatch; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1639 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1640 | // Perform deduction on the element types. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1641 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1642 | VectorParam->getElementType(), |
| 1643 | VectorArg->getElementType(), |
| 1644 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1645 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1646 | |
| 1647 | if (const DependentSizedExtVectorType *VectorArg |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1648 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1649 | // We can't check the number of elements, since the argument has a |
| 1650 | // dependent number of elements. This can only occur during partial |
| 1651 | // ordering. |
| 1652 | |
| 1653 | // Perform deduction on the element types. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1654 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1655 | VectorParam->getElementType(), |
| 1656 | VectorArg->getElementType(), |
| 1657 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1658 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1659 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1660 | return Sema::TDK_NonDeducedMismatch; |
| 1661 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1662 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1663 | // (clang extension) |
| 1664 | // |
| 1665 | // T __attribute__(((ext_vector_type(N)))) |
| 1666 | case Type::DependentSizedExtVector: { |
| 1667 | const DependentSizedExtVectorType *VectorParam |
| 1668 | = cast<DependentSizedExtVectorType>(Param); |
| 1669 | |
| 1670 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1671 | // Perform deduction on the element types. |
| 1672 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1673 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1674 | VectorParam->getElementType(), |
| 1675 | VectorArg->getElementType(), |
| 1676 | Info, Deduced, TDF)) |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1677 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1678 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1679 | // Perform deduction on the vector size, if we can. |
| 1680 | NonTypeTemplateParmDecl *NTTP |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1681 | = getDeducedParameterFromExpr(Info, VectorParam->getSizeExpr()); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1682 | if (!NTTP) |
| 1683 | return Sema::TDK_Success; |
| 1684 | |
| 1685 | llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); |
| 1686 | ArgSize = VectorArg->getNumElements(); |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1687 | // Note that we use the "array bound" rules here; just like in that |
| 1688 | // case, we don't have any particular type for the vector size, but |
| 1689 | // we can provide one if necessary. |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1690 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1691 | S.Context.IntTy, true, Info, |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 1692 | Deduced); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1693 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1694 | |
| 1695 | if (const DependentSizedExtVectorType *VectorArg |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1696 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1697 | // Perform deduction on the element types. |
| 1698 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1699 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1700 | VectorParam->getElementType(), |
| 1701 | VectorArg->getElementType(), |
| 1702 | Info, Deduced, TDF)) |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1703 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1704 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1705 | // Perform deduction on the vector size, if we can. |
| 1706 | NonTypeTemplateParmDecl *NTTP |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1707 | = getDeducedParameterFromExpr(Info, VectorParam->getSizeExpr()); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1708 | if (!NTTP) |
| 1709 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1710 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1711 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 1712 | VectorArg->getSizeExpr(), |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1713 | Info, Deduced); |
| 1714 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1715 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1716 | return Sema::TDK_NonDeducedMismatch; |
| 1717 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1718 | |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1719 | case Type::TypeOfExpr: |
| 1720 | case Type::TypeOf: |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 1721 | case Type::DependentName: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1722 | case Type::UnresolvedUsing: |
| 1723 | case Type::Decltype: |
| 1724 | case Type::UnaryTransform: |
| 1725 | case Type::Auto: |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1726 | case Type::DeducedTemplateSpecialization: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1727 | case Type::DependentTemplateSpecialization: |
| 1728 | case Type::PackExpansion: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 1729 | case Type::Pipe: |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1730 | // No template argument deduction for these types |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1731 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1734 | llvm_unreachable("Invalid Type Class!"); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1737 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1738 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1739 | TemplateParameterList *TemplateParams, |
| 1740 | const TemplateArgument &Param, |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1741 | TemplateArgument Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1742 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 1743 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1744 | // If the template argument is a pack expansion, perform template argument |
| 1745 | // deduction against the pattern of that expansion. This only occurs during |
| 1746 | // partial ordering. |
| 1747 | if (Arg.isPackExpansion()) |
| 1748 | Arg = Arg.getPackExpansionPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1749 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1750 | switch (Param.getKind()) { |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1751 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1752 | llvm_unreachable("Null template argument in parameter list"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1753 | |
| 1754 | case TemplateArgument::Type: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1755 | if (Arg.getKind() == TemplateArgument::Type) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1756 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1757 | Param.getAsType(), |
| 1758 | Arg.getAsType(), |
| 1759 | Info, Deduced, 0); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1760 | Info.FirstArg = Param; |
| 1761 | Info.SecondArg = Arg; |
| 1762 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1763 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1764 | case TemplateArgument::Template: |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1765 | if (Arg.getKind() == TemplateArgument::Template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1766 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1767 | Param.getAsTemplate(), |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1768 | Arg.getAsTemplate(), Info, Deduced); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1769 | Info.FirstArg = Param; |
| 1770 | Info.SecondArg = Arg; |
| 1771 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1772 | |
| 1773 | case TemplateArgument::TemplateExpansion: |
| 1774 | llvm_unreachable("caller should handle pack expansions"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1775 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1776 | case TemplateArgument::Declaration: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1777 | if (Arg.getKind() == TemplateArgument::Declaration && |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 1778 | isSameDeclaration(Param.getAsDecl(), Arg.getAsDecl())) |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 1779 | return Sema::TDK_Success; |
| 1780 | |
| 1781 | Info.FirstArg = Param; |
| 1782 | Info.SecondArg = Arg; |
| 1783 | return Sema::TDK_NonDeducedMismatch; |
| 1784 | |
| 1785 | case TemplateArgument::NullPtr: |
| 1786 | if (Arg.getKind() == TemplateArgument::NullPtr && |
| 1787 | S.Context.hasSameType(Param.getNullPtrType(), Arg.getNullPtrType())) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1788 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1789 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1790 | Info.FirstArg = Param; |
| 1791 | Info.SecondArg = Arg; |
| 1792 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1793 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1794 | case TemplateArgument::Integral: |
| 1795 | if (Arg.getKind() == TemplateArgument::Integral) { |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1796 | if (hasSameExtendedValue(Param.getAsIntegral(), Arg.getAsIntegral())) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1797 | return Sema::TDK_Success; |
| 1798 | |
| 1799 | Info.FirstArg = Param; |
| 1800 | Info.SecondArg = Arg; |
| 1801 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1802 | } |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1803 | |
| 1804 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 1805 | Info.FirstArg = Param; |
| 1806 | Info.SecondArg = Arg; |
| 1807 | return Sema::TDK_NonDeducedMismatch; |
| 1808 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1809 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1810 | Info.FirstArg = Param; |
| 1811 | Info.SecondArg = Arg; |
| 1812 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1813 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1814 | case TemplateArgument::Expression: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1815 | if (NonTypeTemplateParmDecl *NTTP |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1816 | = getDeducedParameterFromExpr(Info, Param.getAsExpr())) { |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1817 | if (Arg.getKind() == TemplateArgument::Integral) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1818 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1819 | Arg.getAsIntegral(), |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1820 | Arg.getIntegralType(), |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1821 | /*ArrayBound=*/false, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1822 | Info, Deduced); |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 1823 | if (Arg.getKind() == TemplateArgument::NullPtr) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1824 | return DeduceNullPtrTemplateArgument(S, TemplateParams, NTTP, |
| 1825 | Arg.getNullPtrType(), |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 1826 | Info, Deduced); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1827 | if (Arg.getKind() == TemplateArgument::Expression) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1828 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 1829 | Arg.getAsExpr(), Info, Deduced); |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 1830 | if (Arg.getKind() == TemplateArgument::Declaration) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1831 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 1832 | Arg.getAsDecl(), |
| 1833 | Arg.getParamTypeForDecl(), |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 1834 | Info, Deduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1835 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1836 | Info.FirstArg = Param; |
| 1837 | Info.SecondArg = Arg; |
| 1838 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1839 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1840 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1841 | // Can't deduce anything, but that's okay. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1842 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1843 | } |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 1844 | case TemplateArgument::Pack: |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1845 | llvm_unreachable("Argument packs should be expanded by the caller!"); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1846 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1847 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1848 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1851 | /// \brief Determine whether there is a template argument to be used for |
| 1852 | /// deduction. |
| 1853 | /// |
| 1854 | /// This routine "expands" argument packs in-place, overriding its input |
| 1855 | /// parameters so that \c Args[ArgIdx] will be the available template argument. |
| 1856 | /// |
| 1857 | /// \returns true if there is another template argument (which will be at |
| 1858 | /// \c Args[ArgIdx]), false otherwise. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1859 | static bool hasTemplateArgumentForDeduction(ArrayRef<TemplateArgument> &Args, |
| 1860 | unsigned &ArgIdx) { |
| 1861 | if (ArgIdx == Args.size()) |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1862 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1863 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1864 | const TemplateArgument &Arg = Args[ArgIdx]; |
| 1865 | if (Arg.getKind() != TemplateArgument::Pack) |
| 1866 | return true; |
| 1867 | |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1868 | assert(ArgIdx == Args.size() - 1 && "Pack not at the end of argument list?"); |
| 1869 | Args = Arg.pack_elements(); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1870 | ArgIdx = 0; |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1871 | return ArgIdx < Args.size(); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1872 | } |
| 1873 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1874 | /// \brief Determine whether the given set of template arguments has a pack |
| 1875 | /// expansion that is not the last template argument. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1876 | static bool hasPackExpansionBeforeEnd(ArrayRef<TemplateArgument> Args) { |
| 1877 | bool FoundPackExpansion = false; |
| 1878 | for (const auto &A : Args) { |
| 1879 | if (FoundPackExpansion) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1880 | return true; |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1881 | |
| 1882 | if (A.getKind() == TemplateArgument::Pack) |
| 1883 | return hasPackExpansionBeforeEnd(A.pack_elements()); |
| 1884 | |
| 1885 | if (A.isPackExpansion()) |
| 1886 | FoundPackExpansion = true; |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1887 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1888 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1889 | return false; |
| 1890 | } |
| 1891 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1892 | static Sema::TemplateDeductionResult |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1893 | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1894 | ArrayRef<TemplateArgument> Params, |
| 1895 | ArrayRef<TemplateArgument> Args, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1896 | TemplateDeductionInfo &Info, |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1897 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 1898 | bool NumberOfArgumentsMustMatch) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1899 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1900 | // If the template argument list of P contains a pack expansion that is not |
| 1901 | // the last template argument, the entire template argument list is a |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1902 | // non-deduced context. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1903 | if (hasPackExpansionBeforeEnd(Params)) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1904 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1905 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1906 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1907 | // If P has a form that contains <T> or <i>, then each argument Pi of the |
| 1908 | // respective template argument list P is compared with the corresponding |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1909 | // argument Ai of the corresponding template argument list of A. |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1910 | unsigned ArgIdx = 0, ParamIdx = 0; |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1911 | for (; hasTemplateArgumentForDeduction(Params, ParamIdx); ++ParamIdx) { |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1912 | if (!Params[ParamIdx].isPackExpansion()) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1913 | // The simple case: deduce template arguments by matching Pi and Ai. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1914 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1915 | // Check whether we have enough arguments. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1916 | if (!hasTemplateArgumentForDeduction(Args, ArgIdx)) |
Richard Smith | ec7176e | 2017-01-05 02:31:32 +0000 | [diff] [blame] | 1917 | return NumberOfArgumentsMustMatch |
| 1918 | ? Sema::TDK_MiscellaneousDeductionFailure |
| 1919 | : Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1920 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 1921 | // C++1z [temp.deduct.type]p9: |
| 1922 | // During partial ordering, if Ai was originally a pack expansion [and] |
| 1923 | // Pi is not a pack expansion, template argument deduction fails. |
| 1924 | if (Args[ArgIdx].isPackExpansion()) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 1925 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1926 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1927 | // Perform deduction for this Pi/Ai pair. |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1928 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1929 | = DeduceTemplateArguments(S, TemplateParams, |
| 1930 | Params[ParamIdx], Args[ArgIdx], |
| 1931 | Info, Deduced)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1932 | return Result; |
| 1933 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1934 | // Move to the next argument. |
| 1935 | ++ArgIdx; |
| 1936 | continue; |
| 1937 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1938 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1939 | // The parameter is a pack expansion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1940 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1941 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1942 | // If Pi is a pack expansion, then the pattern of Pi is compared with |
| 1943 | // each remaining argument in the template argument list of A. Each |
| 1944 | // comparison deduces template arguments for subsequent positions in the |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1945 | // template parameter packs expanded by Pi. |
| 1946 | TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1947 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1948 | // FIXME: If there are no remaining arguments, we can bail out early |
| 1949 | // and set any deduced parameter packs to an empty argument pack. |
| 1950 | // The latter part of this is a (minor) correctness issue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1951 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1952 | // Prepare to deduce the packs within the pattern. |
| 1953 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1954 | |
| 1955 | // Keep track of the deduced template arguments for each parameter pack |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1956 | // expanded by this pack expansion (the outer index) and for each |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1957 | // template argument (the inner SmallVectors). |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1958 | for (; hasTemplateArgumentForDeduction(Args, ArgIdx); ++ArgIdx) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1959 | // Deduce template arguments from the pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1960 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1961 | = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx], |
| 1962 | Info, Deduced)) |
| 1963 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1964 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1965 | PackScope.nextPackElement(); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1966 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1967 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1968 | // Build argument packs for each of the parameter packs expanded by this |
| 1969 | // pack expansion. |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 1970 | if (auto Result = PackScope.finish()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1971 | return Result; |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1972 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1973 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1974 | return Sema::TDK_Success; |
| 1975 | } |
| 1976 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1977 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1978 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1979 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1980 | const TemplateArgumentList &ParamList, |
| 1981 | const TemplateArgumentList &ArgList, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1982 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 1983 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1984 | return DeduceTemplateArguments(S, TemplateParams, ParamList.asArray(), |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 1985 | ArgList.asArray(), Info, Deduced, |
| 1986 | /*NumberOfArgumentsMustMatch*/false); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1989 | /// \brief Determine whether two template arguments are the same. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1990 | static bool isSameTemplateArg(ASTContext &Context, |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 1991 | TemplateArgument X, |
| 1992 | const TemplateArgument &Y, |
| 1993 | bool PackExpansionMatchesPack = false) { |
| 1994 | // If we're checking deduced arguments (X) against original arguments (Y), |
| 1995 | // we will have flattened packs to non-expansions in X. |
| 1996 | if (PackExpansionMatchesPack && X.isPackExpansion() && !Y.isPackExpansion()) |
| 1997 | X = X.getPackExpansionPattern(); |
| 1998 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1999 | if (X.getKind() != Y.getKind()) |
| 2000 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2001 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2002 | switch (X.getKind()) { |
| 2003 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2004 | llvm_unreachable("Comparing NULL template argument"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2005 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2006 | case TemplateArgument::Type: |
| 2007 | return Context.getCanonicalType(X.getAsType()) == |
| 2008 | Context.getCanonicalType(Y.getAsType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2010 | case TemplateArgument::Declaration: |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 2011 | return isSameDeclaration(X.getAsDecl(), Y.getAsDecl()); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2012 | |
| 2013 | case TemplateArgument::NullPtr: |
| 2014 | return Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2015 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2016 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2017 | case TemplateArgument::TemplateExpansion: |
| 2018 | return Context.getCanonicalTemplateName( |
| 2019 | X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() == |
| 2020 | Context.getCanonicalTemplateName( |
| 2021 | Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2022 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2023 | case TemplateArgument::Integral: |
Richard Smith | 993f203 | 2016-12-25 20:21:12 +0000 | [diff] [blame] | 2024 | return hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2025 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2026 | case TemplateArgument::Expression: { |
| 2027 | llvm::FoldingSetNodeID XID, YID; |
| 2028 | X.getAsExpr()->Profile(XID, Context, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2029 | Y.getAsExpr()->Profile(YID, Context, true); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2030 | return XID == YID; |
| 2031 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2032 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2033 | case TemplateArgument::Pack: |
| 2034 | if (X.pack_size() != Y.pack_size()) |
| 2035 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2036 | |
| 2037 | for (TemplateArgument::pack_iterator XP = X.pack_begin(), |
| 2038 | XPEnd = X.pack_end(), |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2039 | YP = Y.pack_begin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2040 | XP != XPEnd; ++XP, ++YP) |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2041 | if (!isSameTemplateArg(Context, *XP, *YP, PackExpansionMatchesPack)) |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2042 | return false; |
| 2043 | |
| 2044 | return true; |
| 2045 | } |
| 2046 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2047 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2048 | } |
| 2049 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2050 | /// \brief Allocate a TemplateArgumentLoc where all locations have |
| 2051 | /// been initialized to the given location. |
| 2052 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2053 | /// \param Arg The template argument we are producing template argument |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2054 | /// location information for. |
| 2055 | /// |
| 2056 | /// \param NTTPType For a declaration template argument, the type of |
| 2057 | /// the non-type template parameter that corresponds to this template |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2058 | /// argument. Can be null if no type sugar is available to add to the |
| 2059 | /// type from the template argument. |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2060 | /// |
| 2061 | /// \param Loc The source location to use for the resulting template |
| 2062 | /// argument. |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2063 | TemplateArgumentLoc |
| 2064 | Sema::getTrivialTemplateArgumentLoc(const TemplateArgument &Arg, |
| 2065 | QualType NTTPType, SourceLocation Loc) { |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2066 | switch (Arg.getKind()) { |
| 2067 | case TemplateArgument::Null: |
| 2068 | llvm_unreachable("Can't get a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2069 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2070 | case TemplateArgument::Type: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2071 | return TemplateArgumentLoc( |
| 2072 | Arg, Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2073 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2074 | case TemplateArgument::Declaration: { |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2075 | if (NTTPType.isNull()) |
| 2076 | NTTPType = Arg.getParamTypeForDecl(); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2077 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
| 2078 | .getAs<Expr>(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2079 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2080 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2081 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2082 | case TemplateArgument::NullPtr: { |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2083 | if (NTTPType.isNull()) |
| 2084 | NTTPType = Arg.getNullPtrType(); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2085 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
| 2086 | .getAs<Expr>(); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2087 | return TemplateArgumentLoc(TemplateArgument(NTTPType, /*isNullPtr*/true), |
| 2088 | E); |
| 2089 | } |
| 2090 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2091 | case TemplateArgument::Integral: { |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2092 | Expr *E = |
| 2093 | BuildExpressionFromIntegralTemplateArgument(Arg, Loc).getAs<Expr>(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2094 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2095 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2096 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2097 | case TemplateArgument::Template: |
| 2098 | case TemplateArgument::TemplateExpansion: { |
| 2099 | NestedNameSpecifierLocBuilder Builder; |
| 2100 | TemplateName Template = Arg.getAsTemplate(); |
| 2101 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2102 | Builder.MakeTrivial(Context, DTN->getQualifier(), Loc); |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 2103 | else if (QualifiedTemplateName *QTN = |
| 2104 | Template.getAsQualifiedTemplateName()) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2105 | Builder.MakeTrivial(Context, QTN->getQualifier(), Loc); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2106 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2107 | if (Arg.getKind() == TemplateArgument::Template) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2108 | return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(Context), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2109 | Loc); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2110 | |
| 2111 | return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(Context), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2112 | Loc, Loc); |
| 2113 | } |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2114 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2115 | case TemplateArgument::Expression: |
| 2116 | return TemplateArgumentLoc(Arg, Arg.getAsExpr()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2117 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2118 | case TemplateArgument::Pack: |
| 2119 | return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo()); |
| 2120 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2121 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2122 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | |
| 2126 | /// \brief Convert the given deduced template argument and add it to the set of |
| 2127 | /// fully-converted template arguments. |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2128 | static bool |
| 2129 | ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param, |
| 2130 | DeducedTemplateArgument Arg, |
| 2131 | NamedDecl *Template, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2132 | TemplateDeductionInfo &Info, |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2133 | bool IsDeduced, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2134 | SmallVectorImpl<TemplateArgument> &Output) { |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2135 | auto ConvertArg = [&](DeducedTemplateArgument Arg, |
| 2136 | unsigned ArgumentPackIndex) { |
| 2137 | // Convert the deduced template argument into a template |
| 2138 | // argument that we can check, almost as if the user had written |
| 2139 | // the template argument explicitly. |
| 2140 | TemplateArgumentLoc ArgLoc = |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2141 | S.getTrivialTemplateArgumentLoc(Arg, QualType(), Info.getLocation()); |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2142 | |
| 2143 | // Check the template argument, converting it as necessary. |
| 2144 | return S.CheckTemplateArgument( |
| 2145 | Param, ArgLoc, Template, Template->getLocation(), |
| 2146 | Template->getSourceRange().getEnd(), ArgumentPackIndex, Output, |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2147 | IsDeduced |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2148 | ? (Arg.wasDeducedFromArrayBound() ? Sema::CTAK_DeducedFromArrayBound |
| 2149 | : Sema::CTAK_Deduced) |
| 2150 | : Sema::CTAK_Specified); |
| 2151 | }; |
| 2152 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2153 | if (Arg.getKind() == TemplateArgument::Pack) { |
| 2154 | // This is a template argument pack, so check each of its arguments against |
| 2155 | // the template parameter. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2156 | SmallVector<TemplateArgument, 2> PackedArgsBuilder; |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 2157 | for (const auto &P : Arg.pack_elements()) { |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2158 | // When converting the deduced template argument, append it to the |
| 2159 | // general output list. We need to do this so that the template argument |
| 2160 | // checking logic has all of the prior template arguments available. |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 2161 | DeducedTemplateArgument InnerArg(P); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2162 | InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound()); |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2163 | assert(InnerArg.getKind() != TemplateArgument::Pack && |
| 2164 | "deduced nested pack"); |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 2165 | if (P.isNull()) { |
| 2166 | // We deduced arguments for some elements of this pack, but not for |
| 2167 | // all of them. This happens if we get a conditionally-non-deduced |
| 2168 | // context in a pack expansion (such as an overload set in one of the |
| 2169 | // arguments). |
| 2170 | S.Diag(Param->getLocation(), |
| 2171 | diag::err_template_arg_deduced_incomplete_pack) |
| 2172 | << Arg << Param; |
| 2173 | return true; |
| 2174 | } |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2175 | if (ConvertArg(InnerArg, PackedArgsBuilder.size())) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2176 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2177 | |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2178 | // Move the converted template argument into our argument pack. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 2179 | PackedArgsBuilder.push_back(Output.pop_back_val()); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2180 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2181 | |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2182 | // 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] | 2183 | // itself, in case that substitution fails. |
| 2184 | if (PackedArgsBuilder.empty()) { |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2185 | LocalInstantiationScope Scope(S); |
Richard Smith | e824775 | 2016-12-22 07:24:39 +0000 | [diff] [blame] | 2186 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Output); |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2187 | MultiLevelTemplateArgumentList Args(TemplateArgs); |
| 2188 | |
| 2189 | if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2190 | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
| 2191 | NTTP, Output, |
| 2192 | Template->getSourceRange()); |
Simon Pilgrim | 6f3e1ea | 2016-12-26 18:11:49 +0000 | [diff] [blame] | 2193 | if (Inst.isInvalid() || |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2194 | S.SubstType(NTTP->getType(), Args, NTTP->getLocation(), |
| 2195 | NTTP->getDeclName()).isNull()) |
| 2196 | return true; |
| 2197 | } else if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 2198 | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
| 2199 | TTP, Output, |
| 2200 | Template->getSourceRange()); |
| 2201 | if (Inst.isInvalid() || !S.SubstDecl(TTP, S.CurContext, Args)) |
| 2202 | return true; |
| 2203 | } |
| 2204 | // For type parameters, no substitution is ever required. |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2205 | } |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2206 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2207 | // Create the resulting argument pack. |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 2208 | Output.push_back( |
| 2209 | TemplateArgument::CreatePackCopy(S.Context, PackedArgsBuilder)); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2210 | return false; |
| 2211 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2212 | |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2213 | return ConvertArg(Arg, 0); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2216 | // FIXME: This should not be a template, but |
| 2217 | // ClassTemplatePartialSpecializationDecl sadly does not derive from |
| 2218 | // TemplateDecl. |
| 2219 | template<typename TemplateDeclT> |
| 2220 | static Sema::TemplateDeductionResult ConvertDeducedTemplateArguments( |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2221 | Sema &S, TemplateDeclT *Template, bool IsDeduced, |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2222 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2223 | TemplateDeductionInfo &Info, SmallVectorImpl<TemplateArgument> &Builder, |
| 2224 | LocalInstantiationScope *CurrentInstantiationScope = nullptr, |
| 2225 | unsigned NumAlreadyConverted = 0, bool PartialOverloading = false) { |
| 2226 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| 2227 | |
| 2228 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 2229 | NamedDecl *Param = TemplateParams->getParam(I); |
| 2230 | |
| 2231 | if (!Deduced[I].isNull()) { |
| 2232 | if (I < NumAlreadyConverted) { |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2233 | // We may have had explicitly-specified template arguments for a |
| 2234 | // template parameter pack (that may or may not have been extended |
| 2235 | // via additional deduced arguments). |
Richard Smith | 9c0c986 | 2017-01-05 20:27:28 +0000 | [diff] [blame] | 2236 | if (Param->isParameterPack() && CurrentInstantiationScope && |
| 2237 | CurrentInstantiationScope->getPartiallySubstitutedPack() == Param) { |
| 2238 | // Forget the partially-substituted pack; its substitution is now |
| 2239 | // complete. |
| 2240 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2241 | // We still need to check the argument in case it was extended by |
| 2242 | // deduction. |
| 2243 | } else { |
| 2244 | // We have already fully type-checked and converted this |
| 2245 | // argument, because it was explicitly-specified. Just record the |
| 2246 | // presence of this argument. |
| 2247 | Builder.push_back(Deduced[I]); |
| 2248 | continue; |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2249 | } |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2250 | } |
| 2251 | |
Richard Smith | 9c0c986 | 2017-01-05 20:27:28 +0000 | [diff] [blame] | 2252 | // We may have deduced this argument, so it still needs to be |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2253 | // checked and converted. |
| 2254 | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Template, Info, |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2255 | IsDeduced, Builder)) { |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2256 | Info.Param = makeTemplateParameter(Param); |
| 2257 | // FIXME: These template arguments are temporary. Free them! |
| 2258 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2259 | return Sema::TDK_SubstitutionFailure; |
| 2260 | } |
| 2261 | |
| 2262 | continue; |
| 2263 | } |
| 2264 | |
| 2265 | // C++0x [temp.arg.explicit]p3: |
| 2266 | // A trailing template parameter pack (14.5.3) not otherwise deduced will |
| 2267 | // be deduced to an empty sequence of template arguments. |
| 2268 | // FIXME: Where did the word "trailing" come from? |
| 2269 | if (Param->isTemplateParameterPack()) { |
| 2270 | // We may have had explicitly-specified template arguments for this |
| 2271 | // template parameter pack. If so, our empty deduction extends the |
| 2272 | // explicitly-specified set (C++0x [temp.arg.explicit]p9). |
| 2273 | const TemplateArgument *ExplicitArgs; |
| 2274 | unsigned NumExplicitArgs; |
| 2275 | if (CurrentInstantiationScope && |
| 2276 | CurrentInstantiationScope->getPartiallySubstitutedPack( |
| 2277 | &ExplicitArgs, &NumExplicitArgs) == Param) { |
| 2278 | Builder.push_back(TemplateArgument( |
| 2279 | llvm::makeArrayRef(ExplicitArgs, NumExplicitArgs))); |
| 2280 | |
| 2281 | // Forget the partially-substituted pack; its substitution is now |
| 2282 | // complete. |
| 2283 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2284 | } else { |
| 2285 | // Go through the motions of checking the empty argument pack against |
| 2286 | // the parameter pack. |
| 2287 | DeducedTemplateArgument DeducedPack(TemplateArgument::getEmptyPack()); |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2288 | if (ConvertDeducedTemplateArgument(S, Param, DeducedPack, Template, |
| 2289 | Info, IsDeduced, Builder)) { |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2290 | Info.Param = makeTemplateParameter(Param); |
| 2291 | // FIXME: These template arguments are temporary. Free them! |
| 2292 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2293 | return Sema::TDK_SubstitutionFailure; |
| 2294 | } |
| 2295 | } |
| 2296 | continue; |
| 2297 | } |
| 2298 | |
| 2299 | // Substitute into the default template argument, if available. |
| 2300 | bool HasDefaultArg = false; |
| 2301 | TemplateDecl *TD = dyn_cast<TemplateDecl>(Template); |
| 2302 | if (!TD) { |
| 2303 | assert(isa<ClassTemplatePartialSpecializationDecl>(Template)); |
| 2304 | return Sema::TDK_Incomplete; |
| 2305 | } |
| 2306 | |
| 2307 | TemplateArgumentLoc DefArg = S.SubstDefaultTemplateArgumentIfAvailable( |
| 2308 | TD, TD->getLocation(), TD->getSourceRange().getEnd(), Param, Builder, |
| 2309 | HasDefaultArg); |
| 2310 | |
| 2311 | // If there was no default argument, deduction is incomplete. |
| 2312 | if (DefArg.getArgument().isNull()) { |
| 2313 | Info.Param = makeTemplateParameter( |
| 2314 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
| 2315 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2316 | if (PartialOverloading) break; |
| 2317 | |
| 2318 | return HasDefaultArg ? Sema::TDK_SubstitutionFailure |
| 2319 | : Sema::TDK_Incomplete; |
| 2320 | } |
| 2321 | |
| 2322 | // Check whether we can actually use the default argument. |
| 2323 | if (S.CheckTemplateArgument(Param, DefArg, TD, TD->getLocation(), |
| 2324 | TD->getSourceRange().getEnd(), 0, Builder, |
| 2325 | Sema::CTAK_Specified)) { |
| 2326 | Info.Param = makeTemplateParameter( |
| 2327 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
| 2328 | // FIXME: These template arguments are temporary. Free them! |
| 2329 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2330 | return Sema::TDK_SubstitutionFailure; |
| 2331 | } |
| 2332 | |
| 2333 | // If we get here, we successfully used the default template argument. |
| 2334 | } |
| 2335 | |
| 2336 | return Sema::TDK_Success; |
| 2337 | } |
| 2338 | |
Benjamin Kramer | 357c9e1 | 2017-02-11 12:21:17 +0000 | [diff] [blame^] | 2339 | static DeclContext *getAsDeclContextOrEnclosing(Decl *D) { |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2340 | if (auto *DC = dyn_cast<DeclContext>(D)) |
| 2341 | return DC; |
| 2342 | return D->getDeclContext(); |
| 2343 | } |
| 2344 | |
| 2345 | template<typename T> struct IsPartialSpecialization { |
| 2346 | static constexpr bool value = false; |
| 2347 | }; |
| 2348 | template<> |
| 2349 | struct IsPartialSpecialization<ClassTemplatePartialSpecializationDecl> { |
| 2350 | static constexpr bool value = true; |
| 2351 | }; |
| 2352 | template<> |
| 2353 | struct IsPartialSpecialization<VarTemplatePartialSpecializationDecl> { |
| 2354 | static constexpr bool value = true; |
| 2355 | }; |
| 2356 | |
| 2357 | /// Complete template argument deduction for a partial specialization. |
| 2358 | template <typename T> |
| 2359 | static typename std::enable_if<IsPartialSpecialization<T>::value, |
| 2360 | Sema::TemplateDeductionResult>::type |
| 2361 | FinishTemplateArgumentDeduction( |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2362 | Sema &S, T *Partial, bool IsPartialOrdering, |
| 2363 | const TemplateArgumentList &TemplateArgs, |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2364 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2365 | TemplateDeductionInfo &Info) { |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2366 | // Unevaluated SFINAE context. |
| 2367 | EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2368 | Sema::SFINAETrap Trap(S); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2369 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2370 | Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Partial)); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2371 | |
| 2372 | // C++ [temp.deduct.type]p2: |
| 2373 | // [...] or if any template argument remains neither deduced nor |
| 2374 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2375 | SmallVector<TemplateArgument, 4> Builder; |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2376 | if (auto Result = ConvertDeducedTemplateArguments( |
| 2377 | S, Partial, IsPartialOrdering, Deduced, Info, Builder)) |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2378 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2379 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2380 | // Form the template argument list from the deduced template arguments. |
| 2381 | TemplateArgumentList *DeducedArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2382 | = TemplateArgumentList::CreateCopy(S.Context, Builder); |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2383 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2384 | Info.reset(DeducedArgumentList); |
| 2385 | |
| 2386 | // Substitute the deduced template arguments into the template |
| 2387 | // arguments of the class template partial specialization, and |
| 2388 | // verify that the instantiated template arguments are both valid |
| 2389 | // and are equivalent to the template arguments originally provided |
| 2390 | // to the class template. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2391 | LocalInstantiationScope InstScope(S); |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2392 | auto *Template = Partial->getSpecializedTemplate(); |
| 2393 | const ASTTemplateArgumentListInfo *PartialTemplArgInfo = |
| 2394 | Partial->getTemplateArgsAsWritten(); |
| 2395 | const TemplateArgumentLoc *PartialTemplateArgs = |
| 2396 | PartialTemplArgInfo->getTemplateArgs(); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2397 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2398 | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, |
| 2399 | PartialTemplArgInfo->RAngleLoc); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2400 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2401 | if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs, |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2402 | InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { |
| 2403 | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; |
| 2404 | if (ParamIdx >= Partial->getTemplateParameters()->size()) |
| 2405 | ParamIdx = Partial->getTemplateParameters()->size() - 1; |
| 2406 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2407 | Decl *Param = const_cast<NamedDecl *>( |
| 2408 | Partial->getTemplateParameters()->getParam(ParamIdx)); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2409 | Info.Param = makeTemplateParameter(Param); |
| 2410 | Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); |
| 2411 | return Sema::TDK_SubstitutionFailure; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2414 | SmallVector<TemplateArgument, 4> ConvertedInstArgs; |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2415 | if (S.CheckTemplateArgumentList(Template, Partial->getLocation(), InstArgs, |
| 2416 | false, ConvertedInstArgs)) |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2417 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2418 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2419 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2420 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2421 | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2422 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 2423 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2424 | Info.FirstArg = TemplateArgs[I]; |
| 2425 | Info.SecondArg = InstArg; |
| 2426 | return Sema::TDK_NonDeducedMismatch; |
| 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | if (Trap.hasErrorOccurred()) |
| 2431 | return Sema::TDK_SubstitutionFailure; |
| 2432 | |
| 2433 | return Sema::TDK_Success; |
| 2434 | } |
| 2435 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2436 | /// Complete template argument deduction for a class or variable template, |
| 2437 | /// when partial ordering against a partial specialization. |
| 2438 | // FIXME: Factor out duplication with partial specialization version above. |
Benjamin Kramer | 357c9e1 | 2017-02-11 12:21:17 +0000 | [diff] [blame^] | 2439 | static Sema::TemplateDeductionResult FinishTemplateArgumentDeduction( |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2440 | Sema &S, TemplateDecl *Template, bool PartialOrdering, |
| 2441 | const TemplateArgumentList &TemplateArgs, |
| 2442 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2443 | TemplateDeductionInfo &Info) { |
| 2444 | // Unevaluated SFINAE context. |
| 2445 | EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated); |
| 2446 | Sema::SFINAETrap Trap(S); |
| 2447 | |
| 2448 | Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Template)); |
| 2449 | |
| 2450 | // C++ [temp.deduct.type]p2: |
| 2451 | // [...] or if any template argument remains neither deduced nor |
| 2452 | // explicitly specified, template argument deduction fails. |
| 2453 | SmallVector<TemplateArgument, 4> Builder; |
| 2454 | if (auto Result = ConvertDeducedTemplateArguments( |
| 2455 | S, Template, /*IsDeduced*/PartialOrdering, Deduced, Info, Builder)) |
| 2456 | return Result; |
| 2457 | |
| 2458 | // Check that we produced the correct argument list. |
| 2459 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| 2460 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
| 2461 | TemplateArgument InstArg = Builder[I]; |
| 2462 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg, |
| 2463 | /*PackExpansionMatchesPack*/true)) { |
| 2464 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
| 2465 | Info.FirstArg = TemplateArgs[I]; |
| 2466 | Info.SecondArg = InstArg; |
| 2467 | return Sema::TDK_NonDeducedMismatch; |
| 2468 | } |
| 2469 | } |
| 2470 | |
| 2471 | if (Trap.hasErrorOccurred()) |
| 2472 | return Sema::TDK_SubstitutionFailure; |
| 2473 | |
| 2474 | return Sema::TDK_Success; |
| 2475 | } |
| 2476 | |
| 2477 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2478 | /// \brief Perform template argument deduction to determine whether |
Larisse Voufo | 833b05a | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 2479 | /// the given template arguments match the given class template |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2480 | /// partial specialization per C++ [temp.class.spec.match]. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2481 | Sema::TemplateDeductionResult |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2482 | Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2483 | const TemplateArgumentList &TemplateArgs, |
| 2484 | TemplateDeductionInfo &Info) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 2485 | if (Partial->isInvalidDecl()) |
| 2486 | return TDK_Invalid; |
| 2487 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2488 | // C++ [temp.class.spec.match]p2: |
| 2489 | // A partial specialization matches a given actual template |
| 2490 | // argument list if the template arguments of the partial |
| 2491 | // specialization can be deduced from the actual template argument |
| 2492 | // list (14.8.2). |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2493 | |
| 2494 | // Unevaluated SFINAE context. |
| 2495 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2496 | SFINAETrap Trap(*this); |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2497 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2498 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2499 | Deduced.resize(Partial->getTemplateParameters()->size()); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2500 | if (TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2501 | = ::DeduceTemplateArguments(*this, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2502 | Partial->getTemplateParameters(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2503 | Partial->getTemplateArgs(), |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2504 | TemplateArgs, Info, Deduced)) |
| 2505 | return Result; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 2506 | |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2507 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2508 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2509 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2510 | if (Inst.isInvalid()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2511 | return TDK_InstantiationDepth; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2512 | |
Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2513 | if (Trap.hasErrorOccurred()) |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2514 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2515 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2516 | return ::FinishTemplateArgumentDeduction( |
| 2517 | *this, Partial, /*PartialOrdering=*/false, TemplateArgs, Deduced, Info); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2518 | } |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2519 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2520 | /// \brief Perform template argument deduction to determine whether |
| 2521 | /// the given template arguments match the given variable template |
| 2522 | /// partial specialization per C++ [temp.class.spec.match]. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2523 | Sema::TemplateDeductionResult |
| 2524 | Sema::DeduceTemplateArguments(VarTemplatePartialSpecializationDecl *Partial, |
| 2525 | const TemplateArgumentList &TemplateArgs, |
| 2526 | TemplateDeductionInfo &Info) { |
| 2527 | if (Partial->isInvalidDecl()) |
| 2528 | return TDK_Invalid; |
| 2529 | |
| 2530 | // C++ [temp.class.spec.match]p2: |
| 2531 | // A partial specialization matches a given actual template |
| 2532 | // argument list if the template arguments of the partial |
| 2533 | // specialization can be deduced from the actual template argument |
| 2534 | // list (14.8.2). |
| 2535 | |
| 2536 | // Unevaluated SFINAE context. |
| 2537 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
| 2538 | SFINAETrap Trap(*this); |
| 2539 | |
| 2540 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 2541 | Deduced.resize(Partial->getTemplateParameters()->size()); |
| 2542 | if (TemplateDeductionResult Result = ::DeduceTemplateArguments( |
| 2543 | *this, Partial->getTemplateParameters(), Partial->getTemplateArgs(), |
| 2544 | TemplateArgs, Info, Deduced)) |
| 2545 | return Result; |
| 2546 | |
| 2547 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2548 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2549 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2550 | if (Inst.isInvalid()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2551 | return TDK_InstantiationDepth; |
| 2552 | |
| 2553 | if (Trap.hasErrorOccurred()) |
| 2554 | return Sema::TDK_SubstitutionFailure; |
| 2555 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2556 | return ::FinishTemplateArgumentDeduction( |
| 2557 | *this, Partial, /*PartialOrdering=*/false, TemplateArgs, Deduced, Info); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2558 | } |
| 2559 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2560 | /// \brief Determine whether the given type T is a simple-template-id type. |
| 2561 | static bool isSimpleTemplateIdType(QualType T) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2562 | if (const TemplateSpecializationType *Spec |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2563 | = T->getAs<TemplateSpecializationType>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2564 | return Spec->getTemplateName().getAsTemplateDecl() != nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2565 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2566 | return false; |
| 2567 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2568 | |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 2569 | static void |
| 2570 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
| 2571 | bool OnlyDeduced, |
| 2572 | unsigned Level, |
| 2573 | llvm::SmallBitVector &Deduced); |
| 2574 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2575 | /// \brief Substitute the explicitly-provided template arguments into the |
| 2576 | /// given function template according to C++ [temp.arg.explicit]. |
| 2577 | /// |
| 2578 | /// \param FunctionTemplate the function template into which the explicit |
| 2579 | /// template arguments will be substituted. |
| 2580 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2581 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2582 | /// arguments. |
| 2583 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2584 | /// \param Deduced the deduced template arguments, which will be populated |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2585 | /// with the converted and checked explicit template arguments. |
| 2586 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2587 | /// \param ParamTypes will be populated with the instantiated function |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2588 | /// parameters. |
| 2589 | /// |
| 2590 | /// \param FunctionType if non-NULL, the result type of the function template |
| 2591 | /// will also be instantiated and the pointed-to value will be updated with |
| 2592 | /// the instantiated function type. |
| 2593 | /// |
| 2594 | /// \param Info if substitution fails for any reason, this object will be |
| 2595 | /// populated with more information about the failure. |
| 2596 | /// |
| 2597 | /// \returns TDK_Success if substitution was successful, or some failure |
| 2598 | /// condition. |
| 2599 | Sema::TemplateDeductionResult |
| 2600 | Sema::SubstituteExplicitTemplateArguments( |
| 2601 | FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2602 | TemplateArgumentListInfo &ExplicitTemplateArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2603 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2604 | SmallVectorImpl<QualType> &ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2605 | QualType *FunctionType, |
| 2606 | TemplateDeductionInfo &Info) { |
| 2607 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 2608 | TemplateParameterList *TemplateParams |
| 2609 | = FunctionTemplate->getTemplateParameters(); |
| 2610 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2611 | if (ExplicitTemplateArgs.size() == 0) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2612 | // No arguments to substitute; just copy over the parameter types and |
| 2613 | // fill in the function type. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2614 | for (auto P : Function->parameters()) |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 2615 | ParamTypes.push_back(P->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2616 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2617 | if (FunctionType) |
| 2618 | *FunctionType = Function->getType(); |
| 2619 | return TDK_Success; |
| 2620 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2621 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2622 | // Unevaluated SFINAE context. |
| 2623 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2624 | SFINAETrap Trap(*this); |
| 2625 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2626 | // C++ [temp.arg.explicit]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2627 | // Template arguments that are present shall be specified in the |
| 2628 | // declaration order of their corresponding template-parameters. The |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2629 | // template argument list shall not specify more template-arguments than |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2630 | // there are corresponding template-parameters. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2631 | SmallVector<TemplateArgument, 4> Builder; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2632 | |
| 2633 | // Enter a new template instantiation context where we check the |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2634 | // explicitly-specified template arguments against this function template, |
| 2635 | // and then substitute them into the function parameter types. |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 2636 | SmallVector<TemplateArgument, 4> DeducedArgs; |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2637 | InstantiatingTemplate Inst(*this, Info.getLocation(), FunctionTemplate, |
| 2638 | DeducedArgs, |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2639 | ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution, |
| 2640 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2641 | if (Inst.isInvalid()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2642 | return TDK_InstantiationDepth; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2643 | |
Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 2644 | if (CheckTemplateArgumentList(FunctionTemplate, SourceLocation(), |
| 2645 | ExplicitTemplateArgs, true, Builder, false) || |
| 2646 | Trap.hasErrorOccurred()) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2647 | unsigned Index = Builder.size(); |
Douglas Gregor | 62c281a | 2010-05-09 01:26:06 +0000 | [diff] [blame] | 2648 | if (Index >= TemplateParams->size()) |
| 2649 | Index = TemplateParams->size() - 1; |
| 2650 | Info.Param = makeTemplateParameter(TemplateParams->getParam(Index)); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2651 | return TDK_InvalidExplicitArguments; |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 2652 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2653 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2654 | // Form the template argument list from the explicitly-specified |
| 2655 | // template arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2656 | TemplateArgumentList *ExplicitArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2657 | = TemplateArgumentList::CreateCopy(Context, Builder); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2658 | Info.reset(ExplicitArgumentList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2659 | |
John McCall | 036855a | 2010-10-12 19:40:14 +0000 | [diff] [blame] | 2660 | // Template argument deduction and the final substitution should be |
| 2661 | // done in the context of the templated declaration. Explicit |
| 2662 | // argument substitution, on the other hand, needs to happen in the |
| 2663 | // calling context. |
| 2664 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
| 2665 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2666 | // If we deduced template arguments for a template parameter pack, |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2667 | // note that the template argument pack is partially substituted and record |
| 2668 | // the explicit template arguments. They'll be used as part of deduction |
| 2669 | // for this template parameter pack. |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2670 | for (unsigned I = 0, N = Builder.size(); I != N; ++I) { |
| 2671 | const TemplateArgument &Arg = Builder[I]; |
| 2672 | if (Arg.getKind() == TemplateArgument::Pack) { |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2673 | CurrentInstantiationScope->SetPartiallySubstitutedPack( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2674 | TemplateParams->getParam(I), |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2675 | Arg.pack_begin(), |
| 2676 | Arg.pack_size()); |
| 2677 | break; |
| 2678 | } |
| 2679 | } |
| 2680 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2681 | const FunctionProtoType *Proto |
| 2682 | = Function->getType()->getAs<FunctionProtoType>(); |
| 2683 | assert(Proto && "Function template does not have a prototype?"); |
| 2684 | |
Richard Smith | 70b1304 | 2015-01-09 01:19:56 +0000 | [diff] [blame] | 2685 | // Isolate our substituted parameters from our caller. |
| 2686 | LocalInstantiationScope InstScope(*this, /*MergeWithOuterScope*/true); |
| 2687 | |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2688 | ExtParameterInfoBuilder ExtParamInfos; |
| 2689 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2690 | // Instantiate the types of each of the function parameters given the |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2691 | // explicitly-specified template arguments. If the function has a trailing |
| 2692 | // return type, substitute it after the arguments to ensure we substitute |
| 2693 | // in lexical order. |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2694 | if (Proto->hasTrailingReturn()) { |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2695 | if (SubstParmTypes(Function->getLocation(), Function->parameters(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2696 | Proto->getExtParameterInfosOrNull(), |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2697 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2698 | ParamTypes, /*params*/ nullptr, ExtParamInfos)) |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2699 | return TDK_SubstitutionFailure; |
| 2700 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2701 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2702 | // Instantiate the return type. |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2703 | QualType ResultType; |
| 2704 | { |
| 2705 | // C++11 [expr.prim.general]p3: |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2706 | // If a declaration declares a member function or member function |
| 2707 | // 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] | 2708 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2709 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2710 | // declarator. |
| 2711 | unsigned ThisTypeQuals = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2712 | CXXRecordDecl *ThisContext = nullptr; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2713 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
| 2714 | ThisContext = Method->getParent(); |
| 2715 | ThisTypeQuals = Method->getTypeQualifiers(); |
| 2716 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2717 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2718 | CXXThisScopeRAII ThisScope(*this, ThisContext, ThisTypeQuals, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2719 | getLangOpts().CPlusPlus11); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2720 | |
| 2721 | ResultType = |
| 2722 | SubstType(Proto->getReturnType(), |
| 2723 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 2724 | Function->getTypeSpecStartLoc(), Function->getDeclName()); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2725 | if (ResultType.isNull() || Trap.hasErrorOccurred()) |
| 2726 | return TDK_SubstitutionFailure; |
| 2727 | } |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2728 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2729 | // Instantiate the types of each of the function parameters given the |
| 2730 | // explicitly-specified template arguments if we didn't do so earlier. |
| 2731 | if (!Proto->hasTrailingReturn() && |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2732 | SubstParmTypes(Function->getLocation(), Function->parameters(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2733 | Proto->getExtParameterInfosOrNull(), |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2734 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2735 | ParamTypes, /*params*/ nullptr, ExtParamInfos)) |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2736 | return TDK_SubstitutionFailure; |
| 2737 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2738 | if (FunctionType) { |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2739 | auto EPI = Proto->getExtProtoInfo(); |
| 2740 | EPI.ExtParameterInfos = ExtParamInfos.getPointerOrNull(ParamTypes.size()); |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2741 | *FunctionType = BuildFunctionType(ResultType, ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2742 | Function->getLocation(), |
Eli Friedman | d8725a9 | 2010-08-05 02:54:05 +0000 | [diff] [blame] | 2743 | Function->getDeclName(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2744 | EPI); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2745 | if (FunctionType->isNull() || Trap.hasErrorOccurred()) |
| 2746 | return TDK_SubstitutionFailure; |
| 2747 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2748 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2749 | // C++ [temp.arg.explicit]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2750 | // Trailing template arguments that can be deduced (14.8.2) may be |
| 2751 | // omitted from the list of explicit template-arguments. If all of the |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2752 | // template arguments can be deduced, they may all be omitted; in this |
| 2753 | // case, the empty template argument list <> itself may also be omitted. |
| 2754 | // |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2755 | // Take all of the explicitly-specified arguments and put them into |
| 2756 | // the set of deduced template arguments. Explicitly-specified |
| 2757 | // parameter packs, however, will be set to NULL since the deduction |
| 2758 | // mechanisms handle explicitly-specified argument packs directly. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2759 | Deduced.reserve(TemplateParams->size()); |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2760 | for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) { |
| 2761 | const TemplateArgument &Arg = ExplicitArgumentList->get(I); |
| 2762 | if (Arg.getKind() == TemplateArgument::Pack) |
| 2763 | Deduced.push_back(DeducedTemplateArgument()); |
| 2764 | else |
| 2765 | Deduced.push_back(Arg); |
| 2766 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2767 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2768 | return TDK_Success; |
| 2769 | } |
| 2770 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2771 | /// \brief Check whether the deduced argument type for a call to a function |
| 2772 | /// template matches the actual argument type per C++ [temp.deduct.call]p4. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2773 | static bool |
| 2774 | CheckOriginalCallArgDeduction(Sema &S, Sema::OriginalCallArg OriginalArg, |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2775 | QualType DeducedA) { |
| 2776 | ASTContext &Context = S.Context; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2777 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2778 | QualType A = OriginalArg.OriginalArgType; |
| 2779 | QualType OriginalParamType = OriginalArg.OriginalParamType; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2780 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2781 | // Check for type equality (top-level cv-qualifiers are ignored). |
| 2782 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| 2783 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2784 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2785 | // Strip off references on the argument types; they aren't needed for |
| 2786 | // the following checks. |
| 2787 | if (const ReferenceType *DeducedARef = DeducedA->getAs<ReferenceType>()) |
| 2788 | DeducedA = DeducedARef->getPointeeType(); |
| 2789 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
| 2790 | A = ARef->getPointeeType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2791 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2792 | // C++ [temp.deduct.call]p4: |
| 2793 | // [...] However, there are three cases that allow a difference: |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2794 | // - If the original P is a reference type, the deduced A (i.e., the |
| 2795 | // type referred to by the reference) can be more cv-qualified than |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2796 | // the transformed A. |
| 2797 | if (const ReferenceType *OriginalParamRef |
| 2798 | = OriginalParamType->getAs<ReferenceType>()) { |
| 2799 | // We don't want to keep the reference around any more. |
| 2800 | OriginalParamType = OriginalParamRef->getPointeeType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2801 | |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 2802 | // FIXME: Resolve core issue (no number yet): if the original P is a |
| 2803 | // reference type and the transformed A is function type "noexcept F", |
| 2804 | // the deduced A can be F. |
| 2805 | QualType Tmp; |
| 2806 | if (A->isFunctionType() && S.IsFunctionConversion(A, DeducedA, Tmp)) |
| 2807 | return false; |
| 2808 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2809 | Qualifiers AQuals = A.getQualifiers(); |
| 2810 | Qualifiers DeducedAQuals = DeducedA.getQualifiers(); |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2811 | |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2812 | // Under Objective-C++ ARC, the deduced type may have implicitly |
| 2813 | // been given strong or (when dealing with a const reference) |
| 2814 | // unsafe_unretained lifetime. If so, update the original |
| 2815 | // qualifiers to include this lifetime. |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2816 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2817 | ((DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_Strong && |
| 2818 | AQuals.getObjCLifetime() == Qualifiers::OCL_None) || |
| 2819 | (DeducedAQuals.hasConst() && |
| 2820 | DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone))) { |
| 2821 | AQuals.setObjCLifetime(DeducedAQuals.getObjCLifetime()); |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2822 | } |
| 2823 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2824 | if (AQuals == DeducedAQuals) { |
| 2825 | // Qualifiers match; there's nothing to do. |
| 2826 | } else if (!DeducedAQuals.compatiblyIncludes(AQuals)) { |
Douglas Gregor | ddaae52 | 2011-06-17 14:36:00 +0000 | [diff] [blame] | 2827 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2828 | } else { |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2829 | // Qualifiers are compatible, so have the argument type adopt the |
| 2830 | // deduced argument type's qualifiers as if we had performed the |
| 2831 | // qualification conversion. |
| 2832 | A = Context.getQualifiedType(A.getUnqualifiedType(), DeducedAQuals); |
| 2833 | } |
| 2834 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2835 | |
| 2836 | // - The transformed A can be another pointer or pointer to member |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 2837 | // type that can be converted to the deduced A via a function pointer |
| 2838 | // conversion and/or a qualification conversion. |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2839 | // |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 2840 | // Also allow conversions which merely strip __attribute__((noreturn)) from |
| 2841 | // function types (recursively). |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2842 | bool ObjCLifetimeConversion = false; |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2843 | QualType ResultTy; |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2844 | if ((A->isAnyPointerType() || A->isMemberPointerType()) && |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2845 | (S.IsQualificationConversion(A, DeducedA, false, |
| 2846 | ObjCLifetimeConversion) || |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 2847 | S.IsFunctionConversion(A, DeducedA, ResultTy))) |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2848 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2849 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2850 | // - 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] | 2851 | // transformed A can be a derived class of the deduced A. [...] |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2852 | // [...] Likewise, if P is a pointer to a class of the form |
| 2853 | // simple-template-id, the transformed A can be a pointer to a |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2854 | // derived class pointed to by the deduced A. |
| 2855 | if (const PointerType *OriginalParamPtr |
| 2856 | = OriginalParamType->getAs<PointerType>()) { |
| 2857 | if (const PointerType *DeducedAPtr = DeducedA->getAs<PointerType>()) { |
| 2858 | if (const PointerType *APtr = A->getAs<PointerType>()) { |
| 2859 | if (A->getPointeeType()->isRecordType()) { |
| 2860 | OriginalParamType = OriginalParamPtr->getPointeeType(); |
| 2861 | DeducedA = DeducedAPtr->getPointeeType(); |
| 2862 | A = APtr->getPointeeType(); |
| 2863 | } |
| 2864 | } |
| 2865 | } |
| 2866 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2867 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2868 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| 2869 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2870 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2871 | if (A->isRecordType() && isSimpleTemplateIdType(OriginalParamType) && |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 2872 | S.IsDerivedFrom(SourceLocation(), A, DeducedA)) |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2873 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2874 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2875 | return true; |
| 2876 | } |
| 2877 | |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 2878 | /// Find the pack index for a particular parameter index in an instantiation of |
| 2879 | /// a function template with specific arguments. |
| 2880 | /// |
| 2881 | /// \return The pack index for whichever pack produced this parameter, or -1 |
| 2882 | /// if this was not produced by a parameter. Intended to be used as the |
| 2883 | /// ArgumentPackSubstitutionIndex for further substitutions. |
| 2884 | // FIXME: We should track this in OriginalCallArgs so we don't need to |
| 2885 | // reconstruct it here. |
| 2886 | static unsigned getPackIndexForParam(Sema &S, |
| 2887 | FunctionTemplateDecl *FunctionTemplate, |
| 2888 | const MultiLevelTemplateArgumentList &Args, |
| 2889 | unsigned ParamIdx) { |
| 2890 | unsigned Idx = 0; |
| 2891 | for (auto *PD : FunctionTemplate->getTemplatedDecl()->parameters()) { |
| 2892 | if (PD->isParameterPack()) { |
| 2893 | unsigned NumExpansions = |
| 2894 | S.getNumArgumentsInExpansion(PD->getType(), Args).getValueOr(1); |
| 2895 | if (Idx + NumExpansions > ParamIdx) |
| 2896 | return ParamIdx - Idx; |
| 2897 | Idx += NumExpansions; |
| 2898 | } else { |
| 2899 | if (Idx == ParamIdx) |
| 2900 | return -1; // Not a pack expansion |
| 2901 | ++Idx; |
| 2902 | } |
| 2903 | } |
| 2904 | |
| 2905 | llvm_unreachable("parameter index would not be produced from template"); |
| 2906 | } |
| 2907 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2908 | /// \brief Finish template argument deduction for a function template, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2909 | /// checking the deduced template arguments for completeness and forming |
| 2910 | /// the function template specialization. |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2911 | /// |
| 2912 | /// \param OriginalCallArgs If non-NULL, the original call arguments against |
| 2913 | /// which the deduced argument types should be compared. |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 2914 | Sema::TemplateDeductionResult Sema::FinishTemplateArgumentDeduction( |
| 2915 | FunctionTemplateDecl *FunctionTemplate, |
| 2916 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2917 | unsigned NumExplicitlySpecified, FunctionDecl *&Specialization, |
| 2918 | TemplateDeductionInfo &Info, |
| 2919 | SmallVectorImpl<OriginalCallArg> const *OriginalCallArgs, |
| 2920 | bool PartialOverloading, llvm::function_ref<bool()> CheckNonDependent) { |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2921 | // Unevaluated SFINAE context. |
| 2922 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2923 | SFINAETrap Trap(*this); |
| 2924 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2925 | // Enter a new template instantiation context while we instantiate the |
| 2926 | // actual function declaration. |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2927 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2928 | InstantiatingTemplate Inst(*this, Info.getLocation(), FunctionTemplate, |
| 2929 | DeducedArgs, |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2930 | ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution, |
| 2931 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2932 | if (Inst.isInvalid()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2933 | return TDK_InstantiationDepth; |
| 2934 | |
John McCall | e23b871 | 2010-04-29 01:18:58 +0000 | [diff] [blame] | 2935 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
John McCall | 80e58cd | 2010-04-29 00:35:03 +0000 | [diff] [blame] | 2936 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2937 | // C++ [temp.deduct.type]p2: |
| 2938 | // [...] or if any template argument remains neither deduced nor |
| 2939 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2940 | SmallVector<TemplateArgument, 4> Builder; |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2941 | if (auto Result = ConvertDeducedTemplateArguments( |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2942 | *this, FunctionTemplate, /*IsDeduced*/true, Deduced, Info, Builder, |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2943 | CurrentInstantiationScope, NumExplicitlySpecified, |
| 2944 | PartialOverloading)) |
| 2945 | return Result; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2946 | |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 2947 | // C++ [temp.deduct.call]p10: [DR1391] |
| 2948 | // If deduction succeeds for all parameters that contain |
| 2949 | // template-parameters that participate in template argument deduction, |
| 2950 | // and all template arguments are explicitly specified, deduced, or |
| 2951 | // obtained from default template arguments, remaining parameters are then |
| 2952 | // compared with the corresponding arguments. For each remaining parameter |
| 2953 | // P with a type that was non-dependent before substitution of any |
| 2954 | // explicitly-specified template arguments, if the corresponding argument |
| 2955 | // A cannot be implicitly converted to P, deduction fails. |
| 2956 | if (CheckNonDependent()) |
| 2957 | return TDK_NonDependentConversionFailure; |
| 2958 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2959 | // Form the template argument list from the deduced template arguments. |
| 2960 | TemplateArgumentList *DeducedArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2961 | = TemplateArgumentList::CreateCopy(Context, Builder); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2962 | Info.reset(DeducedArgumentList); |
| 2963 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2964 | // Substitute the deduced template arguments into the function template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2965 | // declaration to produce the function template specialization. |
Douglas Gregor | 1614237 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 2966 | DeclContext *Owner = FunctionTemplate->getDeclContext(); |
| 2967 | if (FunctionTemplate->getFriendObjectKind()) |
| 2968 | Owner = FunctionTemplate->getLexicalDeclContext(); |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 2969 | MultiLevelTemplateArgumentList SubstArgs(*DeducedArgumentList); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2970 | Specialization = cast_or_null<FunctionDecl>( |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 2971 | SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner, SubstArgs)); |
Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 2972 | if (!Specialization || Specialization->isInvalidDecl()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2973 | return TDK_SubstitutionFailure; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2974 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2975 | assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == |
Douglas Gregor | 31fae89 | 2009-09-15 18:26:13 +0000 | [diff] [blame] | 2976 | FunctionTemplate->getCanonicalDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2977 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2978 | // If the template argument list is owned by the function template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2979 | // specialization, release it. |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 2980 | if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList && |
| 2981 | !Trap.hasErrorOccurred()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2982 | Info.take(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2983 | |
Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 2984 | // There may have been an error that did not prevent us from constructing a |
| 2985 | // declaration. Mark the declaration invalid and return with a substitution |
| 2986 | // failure. |
| 2987 | if (Trap.hasErrorOccurred()) { |
| 2988 | Specialization->setInvalidDecl(true); |
| 2989 | return TDK_SubstitutionFailure; |
| 2990 | } |
| 2991 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2992 | if (OriginalCallArgs) { |
| 2993 | // C++ [temp.deduct.call]p4: |
| 2994 | // In general, the deduction process attempts to find template argument |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2995 | // 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] | 2996 | // is transformed as described above). [...] |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 2997 | llvm::SmallDenseMap<std::pair<unsigned, QualType>, QualType> DeducedATypes; |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2998 | for (unsigned I = 0, N = OriginalCallArgs->size(); I != N; ++I) { |
| 2999 | OriginalCallArg OriginalArg = (*OriginalCallArgs)[I]; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3000 | |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3001 | auto ParamIdx = OriginalArg.ArgIdx; |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3002 | if (ParamIdx >= Specialization->getNumParams()) |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3003 | // FIXME: This presumably means a pack ended up smaller than we |
| 3004 | // expected while deducing. Should this not result in deduction |
| 3005 | // failure? Can it even happen? |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3006 | continue; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3007 | |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3008 | QualType DeducedA; |
| 3009 | if (!OriginalArg.DecomposedParam) { |
| 3010 | // P is one of the function parameters, just look up its substituted |
| 3011 | // type. |
| 3012 | DeducedA = Specialization->getParamDecl(ParamIdx)->getType(); |
| 3013 | } else { |
| 3014 | // P is a decomposed element of a parameter corresponding to a |
| 3015 | // braced-init-list argument. Substitute back into P to find the |
| 3016 | // deduced A. |
| 3017 | QualType &CacheEntry = |
| 3018 | DeducedATypes[{ParamIdx, OriginalArg.OriginalParamType}]; |
| 3019 | if (CacheEntry.isNull()) { |
| 3020 | ArgumentPackSubstitutionIndexRAII PackIndex( |
| 3021 | *this, getPackIndexForParam(*this, FunctionTemplate, SubstArgs, |
| 3022 | ParamIdx)); |
| 3023 | CacheEntry = |
| 3024 | SubstType(OriginalArg.OriginalParamType, SubstArgs, |
| 3025 | Specialization->getTypeSpecStartLoc(), |
| 3026 | Specialization->getDeclName()); |
| 3027 | } |
| 3028 | DeducedA = CacheEntry; |
| 3029 | } |
| 3030 | |
Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 3031 | if (CheckOriginalCallArgDeduction(*this, OriginalArg, DeducedA)) { |
| 3032 | Info.FirstArg = TemplateArgument(DeducedA); |
| 3033 | Info.SecondArg = TemplateArgument(OriginalArg.OriginalArgType); |
| 3034 | Info.CallArgIndex = OriginalArg.ArgIdx; |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3035 | return OriginalArg.DecomposedParam ? TDK_DeducedMismatchNested |
| 3036 | : TDK_DeducedMismatch; |
Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 3037 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3038 | } |
| 3039 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3040 | |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3041 | // If we suppressed any diagnostics while performing template argument |
| 3042 | // deduction, and if we haven't already instantiated this declaration, |
| 3043 | // keep track of these diagnostics. They'll be emitted if this specialization |
| 3044 | // is actually used. |
| 3045 | if (Info.diag_begin() != Info.diag_end()) { |
Craig Topper | 79be4cd | 2013-07-05 04:33:53 +0000 | [diff] [blame] | 3046 | SuppressedDiagnosticsMap::iterator |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3047 | Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl()); |
| 3048 | if (Pos == SuppressedDiagnostics.end()) |
| 3049 | SuppressedDiagnostics[Specialization->getCanonicalDecl()] |
| 3050 | .append(Info.diag_begin(), Info.diag_end()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3051 | } |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3052 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3053 | return TDK_Success; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3054 | } |
| 3055 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3056 | /// Gets the type of a function for template-argument-deducton |
| 3057 | /// purposes when it's considered as part of an overload set. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3058 | static QualType GetTypeOfFunction(Sema &S, const OverloadExpr::FindResult &R, |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3059 | FunctionDecl *Fn) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3060 | // We may need to deduce the return type of the function now. |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3061 | if (S.getLangOpts().CPlusPlus14 && Fn->getReturnType()->isUndeducedType() && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3062 | S.DeduceReturnType(Fn, R.Expression->getExprLoc(), /*Diagnose*/ false)) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3063 | return QualType(); |
| 3064 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3065 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3066 | if (Method->isInstance()) { |
| 3067 | // An instance method that's referenced in a form that doesn't |
| 3068 | // look like a member pointer is just invalid. |
| 3069 | if (!R.HasFormOfMemberPointer) return QualType(); |
| 3070 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3071 | return S.Context.getMemberPointerType(Fn->getType(), |
| 3072 | S.Context.getTypeDeclType(Method->getParent()).getTypePtr()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
| 3075 | if (!R.IsAddressOfOperand) return Fn->getType(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3076 | return S.Context.getPointerType(Fn->getType()); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3077 | } |
| 3078 | |
| 3079 | /// Apply the deduction rules for overload sets. |
| 3080 | /// |
| 3081 | /// \return the null type if this argument should be treated as an |
| 3082 | /// undeduced context |
| 3083 | static QualType |
| 3084 | ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams, |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3085 | Expr *Arg, QualType ParamType, |
| 3086 | bool ParamWasReference) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3087 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3088 | OverloadExpr::FindResult R = OverloadExpr::find(Arg); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3089 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3090 | OverloadExpr *Ovl = R.Expression; |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3091 | |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3092 | // C++0x [temp.deduct.call]p4 |
| 3093 | unsigned TDF = 0; |
| 3094 | if (ParamWasReference) |
| 3095 | TDF |= TDF_ParamWithReferenceType; |
| 3096 | if (R.IsAddressOfOperand) |
| 3097 | TDF |= TDF_IgnoreQualifiers; |
| 3098 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3099 | // C++0x [temp.deduct.call]p6: |
| 3100 | // When P is a function type, pointer to function type, or pointer |
| 3101 | // to member function type: |
| 3102 | |
| 3103 | if (!ParamType->isFunctionType() && |
| 3104 | !ParamType->isFunctionPointerType() && |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3105 | !ParamType->isMemberFunctionPointerType()) { |
| 3106 | if (Ovl->hasExplicitTemplateArgs()) { |
| 3107 | // But we can still look for an explicit specialization. |
| 3108 | if (FunctionDecl *ExplicitSpec |
| 3109 | = S.ResolveSingleFunctionTemplateSpecialization(Ovl)) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3110 | return GetTypeOfFunction(S, R, ExplicitSpec); |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3111 | } |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3112 | |
George Burgess IV | cc2f355 | 2016-03-19 21:51:45 +0000 | [diff] [blame] | 3113 | DeclAccessPair DAP; |
| 3114 | if (FunctionDecl *Viable = |
| 3115 | S.resolveAddressOfOnlyViableOverloadCandidate(Arg, DAP)) |
| 3116 | return GetTypeOfFunction(S, R, Viable); |
| 3117 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3118 | return QualType(); |
| 3119 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3120 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3121 | // Gather the explicit template arguments, if any. |
| 3122 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 3123 | if (Ovl->hasExplicitTemplateArgs()) |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 3124 | Ovl->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3125 | QualType Match; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 3126 | for (UnresolvedSetIterator I = Ovl->decls_begin(), |
| 3127 | E = Ovl->decls_end(); I != E; ++I) { |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3128 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 3129 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3130 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) { |
| 3131 | // - If the argument is an overload set containing one or more |
| 3132 | // function templates, the parameter is treated as a |
| 3133 | // non-deduced context. |
| 3134 | if (!Ovl->hasExplicitTemplateArgs()) |
| 3135 | return QualType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3136 | |
| 3137 | // Otherwise, see if we can resolve a function type |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3138 | FunctionDecl *Specialization = nullptr; |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3139 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3140 | if (S.DeduceTemplateArguments(FunTmpl, &ExplicitTemplateArgs, |
| 3141 | Specialization, Info)) |
| 3142 | continue; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3143 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3144 | D = Specialization; |
| 3145 | } |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3146 | |
| 3147 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3148 | QualType ArgType = GetTypeOfFunction(S, R, Fn); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3149 | if (ArgType.isNull()) continue; |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3150 | |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3151 | // Function-to-pointer conversion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3152 | if (!ParamWasReference && ParamType->isPointerType() && |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3153 | ArgType->isFunctionType()) |
| 3154 | ArgType = S.Context.getPointerType(ArgType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3155 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3156 | // - If the argument is an overload set (not containing function |
| 3157 | // templates), trial argument deduction is attempted using each |
| 3158 | // of the members of the set. If deduction succeeds for only one |
| 3159 | // of the overload set members, that member is used as the |
| 3160 | // argument value for the deduction. If deduction succeeds for |
| 3161 | // more than one member of the overload set the parameter is |
| 3162 | // treated as a non-deduced context. |
| 3163 | |
| 3164 | // We do all of this in a fresh context per C++0x [temp.deduct.type]p2: |
| 3165 | // Type deduction is done independently for each P/A pair, and |
| 3166 | // the deduced template argument values are then combined. |
| 3167 | // So we do not reject deductions which were made elsewhere. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3168 | SmallVector<DeducedTemplateArgument, 8> |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3169 | Deduced(TemplateParams->size()); |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3170 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3171 | Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3172 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
| 3173 | ArgType, Info, Deduced, TDF); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3174 | if (Result) continue; |
| 3175 | if (!Match.isNull()) return QualType(); |
| 3176 | Match = ArgType; |
| 3177 | } |
| 3178 | |
| 3179 | return Match; |
| 3180 | } |
| 3181 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3182 | /// \brief Perform the adjustments to the parameter and argument types |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3183 | /// described in C++ [temp.deduct.call]. |
| 3184 | /// |
| 3185 | /// \returns true if the caller should not attempt to perform any template |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3186 | /// argument deduction based on this P/A pair because the argument is an |
| 3187 | /// overloaded function set that could not be resolved. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3188 | static bool AdjustFunctionParmAndArgTypesForDeduction(Sema &S, |
| 3189 | TemplateParameterList *TemplateParams, |
| 3190 | QualType &ParamType, |
| 3191 | QualType &ArgType, |
| 3192 | Expr *Arg, |
| 3193 | unsigned &TDF) { |
| 3194 | // C++0x [temp.deduct.call]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3195 | // 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] | 3196 | // are ignored for type deduction. |
Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3197 | if (ParamType.hasQualifiers()) |
| 3198 | ParamType = ParamType.getUnqualifiedType(); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3199 | |
| 3200 | // [...] If P is a reference type, the type referred to by P is |
| 3201 | // used for type deduction. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3202 | const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>(); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3203 | if (ParamRefType) |
| 3204 | ParamType = ParamRefType->getPointeeType(); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3205 | |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3206 | // Overload sets usually make this parameter an undeduced context, |
| 3207 | // but there are sometimes special circumstances. Typically |
| 3208 | // involving a template-id-expr. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3209 | if (ArgType == S.Context.OverloadTy) { |
| 3210 | ArgType = ResolveOverloadForDeduction(S, TemplateParams, |
| 3211 | Arg, ParamType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3212 | ParamRefType != nullptr); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3213 | if (ArgType.isNull()) |
| 3214 | return true; |
| 3215 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3216 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3217 | if (ParamRefType) { |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3218 | // If the argument has incomplete array type, try to complete its type. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3219 | if (ArgType->isIncompleteArrayType()) { |
| 3220 | S.completeExprArrayBound(Arg); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3221 | ArgType = Arg->getType(); |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3222 | } |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3223 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3224 | // C++0x [temp.deduct.call]p3: |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3225 | // If P is an rvalue reference to a cv-unqualified template |
| 3226 | // parameter and the argument is an lvalue, the type "lvalue |
| 3227 | // reference to A" is used in place of A for type deduction. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3228 | if (ParamRefType->isRValueReferenceType() && |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3229 | !ParamType.getQualifiers() && |
| 3230 | isa<TemplateTypeParmType>(ParamType) && |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3231 | Arg->isLValue()) |
| 3232 | ArgType = S.Context.getLValueReferenceType(ArgType); |
| 3233 | } else { |
| 3234 | // C++ [temp.deduct.call]p2: |
| 3235 | // If P is not a reference type: |
| 3236 | // - If A is an array type, the pointer type produced by the |
| 3237 | // array-to-pointer standard conversion (4.2) is used in place of |
| 3238 | // A for type deduction; otherwise, |
| 3239 | if (ArgType->isArrayType()) |
| 3240 | ArgType = S.Context.getArrayDecayedType(ArgType); |
| 3241 | // - If A is a function type, the pointer type produced by the |
| 3242 | // function-to-pointer standard conversion (4.3) is used in place |
| 3243 | // of A for type deduction; otherwise, |
| 3244 | else if (ArgType->isFunctionType()) |
| 3245 | ArgType = S.Context.getPointerType(ArgType); |
| 3246 | else { |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3247 | // - 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] | 3248 | // type are ignored for type deduction. |
Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3249 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3250 | } |
| 3251 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3252 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3253 | // C++0x [temp.deduct.call]p4: |
| 3254 | // In general, the deduction process attempts to find template argument |
| 3255 | // values that will make the deduced A identical to A (after the type A |
| 3256 | // is transformed as described above). [...] |
| 3257 | TDF = TDF_SkipNonDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3258 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3259 | // - If the original P is a reference type, the deduced A (i.e., the |
| 3260 | // type referred to by the reference) can be more cv-qualified than |
| 3261 | // the transformed A. |
| 3262 | if (ParamRefType) |
| 3263 | TDF |= TDF_ParamWithReferenceType; |
| 3264 | // - The transformed A can be another pointer or pointer to member |
| 3265 | // type that can be converted to the deduced A via a qualification |
| 3266 | // conversion (4.4). |
| 3267 | if (ArgType->isPointerType() || ArgType->isMemberPointerType() || |
| 3268 | ArgType->isObjCObjectPointerType()) |
| 3269 | TDF |= TDF_IgnoreQualifiers; |
| 3270 | // - If P is a class and P has the form simple-template-id, then the |
| 3271 | // transformed A can be a derived class of the deduced A. Likewise, |
| 3272 | // if P is a pointer to a class of the form simple-template-id, the |
| 3273 | // transformed A can be a pointer to a derived class pointed to by |
| 3274 | // the deduced A. |
| 3275 | if (isSimpleTemplateIdType(ParamType) || |
| 3276 | (isa<PointerType>(ParamType) && |
| 3277 | isSimpleTemplateIdType( |
| 3278 | ParamType->getAs<PointerType>()->getPointeeType()))) |
| 3279 | TDF |= TDF_DerivedClass; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3280 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3281 | return false; |
| 3282 | } |
| 3283 | |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3284 | static bool |
| 3285 | hasDeducibleTemplateParameters(Sema &S, FunctionTemplateDecl *FunctionTemplate, |
| 3286 | QualType T); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3287 | |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3288 | static Sema::TemplateDeductionResult DeduceTemplateArgumentsFromCallArgument( |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3289 | Sema &S, TemplateParameterList *TemplateParams, QualType ParamType, |
| 3290 | Expr *Arg, TemplateDeductionInfo &Info, |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3291 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3292 | SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3293 | bool DecomposedParam, unsigned ArgIdx, unsigned TDF); |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3294 | |
| 3295 | /// \brief Attempt template argument deduction from an initializer list |
| 3296 | /// deemed to be an argument in a function call. |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3297 | static Sema::TemplateDeductionResult DeduceFromInitializerList( |
| 3298 | Sema &S, TemplateParameterList *TemplateParams, QualType AdjustedParamType, |
| 3299 | InitListExpr *ILE, TemplateDeductionInfo &Info, |
| 3300 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3301 | SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, unsigned ArgIdx, |
| 3302 | unsigned TDF) { |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3303 | // C++ [temp.deduct.call]p1: (CWG 1591) |
| 3304 | // If removing references and cv-qualifiers from P gives |
| 3305 | // std::initializer_list<P0> or P0[N] for some P0 and N and the argument is |
| 3306 | // a non-empty initializer list, then deduction is performed instead for |
| 3307 | // each element of the initializer list, taking P0 as a function template |
| 3308 | // parameter type and the initializer element as its argument |
| 3309 | // |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3310 | // We've already removed references and cv-qualifiers here. |
Richard Smith | 9c5534c | 2017-01-05 04:16:30 +0000 | [diff] [blame] | 3311 | if (!ILE->getNumInits()) |
| 3312 | return Sema::TDK_Success; |
| 3313 | |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3314 | QualType ElTy; |
| 3315 | auto *ArrTy = S.Context.getAsArrayType(AdjustedParamType); |
| 3316 | if (ArrTy) |
| 3317 | ElTy = ArrTy->getElementType(); |
| 3318 | else if (!S.isStdInitializerList(AdjustedParamType, &ElTy)) { |
| 3319 | // Otherwise, an initializer list argument causes the parameter to be |
| 3320 | // considered a non-deduced context |
| 3321 | return Sema::TDK_Success; |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3322 | } |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3323 | |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3324 | // Deduction only needs to be done for dependent types. |
| 3325 | if (ElTy->isDependentType()) { |
| 3326 | for (Expr *E : ILE->inits()) { |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3327 | if (auto Result = DeduceTemplateArgumentsFromCallArgument( |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3328 | S, TemplateParams, ElTy, E, Info, Deduced, OriginalCallArgs, true, |
| 3329 | ArgIdx, TDF)) |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3330 | return Result; |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3331 | } |
| 3332 | } |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3333 | |
| 3334 | // in the P0[N] case, if N is a non-type template parameter, N is deduced |
| 3335 | // from the length of the initializer list. |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3336 | if (auto *DependentArrTy = dyn_cast_or_null<DependentSizedArrayType>(ArrTy)) { |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3337 | // Determine the array bound is something we can deduce. |
| 3338 | if (NonTypeTemplateParmDecl *NTTP = |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3339 | getDeducedParameterFromExpr(Info, DependentArrTy->getSizeExpr())) { |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3340 | // We can perform template argument deduction for the given non-type |
| 3341 | // template parameter. |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3342 | llvm::APInt Size(S.Context.getIntWidth(NTTP->getType()), |
| 3343 | ILE->getNumInits()); |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3344 | if (auto Result = DeduceNonTypeTemplateArgument( |
| 3345 | S, TemplateParams, NTTP, llvm::APSInt(Size), NTTP->getType(), |
| 3346 | /*ArrayBound=*/true, Info, Deduced)) |
| 3347 | return Result; |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3348 | } |
| 3349 | } |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3350 | |
| 3351 | return Sema::TDK_Success; |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3352 | } |
| 3353 | |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3354 | /// \brief Perform template argument deduction per [temp.deduct.call] for a |
| 3355 | /// single parameter / argument pair. |
| 3356 | static Sema::TemplateDeductionResult DeduceTemplateArgumentsFromCallArgument( |
| 3357 | Sema &S, TemplateParameterList *TemplateParams, QualType ParamType, |
| 3358 | Expr *Arg, TemplateDeductionInfo &Info, |
| 3359 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3360 | SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3361 | bool DecomposedParam, unsigned ArgIdx, unsigned TDF) { |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3362 | QualType ArgType = Arg->getType(); |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3363 | QualType OrigParamType = ParamType; |
| 3364 | |
| 3365 | // If P is a reference type [...] |
| 3366 | // If P is a cv-qualified type [...] |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3367 | if (AdjustFunctionParmAndArgTypesForDeduction(S, TemplateParams, ParamType, |
Richard Smith | 363ae81 | 2017-01-04 22:03:59 +0000 | [diff] [blame] | 3368 | ArgType, Arg, TDF)) |
| 3369 | return Sema::TDK_Success; |
| 3370 | |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3371 | // If [...] the argument is a non-empty initializer list [...] |
| 3372 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) |
| 3373 | return DeduceFromInitializerList(S, TemplateParams, ParamType, ILE, Info, |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3374 | Deduced, OriginalCallArgs, ArgIdx, TDF); |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3375 | |
| 3376 | // [...] the deduction process attempts to find template argument values |
| 3377 | // that will make the deduced A identical to A |
| 3378 | // |
| 3379 | // Keep track of the argument type and corresponding parameter index, |
| 3380 | // so we can check for compatibility between the deduced A and A. |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3381 | OriginalCallArgs.push_back( |
| 3382 | Sema::OriginalCallArg(OrigParamType, DecomposedParam, ArgIdx, ArgType)); |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3383 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3384 | ArgType, Info, Deduced, TDF); |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3385 | } |
| 3386 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3387 | /// \brief Perform template argument deduction from a function call |
| 3388 | /// (C++ [temp.deduct.call]). |
| 3389 | /// |
| 3390 | /// \param FunctionTemplate the function template for which we are performing |
| 3391 | /// template argument deduction. |
| 3392 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3393 | /// \param ExplicitTemplateArgs the explicit template arguments provided |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 3394 | /// for this call. |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3395 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3396 | /// \param Args the function call arguments |
| 3397 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3398 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3399 | /// this will be set to the function template specialization produced by |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3400 | /// template argument deduction. |
| 3401 | /// |
| 3402 | /// \param Info the argument will be updated to provide additional information |
| 3403 | /// about template argument deduction. |
| 3404 | /// |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3405 | /// \param CheckNonDependent A callback to invoke to check conversions for |
| 3406 | /// non-dependent parameters, between deduction and substitution, per DR1391. |
| 3407 | /// If this returns true, substitution will be skipped and we return |
| 3408 | /// TDK_NonDependentConversionFailure. The callback is passed the parameter |
| 3409 | /// types (after substituting explicit template arguments). |
| 3410 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3411 | /// \returns the result of template argument deduction. |
Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 3412 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3413 | FunctionTemplateDecl *FunctionTemplate, |
| 3414 | TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3415 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3416 | bool PartialOverloading, |
| 3417 | llvm::function_ref<bool(ArrayRef<QualType>)> CheckNonDependent) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3418 | if (FunctionTemplate->isInvalidDecl()) |
| 3419 | return TDK_Invalid; |
| 3420 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3421 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3422 | unsigned NumParams = Function->getNumParams(); |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3423 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3424 | // C++ [temp.deduct.call]p1: |
| 3425 | // Template argument deduction is done by comparing each function template |
| 3426 | // parameter type (call it P) with the type of the corresponding argument |
| 3427 | // of the call (call it A) as described below. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3428 | if (Args.size() < Function->getMinRequiredArguments() && !PartialOverloading) |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3429 | return TDK_TooFewArguments; |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3430 | else if (TooManyArguments(NumParams, Args.size(), PartialOverloading)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3431 | const FunctionProtoType *Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3432 | = Function->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3433 | if (Proto->isTemplateVariadic()) |
| 3434 | /* Do nothing */; |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3435 | else if (!Proto->isVariadic()) |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3436 | return TDK_TooManyArguments; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3437 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3438 | |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3439 | // The types of the parameters from which we will perform template argument |
| 3440 | // deduction. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3441 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3442 | TemplateParameterList *TemplateParams |
| 3443 | = FunctionTemplate->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3444 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3445 | SmallVector<QualType, 8> ParamTypes; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3446 | unsigned NumExplicitlySpecified = 0; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3447 | if (ExplicitTemplateArgs) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3448 | TemplateDeductionResult Result = |
| 3449 | SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3450 | *ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3451 | Deduced, |
| 3452 | ParamTypes, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3453 | nullptr, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3454 | Info); |
| 3455 | if (Result) |
| 3456 | return Result; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3457 | |
| 3458 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3459 | } else { |
| 3460 | // Just fill in the parameter types from the function declaration. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3461 | for (unsigned I = 0; I != NumParams; ++I) |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3462 | ParamTypes.push_back(Function->getParamDecl(I)->getType()); |
| 3463 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3464 | |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3465 | SmallVector<OriginalCallArg, 8> OriginalCallArgs; |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3466 | |
| 3467 | // Deduce an argument of type ParamType from an expression with index ArgIdx. |
| 3468 | auto DeduceCallArgument = [&](QualType ParamType, unsigned ArgIdx) { |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3469 | // C++ [demp.deduct.call]p1: (DR1391) |
| 3470 | // Template argument deduction is done by comparing each function template |
| 3471 | // parameter that contains template-parameters that participate in |
| 3472 | // template argument deduction ... |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3473 | if (!hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
| 3474 | return Sema::TDK_Success; |
| 3475 | |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3476 | // ... with the type of the corresponding argument |
| 3477 | return DeduceTemplateArgumentsFromCallArgument( |
| 3478 | *this, TemplateParams, ParamType, Args[ArgIdx], Info, Deduced, |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3479 | OriginalCallArgs, /*Decomposed*/false, ArgIdx, /*TDF*/ 0); |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3480 | }; |
| 3481 | |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3482 | // Deduce template arguments from the function parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3483 | Deduced.resize(TemplateParams->size()); |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3484 | SmallVector<QualType, 8> ParamTypesForArgChecking; |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3485 | for (unsigned ParamIdx = 0, NumParamTypes = ParamTypes.size(), ArgIdx = 0; |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3486 | ParamIdx != NumParamTypes; ++ParamIdx) { |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3487 | QualType ParamType = ParamTypes[ParamIdx]; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3488 | |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3489 | const PackExpansionType *ParamExpansion = |
| 3490 | dyn_cast<PackExpansionType>(ParamType); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3491 | if (!ParamExpansion) { |
| 3492 | // Simple case: matching a function parameter to a function argument. |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3493 | if (ArgIdx >= Args.size()) |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3494 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3495 | |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3496 | ParamTypesForArgChecking.push_back(ParamType); |
Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3497 | if (auto Result = DeduceCallArgument(ParamType, ArgIdx++)) |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3498 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3499 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3500 | continue; |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3501 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3502 | |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3503 | QualType ParamPattern = ParamExpansion->getPattern(); |
| 3504 | PackDeductionScope PackScope(*this, TemplateParams, Deduced, Info, |
| 3505 | ParamPattern); |
| 3506 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3507 | // C++0x [temp.deduct.call]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3508 | // For a function parameter pack that occurs at the end of the |
| 3509 | // parameter-declaration-list, the type A of each remaining argument of |
| 3510 | // the call is compared with the type P of the declarator-id of the |
| 3511 | // function parameter pack. Each comparison deduces template arguments |
| 3512 | // for subsequent positions in the template parameter packs expanded by |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3513 | // the function parameter pack. When a function parameter pack appears |
| 3514 | // in a non-deduced context [not at the end of the list], the type of |
| 3515 | // that parameter pack is never deduced. |
| 3516 | // |
| 3517 | // FIXME: The above rule allows the size of the parameter pack to change |
| 3518 | // after we skip it (in the non-deduced case). That makes no sense, so |
| 3519 | // we instead notionally deduce the pack against N arguments, where N is |
| 3520 | // the length of the explicitly-specified pack if it's expanded by the |
| 3521 | // parameter pack and 0 otherwise, and we treat each deduction as a |
| 3522 | // non-deduced context. |
| 3523 | if (ParamIdx + 1 == NumParamTypes) { |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3524 | for (; ArgIdx < Args.size(); PackScope.nextPackElement(), ++ArgIdx) { |
| 3525 | ParamTypesForArgChecking.push_back(ParamPattern); |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3526 | if (auto Result = DeduceCallArgument(ParamPattern, ArgIdx)) |
| 3527 | return Result; |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3528 | } |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3529 | } else { |
| 3530 | // If the parameter type contains an explicitly-specified pack that we |
| 3531 | // could not expand, skip the number of parameters notionally created |
| 3532 | // by the expansion. |
| 3533 | Optional<unsigned> NumExpansions = ParamExpansion->getNumExpansions(); |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3534 | if (NumExpansions && !PackScope.isPartiallyExpanded()) { |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3535 | for (unsigned I = 0; I != *NumExpansions && ArgIdx < Args.size(); |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3536 | ++I, ++ArgIdx) { |
| 3537 | ParamTypesForArgChecking.push_back(ParamPattern); |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3538 | // FIXME: Should we add OriginalCallArgs for these? What if the |
| 3539 | // corresponding argument is a list? |
| 3540 | PackScope.nextPackElement(); |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3541 | } |
| 3542 | } |
Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3543 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3544 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3545 | // Build argument packs for each of the parameter packs expanded by this |
| 3546 | // pack expansion. |
Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 3547 | if (auto Result = PackScope.finish()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3548 | return Result; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3549 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3550 | |
Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3551 | return FinishTemplateArgumentDeduction( |
| 3552 | FunctionTemplate, Deduced, NumExplicitlySpecified, Specialization, Info, |
| 3553 | &OriginalCallArgs, PartialOverloading, |
| 3554 | [&]() { return CheckNonDependent(ParamTypesForArgChecking); }); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3555 | } |
| 3556 | |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3557 | QualType Sema::adjustCCAndNoReturn(QualType ArgFunctionType, |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3558 | QualType FunctionType, |
| 3559 | bool AdjustExceptionSpec) { |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3560 | if (ArgFunctionType.isNull()) |
| 3561 | return ArgFunctionType; |
| 3562 | |
| 3563 | const FunctionProtoType *FunctionTypeP = |
| 3564 | FunctionType->castAs<FunctionProtoType>(); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3565 | const FunctionProtoType *ArgFunctionTypeP = |
| 3566 | ArgFunctionType->getAs<FunctionProtoType>(); |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3567 | |
| 3568 | FunctionProtoType::ExtProtoInfo EPI = ArgFunctionTypeP->getExtProtoInfo(); |
| 3569 | bool Rebuild = false; |
| 3570 | |
| 3571 | CallingConv CC = FunctionTypeP->getCallConv(); |
| 3572 | if (EPI.ExtInfo.getCC() != CC) { |
| 3573 | EPI.ExtInfo = EPI.ExtInfo.withCallingConv(CC); |
| 3574 | Rebuild = true; |
| 3575 | } |
| 3576 | |
| 3577 | bool NoReturn = FunctionTypeP->getNoReturnAttr(); |
| 3578 | if (EPI.ExtInfo.getNoReturn() != NoReturn) { |
| 3579 | EPI.ExtInfo = EPI.ExtInfo.withNoReturn(NoReturn); |
| 3580 | Rebuild = true; |
| 3581 | } |
| 3582 | |
| 3583 | if (AdjustExceptionSpec && (FunctionTypeP->hasExceptionSpec() || |
| 3584 | ArgFunctionTypeP->hasExceptionSpec())) { |
| 3585 | EPI.ExceptionSpec = FunctionTypeP->getExtProtoInfo().ExceptionSpec; |
| 3586 | Rebuild = true; |
| 3587 | } |
| 3588 | |
| 3589 | if (!Rebuild) |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3590 | return ArgFunctionType; |
| 3591 | |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3592 | return Context.getFunctionType(ArgFunctionTypeP->getReturnType(), |
| 3593 | ArgFunctionTypeP->getParamTypes(), EPI); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3596 | /// \brief Deduce template arguments when taking the address of a function |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3597 | /// template (C++ [temp.deduct.funcaddr]) or matching a specialization to |
| 3598 | /// a template. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3599 | /// |
| 3600 | /// \param FunctionTemplate the function template for which we are performing |
| 3601 | /// template argument deduction. |
| 3602 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3603 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3604 | /// arguments. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3605 | /// |
| 3606 | /// \param ArgFunctionType the function type that will be used as the |
| 3607 | /// "argument" type (A) when performing template argument deduction from the |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3608 | /// function template's function type. This type may be NULL, if there is no |
| 3609 | /// argument type to compare against, in C++0x [temp.arg.explicit]p3. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3610 | /// |
| 3611 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3612 | /// this will be set to the function template specialization produced by |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3613 | /// template argument deduction. |
| 3614 | /// |
| 3615 | /// \param Info the argument will be updated to provide additional information |
| 3616 | /// about template argument deduction. |
| 3617 | /// |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3618 | /// \param IsAddressOfFunction If \c true, we are deducing as part of taking |
| 3619 | /// the address of a function template per [temp.deduct.funcaddr] and |
| 3620 | /// [over.over]. If \c false, we are looking up a function template |
| 3621 | /// specialization based on its signature, per [temp.deduct.decl]. |
| 3622 | /// |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3623 | /// \returns the result of template argument deduction. |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3624 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3625 | FunctionTemplateDecl *FunctionTemplate, |
| 3626 | TemplateArgumentListInfo *ExplicitTemplateArgs, QualType ArgFunctionType, |
| 3627 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 3628 | bool IsAddressOfFunction) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3629 | if (FunctionTemplate->isInvalidDecl()) |
| 3630 | return TDK_Invalid; |
| 3631 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3632 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 3633 | TemplateParameterList *TemplateParams |
| 3634 | = FunctionTemplate->getTemplateParameters(); |
| 3635 | QualType FunctionType = Function->getType(); |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3636 | |
| 3637 | // When taking the address of a function, we require convertibility of |
| 3638 | // the resulting function type. Otherwise, we allow arbitrary mismatches |
| 3639 | // of calling convention, noreturn, and noexcept. |
| 3640 | if (!IsAddressOfFunction) |
| 3641 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, FunctionType, |
| 3642 | /*AdjustExceptionSpec*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3643 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3644 | // Substitute any explicit template arguments. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3645 | LocalInstantiationScope InstScope(*this); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3646 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3647 | unsigned NumExplicitlySpecified = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3648 | SmallVector<QualType, 4> ParamTypes; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3649 | if (ExplicitTemplateArgs) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3650 | if (TemplateDeductionResult Result |
| 3651 | = SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3652 | *ExplicitTemplateArgs, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3653 | Deduced, ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3654 | &FunctionType, Info)) |
| 3655 | return Result; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3656 | |
| 3657 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3658 | } |
| 3659 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3660 | // Unevaluated SFINAE context. |
| 3661 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3662 | SFINAETrap Trap(*this); |
| 3663 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3664 | Deduced.resize(TemplateParams->size()); |
| 3665 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3666 | // 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] | 3667 | // type so that we treat it as a non-deduced context in what follows. If we |
| 3668 | // are looking up by signature, the signature type should also have a deduced |
| 3669 | // return type, which we instead expect to exactly match. |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3670 | bool HasDeducedReturnType = false; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3671 | if (getLangOpts().CPlusPlus14 && IsAddressOfFunction && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3672 | Function->getReturnType()->getContainedAutoType()) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3673 | FunctionType = SubstAutoType(FunctionType, Context.DependentTy); |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3674 | HasDeducedReturnType = true; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3675 | } |
| 3676 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3677 | if (!ArgFunctionType.isNull()) { |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3678 | unsigned TDF = TDF_TopLevelParameterTypeList; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3679 | if (IsAddressOfFunction) |
| 3680 | TDF |= TDF_InOverloadResolution; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3681 | // Deduce template arguments from the function type. |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3682 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3683 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3684 | FunctionType, ArgFunctionType, |
| 3685 | Info, Deduced, TDF)) |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3686 | return Result; |
| 3687 | } |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3688 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3689 | if (TemplateDeductionResult Result |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3690 | = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
| 3691 | NumExplicitlySpecified, |
| 3692 | Specialization, Info)) |
| 3693 | return Result; |
| 3694 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3695 | // If the function has a deduced return type, deduce it now, so we can check |
| 3696 | // that the deduced function type matches the requested type. |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3697 | if (HasDeducedReturnType && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3698 | Specialization->getReturnType()->isUndeducedType() && |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3699 | DeduceReturnType(Specialization, Info.getLocation(), false)) |
| 3700 | return TDK_MiscellaneousDeductionFailure; |
| 3701 | |
Richard Smith | 9095e5b | 2016-11-01 01:31:23 +0000 | [diff] [blame] | 3702 | // If the function has a dependent exception specification, resolve it now, |
| 3703 | // so we can check that the exception specification matches. |
| 3704 | auto *SpecializationFPT = |
| 3705 | Specialization->getType()->castAs<FunctionProtoType>(); |
| 3706 | if (getLangOpts().CPlusPlus1z && |
| 3707 | isUnresolvedExceptionSpec(SpecializationFPT->getExceptionSpecType()) && |
| 3708 | !ResolveExceptionSpec(Info.getLocation(), SpecializationFPT)) |
| 3709 | return TDK_MiscellaneousDeductionFailure; |
| 3710 | |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3711 | // Adjust the exception specification of the argument again to match the |
| 3712 | // substituted and resolved type we just formed. (Calling convention and |
| 3713 | // noreturn can't be dependent, so we don't actually need this for them |
| 3714 | // right now.) |
| 3715 | QualType SpecializationType = Specialization->getType(); |
| 3716 | if (!IsAddressOfFunction) |
| 3717 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, SpecializationType, |
| 3718 | /*AdjustExceptionSpec*/true); |
| 3719 | |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3720 | // 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] | 3721 | // specialization with respect to arguments of compatible pointer to function |
| 3722 | // types, template argument deduction fails. |
| 3723 | if (!ArgFunctionType.isNull()) { |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3724 | if (IsAddressOfFunction && |
| 3725 | !isSameOrCompatibleFunctionType( |
| 3726 | Context.getCanonicalType(SpecializationType), |
| 3727 | Context.getCanonicalType(ArgFunctionType))) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3728 | return TDK_MiscellaneousDeductionFailure; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3729 | |
| 3730 | if (!IsAddressOfFunction && |
| 3731 | !Context.hasSameType(SpecializationType, ArgFunctionType)) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3732 | return TDK_MiscellaneousDeductionFailure; |
| 3733 | } |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3734 | |
| 3735 | return TDK_Success; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3736 | } |
| 3737 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3738 | /// \brief Given a function declaration (e.g. a generic lambda conversion |
| 3739 | /// function) that contains an 'auto' in its result type, substitute it |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3740 | /// with TypeToReplaceAutoWith. Be careful to pass in the type you want |
| 3741 | /// to replace 'auto' with and not the actual result type you want |
| 3742 | /// to set the function to. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3743 | static inline void |
| 3744 | SubstAutoWithinFunctionReturnType(FunctionDecl *F, |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3745 | QualType TypeToReplaceAutoWith, Sema &S) { |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3746 | assert(!TypeToReplaceAutoWith->getContainedAutoType()); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3747 | QualType AutoResultType = F->getReturnType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3748 | assert(AutoResultType->getContainedAutoType()); |
| 3749 | QualType DeducedResultType = S.SubstAutoType(AutoResultType, |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3750 | TypeToReplaceAutoWith); |
| 3751 | S.Context.adjustDeducedFunctionResultType(F, DeducedResultType); |
| 3752 | } |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3753 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3754 | /// \brief Given a specialized conversion operator of a generic lambda |
| 3755 | /// create the corresponding specializations of the call operator and |
| 3756 | /// the static-invoker. If the return type of the call operator is auto, |
| 3757 | /// deduce its return type and check if that matches the |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3758 | /// return type of the destination function ptr. |
| 3759 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3760 | static inline Sema::TemplateDeductionResult |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3761 | SpecializeCorrespondingLambdaCallOperatorAndInvoker( |
| 3762 | CXXConversionDecl *ConversionSpecialized, |
| 3763 | SmallVectorImpl<DeducedTemplateArgument> &DeducedArguments, |
| 3764 | QualType ReturnTypeOfDestFunctionPtr, |
| 3765 | TemplateDeductionInfo &TDInfo, |
| 3766 | Sema &S) { |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3767 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3768 | CXXRecordDecl *LambdaClass = ConversionSpecialized->getParent(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3769 | assert(LambdaClass && LambdaClass->isGenericLambda()); |
| 3770 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3771 | CXXMethodDecl *CallOpGeneric = LambdaClass->getLambdaCallOperator(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3772 | QualType CallOpResultType = CallOpGeneric->getReturnType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3773 | const bool GenericLambdaCallOperatorHasDeducedReturnType = |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3774 | CallOpResultType->getContainedAutoType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3775 | |
| 3776 | FunctionTemplateDecl *CallOpTemplate = |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3777 | CallOpGeneric->getDescribedFunctionTemplate(); |
| 3778 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3779 | FunctionDecl *CallOpSpecialized = nullptr; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3780 | // Use the deduced arguments of the conversion function, to specialize our |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3781 | // generic lambda's call operator. |
| 3782 | if (Sema::TemplateDeductionResult Result |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3783 | = S.FinishTemplateArgumentDeduction(CallOpTemplate, |
| 3784 | DeducedArguments, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3785 | 0, CallOpSpecialized, TDInfo)) |
| 3786 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3787 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3788 | // If we need to deduce the return type, do so (instantiates the callop). |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3789 | if (GenericLambdaCallOperatorHasDeducedReturnType && |
| 3790 | CallOpSpecialized->getReturnType()->isUndeducedType()) |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3791 | S.DeduceReturnType(CallOpSpecialized, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3792 | CallOpSpecialized->getPointOfInstantiation(), |
| 3793 | /*Diagnose*/ true); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3794 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3795 | // Check to see if the return type of the destination ptr-to-function |
| 3796 | // matches the return type of the call operator. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3797 | if (!S.Context.hasSameType(CallOpSpecialized->getReturnType(), |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3798 | ReturnTypeOfDestFunctionPtr)) |
| 3799 | return Sema::TDK_NonDeducedMismatch; |
| 3800 | // Since we have succeeded in matching the source and destination |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3801 | // ptr-to-functions (now including return type), and have successfully |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3802 | // specialized our corresponding call operator, we are ready to |
| 3803 | // specialize the static invoker with the deduced arguments of our |
| 3804 | // ptr-to-function. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3805 | FunctionDecl *InvokerSpecialized = nullptr; |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3806 | FunctionTemplateDecl *InvokerTemplate = LambdaClass-> |
| 3807 | getLambdaStaticInvoker()->getDescribedFunctionTemplate(); |
| 3808 | |
Yaron Keren | f428fcf | 2015-05-13 17:56:46 +0000 | [diff] [blame] | 3809 | #ifndef NDEBUG |
| 3810 | Sema::TemplateDeductionResult LLVM_ATTRIBUTE_UNUSED Result = |
| 3811 | #endif |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3812 | S.FinishTemplateArgumentDeduction(InvokerTemplate, DeducedArguments, 0, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3813 | InvokerSpecialized, TDInfo); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3814 | assert(Result == Sema::TDK_Success && |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3815 | "If the call operator succeeded so should the invoker!"); |
| 3816 | // Set the result type to match the corresponding call operator |
| 3817 | // specialization's result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3818 | if (GenericLambdaCallOperatorHasDeducedReturnType && |
| 3819 | InvokerSpecialized->getReturnType()->isUndeducedType()) { |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3820 | // Be sure to get the type to replace 'auto' with and not |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3821 | // the full result type of the call op specialization |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3822 | // to substitute into the 'auto' of the invoker and conversion |
| 3823 | // function. |
| 3824 | // For e.g. |
| 3825 | // int* (*fp)(int*) = [](auto* a) -> auto* { return a; }; |
| 3826 | // We don't want to subst 'int*' into 'auto' to get int**. |
| 3827 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3828 | QualType TypeToReplaceAutoWith = CallOpSpecialized->getReturnType() |
| 3829 | ->getContainedAutoType() |
| 3830 | ->getDeducedType(); |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3831 | SubstAutoWithinFunctionReturnType(InvokerSpecialized, |
| 3832 | TypeToReplaceAutoWith, S); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3833 | SubstAutoWithinFunctionReturnType(ConversionSpecialized, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3834 | TypeToReplaceAutoWith, S); |
| 3835 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3836 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3837 | // Ensure that static invoker doesn't have a const qualifier. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3838 | // FIXME: When creating the InvokerTemplate in SemaLambda.cpp |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3839 | // do not use the CallOperator's TypeSourceInfo which allows |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3840 | // the const qualifier to leak through. |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3841 | const FunctionProtoType *InvokerFPT = InvokerSpecialized-> |
| 3842 | getType().getTypePtr()->castAs<FunctionProtoType>(); |
| 3843 | FunctionProtoType::ExtProtoInfo EPI = InvokerFPT->getExtProtoInfo(); |
| 3844 | EPI.TypeQuals = 0; |
| 3845 | InvokerSpecialized->setType(S.Context.getFunctionType( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3846 | InvokerFPT->getReturnType(), InvokerFPT->getParamTypes(), EPI)); |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3847 | return Sema::TDK_Success; |
| 3848 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3849 | /// \brief Deduce template arguments for a templated conversion |
| 3850 | /// function (C++ [temp.deduct.conv]) and, if successful, produce a |
| 3851 | /// conversion function template specialization. |
| 3852 | Sema::TemplateDeductionResult |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3853 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *ConversionTemplate, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3854 | QualType ToType, |
| 3855 | CXXConversionDecl *&Specialization, |
| 3856 | TemplateDeductionInfo &Info) { |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3857 | if (ConversionTemplate->isInvalidDecl()) |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3858 | return TDK_Invalid; |
| 3859 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3860 | CXXConversionDecl *ConversionGeneric |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3861 | = cast<CXXConversionDecl>(ConversionTemplate->getTemplatedDecl()); |
| 3862 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3863 | QualType FromType = ConversionGeneric->getConversionType(); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3864 | |
| 3865 | // Canonicalize the types for deduction. |
| 3866 | QualType P = Context.getCanonicalType(FromType); |
| 3867 | QualType A = Context.getCanonicalType(ToType); |
| 3868 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3869 | // C++0x [temp.deduct.conv]p2: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3870 | // If P is a reference type, the type referred to by P is used for |
| 3871 | // type deduction. |
| 3872 | if (const ReferenceType *PRef = P->getAs<ReferenceType>()) |
| 3873 | P = PRef->getPointeeType(); |
| 3874 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3875 | // C++0x [temp.deduct.conv]p4: |
| 3876 | // [...] 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] | 3877 | // for type deduction. |
| 3878 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3879 | A = ARef->getPointeeType().getUnqualifiedType(); |
| 3880 | // C++ [temp.deduct.conv]p3: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3881 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3882 | // If A is not a reference type: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3883 | else { |
| 3884 | assert(!A->isReferenceType() && "Reference types were handled above"); |
| 3885 | |
| 3886 | // - If P is an array type, the pointer type produced by the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3887 | // array-to-pointer standard conversion (4.2) is used in place |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3888 | // of P for type deduction; otherwise, |
| 3889 | if (P->isArrayType()) |
| 3890 | P = Context.getArrayDecayedType(P); |
| 3891 | // - If P is a function type, the pointer type produced by the |
| 3892 | // function-to-pointer standard conversion (4.3) is used in |
| 3893 | // place of P for type deduction; otherwise, |
| 3894 | else if (P->isFunctionType()) |
| 3895 | P = Context.getPointerType(P); |
| 3896 | // - 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] | 3897 | // P's type are ignored for type deduction. |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3898 | else |
| 3899 | P = P.getUnqualifiedType(); |
| 3900 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3901 | // C++0x [temp.deduct.conv]p4: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3902 | // 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] | 3903 | // 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] | 3904 | // referred to by A is used for type deduction. |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3905 | A = A.getUnqualifiedType(); |
| 3906 | } |
| 3907 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3908 | // Unevaluated SFINAE context. |
| 3909 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3910 | SFINAETrap Trap(*this); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3911 | |
| 3912 | // C++ [temp.deduct.conv]p1: |
| 3913 | // Template argument deduction is done by comparing the return |
| 3914 | // type of the template conversion function (call it P) with the |
| 3915 | // type that is required as the result of the conversion (call it |
| 3916 | // A) as described in 14.8.2.4. |
| 3917 | TemplateParameterList *TemplateParams |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3918 | = ConversionTemplate->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3919 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3920 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3921 | |
| 3922 | // C++0x [temp.deduct.conv]p4: |
| 3923 | // In general, the deduction process attempts to find template |
| 3924 | // argument values that will make the deduced A identical to |
| 3925 | // A. However, there are two cases that allow a difference: |
| 3926 | unsigned TDF = 0; |
| 3927 | // - If the original A is a reference type, A can be more |
| 3928 | // cv-qualified than the deduced A (i.e., the type referred to |
| 3929 | // by the reference) |
| 3930 | if (ToType->isReferenceType()) |
| 3931 | TDF |= TDF_ParamWithReferenceType; |
| 3932 | // - The deduced A can be another pointer or pointer to member |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3933 | // type that can be converted to A via a qualification |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3934 | // conversion. |
| 3935 | // |
| 3936 | // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when |
| 3937 | // both P and A are pointers or member pointers. In this case, we |
| 3938 | // just ignore cv-qualifiers completely). |
| 3939 | if ((P->isPointerType() && A->isPointerType()) || |
Douglas Gregor | 9f05ed5 | 2011-08-30 00:37:54 +0000 | [diff] [blame] | 3940 | (P->isMemberPointerType() && A->isMemberPointerType())) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3941 | TDF |= TDF_IgnoreQualifiers; |
| 3942 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3943 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3944 | P, A, Info, Deduced, TDF)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3945 | return Result; |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3946 | |
| 3947 | // Create an Instantiation Scope for finalizing the operator. |
| 3948 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3949 | // Finish template argument deduction. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3950 | FunctionDecl *ConversionSpecialized = nullptr; |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3951 | TemplateDeductionResult Result |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3952 | = FinishTemplateArgumentDeduction(ConversionTemplate, Deduced, 0, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3953 | ConversionSpecialized, Info); |
| 3954 | Specialization = cast_or_null<CXXConversionDecl>(ConversionSpecialized); |
| 3955 | |
| 3956 | // If the conversion operator is being invoked on a lambda closure to convert |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3957 | // to a ptr-to-function, use the deduced arguments from the conversion |
| 3958 | // function to specialize the corresponding call operator. |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3959 | // e.g., int (*fp)(int) = [](auto a) { return a; }; |
| 3960 | if (Result == TDK_Success && isLambdaConversionOperator(ConversionGeneric)) { |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3961 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3962 | // Get the return type of the destination ptr-to-function we are converting |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3963 | // to. This is necessary for matching the lambda call operator's return |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3964 | // type to that of the destination ptr-to-function's return type. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3965 | assert(A->isPointerType() && |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3966 | "Can only convert from lambda to ptr-to-function"); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3967 | const FunctionType *ToFunType = |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3968 | A->getPointeeType().getTypePtr()->getAs<FunctionType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3969 | const QualType DestFunctionPtrReturnType = ToFunType->getReturnType(); |
| 3970 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3971 | // Create the corresponding specializations of the call operator and |
| 3972 | // the static-invoker; and if the return type is auto, |
| 3973 | // deduce the return type and check if it matches the |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3974 | // DestFunctionPtrReturnType. |
| 3975 | // For instance: |
| 3976 | // auto L = [](auto a) { return f(a); }; |
| 3977 | // int (*fp)(int) = L; |
| 3978 | // char (*fp2)(int) = L; <-- Not OK. |
| 3979 | |
| 3980 | Result = SpecializeCorrespondingLambdaCallOperatorAndInvoker( |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3981 | Specialization, Deduced, DestFunctionPtrReturnType, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3982 | Info, *this); |
| 3983 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3984 | return Result; |
| 3985 | } |
| 3986 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3987 | /// \brief Deduce template arguments for a function template when there is |
| 3988 | /// nothing to deduce against (C++0x [temp.arg.explicit]p3). |
| 3989 | /// |
| 3990 | /// \param FunctionTemplate the function template for which we are performing |
| 3991 | /// template argument deduction. |
| 3992 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3993 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3994 | /// arguments. |
| 3995 | /// |
| 3996 | /// \param Specialization if template argument deduction was successful, |
| 3997 | /// this will be set to the function template specialization produced by |
| 3998 | /// template argument deduction. |
| 3999 | /// |
| 4000 | /// \param Info the argument will be updated to provide additional information |
| 4001 | /// about template argument deduction. |
| 4002 | /// |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4003 | /// \param IsAddressOfFunction If \c true, we are deducing as part of taking |
| 4004 | /// the address of a function template in a context where we do not have a |
| 4005 | /// target type, per [over.over]. If \c false, we are looking up a function |
| 4006 | /// template specialization based on its signature, which only happens when |
| 4007 | /// deducing a function parameter type from an argument that is a template-id |
| 4008 | /// naming a function template specialization. |
| 4009 | /// |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4010 | /// \returns the result of template argument deduction. |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4011 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 4012 | FunctionTemplateDecl *FunctionTemplate, |
| 4013 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 4014 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 4015 | bool IsAddressOfFunction) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4016 | return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 4017 | QualType(), Specialization, Info, |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4018 | IsAddressOfFunction); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4019 | } |
| 4020 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4021 | namespace { |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4022 | /// Substitute the 'auto' specifier or deduced template specialization type |
| 4023 | /// specifier within a type for a given replacement type. |
| 4024 | class SubstituteDeducedTypeTransform : |
| 4025 | public TreeTransform<SubstituteDeducedTypeTransform> { |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4026 | QualType Replacement; |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4027 | bool UseTypeSugar; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4028 | public: |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4029 | SubstituteDeducedTypeTransform(Sema &SemaRef, QualType Replacement, |
| 4030 | bool UseTypeSugar = true) |
| 4031 | : TreeTransform<SubstituteDeducedTypeTransform>(SemaRef), |
| 4032 | Replacement(Replacement), UseTypeSugar(UseTypeSugar) {} |
| 4033 | |
| 4034 | QualType TransformDesugared(TypeLocBuilder &TLB, DeducedTypeLoc TL) { |
| 4035 | assert(isa<TemplateTypeParmType>(Replacement) && |
| 4036 | "unexpected unsugared replacement kind"); |
| 4037 | QualType Result = Replacement; |
| 4038 | TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result); |
| 4039 | NewTL.setNameLoc(TL.getNameLoc()); |
| 4040 | return Result; |
| 4041 | } |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 4042 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4043 | QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) { |
| 4044 | // If we're building the type pattern to deduce against, don't wrap the |
| 4045 | // substituted type in an AutoType. Certain template deduction rules |
| 4046 | // apply only when a template type parameter appears directly (and not if |
| 4047 | // the parameter is found through desugaring). For instance: |
| 4048 | // auto &&lref = lvalue; |
| 4049 | // must transform into "rvalue reference to T" not "rvalue reference to |
| 4050 | // 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] | 4051 | // |
| 4052 | // FIXME: Is this still necessary? |
| 4053 | if (!UseTypeSugar) |
| 4054 | return TransformDesugared(TLB, TL); |
| 4055 | |
| 4056 | QualType Result = SemaRef.Context.getAutoType( |
| 4057 | Replacement, TL.getTypePtr()->getKeyword(), Replacement.isNull()); |
| 4058 | auto NewTL = TLB.push<AutoTypeLoc>(Result); |
| 4059 | NewTL.setNameLoc(TL.getNameLoc()); |
| 4060 | return Result; |
| 4061 | } |
| 4062 | |
| 4063 | QualType TransformDeducedTemplateSpecializationType( |
| 4064 | TypeLocBuilder &TLB, DeducedTemplateSpecializationTypeLoc TL) { |
| 4065 | if (!UseTypeSugar) |
| 4066 | return TransformDesugared(TLB, TL); |
| 4067 | |
| 4068 | QualType Result = SemaRef.Context.getDeducedTemplateSpecializationType( |
| 4069 | TL.getTypePtr()->getTemplateName(), |
| 4070 | Replacement, Replacement.isNull()); |
| 4071 | auto NewTL = TLB.push<DeducedTemplateSpecializationTypeLoc>(Result); |
| 4072 | NewTL.setNameLoc(TL.getNameLoc()); |
| 4073 | return Result; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4074 | } |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 4075 | |
| 4076 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 4077 | // Lambdas never need to be transformed. |
| 4078 | return E; |
| 4079 | } |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4080 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4081 | QualType Apply(TypeLoc TL) { |
| 4082 | // Create some scratch storage for the transformed type locations. |
| 4083 | // FIXME: We're just going to throw this information away. Don't build it. |
| 4084 | TypeLocBuilder TLB; |
| 4085 | TLB.reserve(TL.getFullDataSize()); |
| 4086 | return TransformType(TLB, TL); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4087 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4088 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4089 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4090 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4091 | Sema::DeduceAutoResult |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4092 | Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *&Init, QualType &Result, |
| 4093 | Optional<unsigned> DependentDeductionDepth) { |
| 4094 | return DeduceAutoType(Type->getTypeLoc(), Init, Result, |
| 4095 | DependentDeductionDepth); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4096 | } |
| 4097 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4098 | /// \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] | 4099 | /// |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4100 | /// Note that this is done even if the initializer is dependent. (This is |
| 4101 | /// necessary to support partial ordering of templates using 'auto'.) |
| 4102 | /// A dependent type will be produced when deducing from a dependent type. |
| 4103 | /// |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4104 | /// \param Type the type pattern using the auto type-specifier. |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4105 | /// \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] | 4106 | /// \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] | 4107 | /// deduced type. |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4108 | /// \param DependentDeductionDepth Set if we should permit deduction in |
| 4109 | /// dependent cases. This is necessary for template partial ordering with |
| 4110 | /// 'auto' template parameters. The value specified is the template |
| 4111 | /// parameter depth at which we should perform 'auto' deduction. |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4112 | Sema::DeduceAutoResult |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4113 | Sema::DeduceAutoType(TypeLoc Type, Expr *&Init, QualType &Result, |
| 4114 | Optional<unsigned> DependentDeductionDepth) { |
John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4115 | if (Init->getType()->isNonOverloadPlaceholderType()) { |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4116 | ExprResult NonPlaceholder = CheckPlaceholderExpr(Init); |
| 4117 | if (NonPlaceholder.isInvalid()) |
| 4118 | return DAR_FailedAlreadyDiagnosed; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4119 | Init = NonPlaceholder.get(); |
John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4120 | } |
| 4121 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4122 | if (!DependentDeductionDepth && |
| 4123 | (Type.getType()->isDependentType() || Init->isTypeDependent())) { |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4124 | Result = SubstituteDeducedTypeTransform(*this, QualType()).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4125 | assert(!Result.isNull() && "substituting DependentTy can't fail"); |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4126 | return DAR_Succeeded; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4127 | } |
| 4128 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4129 | // Find the depth of template parameter to synthesize. |
| 4130 | unsigned Depth = DependentDeductionDepth.getValueOr(0); |
| 4131 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4132 | // If this is a 'decltype(auto)' specifier, do the decltype dance. |
| 4133 | // Since 'decltype(auto)' can only occur at the top of the type, we |
| 4134 | // don't need to go digging for it. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4135 | if (const AutoType *AT = Type.getType()->getAs<AutoType>()) { |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4136 | if (AT->isDecltypeAuto()) { |
| 4137 | if (isa<InitListExpr>(Init)) { |
| 4138 | Diag(Init->getLocStart(), diag::err_decltype_auto_initializer_list); |
| 4139 | return DAR_FailedAlreadyDiagnosed; |
| 4140 | } |
| 4141 | |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 4142 | QualType Deduced = BuildDecltypeType(Init, Init->getLocStart(), false); |
David Majnemer | 3c20ab2 | 2015-07-01 00:29:28 +0000 | [diff] [blame] | 4143 | if (Deduced.isNull()) |
| 4144 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4145 | // FIXME: Support a non-canonical deduced type for 'auto'. |
| 4146 | Deduced = Context.getCanonicalType(Deduced); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4147 | Result = SubstituteDeducedTypeTransform(*this, Deduced).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4148 | if (Result.isNull()) |
| 4149 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4150 | return DAR_Succeeded; |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4151 | } else if (!getLangOpts().CPlusPlus) { |
| 4152 | if (isa<InitListExpr>(Init)) { |
| 4153 | Diag(Init->getLocStart(), diag::err_auto_init_list_from_c); |
| 4154 | return DAR_FailedAlreadyDiagnosed; |
| 4155 | } |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4156 | } |
| 4157 | } |
| 4158 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4159 | SourceLocation Loc = Init->getExprLoc(); |
| 4160 | |
| 4161 | LocalInstantiationScope InstScope(*this); |
| 4162 | |
| 4163 | // Build template<class TemplParam> void Func(FuncParam); |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4164 | TemplateTypeParmDecl *TemplParam = TemplateTypeParmDecl::Create( |
| 4165 | Context, nullptr, SourceLocation(), Loc, Depth, 0, nullptr, false, false); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4166 | QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0); |
| 4167 | NamedDecl *TemplParamPtr = TemplParam; |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 4168 | FixedSizeTemplateParameterListStorage<1, false> TemplateParamsSt( |
| 4169 | Loc, Loc, TemplParamPtr, Loc, nullptr); |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 4170 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4171 | QualType FuncParam = |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4172 | SubstituteDeducedTypeTransform(*this, TemplArg, /*UseTypeSugar*/false) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4173 | .Apply(Type); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4174 | assert(!FuncParam.isNull() && |
| 4175 | "substituting template parameter for 'auto' failed"); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4176 | |
| 4177 | // Deduce type of TemplParam in Func(Init) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4178 | SmallVector<DeducedTemplateArgument, 1> Deduced; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4179 | Deduced.resize(1); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4180 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4181 | TemplateDeductionInfo Info(Loc, Depth); |
| 4182 | |
| 4183 | // If deduction failed, don't diagnose if the initializer is dependent; it |
| 4184 | // might acquire a matching type in the instantiation. |
| 4185 | auto DeductionFailed = [&]() -> DeduceAutoResult { |
| 4186 | if (Init->isTypeDependent()) { |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4187 | Result = SubstituteDeducedTypeTransform(*this, QualType()).Apply(Type); |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4188 | assert(!Result.isNull() && "substituting DependentTy can't fail"); |
| 4189 | return DAR_Succeeded; |
| 4190 | } |
| 4191 | return DAR_Failed; |
| 4192 | }; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4193 | |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 4194 | SmallVector<OriginalCallArg, 4> OriginalCallArgs; |
| 4195 | |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 4196 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4197 | if (InitList) { |
Richard Smith | c8a32e5 | 2017-01-05 23:12:16 +0000 | [diff] [blame] | 4198 | // Notionally, we substitute std::initializer_list<T> for 'auto' and deduce |
| 4199 | // against that. Such deduction only succeeds if removing cv-qualifiers and |
| 4200 | // references results in std::initializer_list<T>. |
| 4201 | if (!Type.getType().getNonReferenceType()->getAs<AutoType>()) |
| 4202 | return DAR_Failed; |
| 4203 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4204 | for (unsigned i = 0, e = InitList->getNumInits(); i < e; ++i) { |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 4205 | if (DeduceTemplateArgumentsFromCallArgument( |
| 4206 | *this, TemplateParamsSt.get(), TemplArg, InitList->getInit(i), |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 4207 | Info, Deduced, OriginalCallArgs, /*Decomposed*/ true, |
| 4208 | /*ArgIdx*/ 0, /*TDF*/ 0)) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4209 | return DeductionFailed(); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4210 | } |
| 4211 | } else { |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4212 | if (!getLangOpts().CPlusPlus && Init->refersToBitField()) { |
| 4213 | Diag(Loc, diag::err_auto_bitfield); |
| 4214 | return DAR_FailedAlreadyDiagnosed; |
| 4215 | } |
| 4216 | |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 4217 | if (DeduceTemplateArgumentsFromCallArgument( |
| 4218 | *this, TemplateParamsSt.get(), FuncParam, Init, Info, Deduced, |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 4219 | OriginalCallArgs, /*Decomposed*/ false, /*ArgIdx*/ 0, /*TDF*/ 0)) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4220 | return DeductionFailed(); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4221 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4222 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4223 | // Could be null if somehow 'auto' appears in a non-deduced context. |
Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4224 | if (Deduced[0].getKind() != TemplateArgument::Type) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4225 | return DeductionFailed(); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4226 | |
Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4227 | QualType DeducedType = Deduced[0].getAsType(); |
| 4228 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4229 | if (InitList) { |
| 4230 | DeducedType = BuildStdInitializerList(DeducedType, Loc); |
| 4231 | if (DeducedType.isNull()) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4232 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4235 | Result = SubstituteDeducedTypeTransform(*this, DeducedType).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4236 | if (Result.isNull()) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4237 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4238 | |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4239 | // Check that the deduced argument type is compatible with the original |
| 4240 | // argument type per C++ [temp.deduct.call]p4. |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 4241 | QualType DeducedA = InitList ? Deduced[0].getAsType() : Result; |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 4242 | for (const OriginalCallArg &OriginalArg : OriginalCallArgs) { |
Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 4243 | assert((bool)InitList == OriginalArg.DecomposedParam && |
| 4244 | "decomposed non-init-list in auto deduction?"); |
| 4245 | if (CheckOriginalCallArgDeduction(*this, OriginalArg, DeducedA)) { |
Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 4246 | Result = QualType(); |
| 4247 | return DeductionFailed(); |
| 4248 | } |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4249 | } |
| 4250 | |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4251 | return DAR_Succeeded; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4252 | } |
| 4253 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 4254 | QualType Sema::SubstAutoType(QualType TypeWithAuto, |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4255 | QualType TypeToReplaceAuto) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4256 | if (TypeToReplaceAuto->isDependentType()) |
| 4257 | TypeToReplaceAuto = QualType(); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4258 | return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4259 | .TransformType(TypeWithAuto); |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4260 | } |
| 4261 | |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4262 | TypeSourceInfo *Sema::SubstAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto, |
| 4263 | QualType TypeToReplaceAuto) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4264 | if (TypeToReplaceAuto->isDependentType()) |
| 4265 | TypeToReplaceAuto = QualType(); |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4266 | return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4267 | .TransformType(TypeWithAuto); |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 4268 | } |
| 4269 | |
Richard Smith | 33c33c3 | 2017-02-04 01:28:01 +0000 | [diff] [blame] | 4270 | QualType Sema::ReplaceAutoType(QualType TypeWithAuto, |
| 4271 | QualType TypeToReplaceAuto) { |
Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4272 | return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto, |
| 4273 | /*UseTypeSugar*/ false) |
Richard Smith | 33c33c3 | 2017-02-04 01:28:01 +0000 | [diff] [blame] | 4274 | .TransformType(TypeWithAuto); |
| 4275 | } |
| 4276 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4277 | void Sema::DiagnoseAutoDeductionFailure(VarDecl *VDecl, Expr *Init) { |
| 4278 | if (isa<InitListExpr>(Init)) |
| 4279 | Diag(VDecl->getLocation(), |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4280 | VDecl->isInitCapture() |
| 4281 | ? diag::err_init_capture_deduction_failure_from_init_list |
| 4282 | : diag::err_auto_var_deduction_failure_from_init_list) |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4283 | << VDecl->getDeclName() << VDecl->getType() << Init->getSourceRange(); |
| 4284 | else |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4285 | Diag(VDecl->getLocation(), |
| 4286 | VDecl->isInitCapture() ? diag::err_init_capture_deduction_failure |
| 4287 | : diag::err_auto_var_deduction_failure) |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4288 | << VDecl->getDeclName() << VDecl->getType() << Init->getType() |
| 4289 | << Init->getSourceRange(); |
| 4290 | } |
| 4291 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4292 | bool Sema::DeduceReturnType(FunctionDecl *FD, SourceLocation Loc, |
| 4293 | bool Diagnose) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4294 | assert(FD->getReturnType()->isUndeducedType()); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4295 | |
| 4296 | if (FD->getTemplateInstantiationPattern()) |
| 4297 | InstantiateFunctionDefinition(Loc, FD); |
| 4298 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4299 | bool StillUndeduced = FD->getReturnType()->isUndeducedType(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4300 | if (StillUndeduced && Diagnose && !FD->isInvalidDecl()) { |
| 4301 | Diag(Loc, diag::err_auto_fn_used_before_defined) << FD; |
| 4302 | Diag(FD->getLocation(), diag::note_callee_decl) << FD; |
| 4303 | } |
| 4304 | |
| 4305 | return StillUndeduced; |
| 4306 | } |
| 4307 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4308 | /// \brief If this is a non-static member function, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 4309 | static void |
| 4310 | AddImplicitObjectParameterType(ASTContext &Context, |
| 4311 | CXXMethodDecl *Method, |
| 4312 | SmallVectorImpl<QualType> &ArgTypes) { |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4313 | // C++11 [temp.func.order]p3: |
| 4314 | // [...] The new parameter is of type "reference to cv A," where cv are |
| 4315 | // the cv-qualifiers of the function template (if any) and A is |
| 4316 | // the class of which the function template is a member. |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4317 | // |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4318 | // The standard doesn't say explicitly, but we pick the appropriate kind of |
| 4319 | // reference type based on [over.match.funcs]p4. |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4320 | QualType ArgTy = Context.getTypeDeclType(Method->getParent()); |
| 4321 | ArgTy = Context.getQualifiedType(ArgTy, |
| 4322 | Qualifiers::fromCVRMask(Method->getTypeQualifiers())); |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4323 | if (Method->getRefQualifier() == RQ_RValue) |
| 4324 | ArgTy = Context.getRValueReferenceType(ArgTy); |
| 4325 | else |
| 4326 | ArgTy = Context.getLValueReferenceType(ArgTy); |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4327 | ArgTypes.push_back(ArgTy); |
| 4328 | } |
| 4329 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4330 | /// \brief Determine whether the function template \p FT1 is at least as |
| 4331 | /// specialized as \p FT2. |
| 4332 | static bool isAtLeastAsSpecializedAs(Sema &S, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4333 | SourceLocation Loc, |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4334 | FunctionTemplateDecl *FT1, |
| 4335 | FunctionTemplateDecl *FT2, |
| 4336 | TemplatePartialOrderingContext TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4337 | unsigned NumCallArguments1) { |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4338 | FunctionDecl *FD1 = FT1->getTemplatedDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4339 | FunctionDecl *FD2 = FT2->getTemplatedDecl(); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4340 | const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); |
| 4341 | const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4342 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4343 | assert(Proto1 && Proto2 && "Function templates must have prototypes"); |
| 4344 | TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4345 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4346 | Deduced.resize(TemplateParams->size()); |
| 4347 | |
| 4348 | // C++0x [temp.deduct.partial]p3: |
| 4349 | // The types used to determine the ordering depend on the context in which |
| 4350 | // the partial ordering is done: |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4351 | TemplateDeductionInfo Info(Loc); |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4352 | SmallVector<QualType, 4> Args2; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4353 | switch (TPOC) { |
| 4354 | case TPOC_Call: { |
| 4355 | // - In the context of a function call, the function parameter types are |
| 4356 | // used. |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4357 | CXXMethodDecl *Method1 = dyn_cast<CXXMethodDecl>(FD1); |
| 4358 | CXXMethodDecl *Method2 = dyn_cast<CXXMethodDecl>(FD2); |
Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4359 | |
Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4360 | // C++11 [temp.func.order]p3: |
Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4361 | // [...] If only one of the function templates is a non-static |
| 4362 | // member, that function template is considered to have a new |
| 4363 | // first parameter inserted in its function parameter list. The |
| 4364 | // new parameter is of type "reference to cv A," where cv are |
| 4365 | // the cv-qualifiers of the function template (if any) and A is |
| 4366 | // the class of which the function template is a member. |
| 4367 | // |
Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4368 | // Note that we interpret this to mean "if one of the function |
| 4369 | // templates is a non-static member and the other is a non-member"; |
| 4370 | // otherwise, the ordering rules for static functions against non-static |
| 4371 | // functions don't make any sense. |
| 4372 | // |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4373 | // C++98/03 doesn't have this provision but we've extended DR532 to cover |
| 4374 | // it as wording was broken prior to it. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4375 | SmallVector<QualType, 4> Args1; |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4376 | |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4377 | unsigned NumComparedArguments = NumCallArguments1; |
| 4378 | |
| 4379 | if (!Method2 && Method1 && !Method1->isStatic()) { |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4380 | // Compare 'this' from Method1 against first parameter from Method2. |
| 4381 | AddImplicitObjectParameterType(S.Context, Method1, Args1); |
| 4382 | ++NumComparedArguments; |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4383 | } else if (!Method1 && Method2 && !Method2->isStatic()) { |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4384 | // Compare 'this' from Method2 against first parameter from Method1. |
| 4385 | AddImplicitObjectParameterType(S.Context, Method2, Args2); |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4386 | } |
| 4387 | |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4388 | Args1.insert(Args1.end(), Proto1->param_type_begin(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4389 | Proto1->param_type_end()); |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4390 | Args2.insert(Args2.end(), Proto2->param_type_begin(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4391 | Proto2->param_type_end()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4392 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4393 | // C++ [temp.func.order]p5: |
| 4394 | // The presence of unused ellipsis and default arguments has no effect on |
| 4395 | // the partial ordering of function templates. |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4396 | if (Args1.size() > NumComparedArguments) |
| 4397 | Args1.resize(NumComparedArguments); |
| 4398 | if (Args2.size() > NumComparedArguments) |
| 4399 | Args2.resize(NumComparedArguments); |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4400 | if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(), |
| 4401 | Args1.data(), Args1.size(), Info, Deduced, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4402 | TDF_None, /*PartialOrdering=*/true)) |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 4403 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4404 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4405 | break; |
| 4406 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4407 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4408 | case TPOC_Conversion: |
| 4409 | // - In the context of a call to a conversion operator, the return types |
| 4410 | // of the conversion function templates are used. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4411 | if (DeduceTemplateArgumentsByTypeMatch( |
| 4412 | S, TemplateParams, Proto2->getReturnType(), Proto1->getReturnType(), |
| 4413 | Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4414 | /*PartialOrdering=*/true)) |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4415 | return false; |
| 4416 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4417 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4418 | case TPOC_Other: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4419 | // - 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] | 4420 | // is used. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4421 | if (DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 4422 | FD2->getType(), FD1->getType(), |
| 4423 | Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4424 | /*PartialOrdering=*/true)) |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4425 | return false; |
| 4426 | break; |
| 4427 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4428 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4429 | // C++0x [temp.deduct.partial]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4430 | // In most cases, all template parameters must have values in order for |
| 4431 | // deduction to succeed, but for partial ordering purposes a template |
| 4432 | // 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] | 4433 | // types being used for partial ordering. [ Note: a template parameter used |
| 4434 | // in a non-deduced context is considered used. -end note] |
| 4435 | unsigned ArgIdx = 0, NumArgs = Deduced.size(); |
| 4436 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4437 | if (Deduced[ArgIdx].isNull()) |
| 4438 | break; |
| 4439 | |
Richard Smith | cf82486 | 2016-12-30 04:32:02 +0000 | [diff] [blame] | 4440 | // FIXME: We fail to implement [temp.deduct.type]p1 along this path. We need |
| 4441 | // to substitute the deduced arguments back into the template and check that |
| 4442 | // we get the right type. |
| 4443 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4444 | if (ArgIdx == NumArgs) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4445 | // All template arguments were deduced. FT1 is at least as specialized |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4446 | // as FT2. |
| 4447 | return true; |
| 4448 | } |
| 4449 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4450 | // Figure out which template parameters were used. |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4451 | llvm::SmallBitVector UsedParameters(TemplateParams->size()); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4452 | switch (TPOC) { |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4453 | case TPOC_Call: |
| 4454 | for (unsigned I = 0, N = Args2.size(); I != N; ++I) |
| 4455 | ::MarkUsedTemplateParameters(S.Context, Args2[I], false, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4456 | TemplateParams->getDepth(), |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4457 | UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4458 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4459 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4460 | case TPOC_Conversion: |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4461 | ::MarkUsedTemplateParameters(S.Context, Proto2->getReturnType(), false, |
| 4462 | TemplateParams->getDepth(), UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4463 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4464 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4465 | case TPOC_Other: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4466 | ::MarkUsedTemplateParameters(S.Context, FD2->getType(), false, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4467 | TemplateParams->getDepth(), |
| 4468 | UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4469 | break; |
| 4470 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4471 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4472 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4473 | // If this argument had no value deduced but was used in one of the types |
| 4474 | // used for partial ordering, then deduction fails. |
| 4475 | if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) |
| 4476 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4477 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4478 | return true; |
| 4479 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4480 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4481 | /// \brief Determine whether this a function template whose parameter-type-list |
| 4482 | /// ends with a function parameter pack. |
| 4483 | static bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) { |
| 4484 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
| 4485 | unsigned NumParams = Function->getNumParams(); |
| 4486 | if (NumParams == 0) |
| 4487 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4488 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4489 | ParmVarDecl *Last = Function->getParamDecl(NumParams - 1); |
| 4490 | if (!Last->isParameterPack()) |
| 4491 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4492 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4493 | // Make sure that no previous parameter is a parameter pack. |
| 4494 | while (--NumParams > 0) { |
| 4495 | if (Function->getParamDecl(NumParams - 1)->isParameterPack()) |
| 4496 | return false; |
| 4497 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4498 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4499 | return true; |
| 4500 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4501 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4502 | /// \brief Returns the more specialized function template according |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4503 | /// to the rules of function template partial ordering (C++ [temp.func.order]). |
| 4504 | /// |
| 4505 | /// \param FT1 the first function template |
| 4506 | /// |
| 4507 | /// \param FT2 the second function template |
| 4508 | /// |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4509 | /// \param TPOC the context in which we are performing partial ordering of |
| 4510 | /// function templates. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4511 | /// |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4512 | /// \param NumCallArguments1 The number of arguments in the call to FT1, used |
| 4513 | /// only when \c TPOC is \c TPOC_Call. |
| 4514 | /// |
| 4515 | /// \param NumCallArguments2 The number of arguments in the call to FT2, used |
| 4516 | /// only when \c TPOC is \c TPOC_Call. |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4517 | /// |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4518 | /// \returns the more specialized function template. If neither |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4519 | /// template is more specialized, returns NULL. |
| 4520 | FunctionTemplateDecl * |
| 4521 | Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, |
| 4522 | FunctionTemplateDecl *FT2, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4523 | SourceLocation Loc, |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4524 | TemplatePartialOrderingContext TPOC, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4525 | unsigned NumCallArguments1, |
| 4526 | unsigned NumCallArguments2) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4527 | bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4528 | NumCallArguments1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4529 | bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4530 | NumCallArguments2); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4531 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4532 | if (Better1 != Better2) // We have a clear winner |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4533 | return Better1 ? FT1 : FT2; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4534 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4535 | if (!Better1 && !Better2) // Neither is better than the other |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4536 | return nullptr; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4537 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4538 | // FIXME: This mimics what GCC implements, but doesn't match up with the |
| 4539 | // proposed resolution for core issue 692. This area needs to be sorted out, |
| 4540 | // but for now we attempt to maintain compatibility. |
| 4541 | bool Variadic1 = isVariadicFunctionTemplate(FT1); |
| 4542 | bool Variadic2 = isVariadicFunctionTemplate(FT2); |
| 4543 | if (Variadic1 != Variadic2) |
| 4544 | return Variadic1? FT2 : FT1; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4545 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4546 | return nullptr; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4547 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4548 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4549 | /// \brief Determine if the two templates are equivalent. |
| 4550 | static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) { |
| 4551 | if (T1 == T2) |
| 4552 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4553 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4554 | if (!T1 || !T2) |
| 4555 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4556 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4557 | return T1->getCanonicalDecl() == T2->getCanonicalDecl(); |
| 4558 | } |
| 4559 | |
| 4560 | /// \brief Retrieve the most specialized of the given function template |
| 4561 | /// specializations. |
| 4562 | /// |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4563 | /// \param SpecBegin the start iterator of the function template |
| 4564 | /// specializations that we will be comparing. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4565 | /// |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4566 | /// \param SpecEnd the end iterator of the function template |
| 4567 | /// specializations, paired with \p SpecBegin. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4568 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4569 | /// \param Loc the location where the ambiguity or no-specializations |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4570 | /// diagnostic should occur. |
| 4571 | /// |
| 4572 | /// \param NoneDiag partial diagnostic used to diagnose cases where there are |
| 4573 | /// no matching candidates. |
| 4574 | /// |
| 4575 | /// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one |
| 4576 | /// occurs. |
| 4577 | /// |
| 4578 | /// \param CandidateDiag partial diagnostic used for each function template |
| 4579 | /// specialization that is a candidate in the ambiguous ordering. One parameter |
| 4580 | /// in this diagnostic should be unbound, which will correspond to the string |
| 4581 | /// describing the template arguments for the function template specialization. |
| 4582 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4583 | /// \returns the most specialized function template specialization, if |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4584 | /// found. Otherwise, returns SpecEnd. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4585 | UnresolvedSetIterator Sema::getMostSpecialized( |
| 4586 | UnresolvedSetIterator SpecBegin, UnresolvedSetIterator SpecEnd, |
| 4587 | TemplateSpecCandidateSet &FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4588 | SourceLocation Loc, const PartialDiagnostic &NoneDiag, |
| 4589 | const PartialDiagnostic &AmbigDiag, const PartialDiagnostic &CandidateDiag, |
| 4590 | bool Complain, QualType TargetType) { |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4591 | if (SpecBegin == SpecEnd) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4592 | if (Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4593 | Diag(Loc, NoneDiag); |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4594 | FailedCandidates.NoteCandidates(*this, Loc); |
| 4595 | } |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4596 | return SpecEnd; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4597 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4598 | |
| 4599 | if (SpecBegin + 1 == SpecEnd) |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4600 | return SpecBegin; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4601 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4602 | // Find the function template that is better than all of the templates it |
| 4603 | // has been compared to. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4604 | UnresolvedSetIterator Best = SpecBegin; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4605 | FunctionTemplateDecl *BestTemplate |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4606 | = cast<FunctionDecl>(*Best)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4607 | assert(BestTemplate && "Not a function template specialization?"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4608 | for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) { |
| 4609 | FunctionTemplateDecl *Challenger |
| 4610 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4611 | assert(Challenger && "Not a function template specialization?"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4612 | if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4613 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4614 | Challenger)) { |
| 4615 | Best = I; |
| 4616 | BestTemplate = Challenger; |
| 4617 | } |
| 4618 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4619 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4620 | // Make sure that the "best" function template is more specialized than all |
| 4621 | // of the others. |
| 4622 | bool Ambiguous = false; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4623 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4624 | FunctionTemplateDecl *Challenger |
| 4625 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4626 | if (I != Best && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4627 | !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4628 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4629 | BestTemplate)) { |
| 4630 | Ambiguous = true; |
| 4631 | break; |
| 4632 | } |
| 4633 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4634 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4635 | if (!Ambiguous) { |
| 4636 | // We found an answer. Return it. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4637 | return Best; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4638 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4639 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4640 | // Diagnose the ambiguity. |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4641 | if (Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4642 | Diag(Loc, AmbigDiag); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4643 | |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4644 | // FIXME: Can we order the candidates in some sane way? |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4645 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4646 | PartialDiagnostic PD = CandidateDiag; |
Saleem Abdulrasool | 78704fb | 2016-12-22 04:26:57 +0000 | [diff] [blame] | 4647 | const auto *FD = cast<FunctionDecl>(*I); |
| 4648 | PD << FD << getTemplateArgumentBindingsText( |
| 4649 | FD->getPrimaryTemplate()->getTemplateParameters(), |
| 4650 | *FD->getTemplateSpecializationArgs()); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4651 | if (!TargetType.isNull()) |
Saleem Abdulrasool | 78704fb | 2016-12-22 04:26:57 +0000 | [diff] [blame] | 4652 | HandleFunctionTypeMismatch(PD, FD->getType(), TargetType); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4653 | Diag((*I)->getLocation(), PD); |
| 4654 | } |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4655 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4656 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4657 | return SpecEnd; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4658 | } |
| 4659 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4660 | /// Determine whether one partial specialization, P1, is at least as |
| 4661 | /// specialized than another, P2. |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4662 | /// |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 4663 | /// \tparam TemplateLikeDecl The kind of P2, which must be a |
| 4664 | /// TemplateDecl or {Class,Var}TemplatePartialSpecializationDecl. |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4665 | /// \param T1 The injected-class-name of P1 (faked for a variable template). |
| 4666 | /// \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] | 4667 | template<typename TemplateLikeDecl> |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4668 | static bool isAtLeastAsSpecializedAs(Sema &S, QualType T1, QualType T2, |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 4669 | TemplateLikeDecl *P2, |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 4670 | TemplateDeductionInfo &Info) { |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4671 | // C++ [temp.class.order]p1: |
| 4672 | // For two class template partial specializations, the first is at least as |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4673 | // specialized as the second if, given the following rewrite to two |
| 4674 | // function templates, the first function template is at least as |
| 4675 | // specialized as the second according to the ordering rules for function |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4676 | // templates (14.6.6.2): |
| 4677 | // - the first function template has the same template parameters as the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4678 | // first partial specialization and has a single function parameter |
| 4679 | // whose type is a class template specialization with the template |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4680 | // arguments of the first partial specialization, and |
| 4681 | // - the second function template has the same template parameters as the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4682 | // second partial specialization and has a single function parameter |
| 4683 | // whose type is a class template specialization with the template |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4684 | // arguments of the second partial specialization. |
| 4685 | // |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 4686 | // Rather than synthesize function templates, we merely perform the |
| 4687 | // equivalent partial ordering by performing deduction directly on |
| 4688 | // the template arguments of the class template partial |
| 4689 | // specializations. This computation is slightly simpler than the |
| 4690 | // general problem of function template partial ordering, because |
| 4691 | // class template partial specializations are more constrained. We |
| 4692 | // know that every template parameter is deducible from the class |
| 4693 | // template partial specialization's template arguments, for |
| 4694 | // example. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4695 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4696 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4697 | // Determine whether P1 is at least as specialized as P2. |
| 4698 | Deduced.resize(P2->getTemplateParameters()->size()); |
| 4699 | if (DeduceTemplateArgumentsByTypeMatch(S, P2->getTemplateParameters(), |
| 4700 | T2, T1, Info, Deduced, TDF_None, |
| 4701 | /*PartialOrdering=*/true)) |
| 4702 | return false; |
| 4703 | |
| 4704 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), |
| 4705 | Deduced.end()); |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 4706 | Sema::InstantiatingTemplate Inst(S, Info.getLocation(), P2, DeducedArgs, |
| 4707 | Info); |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4708 | auto *TST1 = T1->castAs<TemplateSpecializationType>(); |
| 4709 | if (FinishTemplateArgumentDeduction( |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4710 | S, P2, /*PartialOrdering=*/true, |
| 4711 | TemplateArgumentList(TemplateArgumentList::OnStack, |
| 4712 | TST1->template_arguments()), |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4713 | Deduced, Info)) |
| 4714 | return false; |
| 4715 | |
| 4716 | return true; |
| 4717 | } |
| 4718 | |
| 4719 | /// \brief Returns the more specialized class template partial specialization |
| 4720 | /// according to the rules of partial ordering of class template partial |
| 4721 | /// specializations (C++ [temp.class.order]). |
| 4722 | /// |
| 4723 | /// \param PS1 the first class template partial specialization |
| 4724 | /// |
| 4725 | /// \param PS2 the second class template partial specialization |
| 4726 | /// |
| 4727 | /// \returns the more specialized class template partial specialization. If |
| 4728 | /// neither partial specialization is more specialized, returns NULL. |
| 4729 | ClassTemplatePartialSpecializationDecl * |
| 4730 | Sema::getMoreSpecializedPartialSpecialization( |
| 4731 | ClassTemplatePartialSpecializationDecl *PS1, |
| 4732 | ClassTemplatePartialSpecializationDecl *PS2, |
| 4733 | SourceLocation Loc) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4734 | QualType PT1 = PS1->getInjectedSpecializationType(); |
| 4735 | QualType PT2 = PS2->getInjectedSpecializationType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4736 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 4737 | TemplateDeductionInfo Info(Loc); |
| 4738 | bool Better1 = isAtLeastAsSpecializedAs(*this, PT1, PT2, PS2, Info); |
| 4739 | bool Better2 = isAtLeastAsSpecializedAs(*this, PT2, PT1, PS1, Info); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4740 | |
| 4741 | if (Better1 == Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4742 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4743 | |
| 4744 | return Better1 ? PS1 : PS2; |
| 4745 | } |
| 4746 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 4747 | bool Sema::isMoreSpecializedThanPrimary( |
| 4748 | ClassTemplatePartialSpecializationDecl *Spec, TemplateDeductionInfo &Info) { |
| 4749 | ClassTemplateDecl *Primary = Spec->getSpecializedTemplate(); |
| 4750 | QualType PrimaryT = Primary->getInjectedClassNameSpecialization(); |
| 4751 | QualType PartialT = Spec->getInjectedSpecializationType(); |
| 4752 | if (!isAtLeastAsSpecializedAs(*this, PartialT, PrimaryT, Primary, Info)) |
| 4753 | return false; |
| 4754 | if (isAtLeastAsSpecializedAs(*this, PrimaryT, PartialT, Spec, Info)) { |
| 4755 | Info.clearSFINAEDiagnostic(); |
| 4756 | return false; |
| 4757 | } |
| 4758 | return true; |
| 4759 | } |
| 4760 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4761 | VarTemplatePartialSpecializationDecl * |
| 4762 | Sema::getMoreSpecializedPartialSpecialization( |
| 4763 | VarTemplatePartialSpecializationDecl *PS1, |
| 4764 | VarTemplatePartialSpecializationDecl *PS2, SourceLocation Loc) { |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4765 | // Pretend the variable template specializations are class template |
| 4766 | // specializations and form a fake injected class name type for comparison. |
Richard Smith | f04fd0b | 2013-12-12 23:14:16 +0000 | [diff] [blame] | 4767 | assert(PS1->getSpecializedTemplate() == PS2->getSpecializedTemplate() && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4768 | "the partial specializations being compared should specialize" |
| 4769 | " the same template."); |
| 4770 | TemplateName Name(PS1->getSpecializedTemplate()); |
| 4771 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4772 | QualType PT1 = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4773 | CanonTemplate, PS1->getTemplateArgs().asArray()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4774 | QualType PT2 = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4775 | CanonTemplate, PS2->getTemplateArgs().asArray()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4776 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 4777 | TemplateDeductionInfo Info(Loc); |
| 4778 | bool Better1 = isAtLeastAsSpecializedAs(*this, PT1, PT2, PS2, Info); |
| 4779 | bool Better2 = isAtLeastAsSpecializedAs(*this, PT2, PT1, PS1, Info); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4780 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4781 | if (Better1 == Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4782 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4783 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4784 | return Better1 ? PS1 : PS2; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4785 | } |
| 4786 | |
Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 4787 | bool Sema::isMoreSpecializedThanPrimary( |
| 4788 | VarTemplatePartialSpecializationDecl *Spec, TemplateDeductionInfo &Info) { |
| 4789 | TemplateDecl *Primary = Spec->getSpecializedTemplate(); |
| 4790 | // FIXME: Cache the injected template arguments rather than recomputing |
| 4791 | // them for each partial specialization. |
| 4792 | SmallVector<TemplateArgument, 8> PrimaryArgs; |
| 4793 | Context.getInjectedTemplateArgs(Primary->getTemplateParameters(), |
| 4794 | PrimaryArgs); |
| 4795 | |
| 4796 | TemplateName CanonTemplate = |
| 4797 | Context.getCanonicalTemplateName(TemplateName(Primary)); |
| 4798 | QualType PrimaryT = Context.getTemplateSpecializationType( |
| 4799 | CanonTemplate, PrimaryArgs); |
| 4800 | QualType PartialT = Context.getTemplateSpecializationType( |
| 4801 | CanonTemplate, Spec->getTemplateArgs().asArray()); |
| 4802 | if (!isAtLeastAsSpecializedAs(*this, PartialT, PrimaryT, Primary, Info)) |
| 4803 | return false; |
| 4804 | if (isAtLeastAsSpecializedAs(*this, PrimaryT, PartialT, Spec, Info)) { |
| 4805 | Info.clearSFINAEDiagnostic(); |
| 4806 | return false; |
| 4807 | } |
| 4808 | return true; |
| 4809 | } |
| 4810 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 4811 | bool Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs( |
| 4812 | TemplateParameterList *P, TemplateDecl *AArg, SourceLocation Loc) { |
| 4813 | // C++1z [temp.arg.template]p4: (DR 150) |
| 4814 | // A template template-parameter P is at least as specialized as a |
| 4815 | // template template-argument A if, given the following rewrite to two |
| 4816 | // function templates... |
| 4817 | |
| 4818 | // Rather than synthesize function templates, we merely perform the |
| 4819 | // equivalent partial ordering by performing deduction directly on |
| 4820 | // the template parameter lists of the template template parameters. |
| 4821 | // |
| 4822 | // Given an invented class template X with the template parameter list of |
| 4823 | // A (including default arguments): |
| 4824 | TemplateName X = Context.getCanonicalTemplateName(TemplateName(AArg)); |
| 4825 | TemplateParameterList *A = AArg->getTemplateParameters(); |
| 4826 | |
| 4827 | // - Each function template has a single function parameter whose type is |
| 4828 | // a specialization of X with template arguments corresponding to the |
| 4829 | // template parameters from the respective function template |
| 4830 | SmallVector<TemplateArgument, 8> AArgs; |
| 4831 | Context.getInjectedTemplateArgs(A, AArgs); |
| 4832 | |
| 4833 | // Check P's arguments against A's parameter list. This will fill in default |
| 4834 | // template arguments as needed. AArgs are already correct by construction. |
| 4835 | // We can't just use CheckTemplateIdType because that will expand alias |
| 4836 | // templates. |
| 4837 | SmallVector<TemplateArgument, 4> PArgs; |
| 4838 | { |
| 4839 | SFINAETrap Trap(*this); |
| 4840 | |
| 4841 | Context.getInjectedTemplateArgs(P, PArgs); |
| 4842 | TemplateArgumentListInfo PArgList(P->getLAngleLoc(), P->getRAngleLoc()); |
| 4843 | for (unsigned I = 0, N = P->size(); I != N; ++I) { |
| 4844 | // Unwrap packs that getInjectedTemplateArgs wrapped around pack |
| 4845 | // expansions, to form an "as written" argument list. |
| 4846 | TemplateArgument Arg = PArgs[I]; |
| 4847 | if (Arg.getKind() == TemplateArgument::Pack) { |
| 4848 | assert(Arg.pack_size() == 1 && Arg.pack_begin()->isPackExpansion()); |
| 4849 | Arg = *Arg.pack_begin(); |
| 4850 | } |
| 4851 | PArgList.addArgument(getTrivialTemplateArgumentLoc( |
| 4852 | Arg, QualType(), P->getParam(I)->getLocation())); |
| 4853 | } |
| 4854 | PArgs.clear(); |
| 4855 | |
| 4856 | // C++1z [temp.arg.template]p3: |
| 4857 | // If the rewrite produces an invalid type, then P is not at least as |
| 4858 | // specialized as A. |
| 4859 | if (CheckTemplateArgumentList(AArg, Loc, PArgList, false, PArgs) || |
| 4860 | Trap.hasErrorOccurred()) |
| 4861 | return false; |
| 4862 | } |
| 4863 | |
| 4864 | QualType AType = Context.getTemplateSpecializationType(X, AArgs); |
| 4865 | QualType PType = Context.getTemplateSpecializationType(X, PArgs); |
| 4866 | |
Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 4867 | // ... the function template corresponding to P is at least as specialized |
| 4868 | // as the function template corresponding to A according to the partial |
| 4869 | // ordering rules for function templates. |
| 4870 | TemplateDeductionInfo Info(Loc, A->getDepth()); |
| 4871 | return isAtLeastAsSpecializedAs(*this, PType, AType, AArg, Info); |
| 4872 | } |
| 4873 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4874 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4875 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4876 | const TemplateArgument &TemplateArg, |
| 4877 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4878 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4879 | llvm::SmallBitVector &Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4880 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4881 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4882 | /// expression. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4883 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4884 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4885 | const Expr *E, |
| 4886 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4887 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4888 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 4889 | // We can deduce from a pack expansion. |
| 4890 | if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E)) |
| 4891 | E = Expansion->getPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4892 | |
Richard Smith | 3434900 | 2012-07-09 03:07:20 +0000 | [diff] [blame] | 4893 | // Skip through any implicit casts we added while type-checking, and any |
| 4894 | // substitutions performed by template alias expansion. |
| 4895 | while (1) { |
| 4896 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 4897 | E = ICE->getSubExpr(); |
| 4898 | else if (const SubstNonTypeTemplateParmExpr *Subst = |
| 4899 | dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 4900 | E = Subst->getReplacement(); |
| 4901 | else |
| 4902 | break; |
| 4903 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4904 | |
| 4905 | // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4906 | // find other occurrences of template parameters. |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4907 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
Douglas Gregor | 04b1152 | 2010-01-14 18:13:22 +0000 | [diff] [blame] | 4908 | if (!DRE) |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4909 | return; |
| 4910 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4911 | const NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4912 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 4913 | if (!NTTP) |
| 4914 | return; |
| 4915 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4916 | if (NTTP->getDepth() == Depth) |
| 4917 | Used[NTTP->getIndex()] = true; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 4918 | |
| 4919 | // In C++1z mode, additional arguments may be deduced from the type of a |
| 4920 | // non-type argument. |
| 4921 | if (Ctx.getLangOpts().CPlusPlus1z) |
| 4922 | MarkUsedTemplateParameters(Ctx, NTTP->getType(), OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4923 | } |
| 4924 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4925 | /// \brief Mark the template parameters that are used by the given |
| 4926 | /// nested name specifier. |
| 4927 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4928 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4929 | NestedNameSpecifier *NNS, |
| 4930 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4931 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4932 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4933 | if (!NNS) |
| 4934 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4935 | |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4936 | MarkUsedTemplateParameters(Ctx, NNS->getPrefix(), OnlyDeduced, Depth, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4937 | Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4938 | MarkUsedTemplateParameters(Ctx, QualType(NNS->getAsType(), 0), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4939 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4940 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4941 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4942 | /// \brief Mark the template parameters that are used by the given |
| 4943 | /// template name. |
| 4944 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4945 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4946 | TemplateName Name, |
| 4947 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4948 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4949 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4950 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 4951 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4952 | = dyn_cast<TemplateTemplateParmDecl>(Template)) { |
| 4953 | if (TTP->getDepth() == Depth) |
| 4954 | Used[TTP->getIndex()] = true; |
| 4955 | } |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4956 | return; |
| 4957 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4958 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4959 | if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4960 | MarkUsedTemplateParameters(Ctx, QTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4961 | Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4962 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4963 | MarkUsedTemplateParameters(Ctx, DTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4964 | Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4965 | } |
| 4966 | |
| 4967 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4968 | /// type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4969 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4970 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4971 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4972 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4973 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4974 | if (T.isNull()) |
| 4975 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4976 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4977 | // Non-dependent types have nothing deducible |
| 4978 | if (!T->isDependentType()) |
| 4979 | return; |
| 4980 | |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4981 | T = Ctx.getCanonicalType(T); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4982 | switch (T->getTypeClass()) { |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4983 | case Type::Pointer: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4984 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4985 | cast<PointerType>(T)->getPointeeType(), |
| 4986 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4987 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4988 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4989 | break; |
| 4990 | |
| 4991 | case Type::BlockPointer: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4992 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4993 | cast<BlockPointerType>(T)->getPointeeType(), |
| 4994 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4995 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4996 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4997 | break; |
| 4998 | |
| 4999 | case Type::LValueReference: |
| 5000 | case Type::RValueReference: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5001 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5002 | cast<ReferenceType>(T)->getPointeeType(), |
| 5003 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5004 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5005 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5006 | break; |
| 5007 | |
| 5008 | case Type::MemberPointer: { |
| 5009 | const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5010 | MarkUsedTemplateParameters(Ctx, MemPtr->getPointeeType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5011 | Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5012 | MarkUsedTemplateParameters(Ctx, QualType(MemPtr->getClass(), 0), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5013 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5014 | break; |
| 5015 | } |
| 5016 | |
| 5017 | case Type::DependentSizedArray: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5018 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5019 | cast<DependentSizedArrayType>(T)->getSizeExpr(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5020 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5021 | // Fall through to check the element type |
| 5022 | |
| 5023 | case Type::ConstantArray: |
| 5024 | case Type::IncompleteArray: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5025 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5026 | cast<ArrayType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5027 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5028 | break; |
| 5029 | |
| 5030 | case Type::Vector: |
| 5031 | case Type::ExtVector: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5032 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5033 | cast<VectorType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5034 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5035 | break; |
| 5036 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5037 | case Type::DependentSizedExtVector: { |
| 5038 | const DependentSizedExtVectorType *VecType |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 5039 | = cast<DependentSizedExtVectorType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5040 | MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5041 | Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5042 | MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5043 | Depth, Used); |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5044 | break; |
| 5045 | } |
| 5046 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5047 | case Type::FunctionProto: { |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 5048 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5049 | MarkUsedTemplateParameters(Ctx, Proto->getReturnType(), OnlyDeduced, Depth, |
| 5050 | Used); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 5051 | for (unsigned I = 0, N = Proto->getNumParams(); I != N; ++I) |
| 5052 | MarkUsedTemplateParameters(Ctx, Proto->getParamType(I), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5053 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5054 | break; |
| 5055 | } |
| 5056 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5057 | case Type::TemplateTypeParm: { |
| 5058 | const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T); |
| 5059 | if (TTP->getDepth() == Depth) |
| 5060 | Used[TTP->getIndex()] = true; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5061 | break; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5062 | } |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5063 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 5064 | case Type::SubstTemplateTypeParmPack: { |
| 5065 | const SubstTemplateTypeParmPackType *Subst |
| 5066 | = cast<SubstTemplateTypeParmPackType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5067 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 5068 | QualType(Subst->getReplacedParameter(), 0), |
| 5069 | OnlyDeduced, Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5070 | MarkUsedTemplateParameters(Ctx, Subst->getArgumentPack(), |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 5071 | OnlyDeduced, Depth, Used); |
| 5072 | break; |
| 5073 | } |
| 5074 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 5075 | case Type::InjectedClassName: |
| 5076 | T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); |
| 5077 | // fall through |
| 5078 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5079 | case Type::TemplateSpecialization: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5080 | const TemplateSpecializationType *Spec |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 5081 | = cast<TemplateSpecializationType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5082 | MarkUsedTemplateParameters(Ctx, Spec->getTemplateName(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5083 | Depth, Used); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5084 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5085 | // C++0x [temp.deduct.type]p9: |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 5086 | // If the template argument list of P contains a pack expansion that is |
| 5087 | // not the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5088 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5089 | if (OnlyDeduced && |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 5090 | hasPackExpansionBeforeEnd(Spec->template_arguments())) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5091 | break; |
| 5092 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5093 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5094 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5095 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5096 | break; |
| 5097 | } |
| 5098 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5099 | case Type::Complex: |
| 5100 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5101 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5102 | cast<ComplexType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5103 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5104 | break; |
| 5105 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5106 | case Type::Atomic: |
| 5107 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5108 | MarkUsedTemplateParameters(Ctx, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5109 | cast<AtomicType>(T)->getValueType(), |
| 5110 | OnlyDeduced, Depth, Used); |
| 5111 | break; |
| 5112 | |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5113 | case Type::DependentName: |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5114 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5115 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5116 | cast<DependentNameType>(T)->getQualifier(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5117 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5118 | break; |
| 5119 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5120 | case Type::DependentTemplateSpecialization: { |
Richard Smith | 50d5b97 | 2015-12-30 20:56:05 +0000 | [diff] [blame] | 5121 | // C++14 [temp.deduct.type]p5: |
| 5122 | // The non-deduced contexts are: |
| 5123 | // -- The nested-name-specifier of a type that was specified using a |
| 5124 | // qualified-id |
| 5125 | // |
| 5126 | // C++14 [temp.deduct.type]p6: |
| 5127 | // When a type name is specified in a way that includes a non-deduced |
| 5128 | // context, all of the types that comprise that type name are also |
| 5129 | // non-deduced. |
| 5130 | if (OnlyDeduced) |
| 5131 | break; |
| 5132 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5133 | const DependentTemplateSpecializationType *Spec |
| 5134 | = cast<DependentTemplateSpecializationType>(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5135 | |
Richard Smith | 50d5b97 | 2015-12-30 20:56:05 +0000 | [diff] [blame] | 5136 | MarkUsedTemplateParameters(Ctx, Spec->getQualifier(), |
| 5137 | OnlyDeduced, Depth, Used); |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5138 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5139 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5140 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5141 | Used); |
| 5142 | break; |
| 5143 | } |
| 5144 | |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5145 | case Type::TypeOf: |
| 5146 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5147 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5148 | cast<TypeOfType>(T)->getUnderlyingType(), |
| 5149 | OnlyDeduced, Depth, Used); |
| 5150 | break; |
| 5151 | |
| 5152 | case Type::TypeOfExpr: |
| 5153 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5154 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5155 | cast<TypeOfExprType>(T)->getUnderlyingExpr(), |
| 5156 | OnlyDeduced, Depth, Used); |
| 5157 | break; |
| 5158 | |
| 5159 | case Type::Decltype: |
| 5160 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5161 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5162 | cast<DecltypeType>(T)->getUnderlyingExpr(), |
| 5163 | OnlyDeduced, Depth, Used); |
| 5164 | break; |
| 5165 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 5166 | case Type::UnaryTransform: |
| 5167 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5168 | MarkUsedTemplateParameters(Ctx, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5169 | cast<UnaryTransformType>(T)->getUnderlyingType(), |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 5170 | OnlyDeduced, Depth, Used); |
| 5171 | break; |
| 5172 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 5173 | case Type::PackExpansion: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5174 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 5175 | cast<PackExpansionType>(T)->getPattern(), |
| 5176 | OnlyDeduced, Depth, Used); |
| 5177 | break; |
| 5178 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5179 | case Type::Auto: |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 5180 | case Type::DeducedTemplateSpecialization: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5181 | MarkUsedTemplateParameters(Ctx, |
Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 5182 | cast<DeducedType>(T)->getDeducedType(), |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5183 | OnlyDeduced, Depth, Used); |
| 5184 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5185 | // None of these types have any template parameters in them. |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5186 | case Type::Builtin: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5187 | case Type::VariableArray: |
| 5188 | case Type::FunctionNoProto: |
| 5189 | case Type::Record: |
| 5190 | case Type::Enum: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5191 | case Type::ObjCInterface: |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5192 | case Type::ObjCObject: |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 5193 | case Type::ObjCObjectPointer: |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5194 | case Type::UnresolvedUsing: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5195 | case Type::Pipe: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5196 | #define TYPE(Class, Base) |
| 5197 | #define ABSTRACT_TYPE(Class, Base) |
| 5198 | #define DEPENDENT_TYPE(Class, Base) |
| 5199 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 5200 | #include "clang/AST/TypeNodes.def" |
| 5201 | break; |
| 5202 | } |
| 5203 | } |
| 5204 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5205 | /// \brief Mark the template parameters that are used by this |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5206 | /// template argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5207 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5208 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5209 | const TemplateArgument &TemplateArg, |
| 5210 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5211 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5212 | llvm::SmallBitVector &Used) { |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5213 | switch (TemplateArg.getKind()) { |
| 5214 | case TemplateArgument::Null: |
| 5215 | case TemplateArgument::Integral: |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5216 | case TemplateArgument::Declaration: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5217 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5218 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5219 | case TemplateArgument::NullPtr: |
| 5220 | MarkUsedTemplateParameters(Ctx, TemplateArg.getNullPtrType(), OnlyDeduced, |
| 5221 | Depth, Used); |
| 5222 | break; |
| 5223 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5224 | case TemplateArgument::Type: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5225 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5226 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5227 | break; |
| 5228 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5229 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 5230 | case TemplateArgument::TemplateExpansion: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5231 | MarkUsedTemplateParameters(Ctx, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5232 | TemplateArg.getAsTemplateOrTemplatePattern(), |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5233 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5234 | break; |
| 5235 | |
| 5236 | case TemplateArgument::Expression: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5237 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsExpr(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5238 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5239 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5240 | |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5241 | case TemplateArgument::Pack: |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 5242 | for (const auto &P : TemplateArg.pack_elements()) |
| 5243 | MarkUsedTemplateParameters(Ctx, P, OnlyDeduced, Depth, Used); |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5244 | break; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5245 | } |
| 5246 | } |
| 5247 | |
James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 5248 | /// \brief Mark which template parameters can be deduced from a given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5249 | /// template argument list. |
| 5250 | /// |
| 5251 | /// \param TemplateArgs the template argument list from which template |
| 5252 | /// parameters will be deduced. |
| 5253 | /// |
James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 5254 | /// \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] | 5255 | /// to indicate when the corresponding template parameter will be |
| 5256 | /// deduced. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5257 | void |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5258 | Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5259 | bool OnlyDeduced, unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5260 | llvm::SmallBitVector &Used) { |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5261 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5262 | // If the template argument list of P contains a pack expansion that is not |
| 5263 | // the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5264 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5265 | if (OnlyDeduced && |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 5266 | hasPackExpansionBeforeEnd(TemplateArgs.asArray())) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5267 | return; |
| 5268 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5269 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5270 | ::MarkUsedTemplateParameters(Context, TemplateArgs[I], OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5271 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5272 | } |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5273 | |
| 5274 | /// \brief Marks all of the template parameters that will be deduced by a |
| 5275 | /// call to the given function template. |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 5276 | void Sema::MarkDeducedTemplateParameters( |
| 5277 | ASTContext &Ctx, const FunctionTemplateDecl *FunctionTemplate, |
| 5278 | llvm::SmallBitVector &Deduced) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5279 | TemplateParameterList *TemplateParams |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5280 | = FunctionTemplate->getTemplateParameters(); |
| 5281 | Deduced.clear(); |
| 5282 | Deduced.resize(TemplateParams->size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5283 | |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5284 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 5285 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5286 | ::MarkUsedTemplateParameters(Ctx, Function->getParamDecl(I)->getType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5287 | true, TemplateParams->getDepth(), Deduced); |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5288 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5289 | |
| 5290 | bool hasDeducibleTemplateParameters(Sema &S, |
| 5291 | FunctionTemplateDecl *FunctionTemplate, |
| 5292 | QualType T) { |
| 5293 | if (!T->isDependentType()) |
| 5294 | return false; |
| 5295 | |
| 5296 | TemplateParameterList *TemplateParams |
| 5297 | = FunctionTemplate->getTemplateParameters(); |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5298 | llvm::SmallBitVector Deduced(TemplateParams->size()); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 5299 | ::MarkUsedTemplateParameters(S.Context, T, true, TemplateParams->getDepth(), |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5300 | Deduced); |
| 5301 | |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5302 | return Deduced.any(); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5303 | } |