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" |
| 22 | #include "clang/Sema/DeclSpec.h" |
| 23 | #include "clang/Sema/Sema.h" |
| 24 | #include "clang/Sema/Template.h" |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallBitVector.h" |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 27 | |
| 28 | namespace clang { |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 29 | using namespace sema; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 30 | /// \brief Various flags that control template argument deduction. |
| 31 | /// |
| 32 | /// These flags can be bitwise-OR'd together. |
| 33 | enum TemplateDeductionFlags { |
| 34 | /// \brief No template argument deduction flags, which indicates the |
| 35 | /// strictest results for template argument deduction (as used for, e.g., |
| 36 | /// matching class template partial specializations). |
| 37 | TDF_None = 0, |
| 38 | /// \brief Within template argument deduction from a function call, we are |
| 39 | /// matching with a parameter type for which the original parameter was |
| 40 | /// a reference. |
| 41 | TDF_ParamWithReferenceType = 0x1, |
| 42 | /// \brief Within template argument deduction from a function call, we |
| 43 | /// are matching in a case where we ignore cv-qualifiers. |
| 44 | TDF_IgnoreQualifiers = 0x02, |
| 45 | /// \brief Within template argument deduction from a function call, |
| 46 | /// we are matching in a case where we can perform template argument |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 47 | /// 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] | 48 | TDF_DerivedClass = 0x04, |
| 49 | /// \brief Allow non-dependent types to differ, e.g., when performing |
| 50 | /// template argument deduction from a function call where conversions |
| 51 | /// may apply. |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 52 | TDF_SkipNonDependent = 0x08, |
| 53 | /// \brief Whether we are performing template argument deduction for |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 54 | /// parameters and arguments in a top-level template argument |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 55 | TDF_TopLevelParameterTypeList = 0x10, |
| 56 | /// \brief Within template argument deduction from overload resolution per |
| 57 | /// C++ [over.over] allow matching function types that are compatible in |
| 58 | /// terms of noreturn and default calling convention adjustments. |
| 59 | TDF_InOverloadResolution = 0x20 |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 60 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 61 | } |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 62 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 63 | using namespace clang; |
| 64 | |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 65 | /// \brief Compare two APSInts, extending and switching the sign as |
| 66 | /// necessary to compare their values regardless of underlying type. |
| 67 | static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) { |
| 68 | if (Y.getBitWidth() > X.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 69 | X = X.extend(Y.getBitWidth()); |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 70 | else if (Y.getBitWidth() < X.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 71 | Y = Y.extend(X.getBitWidth()); |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 72 | |
| 73 | // If there is a signedness mismatch, correct it. |
| 74 | if (X.isSigned() != Y.isSigned()) { |
| 75 | // If the signed value is negative, then the values cannot be the same. |
| 76 | if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative())) |
| 77 | return false; |
| 78 | |
| 79 | Y.setIsSigned(true); |
| 80 | X.setIsSigned(true); |
| 81 | } |
| 82 | |
| 83 | return X == Y; |
| 84 | } |
| 85 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 86 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 87 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 88 | TemplateParameterList *TemplateParams, |
| 89 | const TemplateArgument &Param, |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 90 | TemplateArgument Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 91 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 92 | SmallVectorImpl<DeducedTemplateArgument> &Deduced); |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 93 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 94 | static Sema::TemplateDeductionResult |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 95 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 96 | TemplateParameterList *TemplateParams, |
| 97 | QualType Param, |
| 98 | QualType Arg, |
| 99 | TemplateDeductionInfo &Info, |
| 100 | SmallVectorImpl<DeducedTemplateArgument> & |
| 101 | Deduced, |
| 102 | unsigned TDF, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 103 | bool PartialOrdering = false, |
| 104 | bool DeducedFromArrayBound = false); |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 105 | |
| 106 | static Sema::TemplateDeductionResult |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 107 | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 108 | ArrayRef<TemplateArgument> Params, |
| 109 | ArrayRef<TemplateArgument> Args, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 110 | TemplateDeductionInfo &Info, |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 111 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 112 | bool NumberOfArgumentsMustMatch); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 113 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 114 | /// \brief If the given expression is of a form that permits the deduction |
| 115 | /// of a non-type template parameter, return the declaration of that |
| 116 | /// non-type template parameter. |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 117 | static NonTypeTemplateParmDecl * |
| 118 | getDeducedParameterFromExpr(TemplateDeductionInfo &Info, Expr *E) { |
Richard Smith | 7ebb07c | 2012-07-08 04:37:51 +0000 | [diff] [blame] | 119 | // If we are within an alias template, the expression may have undergone |
| 120 | // any number of parameter substitutions already. |
| 121 | while (1) { |
| 122 | if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E)) |
| 123 | E = IC->getSubExpr(); |
| 124 | else if (SubstNonTypeTemplateParmExpr *Subst = |
| 125 | dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 126 | E = Subst->getReplacement(); |
| 127 | else |
| 128 | break; |
| 129 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 131 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 132 | if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) |
| 133 | if (NTTP->getDepth() == Info.getDeducedDepth()) |
| 134 | return NTTP; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 136 | return nullptr; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 139 | /// \brief Determine whether two declaration pointers refer to the same |
| 140 | /// declaration. |
| 141 | static bool isSameDeclaration(Decl *X, Decl *Y) { |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 142 | if (NamedDecl *NX = dyn_cast<NamedDecl>(X)) |
| 143 | X = NX->getUnderlyingDecl(); |
| 144 | if (NamedDecl *NY = dyn_cast<NamedDecl>(Y)) |
| 145 | Y = NY->getUnderlyingDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 146 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 147 | return X->getCanonicalDecl() == Y->getCanonicalDecl(); |
| 148 | } |
| 149 | |
| 150 | /// \brief Verify that the given, deduced template arguments are compatible. |
| 151 | /// |
| 152 | /// \returns The deduced template argument, or a NULL template argument if |
| 153 | /// the deduced template arguments were incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 154 | static DeducedTemplateArgument |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 155 | checkDeducedTemplateArguments(ASTContext &Context, |
| 156 | const DeducedTemplateArgument &X, |
| 157 | const DeducedTemplateArgument &Y) { |
| 158 | // We have no deduction for one or both of the arguments; they're compatible. |
| 159 | if (X.isNull()) |
| 160 | return Y; |
| 161 | if (Y.isNull()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 162 | return X; |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 163 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 164 | // If we have two non-type template argument values deduced for the same |
| 165 | // parameter, they must both match the type of the parameter, and thus must |
| 166 | // match each other's type. As we're only keeping one of them, we must check |
| 167 | // for that now. The exception is that if either was deduced from an array |
| 168 | // bound, the type is permitted to differ. |
| 169 | if (!X.wasDeducedFromArrayBound() && !Y.wasDeducedFromArrayBound()) { |
| 170 | QualType XType = X.getNonTypeTemplateArgumentType(); |
| 171 | if (!XType.isNull()) { |
| 172 | QualType YType = Y.getNonTypeTemplateArgumentType(); |
| 173 | if (YType.isNull() || !Context.hasSameType(XType, YType)) |
| 174 | return DeducedTemplateArgument(); |
| 175 | } |
| 176 | } |
| 177 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 178 | switch (X.getKind()) { |
| 179 | case TemplateArgument::Null: |
| 180 | llvm_unreachable("Non-deduced template arguments handled above"); |
| 181 | |
| 182 | case TemplateArgument::Type: |
| 183 | // If two template type arguments have the same type, they're compatible. |
| 184 | if (Y.getKind() == TemplateArgument::Type && |
| 185 | Context.hasSameType(X.getAsType(), Y.getAsType())) |
| 186 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 187 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 188 | // If one of the two arguments was deduced from an array bound, the other |
| 189 | // supersedes it. |
| 190 | if (X.wasDeducedFromArrayBound() != Y.wasDeducedFromArrayBound()) |
| 191 | return X.wasDeducedFromArrayBound() ? Y : X; |
| 192 | |
| 193 | // The arguments are not compatible. |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 194 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 195 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 196 | case TemplateArgument::Integral: |
| 197 | // If we deduced a constant in one case and either a dependent expression or |
| 198 | // declaration in another case, keep the integral constant. |
| 199 | // If both are integral constants with the same value, keep that value. |
| 200 | if (Y.getKind() == TemplateArgument::Expression || |
| 201 | Y.getKind() == TemplateArgument::Declaration || |
| 202 | (Y.getKind() == TemplateArgument::Integral && |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 203 | hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()))) |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 204 | return X.wasDeducedFromArrayBound() ? Y : X; |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 205 | |
| 206 | // All other combinations are incompatible. |
| 207 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 208 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 209 | case TemplateArgument::Template: |
| 210 | if (Y.getKind() == TemplateArgument::Template && |
| 211 | Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate())) |
| 212 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 213 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 214 | // All other combinations are incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 215 | return DeducedTemplateArgument(); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 216 | |
| 217 | case TemplateArgument::TemplateExpansion: |
| 218 | if (Y.getKind() == TemplateArgument::TemplateExpansion && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 219 | Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(), |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 220 | Y.getAsTemplateOrTemplatePattern())) |
| 221 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 222 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 223 | // All other combinations are incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 224 | return DeducedTemplateArgument(); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 225 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 226 | case TemplateArgument::Expression: { |
| 227 | if (Y.getKind() != TemplateArgument::Expression) |
| 228 | return checkDeducedTemplateArguments(Context, Y, X); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 229 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 230 | // Compare the expressions for equality |
| 231 | llvm::FoldingSetNodeID ID1, ID2; |
| 232 | X.getAsExpr()->Profile(ID1, Context, true); |
| 233 | Y.getAsExpr()->Profile(ID2, Context, true); |
| 234 | if (ID1 == ID2) |
| 235 | return X.wasDeducedFromArrayBound() ? Y : X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 236 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 237 | // Differing dependent expressions are incompatible. |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 238 | return DeducedTemplateArgument(); |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 239 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 240 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 241 | case TemplateArgument::Declaration: |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 242 | assert(!X.wasDeducedFromArrayBound()); |
| 243 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 244 | // If we deduced a declaration and a dependent expression, keep the |
| 245 | // declaration. |
| 246 | if (Y.getKind() == TemplateArgument::Expression) |
| 247 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 248 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 249 | // If we deduced a declaration and an integral constant, keep the |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 250 | // integral constant and whichever type did not come from an array |
| 251 | // bound. |
| 252 | if (Y.getKind() == TemplateArgument::Integral) { |
| 253 | if (Y.wasDeducedFromArrayBound()) |
| 254 | return TemplateArgument(Context, Y.getAsIntegral(), |
| 255 | X.getParamTypeForDecl()); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 256 | return Y; |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 257 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 258 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 259 | // If we deduced two declarations, make sure they they refer to the |
| 260 | // same declaration. |
| 261 | if (Y.getKind() == TemplateArgument::Declaration && |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 262 | isSameDeclaration(X.getAsDecl(), Y.getAsDecl())) |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 263 | return X; |
| 264 | |
| 265 | // All other combinations are incompatible. |
| 266 | return DeducedTemplateArgument(); |
| 267 | |
| 268 | case TemplateArgument::NullPtr: |
| 269 | // If we deduced a null pointer and a dependent expression, keep the |
| 270 | // null pointer. |
| 271 | if (Y.getKind() == TemplateArgument::Expression) |
| 272 | return X; |
| 273 | |
| 274 | // If we deduced a null pointer and an integral constant, keep the |
| 275 | // integral constant. |
| 276 | if (Y.getKind() == TemplateArgument::Integral) |
| 277 | return Y; |
| 278 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 279 | // If we deduced two null pointers, they are the same. |
| 280 | if (Y.getKind() == TemplateArgument::NullPtr) |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 281 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 282 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 283 | // All other combinations are incompatible. |
| 284 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 285 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 286 | case TemplateArgument::Pack: |
| 287 | if (Y.getKind() != TemplateArgument::Pack || |
| 288 | X.pack_size() != Y.pack_size()) |
| 289 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 290 | |
| 291 | for (TemplateArgument::pack_iterator XA = X.pack_begin(), |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 292 | XAEnd = X.pack_end(), |
| 293 | YA = Y.pack_begin(); |
| 294 | XA != XAEnd; ++XA, ++YA) { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 295 | // FIXME: Do we need to merge the results together here? |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 296 | if (checkDeducedTemplateArguments(Context, |
| 297 | DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()), |
Douglas Gregor | f491ee2 | 2011-01-05 21:00:53 +0000 | [diff] [blame] | 298 | DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound())) |
| 299 | .isNull()) |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 300 | return DeducedTemplateArgument(); |
| 301 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 302 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 303 | return X; |
| 304 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 305 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 306 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 309 | /// \brief Deduce the value of the given non-type template parameter |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 310 | /// as the given deduced template argument. All non-type template parameter |
| 311 | /// deduction is funneled through here. |
Benjamin Kramer | 7320b99 | 2016-06-15 14:20:56 +0000 | [diff] [blame] | 312 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 313 | Sema &S, TemplateParameterList *TemplateParams, |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 314 | NonTypeTemplateParmDecl *NTTP, const DeducedTemplateArgument &NewDeduced, |
| 315 | QualType ValueType, TemplateDeductionInfo &Info, |
Benjamin Kramer | 7320b99 | 2016-06-15 14:20:56 +0000 | [diff] [blame] | 316 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 317 | assert(NTTP->getDepth() == Info.getDeducedDepth() && |
| 318 | "deducing non-type template argument with wrong depth"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 319 | |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 320 | DeducedTemplateArgument Result = checkDeducedTemplateArguments( |
| 321 | S.Context, Deduced[NTTP->getIndex()], NewDeduced); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 322 | if (Result.isNull()) { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 323 | Info.Param = NTTP; |
| 324 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 325 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 326 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 327 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 328 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 329 | Deduced[NTTP->getIndex()] = Result; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 330 | return S.getLangOpts().CPlusPlus1z |
| 331 | ? DeduceTemplateArgumentsByTypeMatch( |
| 332 | S, TemplateParams, NTTP->getType(), ValueType, Info, Deduced, |
| 333 | TDF_ParamWithReferenceType | TDF_SkipNonDependent, |
| 334 | /*PartialOrdering=*/false, |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 335 | /*ArrayBound=*/NewDeduced.wasDeducedFromArrayBound()) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 336 | : Sema::TDK_Success; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 339 | /// \brief Deduce the value of the given non-type template parameter |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 340 | /// from the given integral constant. |
| 341 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| 342 | Sema &S, TemplateParameterList *TemplateParams, |
| 343 | NonTypeTemplateParmDecl *NTTP, const llvm::APSInt &Value, |
| 344 | QualType ValueType, bool DeducedFromArrayBound, TemplateDeductionInfo &Info, |
| 345 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 346 | return DeduceNonTypeTemplateArgument( |
| 347 | S, TemplateParams, NTTP, |
| 348 | DeducedTemplateArgument(S.Context, Value, ValueType, |
| 349 | DeducedFromArrayBound), |
| 350 | ValueType, Info, Deduced); |
| 351 | } |
| 352 | |
| 353 | /// \brief Deduce the value of the given non-type template parameter |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 354 | /// from the given null pointer template argument type. |
| 355 | static Sema::TemplateDeductionResult DeduceNullPtrTemplateArgument( |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 356 | Sema &S, TemplateParameterList *TemplateParams, |
| 357 | NonTypeTemplateParmDecl *NTTP, QualType NullPtrType, |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 358 | TemplateDeductionInfo &Info, |
| 359 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 360 | Expr *Value = |
| 361 | S.ImpCastExprToType(new (S.Context) CXXNullPtrLiteralExpr( |
| 362 | S.Context.NullPtrTy, NTTP->getLocation()), |
| 363 | NullPtrType, CK_NullToPointer) |
| 364 | .get(); |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 365 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 366 | DeducedTemplateArgument(Value), |
| 367 | Value->getType(), Info, Deduced); |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /// \brief Deduce the value of the given non-type template parameter |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 371 | /// from the given type- or value-dependent expression. |
| 372 | /// |
| 373 | /// \returns true if deduction succeeded, false otherwise. |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 374 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| 375 | Sema &S, TemplateParameterList *TemplateParams, |
| 376 | NonTypeTemplateParmDecl *NTTP, Expr *Value, TemplateDeductionInfo &Info, |
| 377 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 378 | assert((Value->isTypeDependent() || Value->isValueDependent()) && |
| 379 | "Expression template argument must be type- or value-dependent."); |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 380 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 381 | DeducedTemplateArgument(Value), |
| 382 | Value->getType(), Info, Deduced); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 385 | /// \brief Deduce the value of the given non-type template parameter |
| 386 | /// from the given declaration. |
| 387 | /// |
| 388 | /// \returns true if deduction succeeded, false otherwise. |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 389 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| 390 | Sema &S, TemplateParameterList *TemplateParams, |
| 391 | NonTypeTemplateParmDecl *NTTP, ValueDecl *D, QualType T, |
| 392 | TemplateDeductionInfo &Info, |
| 393 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 394 | D = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 395 | TemplateArgument New(D, T); |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 396 | return DeduceNonTypeTemplateArgument( |
| 397 | S, TemplateParams, NTTP, DeducedTemplateArgument(New), T, Info, Deduced); |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 400 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 401 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 402 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 403 | TemplateName Param, |
| 404 | TemplateName Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 405 | TemplateDeductionInfo &Info, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 406 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 407 | TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 408 | if (!ParamDecl) { |
| 409 | // The parameter type is dependent and is not a template template parameter, |
| 410 | // so there is nothing that we can deduce. |
| 411 | return Sema::TDK_Success; |
| 412 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 413 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 414 | if (TemplateTemplateParmDecl *TempParam |
| 415 | = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 416 | // If we're not deducing at this depth, there's nothing to deduce. |
| 417 | if (TempParam->getDepth() != Info.getDeducedDepth()) |
| 418 | return Sema::TDK_Success; |
| 419 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 420 | DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 421 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 422 | Deduced[TempParam->getIndex()], |
| 423 | NewDeduced); |
| 424 | if (Result.isNull()) { |
| 425 | Info.Param = TempParam; |
| 426 | Info.FirstArg = Deduced[TempParam->getIndex()]; |
| 427 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 428 | return Sema::TDK_Inconsistent; |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 429 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 430 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 431 | Deduced[TempParam->getIndex()] = Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 432 | return Sema::TDK_Success; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 433 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 434 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 435 | // Verify that the two template names are equivalent. |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 436 | if (S.Context.hasSameTemplateName(Param, Arg)) |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 437 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 438 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 439 | // Mismatch of non-dependent template parameter to argument. |
| 440 | Info.FirstArg = TemplateArgument(Param); |
| 441 | Info.SecondArg = TemplateArgument(Arg); |
| 442 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | /// \brief Deduce the template arguments by comparing the template parameter |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 446 | /// type (which is a template-id) with the template argument type. |
| 447 | /// |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 448 | /// \param S the Sema |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 449 | /// |
| 450 | /// \param TemplateParams the template parameters that we are deducing |
| 451 | /// |
| 452 | /// \param Param the parameter type |
| 453 | /// |
| 454 | /// \param Arg the argument type |
| 455 | /// |
| 456 | /// \param Info information about the template argument deduction itself |
| 457 | /// |
| 458 | /// \param Deduced the deduced template arguments |
| 459 | /// |
| 460 | /// \returns the result of template argument deduction so far. Note that a |
| 461 | /// "success" result means that template argument deduction has not yet failed, |
| 462 | /// but it may still fail, later, for other reasons. |
| 463 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 464 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 465 | TemplateParameterList *TemplateParams, |
| 466 | const TemplateSpecializationType *Param, |
| 467 | QualType Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 468 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 469 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 470 | assert(Arg.isCanonical() && "Argument type must be canonical"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 471 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 472 | // Check whether the template argument is a dependent template-id. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | if (const TemplateSpecializationType *SpecArg |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 474 | = dyn_cast<TemplateSpecializationType>(Arg)) { |
| 475 | // Perform template argument deduction for the template name. |
| 476 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 477 | = DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 478 | Param->getTemplateName(), |
| 479 | SpecArg->getTemplateName(), |
| 480 | Info, Deduced)) |
| 481 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 484 | // Perform template argument deduction on each template |
Douglas Gregor | d80ea20 | 2010-12-22 18:55:49 +0000 | [diff] [blame] | 485 | // argument. Ignore any missing/extra arguments, since they could be |
| 486 | // filled in by default arguments. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 487 | return DeduceTemplateArguments(S, TemplateParams, |
| 488 | Param->template_arguments(), |
| 489 | SpecArg->template_arguments(), Info, Deduced, |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 490 | /*NumberOfArgumentsMustMatch=*/false); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 491 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 493 | // If the argument type is a class template specialization, we |
| 494 | // perform template argument deduction using its template |
| 495 | // arguments. |
| 496 | const RecordType *RecordArg = dyn_cast<RecordType>(Arg); |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 497 | if (!RecordArg) { |
| 498 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 499 | Info.SecondArg = TemplateArgument(Arg); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 500 | return Sema::TDK_NonDeducedMismatch; |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 501 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 502 | |
| 503 | ClassTemplateSpecializationDecl *SpecArg |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 504 | = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 505 | if (!SpecArg) { |
| 506 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 507 | Info.SecondArg = TemplateArgument(Arg); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 508 | return Sema::TDK_NonDeducedMismatch; |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 509 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 511 | // Perform template argument deduction for the template name. |
| 512 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 513 | = DeduceTemplateArguments(S, |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 514 | TemplateParams, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 515 | Param->getTemplateName(), |
| 516 | TemplateName(SpecArg->getSpecializedTemplate()), |
| 517 | Info, Deduced)) |
| 518 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 519 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 520 | // Perform template argument deduction for the template arguments. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 521 | return DeduceTemplateArguments(S, TemplateParams, Param->template_arguments(), |
| 522 | SpecArg->getTemplateArgs().asArray(), Info, |
| 523 | Deduced, /*NumberOfArgumentsMustMatch=*/true); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 524 | } |
| 525 | |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 526 | /// \brief Determines whether the given type is an opaque type that |
| 527 | /// might be more qualified when instantiated. |
| 528 | static bool IsPossiblyOpaquelyQualifiedType(QualType T) { |
| 529 | switch (T->getTypeClass()) { |
| 530 | case Type::TypeOfExpr: |
| 531 | case Type::TypeOf: |
| 532 | case Type::DependentName: |
| 533 | case Type::Decltype: |
| 534 | case Type::UnresolvedUsing: |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 535 | case Type::TemplateTypeParm: |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 536 | return true; |
| 537 | |
| 538 | case Type::ConstantArray: |
| 539 | case Type::IncompleteArray: |
| 540 | case Type::VariableArray: |
| 541 | case Type::DependentSizedArray: |
| 542 | return IsPossiblyOpaquelyQualifiedType( |
| 543 | cast<ArrayType>(T)->getElementType()); |
| 544 | |
| 545 | default: |
| 546 | return false; |
| 547 | } |
| 548 | } |
| 549 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 550 | /// \brief Retrieve the depth and index of a template parameter. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 551 | static std::pair<unsigned, unsigned> |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 552 | getDepthAndIndex(NamedDecl *ND) { |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 553 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND)) |
| 554 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 555 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 556 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND)) |
| 557 | return std::make_pair(NTTP->getDepth(), NTTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 558 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 559 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND); |
| 560 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| 561 | } |
| 562 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 563 | /// \brief Retrieve the depth and index of an unexpanded parameter pack. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 564 | static std::pair<unsigned, unsigned> |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 565 | getDepthAndIndex(UnexpandedParameterPack UPP) { |
| 566 | if (const TemplateTypeParmType *TTP |
| 567 | = UPP.first.dyn_cast<const TemplateTypeParmType *>()) |
| 568 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 569 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 570 | return getDepthAndIndex(UPP.first.get<NamedDecl *>()); |
| 571 | } |
| 572 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 573 | /// \brief Helper function to build a TemplateParameter when we don't |
| 574 | /// know its type statically. |
| 575 | static TemplateParameter makeTemplateParameter(Decl *D) { |
| 576 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) |
| 577 | return TemplateParameter(TTP); |
Craig Topper | 4b482ee | 2013-07-08 04:24:47 +0000 | [diff] [blame] | 578 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 579 | return TemplateParameter(NTTP); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 580 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 581 | return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); |
| 582 | } |
| 583 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 584 | /// A pack that we're currently deducing. |
| 585 | struct clang::DeducedPack { |
| 586 | DeducedPack(unsigned Index) : Index(Index), Outer(nullptr) {} |
Craig Topper | 0a4e1f5 | 2013-07-08 04:44:01 +0000 | [diff] [blame] | 587 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 588 | // The index of the pack. |
| 589 | unsigned Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 590 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 591 | // The old value of the pack before we started deducing it. |
| 592 | DeducedTemplateArgument Saved; |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 593 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 594 | // A deferred value of this pack from an inner deduction, that couldn't be |
| 595 | // deduced because this deduction hadn't happened yet. |
| 596 | DeducedTemplateArgument DeferredDeduction; |
| 597 | |
| 598 | // The new value of the pack. |
| 599 | SmallVector<DeducedTemplateArgument, 4> New; |
| 600 | |
| 601 | // The outer deduction for this pack, if any. |
| 602 | DeducedPack *Outer; |
| 603 | }; |
| 604 | |
Benjamin Kramer | d5748c7 | 2015-03-23 12:31:05 +0000 | [diff] [blame] | 605 | namespace { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 606 | /// A scope in which we're performing pack deduction. |
| 607 | class PackDeductionScope { |
| 608 | public: |
| 609 | PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, |
| 610 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 611 | TemplateDeductionInfo &Info, TemplateArgument Pattern) |
| 612 | : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { |
| 613 | // Compute the set of template parameter indices that correspond to |
| 614 | // parameter packs expanded by the pack expansion. |
| 615 | { |
| 616 | llvm::SmallBitVector SawIndices(TemplateParams->size()); |
| 617 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 618 | S.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
| 619 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 620 | unsigned Depth, Index; |
| 621 | std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 622 | if (Depth == Info.getDeducedDepth() && !SawIndices[Index]) { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 623 | SawIndices[Index] = true; |
| 624 | |
| 625 | // Save the deduced template argument for the parameter pack expanded |
| 626 | // by this pack expansion, then clear out the deduction. |
| 627 | DeducedPack Pack(Index); |
| 628 | Pack.Saved = Deduced[Index]; |
| 629 | Deduced[Index] = TemplateArgument(); |
| 630 | |
| 631 | Packs.push_back(Pack); |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | assert(!Packs.empty() && "Pack expansion without unexpanded packs?"); |
| 636 | |
| 637 | for (auto &Pack : Packs) { |
| 638 | if (Info.PendingDeducedPacks.size() > Pack.Index) |
| 639 | Pack.Outer = Info.PendingDeducedPacks[Pack.Index]; |
| 640 | else |
| 641 | Info.PendingDeducedPacks.resize(Pack.Index + 1); |
| 642 | Info.PendingDeducedPacks[Pack.Index] = &Pack; |
| 643 | |
| 644 | if (S.CurrentInstantiationScope) { |
| 645 | // If the template argument pack was explicitly specified, add that to |
| 646 | // the set of deduced arguments. |
| 647 | const TemplateArgument *ExplicitArgs; |
| 648 | unsigned NumExplicitArgs; |
| 649 | NamedDecl *PartiallySubstitutedPack = |
| 650 | S.CurrentInstantiationScope->getPartiallySubstitutedPack( |
| 651 | &ExplicitArgs, &NumExplicitArgs); |
| 652 | if (PartiallySubstitutedPack && |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 653 | getDepthAndIndex(PartiallySubstitutedPack) == |
| 654 | std::make_pair(Info.getDeducedDepth(), Pack.Index)) |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 655 | Pack.New.append(ExplicitArgs, ExplicitArgs + NumExplicitArgs); |
| 656 | } |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 657 | } |
| 658 | } |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 659 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 660 | ~PackDeductionScope() { |
| 661 | for (auto &Pack : Packs) |
| 662 | Info.PendingDeducedPacks[Pack.Index] = Pack.Outer; |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 663 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 664 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 665 | /// Move to deducing the next element in each pack that is being deduced. |
| 666 | void nextPackElement() { |
| 667 | // Capture the deduced template arguments for each parameter pack expanded |
| 668 | // by this pack expansion, add them to the list of arguments we've deduced |
| 669 | // for that pack, then clear out the deduced argument. |
| 670 | for (auto &Pack : Packs) { |
| 671 | DeducedTemplateArgument &DeducedArg = Deduced[Pack.Index]; |
| 672 | if (!DeducedArg.isNull()) { |
| 673 | Pack.New.push_back(DeducedArg); |
| 674 | DeducedArg = DeducedTemplateArgument(); |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | /// \brief Finish template argument deduction for a set of argument packs, |
| 680 | /// producing the argument packs and checking for consistency with prior |
| 681 | /// deductions. |
| 682 | Sema::TemplateDeductionResult finish(bool HasAnyArguments) { |
| 683 | // Build argument packs for each of the parameter packs expanded by this |
| 684 | // pack expansion. |
| 685 | for (auto &Pack : Packs) { |
| 686 | // Put back the old value for this pack. |
| 687 | Deduced[Pack.Index] = Pack.Saved; |
| 688 | |
| 689 | // Build or find a new value for this pack. |
| 690 | DeducedTemplateArgument NewPack; |
| 691 | if (HasAnyArguments && Pack.New.empty()) { |
| 692 | if (Pack.DeferredDeduction.isNull()) { |
| 693 | // We were not able to deduce anything for this parameter pack |
| 694 | // (because it only appeared in non-deduced contexts), so just |
| 695 | // restore the saved argument pack. |
| 696 | continue; |
| 697 | } |
| 698 | |
| 699 | NewPack = Pack.DeferredDeduction; |
| 700 | Pack.DeferredDeduction = TemplateArgument(); |
| 701 | } else if (Pack.New.empty()) { |
| 702 | // If we deduced an empty argument pack, create it now. |
| 703 | NewPack = DeducedTemplateArgument(TemplateArgument::getEmptyPack()); |
| 704 | } else { |
| 705 | TemplateArgument *ArgumentPack = |
| 706 | new (S.Context) TemplateArgument[Pack.New.size()]; |
| 707 | std::copy(Pack.New.begin(), Pack.New.end(), ArgumentPack); |
| 708 | NewPack = DeducedTemplateArgument( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 709 | TemplateArgument(llvm::makeArrayRef(ArgumentPack, Pack.New.size())), |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 710 | Pack.New[0].wasDeducedFromArrayBound()); |
| 711 | } |
| 712 | |
| 713 | // Pick where we're going to put the merged pack. |
| 714 | DeducedTemplateArgument *Loc; |
| 715 | if (Pack.Outer) { |
| 716 | if (Pack.Outer->DeferredDeduction.isNull()) { |
| 717 | // Defer checking this pack until we have a complete pack to compare |
| 718 | // it against. |
| 719 | Pack.Outer->DeferredDeduction = NewPack; |
| 720 | continue; |
| 721 | } |
| 722 | Loc = &Pack.Outer->DeferredDeduction; |
| 723 | } else { |
| 724 | Loc = &Deduced[Pack.Index]; |
| 725 | } |
| 726 | |
| 727 | // Check the new pack matches any previous value. |
| 728 | DeducedTemplateArgument OldPack = *Loc; |
| 729 | DeducedTemplateArgument Result = |
| 730 | checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 731 | |
| 732 | // If we deferred a deduction of this pack, check that one now too. |
| 733 | if (!Result.isNull() && !Pack.DeferredDeduction.isNull()) { |
| 734 | OldPack = Result; |
| 735 | NewPack = Pack.DeferredDeduction; |
| 736 | Result = checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 737 | } |
| 738 | |
| 739 | if (Result.isNull()) { |
| 740 | Info.Param = |
| 741 | makeTemplateParameter(TemplateParams->getParam(Pack.Index)); |
| 742 | Info.FirstArg = OldPack; |
| 743 | Info.SecondArg = NewPack; |
| 744 | return Sema::TDK_Inconsistent; |
| 745 | } |
| 746 | |
| 747 | *Loc = Result; |
| 748 | } |
| 749 | |
| 750 | return Sema::TDK_Success; |
| 751 | } |
| 752 | |
| 753 | private: |
| 754 | Sema &S; |
| 755 | TemplateParameterList *TemplateParams; |
| 756 | SmallVectorImpl<DeducedTemplateArgument> &Deduced; |
| 757 | TemplateDeductionInfo &Info; |
| 758 | |
| 759 | SmallVector<DeducedPack, 2> Packs; |
| 760 | }; |
Benjamin Kramer | d5748c7 | 2015-03-23 12:31:05 +0000 | [diff] [blame] | 761 | } // namespace |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 762 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 763 | /// \brief Deduce the template arguments by comparing the list of parameter |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 764 | /// types to the list of argument types, as in the parameter-type-lists of |
| 765 | /// function types (C++ [temp.deduct.type]p10). |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 766 | /// |
| 767 | /// \param S The semantic analysis object within which we are deducing |
| 768 | /// |
| 769 | /// \param TemplateParams The template parameters that we are deducing |
| 770 | /// |
| 771 | /// \param Params The list of parameter types |
| 772 | /// |
| 773 | /// \param NumParams The number of types in \c Params |
| 774 | /// |
| 775 | /// \param Args The list of argument types |
| 776 | /// |
| 777 | /// \param NumArgs The number of types in \c Args |
| 778 | /// |
| 779 | /// \param Info information about the template argument deduction itself |
| 780 | /// |
| 781 | /// \param Deduced the deduced template arguments |
| 782 | /// |
| 783 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
| 784 | /// how template argument deduction is performed. |
| 785 | /// |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 786 | /// \param PartialOrdering If true, we are performing template argument |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 787 | /// deduction for during partial ordering for a call |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 788 | /// (C++0x [temp.deduct.partial]). |
| 789 | /// |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 790 | /// \returns the result of template argument deduction so far. Note that a |
| 791 | /// "success" result means that template argument deduction has not yet failed, |
| 792 | /// but it may still fail, later, for other reasons. |
| 793 | static Sema::TemplateDeductionResult |
| 794 | DeduceTemplateArguments(Sema &S, |
| 795 | TemplateParameterList *TemplateParams, |
| 796 | const QualType *Params, unsigned NumParams, |
| 797 | const QualType *Args, unsigned NumArgs, |
| 798 | TemplateDeductionInfo &Info, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 799 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 800 | unsigned TDF, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 801 | bool PartialOrdering = false) { |
Douglas Gregor | 86bea35 | 2011-01-05 23:23:17 +0000 | [diff] [blame] | 802 | // Fast-path check to see if we have too many/too few arguments. |
| 803 | if (NumParams != NumArgs && |
| 804 | !(NumParams && isa<PackExpansionType>(Params[NumParams - 1])) && |
| 805 | !(NumArgs && isa<PackExpansionType>(Args[NumArgs - 1]))) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 806 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 807 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 808 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 809 | // Similarly, if P has a form that contains (T), then each parameter type |
| 810 | // Pi of the respective parameter-type- list of P is compared with the |
| 811 | // corresponding parameter type Ai of the corresponding parameter-type-list |
| 812 | // of A. [...] |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 813 | unsigned ArgIdx = 0, ParamIdx = 0; |
| 814 | for (; ParamIdx != NumParams; ++ParamIdx) { |
| 815 | // Check argument types. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 816 | const PackExpansionType *Expansion |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 817 | = dyn_cast<PackExpansionType>(Params[ParamIdx]); |
| 818 | if (!Expansion) { |
| 819 | // Simple case: compare the parameter and argument types at this point. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 820 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 821 | // Make sure we have an argument. |
| 822 | if (ArgIdx >= NumArgs) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 823 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 824 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 825 | if (isa<PackExpansionType>(Args[ArgIdx])) { |
| 826 | // C++0x [temp.deduct.type]p22: |
| 827 | // If the original function parameter associated with A is a function |
| 828 | // parameter pack and the function parameter associated with P is not |
| 829 | // a function parameter pack, then template argument deduction fails. |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 830 | return Sema::TDK_MiscellaneousDeductionFailure; |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 831 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 832 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 833 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 834 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 835 | Params[ParamIdx], Args[ArgIdx], |
| 836 | Info, Deduced, TDF, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 837 | PartialOrdering)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 838 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 839 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 840 | ++ArgIdx; |
| 841 | continue; |
| 842 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 843 | |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 844 | // C++0x [temp.deduct.type]p5: |
| 845 | // The non-deduced contexts are: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 846 | // - 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] | 847 | // parameter-declaration-clause. |
| 848 | if (ParamIdx + 1 < NumParams) |
| 849 | return Sema::TDK_Success; |
| 850 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 851 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 852 | // If the parameter-declaration corresponding to Pi is a function |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 853 | // parameter pack, then the type of its declarator- id is compared with |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 854 | // each remaining parameter type in the parameter-type-list of A. Each |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 855 | // comparison deduces template arguments for subsequent positions in the |
| 856 | // template parameter packs expanded by the function parameter pack. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 857 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 858 | QualType Pattern = Expansion->getPattern(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 859 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 860 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 861 | bool HasAnyArguments = false; |
| 862 | for (; ArgIdx < NumArgs; ++ArgIdx) { |
| 863 | HasAnyArguments = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 864 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 865 | // Deduce template arguments from the pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 866 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 867 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, Pattern, |
| 868 | Args[ArgIdx], Info, Deduced, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 869 | TDF, PartialOrdering)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 870 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 871 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 872 | PackScope.nextPackElement(); |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 873 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 874 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 875 | // Build argument packs for each of the parameter packs expanded by this |
| 876 | // pack expansion. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 877 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 878 | return Result; |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 879 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 880 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 881 | // Make sure we don't have any extra arguments. |
| 882 | if (ArgIdx < NumArgs) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 883 | return Sema::TDK_MiscellaneousDeductionFailure; |
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 | return Sema::TDK_Success; |
| 886 | } |
| 887 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 888 | /// \brief Determine whether the parameter has qualifiers that are either |
| 889 | /// inconsistent with or a superset of the argument's qualifiers. |
| 890 | static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType, |
| 891 | QualType ArgType) { |
| 892 | Qualifiers ParamQs = ParamType.getQualifiers(); |
| 893 | Qualifiers ArgQs = ArgType.getQualifiers(); |
| 894 | |
| 895 | if (ParamQs == ArgQs) |
| 896 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 897 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 898 | // Mismatched (but not missing) Objective-C GC attributes. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 899 | if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() && |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 900 | ParamQs.hasObjCGCAttr()) |
| 901 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 902 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 903 | // Mismatched (but not missing) address spaces. |
| 904 | if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() && |
| 905 | ParamQs.hasAddressSpace()) |
| 906 | return true; |
| 907 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 908 | // Mismatched (but not missing) Objective-C lifetime qualifiers. |
| 909 | if (ParamQs.getObjCLifetime() != ArgQs.getObjCLifetime() && |
| 910 | ParamQs.hasObjCLifetime()) |
| 911 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 912 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 913 | // CVR qualifier superset. |
| 914 | return (ParamQs.getCVRQualifiers() != ArgQs.getCVRQualifiers()) && |
| 915 | ((ParamQs.getCVRQualifiers() | ArgQs.getCVRQualifiers()) |
| 916 | == ParamQs.getCVRQualifiers()); |
| 917 | } |
| 918 | |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 919 | /// \brief Compare types for equality with respect to possibly compatible |
| 920 | /// function types (noreturn adjustment, implicit calling conventions). If any |
| 921 | /// of parameter and argument is not a function, just perform type comparison. |
| 922 | /// |
| 923 | /// \param Param the template parameter type. |
| 924 | /// |
| 925 | /// \param Arg the argument type. |
| 926 | bool Sema::isSameOrCompatibleFunctionType(CanQualType Param, |
| 927 | CanQualType Arg) { |
| 928 | const FunctionType *ParamFunction = Param->getAs<FunctionType>(), |
| 929 | *ArgFunction = Arg->getAs<FunctionType>(); |
| 930 | |
| 931 | // Just compare if not functions. |
| 932 | if (!ParamFunction || !ArgFunction) |
| 933 | return Param == Arg; |
| 934 | |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 935 | // Noreturn and noexcept adjustment. |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 936 | QualType AdjustedParam; |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 937 | if (IsFunctionConversion(Param, Arg, AdjustedParam)) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 938 | return Arg == Context.getCanonicalType(AdjustedParam); |
| 939 | |
| 940 | // FIXME: Compatible calling conventions. |
| 941 | |
| 942 | return Param == Arg; |
| 943 | } |
| 944 | |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 945 | /// \brief Deduce the template arguments by comparing the parameter type and |
| 946 | /// the argument type (C++ [temp.deduct.type]). |
| 947 | /// |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 948 | /// \param S the semantic analysis object within which we are deducing |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 949 | /// |
| 950 | /// \param TemplateParams the template parameters that we are deducing |
| 951 | /// |
| 952 | /// \param ParamIn the parameter type |
| 953 | /// |
| 954 | /// \param ArgIn the argument type |
| 955 | /// |
| 956 | /// \param Info information about the template argument deduction itself |
| 957 | /// |
| 958 | /// \param Deduced the deduced template arguments |
| 959 | /// |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 960 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 961 | /// how template argument deduction is performed. |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 962 | /// |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 963 | /// \param PartialOrdering Whether we're performing template argument deduction |
| 964 | /// in the context of partial ordering (C++0x [temp.deduct.partial]). |
| 965 | /// |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 966 | /// \returns the result of template argument deduction so far. Note that a |
| 967 | /// "success" result means that template argument deduction has not yet failed, |
| 968 | /// but it may still fail, later, for other reasons. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 969 | static Sema::TemplateDeductionResult |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 970 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 971 | TemplateParameterList *TemplateParams, |
| 972 | QualType ParamIn, QualType ArgIn, |
| 973 | TemplateDeductionInfo &Info, |
| 974 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 975 | unsigned TDF, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 976 | bool PartialOrdering, |
| 977 | bool DeducedFromArrayBound) { |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 978 | // We only want to look at the canonical types, since typedefs and |
| 979 | // sugar are not part of template argument deduction. |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 980 | QualType Param = S.Context.getCanonicalType(ParamIn); |
| 981 | QualType Arg = S.Context.getCanonicalType(ArgIn); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 982 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 983 | // If the argument type is a pack expansion, look at its pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 984 | // This isn't explicitly called out |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 985 | if (const PackExpansionType *ArgExpansion |
| 986 | = dyn_cast<PackExpansionType>(Arg)) |
| 987 | Arg = ArgExpansion->getPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 988 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 989 | if (PartialOrdering) { |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 990 | // C++11 [temp.deduct.partial]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 991 | // Before the partial ordering is done, certain transformations are |
| 992 | // performed on the types used for partial ordering: |
| 993 | // - 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] | 994 | const ReferenceType *ParamRef = Param->getAs<ReferenceType>(); |
| 995 | if (ParamRef) |
| 996 | Param = ParamRef->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 997 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 998 | // - If A is a reference type, A is replaced by the type referred to. |
| 999 | const ReferenceType *ArgRef = Arg->getAs<ReferenceType>(); |
| 1000 | if (ArgRef) |
| 1001 | Arg = ArgRef->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1002 | |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1003 | if (ParamRef && ArgRef && S.Context.hasSameUnqualifiedType(Param, Arg)) { |
| 1004 | // C++11 [temp.deduct.partial]p9: |
| 1005 | // If, for a given type, deduction succeeds in both directions (i.e., |
| 1006 | // the types are identical after the transformations above) and both |
| 1007 | // P and A were reference types [...]: |
| 1008 | // - if [one type] was an lvalue reference and [the other type] was |
| 1009 | // not, [the other type] is not considered to be at least as |
| 1010 | // specialized as [the first type] |
| 1011 | // - if [one type] is more cv-qualified than [the other type], |
| 1012 | // [the other type] is not considered to be at least as specialized |
| 1013 | // as [the first type] |
| 1014 | // Objective-C ARC adds: |
| 1015 | // - [one type] has non-trivial lifetime, [the other type] has |
| 1016 | // __unsafe_unretained lifetime, and the types are otherwise |
| 1017 | // identical |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1018 | // |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1019 | // A is "considered to be at least as specialized" as P iff deduction |
| 1020 | // succeeds, so we model this as a deduction failure. Note that |
| 1021 | // [the first type] is P and [the other type] is A here; the standard |
| 1022 | // gets this backwards. |
Douglas Gregor | 85894a8 | 2011-04-30 17:07:52 +0000 | [diff] [blame] | 1023 | Qualifiers ParamQuals = Param.getQualifiers(); |
| 1024 | Qualifiers ArgQuals = Arg.getQualifiers(); |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1025 | if ((ParamRef->isLValueReferenceType() && |
| 1026 | !ArgRef->isLValueReferenceType()) || |
| 1027 | ParamQuals.isStrictSupersetOf(ArgQuals) || |
| 1028 | (ParamQuals.hasNonTrivialObjCLifetime() && |
| 1029 | ArgQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone && |
| 1030 | ParamQuals.withoutObjCLifetime() == |
| 1031 | ArgQuals.withoutObjCLifetime())) { |
| 1032 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1033 | Info.SecondArg = TemplateArgument(ArgIn); |
| 1034 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 6beabee | 2014-01-02 19:42:02 +0000 | [diff] [blame] | 1035 | } |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1036 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1037 | |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1038 | // C++11 [temp.deduct.partial]p7: |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1039 | // Remove any top-level cv-qualifiers: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1040 | // - 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] | 1041 | // version of P. |
| 1042 | Param = Param.getUnqualifiedType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1043 | // - 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] | 1044 | // version of A. |
| 1045 | Arg = Arg.getUnqualifiedType(); |
| 1046 | } else { |
| 1047 | // C++0x [temp.deduct.call]p4 bullet 1: |
| 1048 | // - If the original P is a reference type, the deduced A (i.e., the type |
| 1049 | // referred to by the reference) can be more cv-qualified than the |
| 1050 | // transformed A. |
| 1051 | if (TDF & TDF_ParamWithReferenceType) { |
| 1052 | Qualifiers Quals; |
| 1053 | QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals); |
| 1054 | Quals.setCVRQualifiers(Quals.getCVRQualifiers() & |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 1055 | Arg.getCVRQualifiers()); |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1056 | Param = S.Context.getQualifiedType(UnqualParam, Quals); |
| 1057 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1058 | |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1059 | if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) { |
| 1060 | // C++0x [temp.deduct.type]p10: |
| 1061 | // If P and A are function types that originated from deduction when |
| 1062 | // taking the address of a function template (14.8.2.2) or when deducing |
| 1063 | // 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] | 1064 | // Ai are parameters of the top-level parameter-type-list of P and A, |
| 1065 | // respectively, Pi is adjusted if it is an rvalue reference to a |
| 1066 | // cv-unqualified template parameter and Ai is an lvalue reference, in |
| 1067 | // 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] | 1068 | // type (i.e., T&& is changed to simply T). [ Note: As a result, when |
| 1069 | // 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] | 1070 | // deduced as X&. - end note ] |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1071 | TDF &= ~TDF_TopLevelParameterTypeList; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1072 | |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1073 | if (const RValueReferenceType *ParamRef |
| 1074 | = Param->getAs<RValueReferenceType>()) { |
| 1075 | if (isa<TemplateTypeParmType>(ParamRef->getPointeeType()) && |
| 1076 | !ParamRef->getPointeeType().getQualifiers()) |
| 1077 | if (Arg->isLValueReferenceType()) |
| 1078 | Param = ParamRef->getPointeeType(); |
| 1079 | } |
| 1080 | } |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1081 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1082 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1083 | // C++ [temp.deduct.type]p9: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1084 | // A template type argument T, a template template argument TT or a |
| 1085 | // 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] | 1086 | // the following forms: |
| 1087 | // |
| 1088 | // T |
| 1089 | // cv-list T |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1090 | if (const TemplateTypeParmType *TemplateTypeParm |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1091 | = Param->getAs<TemplateTypeParmType>()) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1092 | // Just skip any attempts to deduce from a placeholder type or a parameter |
| 1093 | // at a different depth. |
| 1094 | if (Arg->isPlaceholderType() || |
| 1095 | Info.getDeducedDepth() != TemplateTypeParm->getDepth()) |
Douglas Gregor | 4ea5dec | 2011-09-22 15:57:07 +0000 | [diff] [blame] | 1096 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1097 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1098 | unsigned Index = TemplateTypeParm->getIndex(); |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1099 | bool RecanonicalizeArg = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1100 | |
Douglas Gregor | 6045482 | 2009-07-22 20:02:25 +0000 | [diff] [blame] | 1101 | // If the argument type is an array type, move the qualifiers up to the |
| 1102 | // 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] | 1103 | if (isa<ArrayType>(Arg)) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1104 | Qualifiers Quals; |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1105 | Arg = S.Context.getUnqualifiedArrayType(Arg, Quals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1106 | if (Quals) { |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1107 | Arg = S.Context.getQualifiedType(Arg, Quals); |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1108 | RecanonicalizeArg = true; |
| 1109 | } |
| 1110 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1111 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1112 | // The argument type can not be less qualified than the parameter |
| 1113 | // type. |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1114 | if (!(TDF & TDF_IgnoreQualifiers) && |
| 1115 | hasInconsistentOrSupersetQualifiersOf(Param, Arg)) { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1116 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1117 | Info.FirstArg = TemplateArgument(Param); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1118 | Info.SecondArg = TemplateArgument(Arg); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1119 | return Sema::TDK_Underqualified; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1120 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1121 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1122 | assert(TemplateTypeParm->getDepth() == Info.getDeducedDepth() && |
| 1123 | "saw template type parameter with wrong depth"); |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1124 | assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function"); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1125 | QualType DeducedType = Arg; |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1126 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1127 | // Remove any qualifiers on the parameter from the deduced type. |
| 1128 | // We checked the qualifiers for consistency above. |
| 1129 | Qualifiers DeducedQs = DeducedType.getQualifiers(); |
| 1130 | Qualifiers ParamQs = Param.getQualifiers(); |
| 1131 | DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers()); |
| 1132 | if (ParamQs.hasObjCGCAttr()) |
| 1133 | DeducedQs.removeObjCGCAttr(); |
| 1134 | if (ParamQs.hasAddressSpace()) |
| 1135 | DeducedQs.removeAddressSpace(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1136 | if (ParamQs.hasObjCLifetime()) |
| 1137 | DeducedQs.removeObjCLifetime(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1138 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1139 | // Objective-C ARC: |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1140 | // If template deduction would produce a lifetime qualifier on a type |
| 1141 | // that is not a lifetime type, template argument deduction fails. |
| 1142 | if (ParamQs.hasObjCLifetime() && !DeducedType->isObjCLifetimeType() && |
| 1143 | !DeducedType->isDependentType()) { |
| 1144 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1145 | Info.FirstArg = TemplateArgument(Param); |
| 1146 | Info.SecondArg = TemplateArgument(Arg); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1147 | return Sema::TDK_Underqualified; |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1148 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1149 | |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1150 | // Objective-C ARC: |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1151 | // If template deduction would produce an argument type with lifetime type |
| 1152 | // but no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1153 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1154 | DeducedType->isObjCLifetimeType() && |
| 1155 | !DeducedQs.hasObjCLifetime()) |
| 1156 | DeducedQs.setObjCLifetime(Qualifiers::OCL_Strong); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1157 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1158 | DeducedType = S.Context.getQualifiedType(DeducedType.getUnqualifiedType(), |
| 1159 | DeducedQs); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1160 | |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1161 | if (RecanonicalizeArg) |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1162 | DeducedType = S.Context.getCanonicalType(DeducedType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1163 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1164 | DeducedTemplateArgument NewDeduced(DeducedType, DeducedFromArrayBound); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1165 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1166 | Deduced[Index], |
| 1167 | NewDeduced); |
| 1168 | if (Result.isNull()) { |
| 1169 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1170 | Info.FirstArg = Deduced[Index]; |
| 1171 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1172 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1173 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1174 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1175 | Deduced[Index] = Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1176 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1179 | // Set up the template argument deduction information for a failure. |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1180 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1181 | Info.SecondArg = TemplateArgument(ArgIn); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1182 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 1183 | // If the parameter is an already-substituted template parameter |
| 1184 | // pack, do nothing: we don't know which of its arguments to look |
| 1185 | // at, so we have to wait until all of the parameter packs in this |
| 1186 | // expansion have arguments. |
| 1187 | if (isa<SubstTemplateTypeParmPackType>(Param)) |
| 1188 | return Sema::TDK_Success; |
| 1189 | |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1190 | // Check the cv-qualifiers on the parameter and argument types. |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1191 | CanQualType CanParam = S.Context.getCanonicalType(Param); |
| 1192 | CanQualType CanArg = S.Context.getCanonicalType(Arg); |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1193 | if (!(TDF & TDF_IgnoreQualifiers)) { |
| 1194 | if (TDF & TDF_ParamWithReferenceType) { |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1195 | if (hasInconsistentOrSupersetQualifiersOf(Param, Arg)) |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1196 | return Sema::TDK_NonDeducedMismatch; |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 1197 | } else if (!IsPossiblyOpaquelyQualifiedType(Param)) { |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1198 | if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1200 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1201 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1202 | // If the parameter type is not dependent, there is nothing to deduce. |
| 1203 | if (!Param->isDependentType()) { |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1204 | if (!(TDF & TDF_SkipNonDependent)) { |
| 1205 | bool NonDeduced = (TDF & TDF_InOverloadResolution)? |
| 1206 | !S.isSameOrCompatibleFunctionType(CanParam, CanArg) : |
| 1207 | Param != Arg; |
| 1208 | if (NonDeduced) { |
| 1209 | return Sema::TDK_NonDeducedMismatch; |
| 1210 | } |
| 1211 | } |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1212 | return Sema::TDK_Success; |
| 1213 | } |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1214 | } else if (!Param->isDependentType()) { |
| 1215 | CanQualType ParamUnqualType = CanParam.getUnqualifiedType(), |
| 1216 | ArgUnqualType = CanArg.getUnqualifiedType(); |
| 1217 | bool Success = (TDF & TDF_InOverloadResolution)? |
| 1218 | S.isSameOrCompatibleFunctionType(ParamUnqualType, |
| 1219 | ArgUnqualType) : |
| 1220 | ParamUnqualType == ArgUnqualType; |
| 1221 | if (Success) |
| 1222 | return Sema::TDK_Success; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1223 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1224 | |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1225 | switch (Param->getTypeClass()) { |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1226 | // Non-canonical types cannot appear here. |
| 1227 | #define NON_CANONICAL_TYPE(Class, Base) \ |
| 1228 | case Type::Class: llvm_unreachable("deducing non-canonical type: " #Class); |
| 1229 | #define TYPE(Class, Base) |
| 1230 | #include "clang/AST/TypeNodes.def" |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1231 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1232 | case Type::TemplateTypeParm: |
| 1233 | case Type::SubstTemplateTypeParmPack: |
| 1234 | llvm_unreachable("Type nodes handled above"); |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1235 | |
| 1236 | // These types cannot be dependent, so simply check whether the types are |
| 1237 | // the same. |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1238 | case Type::Builtin: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1239 | case Type::VariableArray: |
| 1240 | case Type::Vector: |
| 1241 | case Type::FunctionNoProto: |
| 1242 | case Type::Record: |
| 1243 | case Type::Enum: |
| 1244 | case Type::ObjCObject: |
| 1245 | case Type::ObjCInterface: |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1246 | case Type::ObjCObjectPointer: { |
| 1247 | if (TDF & TDF_SkipNonDependent) |
| 1248 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1249 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1250 | if (TDF & TDF_IgnoreQualifiers) { |
| 1251 | Param = Param.getUnqualifiedType(); |
| 1252 | Arg = Arg.getUnqualifiedType(); |
| 1253 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1254 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1255 | return Param == Arg? Sema::TDK_Success : Sema::TDK_NonDeducedMismatch; |
| 1256 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1257 | |
| 1258 | // _Complex T [placeholder extension] |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1259 | case Type::Complex: |
| 1260 | if (const ComplexType *ComplexArg = Arg->getAs<ComplexType>()) |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1261 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1262 | cast<ComplexType>(Param)->getElementType(), |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1263 | ComplexArg->getElementType(), |
| 1264 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1265 | |
| 1266 | return Sema::TDK_NonDeducedMismatch; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1267 | |
| 1268 | // _Atomic T [extension] |
| 1269 | case Type::Atomic: |
| 1270 | if (const AtomicType *AtomicArg = Arg->getAs<AtomicType>()) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1271 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1272 | cast<AtomicType>(Param)->getValueType(), |
| 1273 | AtomicArg->getValueType(), |
| 1274 | Info, Deduced, TDF); |
| 1275 | |
| 1276 | return Sema::TDK_NonDeducedMismatch; |
| 1277 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1278 | // T * |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1279 | case Type::Pointer: { |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1280 | QualType PointeeType; |
| 1281 | if (const PointerType *PointerArg = Arg->getAs<PointerType>()) { |
| 1282 | PointeeType = PointerArg->getPointeeType(); |
| 1283 | } else if (const ObjCObjectPointerType *PointerArg |
| 1284 | = Arg->getAs<ObjCObjectPointerType>()) { |
| 1285 | PointeeType = PointerArg->getPointeeType(); |
| 1286 | } else { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1287 | return Sema::TDK_NonDeducedMismatch; |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1288 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1289 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1290 | unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1291 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1292 | cast<PointerType>(Param)->getPointeeType(), |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1293 | PointeeType, |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1294 | Info, Deduced, SubTDF); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1295 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1296 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1297 | // T & |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1298 | case Type::LValueReference: { |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 1299 | const LValueReferenceType *ReferenceArg = |
| 1300 | Arg->getAs<LValueReferenceType>(); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1301 | if (!ReferenceArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1302 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1303 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1304 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1305 | cast<LValueReferenceType>(Param)->getPointeeType(), |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1306 | ReferenceArg->getPointeeType(), Info, Deduced, 0); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1307 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1308 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1309 | // T && [C++0x] |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1310 | case Type::RValueReference: { |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 1311 | const RValueReferenceType *ReferenceArg = |
| 1312 | Arg->getAs<RValueReferenceType>(); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1313 | if (!ReferenceArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1314 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1315 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1316 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1317 | cast<RValueReferenceType>(Param)->getPointeeType(), |
| 1318 | ReferenceArg->getPointeeType(), |
| 1319 | Info, Deduced, 0); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1320 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1321 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1322 | // T [] (implied, but not stated explicitly) |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1323 | case Type::IncompleteArray: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1324 | const IncompleteArrayType *IncompleteArrayArg = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1325 | S.Context.getAsIncompleteArrayType(Arg); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1326 | if (!IncompleteArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1327 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1328 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1329 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1330 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1331 | S.Context.getAsIncompleteArrayType(Param)->getElementType(), |
| 1332 | IncompleteArrayArg->getElementType(), |
| 1333 | Info, Deduced, SubTDF); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1334 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1335 | |
| 1336 | // T [integer-constant] |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1337 | case Type::ConstantArray: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1338 | const ConstantArrayType *ConstantArrayArg = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1339 | S.Context.getAsConstantArrayType(Arg); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1340 | if (!ConstantArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1341 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1342 | |
| 1343 | const ConstantArrayType *ConstantArrayParm = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1344 | S.Context.getAsConstantArrayType(Param); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1345 | if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1346 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1347 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1348 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1349 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1350 | ConstantArrayParm->getElementType(), |
| 1351 | ConstantArrayArg->getElementType(), |
| 1352 | Info, Deduced, SubTDF); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1355 | // type [i] |
| 1356 | case Type::DependentSizedArray: { |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1357 | const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1358 | if (!ArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1359 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1360 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1361 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
| 1362 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1363 | // Check the element type of the arrays |
| 1364 | const DependentSizedArrayType *DependentArrayParm |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1365 | = S.Context.getAsDependentSizedArrayType(Param); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1366 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1367 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1368 | DependentArrayParm->getElementType(), |
| 1369 | ArrayArg->getElementType(), |
| 1370 | Info, Deduced, SubTDF)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1371 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1372 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1373 | // Determine the array bound is something we can deduce. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1374 | NonTypeTemplateParmDecl *NTTP |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1375 | = getDeducedParameterFromExpr(Info, DependentArrayParm->getSizeExpr()); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1376 | if (!NTTP) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1377 | return Sema::TDK_Success; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1378 | |
| 1379 | // We can perform template argument deduction for the given non-type |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1380 | // template parameter. |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1381 | assert(NTTP->getDepth() == Info.getDeducedDepth() && |
| 1382 | "saw non-type template parameter with wrong depth"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1383 | if (const ConstantArrayType *ConstantArrayArg |
Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1384 | = dyn_cast<ConstantArrayType>(ArrayArg)) { |
| 1385 | llvm::APSInt Size(ConstantArrayArg->getSize()); |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1386 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, Size, |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1387 | S.Context.getSizeType(), |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1388 | /*ArrayBound=*/true, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1389 | Info, Deduced); |
Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1390 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1391 | if (const DependentSizedArrayType *DependentArrayArg |
| 1392 | = dyn_cast<DependentSizedArrayType>(ArrayArg)) |
Douglas Gregor | 7a49ead | 2010-12-22 23:15:38 +0000 | [diff] [blame] | 1393 | if (DependentArrayArg->getSizeExpr()) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1394 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
Douglas Gregor | 7a49ead | 2010-12-22 23:15:38 +0000 | [diff] [blame] | 1395 | DependentArrayArg->getSizeExpr(), |
| 1396 | Info, Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1397 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1398 | // Incomplete type does not match a dependently-sized array type |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1399 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1400 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1401 | |
| 1402 | // type(*)(T) |
| 1403 | // T(*)() |
| 1404 | // T(*)(T) |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1405 | case Type::FunctionProto: { |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1406 | unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1407 | const FunctionProtoType *FunctionProtoArg = |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1408 | dyn_cast<FunctionProtoType>(Arg); |
| 1409 | if (!FunctionProtoArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1410 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1411 | |
| 1412 | const FunctionProtoType *FunctionProtoParam = |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1413 | cast<FunctionProtoType>(Param); |
Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1414 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1415 | if (FunctionProtoParam->getTypeQuals() |
Douglas Gregor | 54e462a | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1416 | != FunctionProtoArg->getTypeQuals() || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1417 | FunctionProtoParam->getRefQualifier() |
Douglas Gregor | 54e462a | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1418 | != FunctionProtoArg->getRefQualifier() || |
| 1419 | FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1420 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1421 | |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1422 | // Check return types. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1423 | if (Sema::TemplateDeductionResult Result = |
| 1424 | DeduceTemplateArgumentsByTypeMatch( |
| 1425 | S, TemplateParams, FunctionProtoParam->getReturnType(), |
| 1426 | FunctionProtoArg->getReturnType(), Info, Deduced, 0)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1427 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1429 | return DeduceTemplateArguments( |
| 1430 | S, TemplateParams, FunctionProtoParam->param_type_begin(), |
| 1431 | FunctionProtoParam->getNumParams(), |
| 1432 | FunctionProtoArg->param_type_begin(), |
| 1433 | FunctionProtoArg->getNumParams(), Info, Deduced, SubTDF); |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1434 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1435 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1436 | case Type::InjectedClassName: { |
| 1437 | // Treat a template's injected-class-name as if the template |
| 1438 | // specialization type had been used. |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1439 | Param = cast<InjectedClassNameType>(Param) |
| 1440 | ->getInjectedSpecializationType(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1441 | assert(isa<TemplateSpecializationType>(Param) && |
| 1442 | "injected class name is not a template specialization type"); |
| 1443 | // fall through |
| 1444 | } |
| 1445 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1446 | // template-name<T> (where template-name refers to a class template) |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1447 | // template-name<i> |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1448 | // TT<T> |
| 1449 | // TT<i> |
| 1450 | // TT<> |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1451 | case Type::TemplateSpecialization: { |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1452 | const TemplateSpecializationType *SpecParam = |
| 1453 | cast<TemplateSpecializationType>(Param); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1454 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1455 | // When Arg cannot be a derived class, we can just try to deduce template |
| 1456 | // arguments from the template-id. |
| 1457 | const RecordType *RecordT = Arg->getAs<RecordType>(); |
| 1458 | if (!(TDF & TDF_DerivedClass) || !RecordT) |
| 1459 | return DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg, Info, |
| 1460 | Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1461 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1462 | SmallVector<DeducedTemplateArgument, 8> DeducedOrig(Deduced.begin(), |
| 1463 | Deduced.end()); |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1464 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1465 | Sema::TemplateDeductionResult Result = DeduceTemplateArguments( |
| 1466 | S, TemplateParams, SpecParam, Arg, Info, Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1467 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1468 | if (Result == Sema::TDK_Success) |
| 1469 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1470 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1471 | // We cannot inspect base classes as part of deduction when the type |
| 1472 | // is incomplete, so either instantiate any templates necessary to |
| 1473 | // complete the type, or skip over it if it cannot be completed. |
| 1474 | if (!S.isCompleteType(Info.getLocation(), Arg)) |
| 1475 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1476 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1477 | // C++14 [temp.deduct.call] p4b3: |
| 1478 | // If P is a class and P has the form simple-template-id, then the |
| 1479 | // transformed A can be a derived class of the deduced A. Likewise if |
| 1480 | // P is a pointer to a class of the form simple-template-id, the |
| 1481 | // transformed A can be a pointer to a derived class pointed to by the |
| 1482 | // deduced A. |
| 1483 | // |
| 1484 | // These alternatives are considered only if type deduction would |
| 1485 | // otherwise fail. If they yield more than one possible deduced A, the |
| 1486 | // type deduction fails. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1488 | // Reset the incorrectly deduced argument from above. |
| 1489 | Deduced = DeducedOrig; |
| 1490 | |
| 1491 | // Use data recursion to crawl through the list of base classes. |
| 1492 | // Visited contains the set of nodes we have already visited, while |
| 1493 | // ToVisit is our stack of records that we still need to visit. |
| 1494 | llvm::SmallPtrSet<const RecordType *, 8> Visited; |
| 1495 | SmallVector<const RecordType *, 8> ToVisit; |
| 1496 | ToVisit.push_back(RecordT); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1497 | bool Successful = false; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1498 | SmallVector<DeducedTemplateArgument, 8> SuccessfulDeduced; |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1499 | while (!ToVisit.empty()) { |
| 1500 | // Retrieve the next class in the inheritance hierarchy. |
| 1501 | const RecordType *NextT = ToVisit.pop_back_val(); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1502 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1503 | // If we have already seen this type, skip it. |
| 1504 | if (!Visited.insert(NextT).second) |
| 1505 | continue; |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1506 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1507 | // If this is a base class, try to perform template argument |
| 1508 | // deduction from it. |
| 1509 | if (NextT != RecordT) { |
| 1510 | TemplateDeductionInfo BaseInfo(Info.getLocation()); |
| 1511 | Sema::TemplateDeductionResult BaseResult = |
| 1512 | DeduceTemplateArguments(S, TemplateParams, SpecParam, |
| 1513 | QualType(NextT, 0), BaseInfo, Deduced); |
| 1514 | |
| 1515 | // If template argument deduction for this base was successful, |
| 1516 | // note that we had some success. Otherwise, ignore any deductions |
| 1517 | // from this base class. |
| 1518 | if (BaseResult == Sema::TDK_Success) { |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1519 | // If we've already seen some success, then deduction fails due to |
| 1520 | // an ambiguity (temp.deduct.call p5). |
| 1521 | if (Successful) |
| 1522 | return Sema::TDK_MiscellaneousDeductionFailure; |
| 1523 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1524 | Successful = true; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1525 | std::swap(SuccessfulDeduced, Deduced); |
| 1526 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1527 | Info.Param = BaseInfo.Param; |
| 1528 | Info.FirstArg = BaseInfo.FirstArg; |
| 1529 | Info.SecondArg = BaseInfo.SecondArg; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | Deduced = DeducedOrig; |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1533 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1534 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1535 | // Visit base classes |
| 1536 | CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl()); |
| 1537 | for (const auto &Base : Next->bases()) { |
| 1538 | assert(Base.getType()->isRecordType() && |
| 1539 | "Base class that isn't a record?"); |
| 1540 | ToVisit.push_back(Base.getType()->getAs<RecordType>()); |
| 1541 | } |
| 1542 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1543 | |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1544 | if (Successful) { |
| 1545 | std::swap(SuccessfulDeduced, Deduced); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1546 | return Sema::TDK_Success; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1547 | } |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1548 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1549 | return Result; |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1552 | // T type::* |
| 1553 | // T T::* |
| 1554 | // T (type::*)() |
| 1555 | // type (T::*)() |
| 1556 | // type (type::*)(T) |
| 1557 | // type (T::*)(T) |
| 1558 | // T (type::*)(T) |
| 1559 | // T (T::*)() |
| 1560 | // T (T::*)(T) |
| 1561 | case Type::MemberPointer: { |
| 1562 | const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); |
| 1563 | const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); |
| 1564 | if (!MemPtrArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1565 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1566 | |
David Majnemer | a381cda | 2015-11-30 20:34:28 +0000 | [diff] [blame] | 1567 | QualType ParamPointeeType = MemPtrParam->getPointeeType(); |
| 1568 | if (ParamPointeeType->isFunctionType()) |
| 1569 | S.adjustMemberFunctionCC(ParamPointeeType, /*IsStatic=*/true, |
| 1570 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
| 1571 | QualType ArgPointeeType = MemPtrArg->getPointeeType(); |
| 1572 | if (ArgPointeeType->isFunctionType()) |
| 1573 | S.adjustMemberFunctionCC(ArgPointeeType, /*IsStatic=*/true, |
| 1574 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
| 1575 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1576 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1577 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
David Majnemer | a381cda | 2015-11-30 20:34:28 +0000 | [diff] [blame] | 1578 | ParamPointeeType, |
| 1579 | ArgPointeeType, |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1580 | Info, Deduced, |
| 1581 | TDF & TDF_IgnoreQualifiers)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1582 | return Result; |
| 1583 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1584 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1585 | QualType(MemPtrParam->getClass(), 0), |
| 1586 | QualType(MemPtrArg->getClass(), 0), |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1587 | Info, Deduced, |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1588 | TDF & TDF_IgnoreQualifiers); |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1589 | } |
| 1590 | |
Anders Carlsson | 15f1dd1 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1591 | // (clang extension) |
| 1592 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1593 | // type(^)(T) |
| 1594 | // T(^)() |
| 1595 | // T(^)(T) |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1596 | case Type::BlockPointer: { |
| 1597 | const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); |
| 1598 | const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1599 | |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1600 | if (!BlockPtrArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1601 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1602 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1603 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1604 | BlockPtrParam->getPointeeType(), |
| 1605 | BlockPtrArg->getPointeeType(), |
| 1606 | Info, Deduced, 0); |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1609 | // (clang extension) |
| 1610 | // |
| 1611 | // T __attribute__(((ext_vector_type(<integral constant>)))) |
| 1612 | case Type::ExtVector: { |
| 1613 | const ExtVectorType *VectorParam = cast<ExtVectorType>(Param); |
| 1614 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1615 | // Make sure that the vectors have the same number of elements. |
| 1616 | if (VectorParam->getNumElements() != VectorArg->getNumElements()) |
| 1617 | return Sema::TDK_NonDeducedMismatch; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1618 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1619 | // Perform deduction on the element types. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1620 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1621 | VectorParam->getElementType(), |
| 1622 | VectorArg->getElementType(), |
| 1623 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1624 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1625 | |
| 1626 | if (const DependentSizedExtVectorType *VectorArg |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1627 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1628 | // We can't check the number of elements, since the argument has a |
| 1629 | // dependent number of elements. This can only occur during partial |
| 1630 | // ordering. |
| 1631 | |
| 1632 | // Perform deduction on the element types. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1633 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1634 | VectorParam->getElementType(), |
| 1635 | VectorArg->getElementType(), |
| 1636 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1637 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1638 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1639 | return Sema::TDK_NonDeducedMismatch; |
| 1640 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1641 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1642 | // (clang extension) |
| 1643 | // |
| 1644 | // T __attribute__(((ext_vector_type(N)))) |
| 1645 | case Type::DependentSizedExtVector: { |
| 1646 | const DependentSizedExtVectorType *VectorParam |
| 1647 | = cast<DependentSizedExtVectorType>(Param); |
| 1648 | |
| 1649 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1650 | // Perform deduction on the element types. |
| 1651 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1652 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1653 | VectorParam->getElementType(), |
| 1654 | VectorArg->getElementType(), |
| 1655 | Info, Deduced, TDF)) |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1656 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1657 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1658 | // Perform deduction on the vector size, if we can. |
| 1659 | NonTypeTemplateParmDecl *NTTP |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1660 | = getDeducedParameterFromExpr(Info, VectorParam->getSizeExpr()); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1661 | if (!NTTP) |
| 1662 | return Sema::TDK_Success; |
| 1663 | |
| 1664 | llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); |
| 1665 | ArgSize = VectorArg->getNumElements(); |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1666 | // Note that we use the "array bound" rules here; just like in that |
| 1667 | // case, we don't have any particular type for the vector size, but |
| 1668 | // we can provide one if necessary. |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1669 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1670 | S.Context.IntTy, true, Info, |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 1671 | Deduced); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1672 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1673 | |
| 1674 | if (const DependentSizedExtVectorType *VectorArg |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1675 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1676 | // Perform deduction on the element types. |
| 1677 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1678 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1679 | VectorParam->getElementType(), |
| 1680 | VectorArg->getElementType(), |
| 1681 | Info, Deduced, TDF)) |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1682 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1683 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1684 | // Perform deduction on the vector size, if we can. |
| 1685 | NonTypeTemplateParmDecl *NTTP |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1686 | = getDeducedParameterFromExpr(Info, VectorParam->getSizeExpr()); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1687 | if (!NTTP) |
| 1688 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1689 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1690 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 1691 | VectorArg->getSizeExpr(), |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1692 | Info, Deduced); |
| 1693 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1694 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1695 | return Sema::TDK_NonDeducedMismatch; |
| 1696 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1697 | |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1698 | case Type::TypeOfExpr: |
| 1699 | case Type::TypeOf: |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 1700 | case Type::DependentName: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1701 | case Type::UnresolvedUsing: |
| 1702 | case Type::Decltype: |
| 1703 | case Type::UnaryTransform: |
| 1704 | case Type::Auto: |
| 1705 | case Type::DependentTemplateSpecialization: |
| 1706 | case Type::PackExpansion: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 1707 | case Type::Pipe: |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1708 | // No template argument deduction for these types |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1709 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1712 | llvm_unreachable("Invalid Type Class!"); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1715 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1716 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1717 | TemplateParameterList *TemplateParams, |
| 1718 | const TemplateArgument &Param, |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1719 | TemplateArgument Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1720 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 1721 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1722 | // If the template argument is a pack expansion, perform template argument |
| 1723 | // deduction against the pattern of that expansion. This only occurs during |
| 1724 | // partial ordering. |
| 1725 | if (Arg.isPackExpansion()) |
| 1726 | Arg = Arg.getPackExpansionPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1727 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1728 | switch (Param.getKind()) { |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1729 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1730 | llvm_unreachable("Null template argument in parameter list"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1731 | |
| 1732 | case TemplateArgument::Type: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1733 | if (Arg.getKind() == TemplateArgument::Type) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1734 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1735 | Param.getAsType(), |
| 1736 | Arg.getAsType(), |
| 1737 | Info, Deduced, 0); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1738 | Info.FirstArg = Param; |
| 1739 | Info.SecondArg = Arg; |
| 1740 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1741 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1742 | case TemplateArgument::Template: |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1743 | if (Arg.getKind() == TemplateArgument::Template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1744 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1745 | Param.getAsTemplate(), |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1746 | Arg.getAsTemplate(), Info, Deduced); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1747 | Info.FirstArg = Param; |
| 1748 | Info.SecondArg = Arg; |
| 1749 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1750 | |
| 1751 | case TemplateArgument::TemplateExpansion: |
| 1752 | llvm_unreachable("caller should handle pack expansions"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1753 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1754 | case TemplateArgument::Declaration: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1755 | if (Arg.getKind() == TemplateArgument::Declaration && |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 1756 | isSameDeclaration(Param.getAsDecl(), Arg.getAsDecl())) |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 1757 | return Sema::TDK_Success; |
| 1758 | |
| 1759 | Info.FirstArg = Param; |
| 1760 | Info.SecondArg = Arg; |
| 1761 | return Sema::TDK_NonDeducedMismatch; |
| 1762 | |
| 1763 | case TemplateArgument::NullPtr: |
| 1764 | if (Arg.getKind() == TemplateArgument::NullPtr && |
| 1765 | S.Context.hasSameType(Param.getNullPtrType(), Arg.getNullPtrType())) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1766 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1767 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1768 | Info.FirstArg = Param; |
| 1769 | Info.SecondArg = Arg; |
| 1770 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1771 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1772 | case TemplateArgument::Integral: |
| 1773 | if (Arg.getKind() == TemplateArgument::Integral) { |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1774 | if (hasSameExtendedValue(Param.getAsIntegral(), Arg.getAsIntegral())) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1775 | return Sema::TDK_Success; |
| 1776 | |
| 1777 | Info.FirstArg = Param; |
| 1778 | Info.SecondArg = Arg; |
| 1779 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1780 | } |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1781 | |
| 1782 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 1783 | Info.FirstArg = Param; |
| 1784 | Info.SecondArg = Arg; |
| 1785 | return Sema::TDK_NonDeducedMismatch; |
| 1786 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1787 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1788 | Info.FirstArg = Param; |
| 1789 | Info.SecondArg = Arg; |
| 1790 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1791 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1792 | case TemplateArgument::Expression: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1793 | if (NonTypeTemplateParmDecl *NTTP |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1794 | = getDeducedParameterFromExpr(Info, Param.getAsExpr())) { |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1795 | if (Arg.getKind() == TemplateArgument::Integral) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1796 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1797 | Arg.getAsIntegral(), |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1798 | Arg.getIntegralType(), |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1799 | /*ArrayBound=*/false, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1800 | Info, Deduced); |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 1801 | if (Arg.getKind() == TemplateArgument::NullPtr) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1802 | return DeduceNullPtrTemplateArgument(S, TemplateParams, NTTP, |
| 1803 | Arg.getNullPtrType(), |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 1804 | Info, Deduced); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1805 | if (Arg.getKind() == TemplateArgument::Expression) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1806 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 1807 | Arg.getAsExpr(), Info, Deduced); |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 1808 | if (Arg.getKind() == TemplateArgument::Declaration) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1809 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 1810 | Arg.getAsDecl(), |
| 1811 | Arg.getParamTypeForDecl(), |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 1812 | Info, Deduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1813 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1814 | Info.FirstArg = Param; |
| 1815 | Info.SecondArg = Arg; |
| 1816 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1817 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1818 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1819 | // Can't deduce anything, but that's okay. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1820 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1821 | } |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 1822 | case TemplateArgument::Pack: |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1823 | llvm_unreachable("Argument packs should be expanded by the caller!"); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1824 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1825 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1826 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1829 | /// \brief Determine whether there is a template argument to be used for |
| 1830 | /// deduction. |
| 1831 | /// |
| 1832 | /// This routine "expands" argument packs in-place, overriding its input |
| 1833 | /// parameters so that \c Args[ArgIdx] will be the available template argument. |
| 1834 | /// |
| 1835 | /// \returns true if there is another template argument (which will be at |
| 1836 | /// \c Args[ArgIdx]), false otherwise. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1837 | static bool hasTemplateArgumentForDeduction(ArrayRef<TemplateArgument> &Args, |
| 1838 | unsigned &ArgIdx) { |
| 1839 | if (ArgIdx == Args.size()) |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1840 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1841 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1842 | const TemplateArgument &Arg = Args[ArgIdx]; |
| 1843 | if (Arg.getKind() != TemplateArgument::Pack) |
| 1844 | return true; |
| 1845 | |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1846 | assert(ArgIdx == Args.size() - 1 && "Pack not at the end of argument list?"); |
| 1847 | Args = Arg.pack_elements(); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1848 | ArgIdx = 0; |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1849 | return ArgIdx < Args.size(); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1850 | } |
| 1851 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1852 | /// \brief Determine whether the given set of template arguments has a pack |
| 1853 | /// expansion that is not the last template argument. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1854 | static bool hasPackExpansionBeforeEnd(ArrayRef<TemplateArgument> Args) { |
| 1855 | bool FoundPackExpansion = false; |
| 1856 | for (const auto &A : Args) { |
| 1857 | if (FoundPackExpansion) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1858 | return true; |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1859 | |
| 1860 | if (A.getKind() == TemplateArgument::Pack) |
| 1861 | return hasPackExpansionBeforeEnd(A.pack_elements()); |
| 1862 | |
| 1863 | if (A.isPackExpansion()) |
| 1864 | FoundPackExpansion = true; |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1865 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1866 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1867 | return false; |
| 1868 | } |
| 1869 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1870 | static Sema::TemplateDeductionResult |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1871 | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1872 | ArrayRef<TemplateArgument> Params, |
| 1873 | ArrayRef<TemplateArgument> Args, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1874 | TemplateDeductionInfo &Info, |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1875 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 1876 | bool NumberOfArgumentsMustMatch) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1877 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1878 | // If the template argument list of P contains a pack expansion that is not |
| 1879 | // the last template argument, the entire template argument list is a |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1880 | // non-deduced context. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1881 | if (hasPackExpansionBeforeEnd(Params)) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1882 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1883 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1884 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1885 | // If P has a form that contains <T> or <i>, then each argument Pi of the |
| 1886 | // respective template argument list P is compared with the corresponding |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1887 | // argument Ai of the corresponding template argument list of A. |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1888 | unsigned ArgIdx = 0, ParamIdx = 0; |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1889 | for (; hasTemplateArgumentForDeduction(Params, ParamIdx); ++ParamIdx) { |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1890 | if (!Params[ParamIdx].isPackExpansion()) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1891 | // The simple case: deduce template arguments by matching Pi and Ai. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1892 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1893 | // Check whether we have enough arguments. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1894 | if (!hasTemplateArgumentForDeduction(Args, ArgIdx)) |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1895 | return NumberOfArgumentsMustMatch ? Sema::TDK_TooFewArguments |
| 1896 | : Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1897 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1898 | if (Args[ArgIdx].isPackExpansion()) { |
| 1899 | // FIXME: We follow the logic of C++0x [temp.deduct.type]p22 here, |
| 1900 | // but applied to pack expansions that are template arguments. |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 1901 | return Sema::TDK_MiscellaneousDeductionFailure; |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1902 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1903 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1904 | // Perform deduction for this Pi/Ai pair. |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1905 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1906 | = DeduceTemplateArguments(S, TemplateParams, |
| 1907 | Params[ParamIdx], Args[ArgIdx], |
| 1908 | Info, Deduced)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1909 | return Result; |
| 1910 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1911 | // Move to the next argument. |
| 1912 | ++ArgIdx; |
| 1913 | continue; |
| 1914 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1915 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1916 | // The parameter is a pack expansion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1917 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1918 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1919 | // If Pi is a pack expansion, then the pattern of Pi is compared with |
| 1920 | // each remaining argument in the template argument list of A. Each |
| 1921 | // comparison deduces template arguments for subsequent positions in the |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1922 | // template parameter packs expanded by Pi. |
| 1923 | TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1924 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1925 | // FIXME: If there are no remaining arguments, we can bail out early |
| 1926 | // and set any deduced parameter packs to an empty argument pack. |
| 1927 | // The latter part of this is a (minor) correctness issue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1928 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1929 | // Prepare to deduce the packs within the pattern. |
| 1930 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1931 | |
| 1932 | // Keep track of the deduced template arguments for each parameter pack |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1933 | // expanded by this pack expansion (the outer index) and for each |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1934 | // template argument (the inner SmallVectors). |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1935 | bool HasAnyArguments = false; |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1936 | for (; hasTemplateArgumentForDeduction(Args, ArgIdx); ++ArgIdx) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1937 | HasAnyArguments = true; |
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 | // Deduce template arguments from the pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1940 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1941 | = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx], |
| 1942 | Info, Deduced)) |
| 1943 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1944 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1945 | PackScope.nextPackElement(); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1946 | } |
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 | // Build argument packs for each of the parameter packs expanded by this |
| 1949 | // pack expansion. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1950 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1951 | return Result; |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1952 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1953 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1954 | return Sema::TDK_Success; |
| 1955 | } |
| 1956 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1957 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1958 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1959 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1960 | const TemplateArgumentList &ParamList, |
| 1961 | const TemplateArgumentList &ArgList, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1962 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 1963 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1964 | return DeduceTemplateArguments(S, TemplateParams, ParamList.asArray(), |
| 1965 | ArgList.asArray(), Info, Deduced, false); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1966 | } |
| 1967 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1968 | /// \brief Determine whether two template arguments are the same. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1969 | static bool isSameTemplateArg(ASTContext &Context, |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1970 | const TemplateArgument &X, |
| 1971 | const TemplateArgument &Y) { |
| 1972 | if (X.getKind() != Y.getKind()) |
| 1973 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1974 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1975 | switch (X.getKind()) { |
| 1976 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1977 | llvm_unreachable("Comparing NULL template argument"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1978 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1979 | case TemplateArgument::Type: |
| 1980 | return Context.getCanonicalType(X.getAsType()) == |
| 1981 | Context.getCanonicalType(Y.getAsType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1982 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1983 | case TemplateArgument::Declaration: |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 1984 | return isSameDeclaration(X.getAsDecl(), Y.getAsDecl()); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 1985 | |
| 1986 | case TemplateArgument::NullPtr: |
| 1987 | return Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1988 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1989 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1990 | case TemplateArgument::TemplateExpansion: |
| 1991 | return Context.getCanonicalTemplateName( |
| 1992 | X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() == |
| 1993 | Context.getCanonicalTemplateName( |
| 1994 | Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1995 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1996 | case TemplateArgument::Integral: |
Richard Smith | 993f203 | 2016-12-25 20:21:12 +0000 | [diff] [blame] | 1997 | return hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1998 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1999 | case TemplateArgument::Expression: { |
| 2000 | llvm::FoldingSetNodeID XID, YID; |
| 2001 | X.getAsExpr()->Profile(XID, Context, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2002 | Y.getAsExpr()->Profile(YID, Context, true); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2003 | return XID == YID; |
| 2004 | } |
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::Pack: |
| 2007 | if (X.pack_size() != Y.pack_size()) |
| 2008 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | |
| 2010 | for (TemplateArgument::pack_iterator XP = X.pack_begin(), |
| 2011 | XPEnd = X.pack_end(), |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2012 | YP = Y.pack_begin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2013 | XP != XPEnd; ++XP, ++YP) |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2014 | if (!isSameTemplateArg(Context, *XP, *YP)) |
| 2015 | return false; |
| 2016 | |
| 2017 | return true; |
| 2018 | } |
| 2019 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2020 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2021 | } |
| 2022 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2023 | /// \brief Allocate a TemplateArgumentLoc where all locations have |
| 2024 | /// been initialized to the given location. |
| 2025 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2026 | /// \param Arg The template argument we are producing template argument |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2027 | /// location information for. |
| 2028 | /// |
| 2029 | /// \param NTTPType For a declaration template argument, the type of |
| 2030 | /// the non-type template parameter that corresponds to this template |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2031 | /// argument. Can be null if no type sugar is available to add to the |
| 2032 | /// type from the template argument. |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2033 | /// |
| 2034 | /// \param Loc The source location to use for the resulting template |
| 2035 | /// argument. |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2036 | TemplateArgumentLoc |
| 2037 | Sema::getTrivialTemplateArgumentLoc(const TemplateArgument &Arg, |
| 2038 | QualType NTTPType, SourceLocation Loc) { |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2039 | switch (Arg.getKind()) { |
| 2040 | case TemplateArgument::Null: |
| 2041 | llvm_unreachable("Can't get a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2042 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2043 | case TemplateArgument::Type: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2044 | return TemplateArgumentLoc( |
| 2045 | Arg, Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2046 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2047 | case TemplateArgument::Declaration: { |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2048 | if (NTTPType.isNull()) |
| 2049 | NTTPType = Arg.getParamTypeForDecl(); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2050 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
| 2051 | .getAs<Expr>(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2052 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2053 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2054 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2055 | case TemplateArgument::NullPtr: { |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2056 | if (NTTPType.isNull()) |
| 2057 | NTTPType = Arg.getNullPtrType(); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2058 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
| 2059 | .getAs<Expr>(); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2060 | return TemplateArgumentLoc(TemplateArgument(NTTPType, /*isNullPtr*/true), |
| 2061 | E); |
| 2062 | } |
| 2063 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2064 | case TemplateArgument::Integral: { |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2065 | Expr *E = |
| 2066 | BuildExpressionFromIntegralTemplateArgument(Arg, Loc).getAs<Expr>(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2067 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2068 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2069 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2070 | case TemplateArgument::Template: |
| 2071 | case TemplateArgument::TemplateExpansion: { |
| 2072 | NestedNameSpecifierLocBuilder Builder; |
| 2073 | TemplateName Template = Arg.getAsTemplate(); |
| 2074 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2075 | Builder.MakeTrivial(Context, DTN->getQualifier(), Loc); |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 2076 | else if (QualifiedTemplateName *QTN = |
| 2077 | Template.getAsQualifiedTemplateName()) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2078 | Builder.MakeTrivial(Context, QTN->getQualifier(), Loc); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2079 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2080 | if (Arg.getKind() == TemplateArgument::Template) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2081 | return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(Context), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2082 | Loc); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2083 | |
| 2084 | return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(Context), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2085 | Loc, Loc); |
| 2086 | } |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2087 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2088 | case TemplateArgument::Expression: |
| 2089 | return TemplateArgumentLoc(Arg, Arg.getAsExpr()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2090 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2091 | case TemplateArgument::Pack: |
| 2092 | return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo()); |
| 2093 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2094 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2095 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
| 2098 | |
| 2099 | /// \brief Convert the given deduced template argument and add it to the set of |
| 2100 | /// fully-converted template arguments. |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2101 | static bool |
| 2102 | ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param, |
| 2103 | DeducedTemplateArgument Arg, |
| 2104 | NamedDecl *Template, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2105 | TemplateDeductionInfo &Info, |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2106 | bool IsDeduced, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2107 | SmallVectorImpl<TemplateArgument> &Output) { |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2108 | auto ConvertArg = [&](DeducedTemplateArgument Arg, |
| 2109 | unsigned ArgumentPackIndex) { |
| 2110 | // Convert the deduced template argument into a template |
| 2111 | // argument that we can check, almost as if the user had written |
| 2112 | // the template argument explicitly. |
| 2113 | TemplateArgumentLoc ArgLoc = |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2114 | S.getTrivialTemplateArgumentLoc(Arg, QualType(), Info.getLocation()); |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2115 | |
| 2116 | // Check the template argument, converting it as necessary. |
| 2117 | return S.CheckTemplateArgument( |
| 2118 | Param, ArgLoc, Template, Template->getLocation(), |
| 2119 | Template->getSourceRange().getEnd(), ArgumentPackIndex, Output, |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2120 | IsDeduced |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2121 | ? (Arg.wasDeducedFromArrayBound() ? Sema::CTAK_DeducedFromArrayBound |
| 2122 | : Sema::CTAK_Deduced) |
| 2123 | : Sema::CTAK_Specified); |
| 2124 | }; |
| 2125 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2126 | if (Arg.getKind() == TemplateArgument::Pack) { |
| 2127 | // This is a template argument pack, so check each of its arguments against |
| 2128 | // the template parameter. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2129 | SmallVector<TemplateArgument, 2> PackedArgsBuilder; |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 2130 | for (const auto &P : Arg.pack_elements()) { |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2131 | // When converting the deduced template argument, append it to the |
| 2132 | // general output list. We need to do this so that the template argument |
| 2133 | // checking logic has all of the prior template arguments available. |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 2134 | DeducedTemplateArgument InnerArg(P); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2135 | InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound()); |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2136 | assert(InnerArg.getKind() != TemplateArgument::Pack && |
| 2137 | "deduced nested pack"); |
| 2138 | if (ConvertArg(InnerArg, PackedArgsBuilder.size())) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2139 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2140 | |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2141 | // Move the converted template argument into our argument pack. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 2142 | PackedArgsBuilder.push_back(Output.pop_back_val()); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2143 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2144 | |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2145 | // 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] | 2146 | // itself, in case that substitution fails. |
| 2147 | if (PackedArgsBuilder.empty()) { |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2148 | LocalInstantiationScope Scope(S); |
Richard Smith | e824775 | 2016-12-22 07:24:39 +0000 | [diff] [blame] | 2149 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Output); |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2150 | MultiLevelTemplateArgumentList Args(TemplateArgs); |
| 2151 | |
| 2152 | if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2153 | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
| 2154 | NTTP, Output, |
| 2155 | Template->getSourceRange()); |
Simon Pilgrim | 6f3e1ea | 2016-12-26 18:11:49 +0000 | [diff] [blame] | 2156 | if (Inst.isInvalid() || |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2157 | S.SubstType(NTTP->getType(), Args, NTTP->getLocation(), |
| 2158 | NTTP->getDeclName()).isNull()) |
| 2159 | return true; |
| 2160 | } else if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 2161 | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
| 2162 | TTP, Output, |
| 2163 | Template->getSourceRange()); |
| 2164 | if (Inst.isInvalid() || !S.SubstDecl(TTP, S.CurContext, Args)) |
| 2165 | return true; |
| 2166 | } |
| 2167 | // For type parameters, no substitution is ever required. |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2168 | } |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2169 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2170 | // Create the resulting argument pack. |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 2171 | Output.push_back( |
| 2172 | TemplateArgument::CreatePackCopy(S.Context, PackedArgsBuilder)); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2173 | return false; |
| 2174 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2175 | |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2176 | return ConvertArg(Arg, 0); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2179 | // FIXME: This should not be a template, but |
| 2180 | // ClassTemplatePartialSpecializationDecl sadly does not derive from |
| 2181 | // TemplateDecl. |
| 2182 | template<typename TemplateDeclT> |
| 2183 | static Sema::TemplateDeductionResult ConvertDeducedTemplateArguments( |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2184 | Sema &S, TemplateDeclT *Template, bool IsDeduced, |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2185 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2186 | TemplateDeductionInfo &Info, SmallVectorImpl<TemplateArgument> &Builder, |
| 2187 | LocalInstantiationScope *CurrentInstantiationScope = nullptr, |
| 2188 | unsigned NumAlreadyConverted = 0, bool PartialOverloading = false) { |
| 2189 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| 2190 | |
| 2191 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 2192 | NamedDecl *Param = TemplateParams->getParam(I); |
| 2193 | |
| 2194 | if (!Deduced[I].isNull()) { |
| 2195 | if (I < NumAlreadyConverted) { |
| 2196 | // We have already fully type-checked and converted this |
| 2197 | // argument, because it was explicitly-specified. Just record the |
| 2198 | // presence of this argument. |
| 2199 | Builder.push_back(Deduced[I]); |
| 2200 | // We may have had explicitly-specified template arguments for a |
| 2201 | // template parameter pack (that may or may not have been extended |
| 2202 | // via additional deduced arguments). |
| 2203 | if (Param->isParameterPack() && CurrentInstantiationScope) { |
| 2204 | if (CurrentInstantiationScope->getPartiallySubstitutedPack() == |
| 2205 | Param) { |
| 2206 | // Forget the partially-substituted pack; its substitution is now |
| 2207 | // complete. |
| 2208 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2209 | } |
| 2210 | } |
| 2211 | continue; |
| 2212 | } |
| 2213 | |
| 2214 | // We have deduced this argument, so it still needs to be |
| 2215 | // checked and converted. |
| 2216 | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Template, Info, |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2217 | IsDeduced, Builder)) { |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2218 | Info.Param = makeTemplateParameter(Param); |
| 2219 | // FIXME: These template arguments are temporary. Free them! |
| 2220 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2221 | return Sema::TDK_SubstitutionFailure; |
| 2222 | } |
| 2223 | |
| 2224 | continue; |
| 2225 | } |
| 2226 | |
| 2227 | // C++0x [temp.arg.explicit]p3: |
| 2228 | // A trailing template parameter pack (14.5.3) not otherwise deduced will |
| 2229 | // be deduced to an empty sequence of template arguments. |
| 2230 | // FIXME: Where did the word "trailing" come from? |
| 2231 | if (Param->isTemplateParameterPack()) { |
| 2232 | // We may have had explicitly-specified template arguments for this |
| 2233 | // template parameter pack. If so, our empty deduction extends the |
| 2234 | // explicitly-specified set (C++0x [temp.arg.explicit]p9). |
| 2235 | const TemplateArgument *ExplicitArgs; |
| 2236 | unsigned NumExplicitArgs; |
| 2237 | if (CurrentInstantiationScope && |
| 2238 | CurrentInstantiationScope->getPartiallySubstitutedPack( |
| 2239 | &ExplicitArgs, &NumExplicitArgs) == Param) { |
| 2240 | Builder.push_back(TemplateArgument( |
| 2241 | llvm::makeArrayRef(ExplicitArgs, NumExplicitArgs))); |
| 2242 | |
| 2243 | // Forget the partially-substituted pack; its substitution is now |
| 2244 | // complete. |
| 2245 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2246 | } else { |
| 2247 | // Go through the motions of checking the empty argument pack against |
| 2248 | // the parameter pack. |
| 2249 | DeducedTemplateArgument DeducedPack(TemplateArgument::getEmptyPack()); |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2250 | if (ConvertDeducedTemplateArgument(S, Param, DeducedPack, Template, |
| 2251 | Info, IsDeduced, Builder)) { |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2252 | Info.Param = makeTemplateParameter(Param); |
| 2253 | // FIXME: These template arguments are temporary. Free them! |
| 2254 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2255 | return Sema::TDK_SubstitutionFailure; |
| 2256 | } |
| 2257 | } |
| 2258 | continue; |
| 2259 | } |
| 2260 | |
| 2261 | // Substitute into the default template argument, if available. |
| 2262 | bool HasDefaultArg = false; |
| 2263 | TemplateDecl *TD = dyn_cast<TemplateDecl>(Template); |
| 2264 | if (!TD) { |
| 2265 | assert(isa<ClassTemplatePartialSpecializationDecl>(Template)); |
| 2266 | return Sema::TDK_Incomplete; |
| 2267 | } |
| 2268 | |
| 2269 | TemplateArgumentLoc DefArg = S.SubstDefaultTemplateArgumentIfAvailable( |
| 2270 | TD, TD->getLocation(), TD->getSourceRange().getEnd(), Param, Builder, |
| 2271 | HasDefaultArg); |
| 2272 | |
| 2273 | // If there was no default argument, deduction is incomplete. |
| 2274 | if (DefArg.getArgument().isNull()) { |
| 2275 | Info.Param = makeTemplateParameter( |
| 2276 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
| 2277 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2278 | if (PartialOverloading) break; |
| 2279 | |
| 2280 | return HasDefaultArg ? Sema::TDK_SubstitutionFailure |
| 2281 | : Sema::TDK_Incomplete; |
| 2282 | } |
| 2283 | |
| 2284 | // Check whether we can actually use the default argument. |
| 2285 | if (S.CheckTemplateArgument(Param, DefArg, TD, TD->getLocation(), |
| 2286 | TD->getSourceRange().getEnd(), 0, Builder, |
| 2287 | Sema::CTAK_Specified)) { |
| 2288 | Info.Param = makeTemplateParameter( |
| 2289 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
| 2290 | // FIXME: These template arguments are temporary. Free them! |
| 2291 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2292 | return Sema::TDK_SubstitutionFailure; |
| 2293 | } |
| 2294 | |
| 2295 | // If we get here, we successfully used the default template argument. |
| 2296 | } |
| 2297 | |
| 2298 | return Sema::TDK_Success; |
| 2299 | } |
| 2300 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2301 | DeclContext *getAsDeclContextOrEnclosing(Decl *D) { |
| 2302 | if (auto *DC = dyn_cast<DeclContext>(D)) |
| 2303 | return DC; |
| 2304 | return D->getDeclContext(); |
| 2305 | } |
| 2306 | |
| 2307 | template<typename T> struct IsPartialSpecialization { |
| 2308 | static constexpr bool value = false; |
| 2309 | }; |
| 2310 | template<> |
| 2311 | struct IsPartialSpecialization<ClassTemplatePartialSpecializationDecl> { |
| 2312 | static constexpr bool value = true; |
| 2313 | }; |
| 2314 | template<> |
| 2315 | struct IsPartialSpecialization<VarTemplatePartialSpecializationDecl> { |
| 2316 | static constexpr bool value = true; |
| 2317 | }; |
| 2318 | |
| 2319 | /// Complete template argument deduction for a partial specialization. |
| 2320 | template <typename T> |
| 2321 | static typename std::enable_if<IsPartialSpecialization<T>::value, |
| 2322 | Sema::TemplateDeductionResult>::type |
| 2323 | FinishTemplateArgumentDeduction( |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2324 | Sema &S, T *Partial, bool IsPartialOrdering, |
| 2325 | const TemplateArgumentList &TemplateArgs, |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2326 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2327 | TemplateDeductionInfo &Info) { |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2328 | // Unevaluated SFINAE context. |
| 2329 | EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2330 | Sema::SFINAETrap Trap(S); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2331 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2332 | Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Partial)); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2333 | |
| 2334 | // C++ [temp.deduct.type]p2: |
| 2335 | // [...] or if any template argument remains neither deduced nor |
| 2336 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2337 | SmallVector<TemplateArgument, 4> Builder; |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2338 | if (auto Result = ConvertDeducedTemplateArguments( |
| 2339 | S, Partial, IsPartialOrdering, Deduced, Info, Builder)) |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2340 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2341 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2342 | // Form the template argument list from the deduced template arguments. |
| 2343 | TemplateArgumentList *DeducedArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2344 | = TemplateArgumentList::CreateCopy(S.Context, Builder); |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2345 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2346 | Info.reset(DeducedArgumentList); |
| 2347 | |
| 2348 | // Substitute the deduced template arguments into the template |
| 2349 | // arguments of the class template partial specialization, and |
| 2350 | // verify that the instantiated template arguments are both valid |
| 2351 | // and are equivalent to the template arguments originally provided |
| 2352 | // to the class template. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2353 | LocalInstantiationScope InstScope(S); |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2354 | auto *Template = Partial->getSpecializedTemplate(); |
| 2355 | const ASTTemplateArgumentListInfo *PartialTemplArgInfo = |
| 2356 | Partial->getTemplateArgsAsWritten(); |
| 2357 | const TemplateArgumentLoc *PartialTemplateArgs = |
| 2358 | PartialTemplArgInfo->getTemplateArgs(); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2359 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2360 | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, |
| 2361 | PartialTemplArgInfo->RAngleLoc); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2362 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2363 | if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs, |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2364 | InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { |
| 2365 | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; |
| 2366 | if (ParamIdx >= Partial->getTemplateParameters()->size()) |
| 2367 | ParamIdx = Partial->getTemplateParameters()->size() - 1; |
| 2368 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2369 | Decl *Param = const_cast<NamedDecl *>( |
| 2370 | Partial->getTemplateParameters()->getParam(ParamIdx)); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2371 | Info.Param = makeTemplateParameter(Param); |
| 2372 | Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); |
| 2373 | return Sema::TDK_SubstitutionFailure; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2374 | } |
| 2375 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2376 | SmallVector<TemplateArgument, 4> ConvertedInstArgs; |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2377 | if (S.CheckTemplateArgumentList(Template, Partial->getLocation(), InstArgs, |
| 2378 | false, ConvertedInstArgs)) |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2379 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2380 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2381 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2382 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2383 | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2384 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 2385 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2386 | Info.FirstArg = TemplateArgs[I]; |
| 2387 | Info.SecondArg = InstArg; |
| 2388 | return Sema::TDK_NonDeducedMismatch; |
| 2389 | } |
| 2390 | } |
| 2391 | |
| 2392 | if (Trap.hasErrorOccurred()) |
| 2393 | return Sema::TDK_SubstitutionFailure; |
| 2394 | |
| 2395 | return Sema::TDK_Success; |
| 2396 | } |
| 2397 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2398 | /// \brief Perform template argument deduction to determine whether |
Larisse Voufo | 833b05a | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 2399 | /// the given template arguments match the given class template |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2400 | /// partial specialization per C++ [temp.class.spec.match]. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2401 | Sema::TemplateDeductionResult |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2402 | Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2403 | const TemplateArgumentList &TemplateArgs, |
| 2404 | TemplateDeductionInfo &Info) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 2405 | if (Partial->isInvalidDecl()) |
| 2406 | return TDK_Invalid; |
| 2407 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2408 | // C++ [temp.class.spec.match]p2: |
| 2409 | // A partial specialization matches a given actual template |
| 2410 | // argument list if the template arguments of the partial |
| 2411 | // specialization can be deduced from the actual template argument |
| 2412 | // list (14.8.2). |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2413 | |
| 2414 | // Unevaluated SFINAE context. |
| 2415 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2416 | SFINAETrap Trap(*this); |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2417 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2418 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2419 | Deduced.resize(Partial->getTemplateParameters()->size()); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2420 | if (TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2421 | = ::DeduceTemplateArguments(*this, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2422 | Partial->getTemplateParameters(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2423 | Partial->getTemplateArgs(), |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2424 | TemplateArgs, Info, Deduced)) |
| 2425 | return Result; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 2426 | |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2427 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2428 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2429 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2430 | if (Inst.isInvalid()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2431 | return TDK_InstantiationDepth; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2432 | |
Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2433 | if (Trap.hasErrorOccurred()) |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2434 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2435 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2436 | return ::FinishTemplateArgumentDeduction( |
| 2437 | *this, Partial, /*PartialOrdering=*/false, TemplateArgs, Deduced, Info); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2438 | } |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2439 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2440 | /// \brief Perform template argument deduction to determine whether |
| 2441 | /// the given template arguments match the given variable template |
| 2442 | /// partial specialization per C++ [temp.class.spec.match]. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2443 | Sema::TemplateDeductionResult |
| 2444 | Sema::DeduceTemplateArguments(VarTemplatePartialSpecializationDecl *Partial, |
| 2445 | const TemplateArgumentList &TemplateArgs, |
| 2446 | TemplateDeductionInfo &Info) { |
| 2447 | if (Partial->isInvalidDecl()) |
| 2448 | return TDK_Invalid; |
| 2449 | |
| 2450 | // C++ [temp.class.spec.match]p2: |
| 2451 | // A partial specialization matches a given actual template |
| 2452 | // argument list if the template arguments of the partial |
| 2453 | // specialization can be deduced from the actual template argument |
| 2454 | // list (14.8.2). |
| 2455 | |
| 2456 | // Unevaluated SFINAE context. |
| 2457 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
| 2458 | SFINAETrap Trap(*this); |
| 2459 | |
| 2460 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 2461 | Deduced.resize(Partial->getTemplateParameters()->size()); |
| 2462 | if (TemplateDeductionResult Result = ::DeduceTemplateArguments( |
| 2463 | *this, Partial->getTemplateParameters(), Partial->getTemplateArgs(), |
| 2464 | TemplateArgs, Info, Deduced)) |
| 2465 | return Result; |
| 2466 | |
| 2467 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2468 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2469 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2470 | if (Inst.isInvalid()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2471 | return TDK_InstantiationDepth; |
| 2472 | |
| 2473 | if (Trap.hasErrorOccurred()) |
| 2474 | return Sema::TDK_SubstitutionFailure; |
| 2475 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2476 | return ::FinishTemplateArgumentDeduction( |
| 2477 | *this, Partial, /*PartialOrdering=*/false, TemplateArgs, Deduced, Info); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2480 | /// \brief Determine whether the given type T is a simple-template-id type. |
| 2481 | static bool isSimpleTemplateIdType(QualType T) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2482 | if (const TemplateSpecializationType *Spec |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2483 | = T->getAs<TemplateSpecializationType>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2484 | return Spec->getTemplateName().getAsTemplateDecl() != nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2485 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2486 | return false; |
| 2487 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2488 | |
| 2489 | /// \brief Substitute the explicitly-provided template arguments into the |
| 2490 | /// given function template according to C++ [temp.arg.explicit]. |
| 2491 | /// |
| 2492 | /// \param FunctionTemplate the function template into which the explicit |
| 2493 | /// template arguments will be substituted. |
| 2494 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2495 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2496 | /// arguments. |
| 2497 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2498 | /// \param Deduced the deduced template arguments, which will be populated |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2499 | /// with the converted and checked explicit template arguments. |
| 2500 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2501 | /// \param ParamTypes will be populated with the instantiated function |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2502 | /// parameters. |
| 2503 | /// |
| 2504 | /// \param FunctionType if non-NULL, the result type of the function template |
| 2505 | /// will also be instantiated and the pointed-to value will be updated with |
| 2506 | /// the instantiated function type. |
| 2507 | /// |
| 2508 | /// \param Info if substitution fails for any reason, this object will be |
| 2509 | /// populated with more information about the failure. |
| 2510 | /// |
| 2511 | /// \returns TDK_Success if substitution was successful, or some failure |
| 2512 | /// condition. |
| 2513 | Sema::TemplateDeductionResult |
| 2514 | Sema::SubstituteExplicitTemplateArguments( |
| 2515 | FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2516 | TemplateArgumentListInfo &ExplicitTemplateArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2517 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2518 | SmallVectorImpl<QualType> &ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2519 | QualType *FunctionType, |
| 2520 | TemplateDeductionInfo &Info) { |
| 2521 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 2522 | TemplateParameterList *TemplateParams |
| 2523 | = FunctionTemplate->getTemplateParameters(); |
| 2524 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2525 | if (ExplicitTemplateArgs.size() == 0) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2526 | // No arguments to substitute; just copy over the parameter types and |
| 2527 | // fill in the function type. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2528 | for (auto P : Function->parameters()) |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 2529 | ParamTypes.push_back(P->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2530 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2531 | if (FunctionType) |
| 2532 | *FunctionType = Function->getType(); |
| 2533 | return TDK_Success; |
| 2534 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2535 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2536 | // Unevaluated SFINAE context. |
| 2537 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2538 | SFINAETrap Trap(*this); |
| 2539 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2540 | // C++ [temp.arg.explicit]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2541 | // Template arguments that are present shall be specified in the |
| 2542 | // declaration order of their corresponding template-parameters. The |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2543 | // template argument list shall not specify more template-arguments than |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2544 | // there are corresponding template-parameters. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2545 | SmallVector<TemplateArgument, 4> Builder; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2546 | |
| 2547 | // Enter a new template instantiation context where we check the |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2548 | // explicitly-specified template arguments against this function template, |
| 2549 | // and then substitute them into the function parameter types. |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2550 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2551 | InstantiatingTemplate Inst(*this, Info.getLocation(), FunctionTemplate, |
| 2552 | DeducedArgs, |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2553 | ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution, |
| 2554 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2555 | if (Inst.isInvalid()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2556 | return TDK_InstantiationDepth; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2557 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2558 | if (CheckTemplateArgumentList(FunctionTemplate, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2559 | SourceLocation(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2560 | ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2561 | true, |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 2562 | Builder) || Trap.hasErrorOccurred()) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2563 | unsigned Index = Builder.size(); |
Douglas Gregor | 62c281a | 2010-05-09 01:26:06 +0000 | [diff] [blame] | 2564 | if (Index >= TemplateParams->size()) |
| 2565 | Index = TemplateParams->size() - 1; |
| 2566 | Info.Param = makeTemplateParameter(TemplateParams->getParam(Index)); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2567 | return TDK_InvalidExplicitArguments; |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 2568 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2569 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2570 | // Form the template argument list from the explicitly-specified |
| 2571 | // template arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2572 | TemplateArgumentList *ExplicitArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2573 | = TemplateArgumentList::CreateCopy(Context, Builder); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2574 | Info.reset(ExplicitArgumentList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2575 | |
John McCall | 036855a | 2010-10-12 19:40:14 +0000 | [diff] [blame] | 2576 | // Template argument deduction and the final substitution should be |
| 2577 | // done in the context of the templated declaration. Explicit |
| 2578 | // argument substitution, on the other hand, needs to happen in the |
| 2579 | // calling context. |
| 2580 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
| 2581 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2582 | // If we deduced template arguments for a template parameter pack, |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2583 | // note that the template argument pack is partially substituted and record |
| 2584 | // the explicit template arguments. They'll be used as part of deduction |
| 2585 | // for this template parameter pack. |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2586 | for (unsigned I = 0, N = Builder.size(); I != N; ++I) { |
| 2587 | const TemplateArgument &Arg = Builder[I]; |
| 2588 | if (Arg.getKind() == TemplateArgument::Pack) { |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2589 | CurrentInstantiationScope->SetPartiallySubstitutedPack( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2590 | TemplateParams->getParam(I), |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2591 | Arg.pack_begin(), |
| 2592 | Arg.pack_size()); |
| 2593 | break; |
| 2594 | } |
| 2595 | } |
| 2596 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2597 | const FunctionProtoType *Proto |
| 2598 | = Function->getType()->getAs<FunctionProtoType>(); |
| 2599 | assert(Proto && "Function template does not have a prototype?"); |
| 2600 | |
Richard Smith | 70b1304 | 2015-01-09 01:19:56 +0000 | [diff] [blame] | 2601 | // Isolate our substituted parameters from our caller. |
| 2602 | LocalInstantiationScope InstScope(*this, /*MergeWithOuterScope*/true); |
| 2603 | |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2604 | ExtParameterInfoBuilder ExtParamInfos; |
| 2605 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2606 | // Instantiate the types of each of the function parameters given the |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2607 | // explicitly-specified template arguments. If the function has a trailing |
| 2608 | // return type, substitute it after the arguments to ensure we substitute |
| 2609 | // in lexical order. |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2610 | if (Proto->hasTrailingReturn()) { |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2611 | if (SubstParmTypes(Function->getLocation(), Function->parameters(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2612 | Proto->getExtParameterInfosOrNull(), |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2613 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2614 | ParamTypes, /*params*/ nullptr, ExtParamInfos)) |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2615 | return TDK_SubstitutionFailure; |
| 2616 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2617 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2618 | // Instantiate the return type. |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2619 | QualType ResultType; |
| 2620 | { |
| 2621 | // C++11 [expr.prim.general]p3: |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2622 | // If a declaration declares a member function or member function |
| 2623 | // 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] | 2624 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2625 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2626 | // declarator. |
| 2627 | unsigned ThisTypeQuals = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2628 | CXXRecordDecl *ThisContext = nullptr; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2629 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
| 2630 | ThisContext = Method->getParent(); |
| 2631 | ThisTypeQuals = Method->getTypeQualifiers(); |
| 2632 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2633 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2634 | CXXThisScopeRAII ThisScope(*this, ThisContext, ThisTypeQuals, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2635 | getLangOpts().CPlusPlus11); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2636 | |
| 2637 | ResultType = |
| 2638 | SubstType(Proto->getReturnType(), |
| 2639 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 2640 | Function->getTypeSpecStartLoc(), Function->getDeclName()); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2641 | if (ResultType.isNull() || Trap.hasErrorOccurred()) |
| 2642 | return TDK_SubstitutionFailure; |
| 2643 | } |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2644 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2645 | // Instantiate the types of each of the function parameters given the |
| 2646 | // explicitly-specified template arguments if we didn't do so earlier. |
| 2647 | if (!Proto->hasTrailingReturn() && |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2648 | SubstParmTypes(Function->getLocation(), Function->parameters(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2649 | Proto->getExtParameterInfosOrNull(), |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2650 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2651 | ParamTypes, /*params*/ nullptr, ExtParamInfos)) |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2652 | return TDK_SubstitutionFailure; |
| 2653 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2654 | if (FunctionType) { |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2655 | auto EPI = Proto->getExtProtoInfo(); |
| 2656 | EPI.ExtParameterInfos = ExtParamInfos.getPointerOrNull(ParamTypes.size()); |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2657 | *FunctionType = BuildFunctionType(ResultType, ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2658 | Function->getLocation(), |
Eli Friedman | d8725a9 | 2010-08-05 02:54:05 +0000 | [diff] [blame] | 2659 | Function->getDeclName(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2660 | EPI); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2661 | if (FunctionType->isNull() || Trap.hasErrorOccurred()) |
| 2662 | return TDK_SubstitutionFailure; |
| 2663 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2664 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2665 | // C++ [temp.arg.explicit]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2666 | // Trailing template arguments that can be deduced (14.8.2) may be |
| 2667 | // omitted from the list of explicit template-arguments. If all of the |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2668 | // template arguments can be deduced, they may all be omitted; in this |
| 2669 | // case, the empty template argument list <> itself may also be omitted. |
| 2670 | // |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2671 | // Take all of the explicitly-specified arguments and put them into |
| 2672 | // the set of deduced template arguments. Explicitly-specified |
| 2673 | // parameter packs, however, will be set to NULL since the deduction |
| 2674 | // mechanisms handle explicitly-specified argument packs directly. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2675 | Deduced.reserve(TemplateParams->size()); |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2676 | for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) { |
| 2677 | const TemplateArgument &Arg = ExplicitArgumentList->get(I); |
| 2678 | if (Arg.getKind() == TemplateArgument::Pack) |
| 2679 | Deduced.push_back(DeducedTemplateArgument()); |
| 2680 | else |
| 2681 | Deduced.push_back(Arg); |
| 2682 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2683 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2684 | return TDK_Success; |
| 2685 | } |
| 2686 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2687 | /// \brief Check whether the deduced argument type for a call to a function |
| 2688 | /// template matches the actual argument type per C++ [temp.deduct.call]p4. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2689 | static bool |
| 2690 | CheckOriginalCallArgDeduction(Sema &S, Sema::OriginalCallArg OriginalArg, |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2691 | QualType DeducedA) { |
| 2692 | ASTContext &Context = S.Context; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2693 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2694 | QualType A = OriginalArg.OriginalArgType; |
| 2695 | QualType OriginalParamType = OriginalArg.OriginalParamType; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2696 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2697 | // Check for type equality (top-level cv-qualifiers are ignored). |
| 2698 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| 2699 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2700 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2701 | // Strip off references on the argument types; they aren't needed for |
| 2702 | // the following checks. |
| 2703 | if (const ReferenceType *DeducedARef = DeducedA->getAs<ReferenceType>()) |
| 2704 | DeducedA = DeducedARef->getPointeeType(); |
| 2705 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
| 2706 | A = ARef->getPointeeType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2707 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2708 | // C++ [temp.deduct.call]p4: |
| 2709 | // [...] However, there are three cases that allow a difference: |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2710 | // - If the original P is a reference type, the deduced A (i.e., the |
| 2711 | // type referred to by the reference) can be more cv-qualified than |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2712 | // the transformed A. |
| 2713 | if (const ReferenceType *OriginalParamRef |
| 2714 | = OriginalParamType->getAs<ReferenceType>()) { |
| 2715 | // We don't want to keep the reference around any more. |
| 2716 | OriginalParamType = OriginalParamRef->getPointeeType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2717 | |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 2718 | // FIXME: Resolve core issue (no number yet): if the original P is a |
| 2719 | // reference type and the transformed A is function type "noexcept F", |
| 2720 | // the deduced A can be F. |
| 2721 | QualType Tmp; |
| 2722 | if (A->isFunctionType() && S.IsFunctionConversion(A, DeducedA, Tmp)) |
| 2723 | return false; |
| 2724 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2725 | Qualifiers AQuals = A.getQualifiers(); |
| 2726 | Qualifiers DeducedAQuals = DeducedA.getQualifiers(); |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2727 | |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2728 | // Under Objective-C++ ARC, the deduced type may have implicitly |
| 2729 | // been given strong or (when dealing with a const reference) |
| 2730 | // unsafe_unretained lifetime. If so, update the original |
| 2731 | // qualifiers to include this lifetime. |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2732 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2733 | ((DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_Strong && |
| 2734 | AQuals.getObjCLifetime() == Qualifiers::OCL_None) || |
| 2735 | (DeducedAQuals.hasConst() && |
| 2736 | DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone))) { |
| 2737 | AQuals.setObjCLifetime(DeducedAQuals.getObjCLifetime()); |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2738 | } |
| 2739 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2740 | if (AQuals == DeducedAQuals) { |
| 2741 | // Qualifiers match; there's nothing to do. |
| 2742 | } else if (!DeducedAQuals.compatiblyIncludes(AQuals)) { |
Douglas Gregor | ddaae52 | 2011-06-17 14:36:00 +0000 | [diff] [blame] | 2743 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2744 | } else { |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2745 | // Qualifiers are compatible, so have the argument type adopt the |
| 2746 | // deduced argument type's qualifiers as if we had performed the |
| 2747 | // qualification conversion. |
| 2748 | A = Context.getQualifiedType(A.getUnqualifiedType(), DeducedAQuals); |
| 2749 | } |
| 2750 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2751 | |
| 2752 | // - The transformed A can be another pointer or pointer to member |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 2753 | // type that can be converted to the deduced A via a function pointer |
| 2754 | // conversion and/or a qualification conversion. |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2755 | // |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 2756 | // Also allow conversions which merely strip __attribute__((noreturn)) from |
| 2757 | // function types (recursively). |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2758 | bool ObjCLifetimeConversion = false; |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2759 | QualType ResultTy; |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2760 | if ((A->isAnyPointerType() || A->isMemberPointerType()) && |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2761 | (S.IsQualificationConversion(A, DeducedA, false, |
| 2762 | ObjCLifetimeConversion) || |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 2763 | S.IsFunctionConversion(A, DeducedA, ResultTy))) |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2764 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2765 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2766 | // - 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] | 2767 | // transformed A can be a derived class of the deduced A. [...] |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2768 | // [...] Likewise, if P is a pointer to a class of the form |
| 2769 | // simple-template-id, the transformed A can be a pointer to a |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2770 | // derived class pointed to by the deduced A. |
| 2771 | if (const PointerType *OriginalParamPtr |
| 2772 | = OriginalParamType->getAs<PointerType>()) { |
| 2773 | if (const PointerType *DeducedAPtr = DeducedA->getAs<PointerType>()) { |
| 2774 | if (const PointerType *APtr = A->getAs<PointerType>()) { |
| 2775 | if (A->getPointeeType()->isRecordType()) { |
| 2776 | OriginalParamType = OriginalParamPtr->getPointeeType(); |
| 2777 | DeducedA = DeducedAPtr->getPointeeType(); |
| 2778 | A = APtr->getPointeeType(); |
| 2779 | } |
| 2780 | } |
| 2781 | } |
| 2782 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2783 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2784 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| 2785 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2786 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2787 | if (A->isRecordType() && isSimpleTemplateIdType(OriginalParamType) && |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 2788 | S.IsDerivedFrom(SourceLocation(), A, DeducedA)) |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2789 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2790 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2791 | return true; |
| 2792 | } |
| 2793 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2794 | /// \brief Finish template argument deduction for a function template, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2795 | /// checking the deduced template arguments for completeness and forming |
| 2796 | /// the function template specialization. |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2797 | /// |
| 2798 | /// \param OriginalCallArgs If non-NULL, the original call arguments against |
| 2799 | /// which the deduced argument types should be compared. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2800 | Sema::TemplateDeductionResult |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2801 | Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2802 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2803 | unsigned NumExplicitlySpecified, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2804 | FunctionDecl *&Specialization, |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2805 | TemplateDeductionInfo &Info, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 2806 | SmallVectorImpl<OriginalCallArg> const *OriginalCallArgs, |
| 2807 | bool PartialOverloading) { |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2808 | // Unevaluated SFINAE context. |
| 2809 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2810 | SFINAETrap Trap(*this); |
| 2811 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2812 | // Enter a new template instantiation context while we instantiate the |
| 2813 | // actual function declaration. |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2814 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2815 | InstantiatingTemplate Inst(*this, Info.getLocation(), FunctionTemplate, |
| 2816 | DeducedArgs, |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2817 | ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution, |
| 2818 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2819 | if (Inst.isInvalid()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2820 | return TDK_InstantiationDepth; |
| 2821 | |
John McCall | e23b871 | 2010-04-29 01:18:58 +0000 | [diff] [blame] | 2822 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
John McCall | 80e58cd | 2010-04-29 00:35:03 +0000 | [diff] [blame] | 2823 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2824 | // C++ [temp.deduct.type]p2: |
| 2825 | // [...] or if any template argument remains neither deduced nor |
| 2826 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2827 | SmallVector<TemplateArgument, 4> Builder; |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2828 | if (auto Result = ConvertDeducedTemplateArguments( |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2829 | *this, FunctionTemplate, /*IsDeduced*/true, Deduced, Info, Builder, |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2830 | CurrentInstantiationScope, NumExplicitlySpecified, |
| 2831 | PartialOverloading)) |
| 2832 | return Result; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2833 | |
| 2834 | // Form the template argument list from the deduced template arguments. |
| 2835 | TemplateArgumentList *DeducedArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2836 | = TemplateArgumentList::CreateCopy(Context, Builder); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2837 | Info.reset(DeducedArgumentList); |
| 2838 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2839 | // Substitute the deduced template arguments into the function template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2840 | // declaration to produce the function template specialization. |
Douglas Gregor | 1614237 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 2841 | DeclContext *Owner = FunctionTemplate->getDeclContext(); |
| 2842 | if (FunctionTemplate->getFriendObjectKind()) |
| 2843 | Owner = FunctionTemplate->getLexicalDeclContext(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2844 | Specialization = cast_or_null<FunctionDecl>( |
Douglas Gregor | 1614237 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 2845 | SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner, |
Douglas Gregor | 39cacdb | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 2846 | MultiLevelTemplateArgumentList(*DeducedArgumentList))); |
Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 2847 | if (!Specialization || Specialization->isInvalidDecl()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2848 | return TDK_SubstitutionFailure; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2849 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2850 | assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == |
Douglas Gregor | 31fae89 | 2009-09-15 18:26:13 +0000 | [diff] [blame] | 2851 | FunctionTemplate->getCanonicalDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2852 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2853 | // If the template argument list is owned by the function template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2854 | // specialization, release it. |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 2855 | if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList && |
| 2856 | !Trap.hasErrorOccurred()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2857 | Info.take(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2858 | |
Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 2859 | // There may have been an error that did not prevent us from constructing a |
| 2860 | // declaration. Mark the declaration invalid and return with a substitution |
| 2861 | // failure. |
| 2862 | if (Trap.hasErrorOccurred()) { |
| 2863 | Specialization->setInvalidDecl(true); |
| 2864 | return TDK_SubstitutionFailure; |
| 2865 | } |
| 2866 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2867 | if (OriginalCallArgs) { |
| 2868 | // C++ [temp.deduct.call]p4: |
| 2869 | // In general, the deduction process attempts to find template argument |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2870 | // 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] | 2871 | // is transformed as described above). [...] |
| 2872 | for (unsigned I = 0, N = OriginalCallArgs->size(); I != N; ++I) { |
| 2873 | OriginalCallArg OriginalArg = (*OriginalCallArgs)[I]; |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2874 | unsigned ParamIdx = OriginalArg.ArgIdx; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2875 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2876 | if (ParamIdx >= Specialization->getNumParams()) |
| 2877 | continue; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2878 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2879 | QualType DeducedA = Specialization->getParamDecl(ParamIdx)->getType(); |
Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 2880 | if (CheckOriginalCallArgDeduction(*this, OriginalArg, DeducedA)) { |
| 2881 | Info.FirstArg = TemplateArgument(DeducedA); |
| 2882 | Info.SecondArg = TemplateArgument(OriginalArg.OriginalArgType); |
| 2883 | Info.CallArgIndex = OriginalArg.ArgIdx; |
| 2884 | return TDK_DeducedMismatch; |
| 2885 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2886 | } |
| 2887 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2888 | |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2889 | // If we suppressed any diagnostics while performing template argument |
| 2890 | // deduction, and if we haven't already instantiated this declaration, |
| 2891 | // keep track of these diagnostics. They'll be emitted if this specialization |
| 2892 | // is actually used. |
| 2893 | if (Info.diag_begin() != Info.diag_end()) { |
Craig Topper | 79be4cd | 2013-07-05 04:33:53 +0000 | [diff] [blame] | 2894 | SuppressedDiagnosticsMap::iterator |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2895 | Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl()); |
| 2896 | if (Pos == SuppressedDiagnostics.end()) |
| 2897 | SuppressedDiagnostics[Specialization->getCanonicalDecl()] |
| 2898 | .append(Info.diag_begin(), Info.diag_end()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2899 | } |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2900 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2901 | return TDK_Success; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2904 | /// Gets the type of a function for template-argument-deducton |
| 2905 | /// purposes when it's considered as part of an overload set. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2906 | static QualType GetTypeOfFunction(Sema &S, const OverloadExpr::FindResult &R, |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2907 | FunctionDecl *Fn) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2908 | // We may need to deduce the return type of the function now. |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 2909 | if (S.getLangOpts().CPlusPlus14 && Fn->getReturnType()->isUndeducedType() && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2910 | S.DeduceReturnType(Fn, R.Expression->getExprLoc(), /*Diagnose*/ false)) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2911 | return QualType(); |
| 2912 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2913 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2914 | if (Method->isInstance()) { |
| 2915 | // An instance method that's referenced in a form that doesn't |
| 2916 | // look like a member pointer is just invalid. |
| 2917 | if (!R.HasFormOfMemberPointer) return QualType(); |
| 2918 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2919 | return S.Context.getMemberPointerType(Fn->getType(), |
| 2920 | S.Context.getTypeDeclType(Method->getParent()).getTypePtr()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
| 2923 | if (!R.IsAddressOfOperand) return Fn->getType(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2924 | return S.Context.getPointerType(Fn->getType()); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2925 | } |
| 2926 | |
| 2927 | /// Apply the deduction rules for overload sets. |
| 2928 | /// |
| 2929 | /// \return the null type if this argument should be treated as an |
| 2930 | /// undeduced context |
| 2931 | static QualType |
| 2932 | ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams, |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 2933 | Expr *Arg, QualType ParamType, |
| 2934 | bool ParamWasReference) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2935 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2936 | OverloadExpr::FindResult R = OverloadExpr::find(Arg); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2937 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2938 | OverloadExpr *Ovl = R.Expression; |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2939 | |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 2940 | // C++0x [temp.deduct.call]p4 |
| 2941 | unsigned TDF = 0; |
| 2942 | if (ParamWasReference) |
| 2943 | TDF |= TDF_ParamWithReferenceType; |
| 2944 | if (R.IsAddressOfOperand) |
| 2945 | TDF |= TDF_IgnoreQualifiers; |
| 2946 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2947 | // C++0x [temp.deduct.call]p6: |
| 2948 | // When P is a function type, pointer to function type, or pointer |
| 2949 | // to member function type: |
| 2950 | |
| 2951 | if (!ParamType->isFunctionType() && |
| 2952 | !ParamType->isFunctionPointerType() && |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 2953 | !ParamType->isMemberFunctionPointerType()) { |
| 2954 | if (Ovl->hasExplicitTemplateArgs()) { |
| 2955 | // But we can still look for an explicit specialization. |
| 2956 | if (FunctionDecl *ExplicitSpec |
| 2957 | = S.ResolveSingleFunctionTemplateSpecialization(Ovl)) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2958 | return GetTypeOfFunction(S, R, ExplicitSpec); |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 2959 | } |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2960 | |
George Burgess IV | cc2f355 | 2016-03-19 21:51:45 +0000 | [diff] [blame] | 2961 | DeclAccessPair DAP; |
| 2962 | if (FunctionDecl *Viable = |
| 2963 | S.resolveAddressOfOnlyViableOverloadCandidate(Arg, DAP)) |
| 2964 | return GetTypeOfFunction(S, R, Viable); |
| 2965 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 2966 | return QualType(); |
| 2967 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2968 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 2969 | // Gather the explicit template arguments, if any. |
| 2970 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 2971 | if (Ovl->hasExplicitTemplateArgs()) |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2972 | Ovl->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2973 | QualType Match; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 2974 | for (UnresolvedSetIterator I = Ovl->decls_begin(), |
| 2975 | E = Ovl->decls_end(); I != E; ++I) { |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2976 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 2977 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 2978 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) { |
| 2979 | // - If the argument is an overload set containing one or more |
| 2980 | // function templates, the parameter is treated as a |
| 2981 | // non-deduced context. |
| 2982 | if (!Ovl->hasExplicitTemplateArgs()) |
| 2983 | return QualType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2984 | |
| 2985 | // Otherwise, see if we can resolve a function type |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2986 | FunctionDecl *Specialization = nullptr; |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 2987 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 2988 | if (S.DeduceTemplateArguments(FunTmpl, &ExplicitTemplateArgs, |
| 2989 | Specialization, Info)) |
| 2990 | continue; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2991 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 2992 | D = Specialization; |
| 2993 | } |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2994 | |
| 2995 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2996 | QualType ArgType = GetTypeOfFunction(S, R, Fn); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2997 | if (ArgType.isNull()) continue; |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2998 | |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 2999 | // Function-to-pointer conversion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3000 | if (!ParamWasReference && ParamType->isPointerType() && |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3001 | ArgType->isFunctionType()) |
| 3002 | ArgType = S.Context.getPointerType(ArgType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3003 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3004 | // - If the argument is an overload set (not containing function |
| 3005 | // templates), trial argument deduction is attempted using each |
| 3006 | // of the members of the set. If deduction succeeds for only one |
| 3007 | // of the overload set members, that member is used as the |
| 3008 | // argument value for the deduction. If deduction succeeds for |
| 3009 | // more than one member of the overload set the parameter is |
| 3010 | // treated as a non-deduced context. |
| 3011 | |
| 3012 | // We do all of this in a fresh context per C++0x [temp.deduct.type]p2: |
| 3013 | // Type deduction is done independently for each P/A pair, and |
| 3014 | // the deduced template argument values are then combined. |
| 3015 | // So we do not reject deductions which were made elsewhere. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3016 | SmallVector<DeducedTemplateArgument, 8> |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3017 | Deduced(TemplateParams->size()); |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3018 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3019 | Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3020 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
| 3021 | ArgType, Info, Deduced, TDF); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3022 | if (Result) continue; |
| 3023 | if (!Match.isNull()) return QualType(); |
| 3024 | Match = ArgType; |
| 3025 | } |
| 3026 | |
| 3027 | return Match; |
| 3028 | } |
| 3029 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3030 | /// \brief Perform the adjustments to the parameter and argument types |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3031 | /// described in C++ [temp.deduct.call]. |
| 3032 | /// |
| 3033 | /// \returns true if the caller should not attempt to perform any template |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3034 | /// argument deduction based on this P/A pair because the argument is an |
| 3035 | /// overloaded function set that could not be resolved. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3036 | static bool AdjustFunctionParmAndArgTypesForDeduction(Sema &S, |
| 3037 | TemplateParameterList *TemplateParams, |
| 3038 | QualType &ParamType, |
| 3039 | QualType &ArgType, |
| 3040 | Expr *Arg, |
| 3041 | unsigned &TDF) { |
| 3042 | // C++0x [temp.deduct.call]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3043 | // 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] | 3044 | // are ignored for type deduction. |
Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3045 | if (ParamType.hasQualifiers()) |
| 3046 | ParamType = ParamType.getUnqualifiedType(); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3047 | |
| 3048 | // [...] If P is a reference type, the type referred to by P is |
| 3049 | // used for type deduction. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3050 | const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>(); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3051 | if (ParamRefType) |
| 3052 | ParamType = ParamRefType->getPointeeType(); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3053 | |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3054 | // Overload sets usually make this parameter an undeduced context, |
| 3055 | // but there are sometimes special circumstances. Typically |
| 3056 | // involving a template-id-expr. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3057 | if (ArgType == S.Context.OverloadTy) { |
| 3058 | ArgType = ResolveOverloadForDeduction(S, TemplateParams, |
| 3059 | Arg, ParamType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3060 | ParamRefType != nullptr); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3061 | if (ArgType.isNull()) |
| 3062 | return true; |
| 3063 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3064 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3065 | if (ParamRefType) { |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3066 | // If the argument has incomplete array type, try to complete its type. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3067 | if (ArgType->isIncompleteArrayType()) { |
| 3068 | S.completeExprArrayBound(Arg); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3069 | ArgType = Arg->getType(); |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3070 | } |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3071 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3072 | // C++0x [temp.deduct.call]p3: |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3073 | // If P is an rvalue reference to a cv-unqualified template |
| 3074 | // parameter and the argument is an lvalue, the type "lvalue |
| 3075 | // reference to A" is used in place of A for type deduction. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3076 | if (ParamRefType->isRValueReferenceType() && |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3077 | !ParamType.getQualifiers() && |
| 3078 | isa<TemplateTypeParmType>(ParamType) && |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3079 | Arg->isLValue()) |
| 3080 | ArgType = S.Context.getLValueReferenceType(ArgType); |
| 3081 | } else { |
| 3082 | // C++ [temp.deduct.call]p2: |
| 3083 | // If P is not a reference type: |
| 3084 | // - If A is an array type, the pointer type produced by the |
| 3085 | // array-to-pointer standard conversion (4.2) is used in place of |
| 3086 | // A for type deduction; otherwise, |
| 3087 | if (ArgType->isArrayType()) |
| 3088 | ArgType = S.Context.getArrayDecayedType(ArgType); |
| 3089 | // - If A is a function type, the pointer type produced by the |
| 3090 | // function-to-pointer standard conversion (4.3) is used in place |
| 3091 | // of A for type deduction; otherwise, |
| 3092 | else if (ArgType->isFunctionType()) |
| 3093 | ArgType = S.Context.getPointerType(ArgType); |
| 3094 | else { |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3095 | // - 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] | 3096 | // type are ignored for type deduction. |
Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3097 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3098 | } |
| 3099 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3100 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3101 | // C++0x [temp.deduct.call]p4: |
| 3102 | // In general, the deduction process attempts to find template argument |
| 3103 | // values that will make the deduced A identical to A (after the type A |
| 3104 | // is transformed as described above). [...] |
| 3105 | TDF = TDF_SkipNonDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3106 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3107 | // - If the original P is a reference type, the deduced A (i.e., the |
| 3108 | // type referred to by the reference) can be more cv-qualified than |
| 3109 | // the transformed A. |
| 3110 | if (ParamRefType) |
| 3111 | TDF |= TDF_ParamWithReferenceType; |
| 3112 | // - The transformed A can be another pointer or pointer to member |
| 3113 | // type that can be converted to the deduced A via a qualification |
| 3114 | // conversion (4.4). |
| 3115 | if (ArgType->isPointerType() || ArgType->isMemberPointerType() || |
| 3116 | ArgType->isObjCObjectPointerType()) |
| 3117 | TDF |= TDF_IgnoreQualifiers; |
| 3118 | // - If P is a class and P has the form simple-template-id, then the |
| 3119 | // transformed A can be a derived class of the deduced A. Likewise, |
| 3120 | // if P is a pointer to a class of the form simple-template-id, the |
| 3121 | // transformed A can be a pointer to a derived class pointed to by |
| 3122 | // the deduced A. |
| 3123 | if (isSimpleTemplateIdType(ParamType) || |
| 3124 | (isa<PointerType>(ParamType) && |
| 3125 | isSimpleTemplateIdType( |
| 3126 | ParamType->getAs<PointerType>()->getPointeeType()))) |
| 3127 | TDF |= TDF_DerivedClass; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3128 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3129 | return false; |
| 3130 | } |
| 3131 | |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3132 | static bool |
| 3133 | hasDeducibleTemplateParameters(Sema &S, FunctionTemplateDecl *FunctionTemplate, |
| 3134 | QualType T); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3135 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3136 | static Sema::TemplateDeductionResult DeduceTemplateArgumentByListElement( |
| 3137 | Sema &S, TemplateParameterList *TemplateParams, QualType ParamType, |
| 3138 | Expr *Arg, TemplateDeductionInfo &Info, |
| 3139 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, unsigned TDF); |
| 3140 | |
| 3141 | /// \brief Attempt template argument deduction from an initializer list |
| 3142 | /// deemed to be an argument in a function call. |
| 3143 | static bool |
| 3144 | DeduceFromInitializerList(Sema &S, TemplateParameterList *TemplateParams, |
| 3145 | QualType AdjustedParamType, InitListExpr *ILE, |
| 3146 | TemplateDeductionInfo &Info, |
| 3147 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3148 | unsigned TDF, Sema::TemplateDeductionResult &Result) { |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3149 | |
| 3150 | // [temp.deduct.call] p1 (post CWG-1591) |
| 3151 | // If removing references and cv-qualifiers from P gives |
| 3152 | // std::initializer_list<P0> or P0[N] for some P0 and N and the argument is a |
| 3153 | // non-empty initializer list (8.5.4), then deduction is performed instead for |
| 3154 | // each element of the initializer list, taking P0 as a function template |
| 3155 | // parameter type and the initializer element as its argument, and in the |
| 3156 | // P0[N] case, if N is a non-type template parameter, N is deduced from the |
| 3157 | // length of the initializer list. Otherwise, an initializer list argument |
| 3158 | // causes the parameter to be considered a non-deduced context |
| 3159 | |
| 3160 | const bool IsConstSizedArray = AdjustedParamType->isConstantArrayType(); |
| 3161 | |
| 3162 | const bool IsDependentSizedArray = |
| 3163 | !IsConstSizedArray && AdjustedParamType->isDependentSizedArrayType(); |
| 3164 | |
Faisal Vali | dd76cc1 | 2015-12-10 12:29:11 +0000 | [diff] [blame] | 3165 | QualType ElTy; // The element type of the std::initializer_list or the array. |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3166 | |
| 3167 | const bool IsSTDList = !IsConstSizedArray && !IsDependentSizedArray && |
| 3168 | S.isStdInitializerList(AdjustedParamType, &ElTy); |
| 3169 | |
| 3170 | if (!IsConstSizedArray && !IsDependentSizedArray && !IsSTDList) |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3171 | return false; |
| 3172 | |
| 3173 | Result = Sema::TDK_Success; |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3174 | // If we are not deducing against the 'T' in a std::initializer_list<T> then |
| 3175 | // deduce against the 'T' in T[N]. |
| 3176 | if (ElTy.isNull()) { |
| 3177 | assert(!IsSTDList); |
| 3178 | ElTy = S.Context.getAsArrayType(AdjustedParamType)->getElementType(); |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3179 | } |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3180 | // Deduction only needs to be done for dependent types. |
| 3181 | if (ElTy->isDependentType()) { |
| 3182 | for (Expr *E : ILE->inits()) { |
Craig Topper | 0852953 | 2015-12-10 08:49:55 +0000 | [diff] [blame] | 3183 | if ((Result = DeduceTemplateArgumentByListElement(S, TemplateParams, ElTy, |
| 3184 | E, Info, Deduced, TDF))) |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3185 | return true; |
| 3186 | } |
| 3187 | } |
| 3188 | if (IsDependentSizedArray) { |
| 3189 | const DependentSizedArrayType *ArrTy = |
| 3190 | S.Context.getAsDependentSizedArrayType(AdjustedParamType); |
| 3191 | // Determine the array bound is something we can deduce. |
| 3192 | if (NonTypeTemplateParmDecl *NTTP = |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 3193 | getDeducedParameterFromExpr(Info, ArrTy->getSizeExpr())) { |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3194 | // We can perform template argument deduction for the given non-type |
| 3195 | // template parameter. |
| 3196 | assert(NTTP->getDepth() == 0 && |
| 3197 | "Cannot deduce non-type template argument at depth > 0"); |
| 3198 | llvm::APInt Size(S.Context.getIntWidth(NTTP->getType()), |
| 3199 | ILE->getNumInits()); |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3200 | |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3201 | Result = DeduceNonTypeTemplateArgument( |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 3202 | S, TemplateParams, NTTP, llvm::APSInt(Size), NTTP->getType(), |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3203 | /*ArrayBound=*/true, Info, Deduced); |
| 3204 | } |
| 3205 | } |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3206 | return true; |
| 3207 | } |
| 3208 | |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3209 | /// \brief Perform template argument deduction by matching a parameter type |
| 3210 | /// against a single expression, where the expression is an element of |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3211 | /// an initializer list that was originally matched against a parameter |
| 3212 | /// of type \c initializer_list\<ParamType\>. |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3213 | static Sema::TemplateDeductionResult |
| 3214 | DeduceTemplateArgumentByListElement(Sema &S, |
| 3215 | TemplateParameterList *TemplateParams, |
| 3216 | QualType ParamType, Expr *Arg, |
| 3217 | TemplateDeductionInfo &Info, |
| 3218 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3219 | unsigned TDF) { |
| 3220 | // Handle the case where an init list contains another init list as the |
| 3221 | // element. |
| 3222 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3223 | Sema::TemplateDeductionResult Result; |
| 3224 | if (!DeduceFromInitializerList(S, TemplateParams, |
| 3225 | ParamType.getNonReferenceType(), ILE, Info, |
| 3226 | Deduced, TDF, Result)) |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3227 | return Sema::TDK_Success; // Just ignore this expression. |
| 3228 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3229 | return Result; |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3230 | } |
| 3231 | |
| 3232 | // For all other cases, just match by type. |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3233 | QualType ArgType = Arg->getType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3234 | if (AdjustFunctionParmAndArgTypesForDeduction(S, TemplateParams, ParamType, |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3235 | ArgType, Arg, TDF)) { |
| 3236 | Info.Expression = Arg; |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3237 | return Sema::TDK_FailedOverloadResolution; |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3238 | } |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3239 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3240 | ArgType, Info, Deduced, TDF); |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3241 | } |
| 3242 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3243 | /// \brief Perform template argument deduction from a function call |
| 3244 | /// (C++ [temp.deduct.call]). |
| 3245 | /// |
| 3246 | /// \param FunctionTemplate the function template for which we are performing |
| 3247 | /// template argument deduction. |
| 3248 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3249 | /// \param ExplicitTemplateArgs the explicit template arguments provided |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 3250 | /// for this call. |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3251 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3252 | /// \param Args the function call arguments |
| 3253 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3254 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3255 | /// this will be set to the function template specialization produced by |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3256 | /// template argument deduction. |
| 3257 | /// |
| 3258 | /// \param Info the argument will be updated to provide additional information |
| 3259 | /// about template argument deduction. |
| 3260 | /// |
| 3261 | /// \returns the result of template argument deduction. |
Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 3262 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3263 | FunctionTemplateDecl *FunctionTemplate, |
| 3264 | TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3265 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 3266 | bool PartialOverloading) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3267 | if (FunctionTemplate->isInvalidDecl()) |
| 3268 | return TDK_Invalid; |
| 3269 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3270 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3271 | unsigned NumParams = Function->getNumParams(); |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3272 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3273 | // C++ [temp.deduct.call]p1: |
| 3274 | // Template argument deduction is done by comparing each function template |
| 3275 | // parameter type (call it P) with the type of the corresponding argument |
| 3276 | // of the call (call it A) as described below. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3277 | unsigned CheckArgs = Args.size(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3278 | if (Args.size() < Function->getMinRequiredArguments() && !PartialOverloading) |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3279 | return TDK_TooFewArguments; |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3280 | else if (TooManyArguments(NumParams, Args.size(), PartialOverloading)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3281 | const FunctionProtoType *Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3282 | = Function->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3283 | if (Proto->isTemplateVariadic()) |
| 3284 | /* Do nothing */; |
| 3285 | else if (Proto->isVariadic()) |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3286 | CheckArgs = NumParams; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3287 | else |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3288 | return TDK_TooManyArguments; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3289 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3290 | |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3291 | // The types of the parameters from which we will perform template argument |
| 3292 | // deduction. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3293 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3294 | TemplateParameterList *TemplateParams |
| 3295 | = FunctionTemplate->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3296 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 3297 | SmallVector<QualType, 4> ParamTypes; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3298 | unsigned NumExplicitlySpecified = 0; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3299 | if (ExplicitTemplateArgs) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3300 | TemplateDeductionResult Result = |
| 3301 | SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3302 | *ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3303 | Deduced, |
| 3304 | ParamTypes, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3305 | nullptr, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3306 | Info); |
| 3307 | if (Result) |
| 3308 | return Result; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3309 | |
| 3310 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3311 | } else { |
| 3312 | // Just fill in the parameter types from the function declaration. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3313 | for (unsigned I = 0; I != NumParams; ++I) |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3314 | ParamTypes.push_back(Function->getParamDecl(I)->getType()); |
| 3315 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3316 | |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3317 | // Deduce template arguments from the function parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3318 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3319 | unsigned ArgIdx = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3320 | SmallVector<OriginalCallArg, 4> OriginalCallArgs; |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3321 | for (unsigned ParamIdx = 0, NumParamTypes = ParamTypes.size(); |
| 3322 | ParamIdx != NumParamTypes; ++ParamIdx) { |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3323 | QualType OrigParamType = ParamTypes[ParamIdx]; |
| 3324 | QualType ParamType = OrigParamType; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3325 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3326 | const PackExpansionType *ParamExpansion |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3327 | = dyn_cast<PackExpansionType>(ParamType); |
| 3328 | if (!ParamExpansion) { |
| 3329 | // Simple case: matching a function parameter to a function argument. |
| 3330 | if (ArgIdx >= CheckArgs) |
| 3331 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3332 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3333 | Expr *Arg = Args[ArgIdx++]; |
| 3334 | QualType ArgType = Arg->getType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3335 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3336 | unsigned TDF = 0; |
| 3337 | if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams, |
| 3338 | ParamType, ArgType, Arg, |
| 3339 | TDF)) |
| 3340 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3341 | |
Douglas Gregor | 0c83c81 | 2011-10-09 22:06:46 +0000 | [diff] [blame] | 3342 | // If we have nothing to deduce, we're done. |
| 3343 | if (!hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
| 3344 | continue; |
| 3345 | |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3346 | // If the argument is an initializer list ... |
| 3347 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3348 | TemplateDeductionResult Result; |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3349 | // Removing references was already done. |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3350 | if (!DeduceFromInitializerList(*this, TemplateParams, ParamType, ILE, |
| 3351 | Info, Deduced, TDF, Result)) |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3352 | continue; |
| 3353 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3354 | if (Result) |
| 3355 | return Result; |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3356 | // Don't track the argument type, since an initializer list has none. |
| 3357 | continue; |
| 3358 | } |
| 3359 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3360 | // Keep track of the argument type and corresponding parameter index, |
| 3361 | // so we can check for compatibility between the deduced A and A. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3362 | OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx-1, |
Douglas Gregor | 0c83c81 | 2011-10-09 22:06:46 +0000 | [diff] [blame] | 3363 | ArgType)); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3364 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3365 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3366 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3367 | ParamType, ArgType, |
| 3368 | Info, Deduced, TDF)) |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3369 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3370 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3371 | continue; |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3372 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3373 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3374 | // C++0x [temp.deduct.call]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3375 | // For a function parameter pack that occurs at the end of the |
| 3376 | // parameter-declaration-list, the type A of each remaining argument of |
| 3377 | // the call is compared with the type P of the declarator-id of the |
| 3378 | // function parameter pack. Each comparison deduces template arguments |
| 3379 | // for subsequent positions in the template parameter packs expanded by |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3380 | // the function parameter pack. For a function parameter pack that does |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3381 | // not occur at the end of the parameter-declaration-list, the type of |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3382 | // the parameter pack is a non-deduced context. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3383 | if (ParamIdx + 1 < NumParamTypes) |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3384 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3385 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3386 | QualType ParamPattern = ParamExpansion->getPattern(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3387 | PackDeductionScope PackScope(*this, TemplateParams, Deduced, Info, |
| 3388 | ParamPattern); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3389 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3390 | bool HasAnyArguments = false; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3391 | for (; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3392 | HasAnyArguments = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3393 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3394 | QualType OrigParamType = ParamPattern; |
| 3395 | ParamType = OrigParamType; |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3396 | Expr *Arg = Args[ArgIdx]; |
| 3397 | QualType ArgType = Arg->getType(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3398 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3399 | unsigned TDF = 0; |
| 3400 | if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams, |
| 3401 | ParamType, ArgType, Arg, |
| 3402 | TDF)) { |
| 3403 | // We can't actually perform any deduction for this argument, so stop |
| 3404 | // deduction at this point. |
| 3405 | ++ArgIdx; |
| 3406 | break; |
| 3407 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3408 | |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3409 | // As above, initializer lists need special handling. |
| 3410 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3411 | TemplateDeductionResult Result; |
| 3412 | if (!DeduceFromInitializerList(*this, TemplateParams, ParamType, ILE, |
| 3413 | Info, Deduced, TDF, Result)) { |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3414 | ++ArgIdx; |
| 3415 | break; |
| 3416 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3417 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3418 | if (Result) |
| 3419 | return Result; |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3420 | } else { |
| 3421 | |
| 3422 | // Keep track of the argument type and corresponding argument index, |
| 3423 | // so we can check for compatibility between the deduced A and A. |
| 3424 | if (hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3425 | OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx, |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3426 | ArgType)); |
| 3427 | |
| 3428 | if (TemplateDeductionResult Result |
| 3429 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3430 | ParamType, ArgType, Info, |
| 3431 | Deduced, TDF)) |
| 3432 | return Result; |
| 3433 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3434 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3435 | PackScope.nextPackElement(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3436 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3437 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3438 | // Build argument packs for each of the parameter packs expanded by this |
| 3439 | // pack expansion. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3440 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3441 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3442 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3443 | // After we've matching against a parameter pack, we're done. |
| 3444 | break; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3445 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3446 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3447 | return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3448 | NumExplicitlySpecified, Specialization, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3449 | Info, &OriginalCallArgs, |
| 3450 | PartialOverloading); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3451 | } |
| 3452 | |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3453 | QualType Sema::adjustCCAndNoReturn(QualType ArgFunctionType, |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3454 | QualType FunctionType, |
| 3455 | bool AdjustExceptionSpec) { |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3456 | if (ArgFunctionType.isNull()) |
| 3457 | return ArgFunctionType; |
| 3458 | |
| 3459 | const FunctionProtoType *FunctionTypeP = |
| 3460 | FunctionType->castAs<FunctionProtoType>(); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3461 | const FunctionProtoType *ArgFunctionTypeP = |
| 3462 | ArgFunctionType->getAs<FunctionProtoType>(); |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3463 | |
| 3464 | FunctionProtoType::ExtProtoInfo EPI = ArgFunctionTypeP->getExtProtoInfo(); |
| 3465 | bool Rebuild = false; |
| 3466 | |
| 3467 | CallingConv CC = FunctionTypeP->getCallConv(); |
| 3468 | if (EPI.ExtInfo.getCC() != CC) { |
| 3469 | EPI.ExtInfo = EPI.ExtInfo.withCallingConv(CC); |
| 3470 | Rebuild = true; |
| 3471 | } |
| 3472 | |
| 3473 | bool NoReturn = FunctionTypeP->getNoReturnAttr(); |
| 3474 | if (EPI.ExtInfo.getNoReturn() != NoReturn) { |
| 3475 | EPI.ExtInfo = EPI.ExtInfo.withNoReturn(NoReturn); |
| 3476 | Rebuild = true; |
| 3477 | } |
| 3478 | |
| 3479 | if (AdjustExceptionSpec && (FunctionTypeP->hasExceptionSpec() || |
| 3480 | ArgFunctionTypeP->hasExceptionSpec())) { |
| 3481 | EPI.ExceptionSpec = FunctionTypeP->getExtProtoInfo().ExceptionSpec; |
| 3482 | Rebuild = true; |
| 3483 | } |
| 3484 | |
| 3485 | if (!Rebuild) |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3486 | return ArgFunctionType; |
| 3487 | |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3488 | return Context.getFunctionType(ArgFunctionTypeP->getReturnType(), |
| 3489 | ArgFunctionTypeP->getParamTypes(), EPI); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3490 | } |
| 3491 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3492 | /// \brief Deduce template arguments when taking the address of a function |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3493 | /// template (C++ [temp.deduct.funcaddr]) or matching a specialization to |
| 3494 | /// a template. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3495 | /// |
| 3496 | /// \param FunctionTemplate the function template for which we are performing |
| 3497 | /// template argument deduction. |
| 3498 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3499 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3500 | /// arguments. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3501 | /// |
| 3502 | /// \param ArgFunctionType the function type that will be used as the |
| 3503 | /// "argument" type (A) when performing template argument deduction from the |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3504 | /// function template's function type. This type may be NULL, if there is no |
| 3505 | /// argument type to compare against, in C++0x [temp.arg.explicit]p3. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3506 | /// |
| 3507 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3508 | /// this will be set to the function template specialization produced by |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3509 | /// template argument deduction. |
| 3510 | /// |
| 3511 | /// \param Info the argument will be updated to provide additional information |
| 3512 | /// about template argument deduction. |
| 3513 | /// |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3514 | /// \param IsAddressOfFunction If \c true, we are deducing as part of taking |
| 3515 | /// the address of a function template per [temp.deduct.funcaddr] and |
| 3516 | /// [over.over]. If \c false, we are looking up a function template |
| 3517 | /// specialization based on its signature, per [temp.deduct.decl]. |
| 3518 | /// |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3519 | /// \returns the result of template argument deduction. |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3520 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3521 | FunctionTemplateDecl *FunctionTemplate, |
| 3522 | TemplateArgumentListInfo *ExplicitTemplateArgs, QualType ArgFunctionType, |
| 3523 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 3524 | bool IsAddressOfFunction) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3525 | if (FunctionTemplate->isInvalidDecl()) |
| 3526 | return TDK_Invalid; |
| 3527 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3528 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 3529 | TemplateParameterList *TemplateParams |
| 3530 | = FunctionTemplate->getTemplateParameters(); |
| 3531 | QualType FunctionType = Function->getType(); |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3532 | |
| 3533 | // When taking the address of a function, we require convertibility of |
| 3534 | // the resulting function type. Otherwise, we allow arbitrary mismatches |
| 3535 | // of calling convention, noreturn, and noexcept. |
| 3536 | if (!IsAddressOfFunction) |
| 3537 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, FunctionType, |
| 3538 | /*AdjustExceptionSpec*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3539 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3540 | // Substitute any explicit template arguments. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3541 | LocalInstantiationScope InstScope(*this); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3542 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3543 | unsigned NumExplicitlySpecified = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3544 | SmallVector<QualType, 4> ParamTypes; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3545 | if (ExplicitTemplateArgs) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3546 | if (TemplateDeductionResult Result |
| 3547 | = SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3548 | *ExplicitTemplateArgs, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3549 | Deduced, ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3550 | &FunctionType, Info)) |
| 3551 | return Result; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3552 | |
| 3553 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3554 | } |
| 3555 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3556 | // Unevaluated SFINAE context. |
| 3557 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3558 | SFINAETrap Trap(*this); |
| 3559 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3560 | Deduced.resize(TemplateParams->size()); |
| 3561 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3562 | // 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] | 3563 | // type so that we treat it as a non-deduced context in what follows. If we |
| 3564 | // are looking up by signature, the signature type should also have a deduced |
| 3565 | // return type, which we instead expect to exactly match. |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3566 | bool HasDeducedReturnType = false; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3567 | if (getLangOpts().CPlusPlus14 && IsAddressOfFunction && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3568 | Function->getReturnType()->getContainedAutoType()) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3569 | FunctionType = SubstAutoType(FunctionType, Context.DependentTy); |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3570 | HasDeducedReturnType = true; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3571 | } |
| 3572 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3573 | if (!ArgFunctionType.isNull()) { |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3574 | unsigned TDF = TDF_TopLevelParameterTypeList; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3575 | if (IsAddressOfFunction) |
| 3576 | TDF |= TDF_InOverloadResolution; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3577 | // Deduce template arguments from the function type. |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3578 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3579 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3580 | FunctionType, ArgFunctionType, |
| 3581 | Info, Deduced, TDF)) |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3582 | return Result; |
| 3583 | } |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3584 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3585 | if (TemplateDeductionResult Result |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3586 | = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
| 3587 | NumExplicitlySpecified, |
| 3588 | Specialization, Info)) |
| 3589 | return Result; |
| 3590 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3591 | // If the function has a deduced return type, deduce it now, so we can check |
| 3592 | // that the deduced function type matches the requested type. |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3593 | if (HasDeducedReturnType && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3594 | Specialization->getReturnType()->isUndeducedType() && |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3595 | DeduceReturnType(Specialization, Info.getLocation(), false)) |
| 3596 | return TDK_MiscellaneousDeductionFailure; |
| 3597 | |
Richard Smith | 9095e5b | 2016-11-01 01:31:23 +0000 | [diff] [blame] | 3598 | // If the function has a dependent exception specification, resolve it now, |
| 3599 | // so we can check that the exception specification matches. |
| 3600 | auto *SpecializationFPT = |
| 3601 | Specialization->getType()->castAs<FunctionProtoType>(); |
| 3602 | if (getLangOpts().CPlusPlus1z && |
| 3603 | isUnresolvedExceptionSpec(SpecializationFPT->getExceptionSpecType()) && |
| 3604 | !ResolveExceptionSpec(Info.getLocation(), SpecializationFPT)) |
| 3605 | return TDK_MiscellaneousDeductionFailure; |
| 3606 | |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3607 | // Adjust the exception specification of the argument again to match the |
| 3608 | // substituted and resolved type we just formed. (Calling convention and |
| 3609 | // noreturn can't be dependent, so we don't actually need this for them |
| 3610 | // right now.) |
| 3611 | QualType SpecializationType = Specialization->getType(); |
| 3612 | if (!IsAddressOfFunction) |
| 3613 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, SpecializationType, |
| 3614 | /*AdjustExceptionSpec*/true); |
| 3615 | |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3616 | // 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] | 3617 | // specialization with respect to arguments of compatible pointer to function |
| 3618 | // types, template argument deduction fails. |
| 3619 | if (!ArgFunctionType.isNull()) { |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3620 | if (IsAddressOfFunction && |
| 3621 | !isSameOrCompatibleFunctionType( |
| 3622 | Context.getCanonicalType(SpecializationType), |
| 3623 | Context.getCanonicalType(ArgFunctionType))) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3624 | return TDK_MiscellaneousDeductionFailure; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3625 | |
| 3626 | if (!IsAddressOfFunction && |
| 3627 | !Context.hasSameType(SpecializationType, ArgFunctionType)) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3628 | return TDK_MiscellaneousDeductionFailure; |
| 3629 | } |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3630 | |
| 3631 | return TDK_Success; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3632 | } |
| 3633 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3634 | /// \brief Given a function declaration (e.g. a generic lambda conversion |
| 3635 | /// function) that contains an 'auto' in its result type, substitute it |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3636 | /// with TypeToReplaceAutoWith. Be careful to pass in the type you want |
| 3637 | /// to replace 'auto' with and not the actual result type you want |
| 3638 | /// to set the function to. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3639 | static inline void |
| 3640 | SubstAutoWithinFunctionReturnType(FunctionDecl *F, |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3641 | QualType TypeToReplaceAutoWith, Sema &S) { |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3642 | assert(!TypeToReplaceAutoWith->getContainedAutoType()); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3643 | QualType AutoResultType = F->getReturnType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3644 | assert(AutoResultType->getContainedAutoType()); |
| 3645 | QualType DeducedResultType = S.SubstAutoType(AutoResultType, |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3646 | TypeToReplaceAutoWith); |
| 3647 | S.Context.adjustDeducedFunctionResultType(F, DeducedResultType); |
| 3648 | } |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3649 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3650 | /// \brief Given a specialized conversion operator of a generic lambda |
| 3651 | /// create the corresponding specializations of the call operator and |
| 3652 | /// the static-invoker. If the return type of the call operator is auto, |
| 3653 | /// deduce its return type and check if that matches the |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3654 | /// return type of the destination function ptr. |
| 3655 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3656 | static inline Sema::TemplateDeductionResult |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3657 | SpecializeCorrespondingLambdaCallOperatorAndInvoker( |
| 3658 | CXXConversionDecl *ConversionSpecialized, |
| 3659 | SmallVectorImpl<DeducedTemplateArgument> &DeducedArguments, |
| 3660 | QualType ReturnTypeOfDestFunctionPtr, |
| 3661 | TemplateDeductionInfo &TDInfo, |
| 3662 | Sema &S) { |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3663 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3664 | CXXRecordDecl *LambdaClass = ConversionSpecialized->getParent(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3665 | assert(LambdaClass && LambdaClass->isGenericLambda()); |
| 3666 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3667 | CXXMethodDecl *CallOpGeneric = LambdaClass->getLambdaCallOperator(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3668 | QualType CallOpResultType = CallOpGeneric->getReturnType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3669 | const bool GenericLambdaCallOperatorHasDeducedReturnType = |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3670 | CallOpResultType->getContainedAutoType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3671 | |
| 3672 | FunctionTemplateDecl *CallOpTemplate = |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3673 | CallOpGeneric->getDescribedFunctionTemplate(); |
| 3674 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3675 | FunctionDecl *CallOpSpecialized = nullptr; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3676 | // Use the deduced arguments of the conversion function, to specialize our |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3677 | // generic lambda's call operator. |
| 3678 | if (Sema::TemplateDeductionResult Result |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3679 | = S.FinishTemplateArgumentDeduction(CallOpTemplate, |
| 3680 | DeducedArguments, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3681 | 0, CallOpSpecialized, TDInfo)) |
| 3682 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3683 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3684 | // 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] | 3685 | if (GenericLambdaCallOperatorHasDeducedReturnType && |
| 3686 | CallOpSpecialized->getReturnType()->isUndeducedType()) |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3687 | S.DeduceReturnType(CallOpSpecialized, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3688 | CallOpSpecialized->getPointOfInstantiation(), |
| 3689 | /*Diagnose*/ true); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3690 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3691 | // Check to see if the return type of the destination ptr-to-function |
| 3692 | // matches the return type of the call operator. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3693 | if (!S.Context.hasSameType(CallOpSpecialized->getReturnType(), |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3694 | ReturnTypeOfDestFunctionPtr)) |
| 3695 | return Sema::TDK_NonDeducedMismatch; |
| 3696 | // Since we have succeeded in matching the source and destination |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3697 | // ptr-to-functions (now including return type), and have successfully |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3698 | // specialized our corresponding call operator, we are ready to |
| 3699 | // specialize the static invoker with the deduced arguments of our |
| 3700 | // ptr-to-function. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3701 | FunctionDecl *InvokerSpecialized = nullptr; |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3702 | FunctionTemplateDecl *InvokerTemplate = LambdaClass-> |
| 3703 | getLambdaStaticInvoker()->getDescribedFunctionTemplate(); |
| 3704 | |
Yaron Keren | f428fcf | 2015-05-13 17:56:46 +0000 | [diff] [blame] | 3705 | #ifndef NDEBUG |
| 3706 | Sema::TemplateDeductionResult LLVM_ATTRIBUTE_UNUSED Result = |
| 3707 | #endif |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3708 | S.FinishTemplateArgumentDeduction(InvokerTemplate, DeducedArguments, 0, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3709 | InvokerSpecialized, TDInfo); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3710 | assert(Result == Sema::TDK_Success && |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3711 | "If the call operator succeeded so should the invoker!"); |
| 3712 | // Set the result type to match the corresponding call operator |
| 3713 | // specialization's result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3714 | if (GenericLambdaCallOperatorHasDeducedReturnType && |
| 3715 | InvokerSpecialized->getReturnType()->isUndeducedType()) { |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3716 | // Be sure to get the type to replace 'auto' with and not |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3717 | // the full result type of the call op specialization |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3718 | // to substitute into the 'auto' of the invoker and conversion |
| 3719 | // function. |
| 3720 | // For e.g. |
| 3721 | // int* (*fp)(int*) = [](auto* a) -> auto* { return a; }; |
| 3722 | // We don't want to subst 'int*' into 'auto' to get int**. |
| 3723 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3724 | QualType TypeToReplaceAutoWith = CallOpSpecialized->getReturnType() |
| 3725 | ->getContainedAutoType() |
| 3726 | ->getDeducedType(); |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3727 | SubstAutoWithinFunctionReturnType(InvokerSpecialized, |
| 3728 | TypeToReplaceAutoWith, S); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3729 | SubstAutoWithinFunctionReturnType(ConversionSpecialized, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3730 | TypeToReplaceAutoWith, S); |
| 3731 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3732 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3733 | // Ensure that static invoker doesn't have a const qualifier. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3734 | // FIXME: When creating the InvokerTemplate in SemaLambda.cpp |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3735 | // do not use the CallOperator's TypeSourceInfo which allows |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3736 | // the const qualifier to leak through. |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3737 | const FunctionProtoType *InvokerFPT = InvokerSpecialized-> |
| 3738 | getType().getTypePtr()->castAs<FunctionProtoType>(); |
| 3739 | FunctionProtoType::ExtProtoInfo EPI = InvokerFPT->getExtProtoInfo(); |
| 3740 | EPI.TypeQuals = 0; |
| 3741 | InvokerSpecialized->setType(S.Context.getFunctionType( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3742 | InvokerFPT->getReturnType(), InvokerFPT->getParamTypes(), EPI)); |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3743 | return Sema::TDK_Success; |
| 3744 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3745 | /// \brief Deduce template arguments for a templated conversion |
| 3746 | /// function (C++ [temp.deduct.conv]) and, if successful, produce a |
| 3747 | /// conversion function template specialization. |
| 3748 | Sema::TemplateDeductionResult |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3749 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *ConversionTemplate, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3750 | QualType ToType, |
| 3751 | CXXConversionDecl *&Specialization, |
| 3752 | TemplateDeductionInfo &Info) { |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3753 | if (ConversionTemplate->isInvalidDecl()) |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3754 | return TDK_Invalid; |
| 3755 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3756 | CXXConversionDecl *ConversionGeneric |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3757 | = cast<CXXConversionDecl>(ConversionTemplate->getTemplatedDecl()); |
| 3758 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3759 | QualType FromType = ConversionGeneric->getConversionType(); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3760 | |
| 3761 | // Canonicalize the types for deduction. |
| 3762 | QualType P = Context.getCanonicalType(FromType); |
| 3763 | QualType A = Context.getCanonicalType(ToType); |
| 3764 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3765 | // C++0x [temp.deduct.conv]p2: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3766 | // If P is a reference type, the type referred to by P is used for |
| 3767 | // type deduction. |
| 3768 | if (const ReferenceType *PRef = P->getAs<ReferenceType>()) |
| 3769 | P = PRef->getPointeeType(); |
| 3770 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3771 | // C++0x [temp.deduct.conv]p4: |
| 3772 | // [...] 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] | 3773 | // for type deduction. |
| 3774 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3775 | A = ARef->getPointeeType().getUnqualifiedType(); |
| 3776 | // C++ [temp.deduct.conv]p3: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3777 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3778 | // If A is not a reference type: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3779 | else { |
| 3780 | assert(!A->isReferenceType() && "Reference types were handled above"); |
| 3781 | |
| 3782 | // - If P is an array type, the pointer type produced by the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3783 | // array-to-pointer standard conversion (4.2) is used in place |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3784 | // of P for type deduction; otherwise, |
| 3785 | if (P->isArrayType()) |
| 3786 | P = Context.getArrayDecayedType(P); |
| 3787 | // - If P is a function type, the pointer type produced by the |
| 3788 | // function-to-pointer standard conversion (4.3) is used in |
| 3789 | // place of P for type deduction; otherwise, |
| 3790 | else if (P->isFunctionType()) |
| 3791 | P = Context.getPointerType(P); |
| 3792 | // - 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] | 3793 | // P's type are ignored for type deduction. |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3794 | else |
| 3795 | P = P.getUnqualifiedType(); |
| 3796 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3797 | // C++0x [temp.deduct.conv]p4: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3798 | // 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] | 3799 | // 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] | 3800 | // referred to by A is used for type deduction. |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3801 | A = A.getUnqualifiedType(); |
| 3802 | } |
| 3803 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3804 | // Unevaluated SFINAE context. |
| 3805 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3806 | SFINAETrap Trap(*this); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3807 | |
| 3808 | // C++ [temp.deduct.conv]p1: |
| 3809 | // Template argument deduction is done by comparing the return |
| 3810 | // type of the template conversion function (call it P) with the |
| 3811 | // type that is required as the result of the conversion (call it |
| 3812 | // A) as described in 14.8.2.4. |
| 3813 | TemplateParameterList *TemplateParams |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3814 | = ConversionTemplate->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3815 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3816 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3817 | |
| 3818 | // C++0x [temp.deduct.conv]p4: |
| 3819 | // In general, the deduction process attempts to find template |
| 3820 | // argument values that will make the deduced A identical to |
| 3821 | // A. However, there are two cases that allow a difference: |
| 3822 | unsigned TDF = 0; |
| 3823 | // - If the original A is a reference type, A can be more |
| 3824 | // cv-qualified than the deduced A (i.e., the type referred to |
| 3825 | // by the reference) |
| 3826 | if (ToType->isReferenceType()) |
| 3827 | TDF |= TDF_ParamWithReferenceType; |
| 3828 | // - The deduced A can be another pointer or pointer to member |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3829 | // type that can be converted to A via a qualification |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3830 | // conversion. |
| 3831 | // |
| 3832 | // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when |
| 3833 | // both P and A are pointers or member pointers. In this case, we |
| 3834 | // just ignore cv-qualifiers completely). |
| 3835 | if ((P->isPointerType() && A->isPointerType()) || |
Douglas Gregor | 9f05ed5 | 2011-08-30 00:37:54 +0000 | [diff] [blame] | 3836 | (P->isMemberPointerType() && A->isMemberPointerType())) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3837 | TDF |= TDF_IgnoreQualifiers; |
| 3838 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3839 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3840 | P, A, Info, Deduced, TDF)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3841 | return Result; |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3842 | |
| 3843 | // Create an Instantiation Scope for finalizing the operator. |
| 3844 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3845 | // Finish template argument deduction. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3846 | FunctionDecl *ConversionSpecialized = nullptr; |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3847 | TemplateDeductionResult Result |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3848 | = FinishTemplateArgumentDeduction(ConversionTemplate, Deduced, 0, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3849 | ConversionSpecialized, Info); |
| 3850 | Specialization = cast_or_null<CXXConversionDecl>(ConversionSpecialized); |
| 3851 | |
| 3852 | // 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] | 3853 | // to a ptr-to-function, use the deduced arguments from the conversion |
| 3854 | // function to specialize the corresponding call operator. |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3855 | // e.g., int (*fp)(int) = [](auto a) { return a; }; |
| 3856 | if (Result == TDK_Success && isLambdaConversionOperator(ConversionGeneric)) { |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3857 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3858 | // 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] | 3859 | // to. This is necessary for matching the lambda call operator's return |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3860 | // type to that of the destination ptr-to-function's return type. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3861 | assert(A->isPointerType() && |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3862 | "Can only convert from lambda to ptr-to-function"); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3863 | const FunctionType *ToFunType = |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3864 | A->getPointeeType().getTypePtr()->getAs<FunctionType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3865 | const QualType DestFunctionPtrReturnType = ToFunType->getReturnType(); |
| 3866 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3867 | // Create the corresponding specializations of the call operator and |
| 3868 | // the static-invoker; and if the return type is auto, |
| 3869 | // deduce the return type and check if it matches the |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3870 | // DestFunctionPtrReturnType. |
| 3871 | // For instance: |
| 3872 | // auto L = [](auto a) { return f(a); }; |
| 3873 | // int (*fp)(int) = L; |
| 3874 | // char (*fp2)(int) = L; <-- Not OK. |
| 3875 | |
| 3876 | Result = SpecializeCorrespondingLambdaCallOperatorAndInvoker( |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3877 | Specialization, Deduced, DestFunctionPtrReturnType, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3878 | Info, *this); |
| 3879 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3880 | return Result; |
| 3881 | } |
| 3882 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3883 | /// \brief Deduce template arguments for a function template when there is |
| 3884 | /// nothing to deduce against (C++0x [temp.arg.explicit]p3). |
| 3885 | /// |
| 3886 | /// \param FunctionTemplate the function template for which we are performing |
| 3887 | /// template argument deduction. |
| 3888 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3889 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3890 | /// arguments. |
| 3891 | /// |
| 3892 | /// \param Specialization if template argument deduction was successful, |
| 3893 | /// this will be set to the function template specialization produced by |
| 3894 | /// template argument deduction. |
| 3895 | /// |
| 3896 | /// \param Info the argument will be updated to provide additional information |
| 3897 | /// about template argument deduction. |
| 3898 | /// |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3899 | /// \param IsAddressOfFunction If \c true, we are deducing as part of taking |
| 3900 | /// the address of a function template in a context where we do not have a |
| 3901 | /// target type, per [over.over]. If \c false, we are looking up a function |
| 3902 | /// template specialization based on its signature, which only happens when |
| 3903 | /// deducing a function parameter type from an argument that is a template-id |
| 3904 | /// naming a function template specialization. |
| 3905 | /// |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3906 | /// \returns the result of template argument deduction. |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3907 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3908 | FunctionTemplateDecl *FunctionTemplate, |
| 3909 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 3910 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 3911 | bool IsAddressOfFunction) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3912 | return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3913 | QualType(), Specialization, Info, |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3914 | IsAddressOfFunction); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3915 | } |
| 3916 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3917 | namespace { |
| 3918 | /// Substitute the 'auto' type specifier within a type for a given replacement |
| 3919 | /// type. |
| 3920 | class SubstituteAutoTransform : |
| 3921 | public TreeTransform<SubstituteAutoTransform> { |
| 3922 | QualType Replacement; |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 3923 | bool UseAutoSugar; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3924 | public: |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 3925 | SubstituteAutoTransform(Sema &SemaRef, QualType Replacement, |
| 3926 | bool UseAutoSugar = true) |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3927 | : TreeTransform<SubstituteAutoTransform>(SemaRef), |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 3928 | Replacement(Replacement), UseAutoSugar(UseAutoSugar) {} |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3929 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3930 | QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) { |
| 3931 | // If we're building the type pattern to deduce against, don't wrap the |
| 3932 | // substituted type in an AutoType. Certain template deduction rules |
| 3933 | // apply only when a template type parameter appears directly (and not if |
| 3934 | // the parameter is found through desugaring). For instance: |
| 3935 | // auto &&lref = lvalue; |
| 3936 | // must transform into "rvalue reference to T" not "rvalue reference to |
| 3937 | // auto type deduced as T" in order for [temp.deduct.call]p3 to apply. |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 3938 | if (!UseAutoSugar) { |
| 3939 | assert(isa<TemplateTypeParmType>(Replacement) && |
| 3940 | "unexpected unsugared replacement kind"); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3941 | QualType Result = Replacement; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 3942 | TemplateTypeParmTypeLoc NewTL = |
| 3943 | TLB.push<TemplateTypeParmTypeLoc>(Result); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3944 | NewTL.setNameLoc(TL.getNameLoc()); |
| 3945 | return Result; |
| 3946 | } else { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 3947 | QualType Result = SemaRef.Context.getAutoType( |
| 3948 | Replacement, TL.getTypePtr()->getKeyword(), Replacement.isNull()); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3949 | AutoTypeLoc NewTL = TLB.push<AutoTypeLoc>(Result); |
| 3950 | NewTL.setNameLoc(TL.getNameLoc()); |
| 3951 | return Result; |
| 3952 | } |
| 3953 | } |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 3954 | |
| 3955 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 3956 | // Lambdas never need to be transformed. |
| 3957 | return E; |
| 3958 | } |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3959 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3960 | QualType Apply(TypeLoc TL) { |
| 3961 | // Create some scratch storage for the transformed type locations. |
| 3962 | // FIXME: We're just going to throw this information away. Don't build it. |
| 3963 | TypeLocBuilder TLB; |
| 3964 | TLB.reserve(TL.getFullDataSize()); |
| 3965 | return TransformType(TLB, TL); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3966 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3967 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3968 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3969 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3970 | Sema::DeduceAutoResult |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 3971 | Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *&Init, QualType &Result, |
| 3972 | Optional<unsigned> DependentDeductionDepth) { |
| 3973 | return DeduceAutoType(Type->getTypeLoc(), Init, Result, |
| 3974 | DependentDeductionDepth); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3975 | } |
| 3976 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3977 | /// \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] | 3978 | /// |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 3979 | /// Note that this is done even if the initializer is dependent. (This is |
| 3980 | /// necessary to support partial ordering of templates using 'auto'.) |
| 3981 | /// A dependent type will be produced when deducing from a dependent type. |
| 3982 | /// |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3983 | /// \param Type the type pattern using the auto type-specifier. |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3984 | /// \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] | 3985 | /// \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] | 3986 | /// deduced type. |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 3987 | /// \param DependentDeductionDepth Set if we should permit deduction in |
| 3988 | /// dependent cases. This is necessary for template partial ordering with |
| 3989 | /// 'auto' template parameters. The value specified is the template |
| 3990 | /// parameter depth at which we should perform 'auto' deduction. |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 3991 | Sema::DeduceAutoResult |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 3992 | Sema::DeduceAutoType(TypeLoc Type, Expr *&Init, QualType &Result, |
| 3993 | Optional<unsigned> DependentDeductionDepth) { |
John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 3994 | if (Init->getType()->isNonOverloadPlaceholderType()) { |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3995 | ExprResult NonPlaceholder = CheckPlaceholderExpr(Init); |
| 3996 | if (NonPlaceholder.isInvalid()) |
| 3997 | return DAR_FailedAlreadyDiagnosed; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3998 | Init = NonPlaceholder.get(); |
John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 3999 | } |
| 4000 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4001 | if (!DependentDeductionDepth && |
| 4002 | (Type.getType()->isDependentType() || Init->isTypeDependent())) { |
| 4003 | Result = SubstituteAutoTransform(*this, QualType()).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4004 | assert(!Result.isNull() && "substituting DependentTy can't fail"); |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4005 | return DAR_Succeeded; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4006 | } |
| 4007 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4008 | // Find the depth of template parameter to synthesize. |
| 4009 | unsigned Depth = DependentDeductionDepth.getValueOr(0); |
| 4010 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4011 | // If this is a 'decltype(auto)' specifier, do the decltype dance. |
| 4012 | // Since 'decltype(auto)' can only occur at the top of the type, we |
| 4013 | // don't need to go digging for it. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4014 | if (const AutoType *AT = Type.getType()->getAs<AutoType>()) { |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4015 | if (AT->isDecltypeAuto()) { |
| 4016 | if (isa<InitListExpr>(Init)) { |
| 4017 | Diag(Init->getLocStart(), diag::err_decltype_auto_initializer_list); |
| 4018 | return DAR_FailedAlreadyDiagnosed; |
| 4019 | } |
| 4020 | |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 4021 | QualType Deduced = BuildDecltypeType(Init, Init->getLocStart(), false); |
David Majnemer | 3c20ab2 | 2015-07-01 00:29:28 +0000 | [diff] [blame] | 4022 | if (Deduced.isNull()) |
| 4023 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4024 | // FIXME: Support a non-canonical deduced type for 'auto'. |
| 4025 | Deduced = Context.getCanonicalType(Deduced); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4026 | Result = SubstituteAutoTransform(*this, Deduced).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4027 | if (Result.isNull()) |
| 4028 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4029 | return DAR_Succeeded; |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4030 | } else if (!getLangOpts().CPlusPlus) { |
| 4031 | if (isa<InitListExpr>(Init)) { |
| 4032 | Diag(Init->getLocStart(), diag::err_auto_init_list_from_c); |
| 4033 | return DAR_FailedAlreadyDiagnosed; |
| 4034 | } |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4035 | } |
| 4036 | } |
| 4037 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4038 | SourceLocation Loc = Init->getExprLoc(); |
| 4039 | |
| 4040 | LocalInstantiationScope InstScope(*this); |
| 4041 | |
| 4042 | // Build template<class TemplParam> void Func(FuncParam); |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4043 | TemplateTypeParmDecl *TemplParam = TemplateTypeParmDecl::Create( |
| 4044 | Context, nullptr, SourceLocation(), Loc, Depth, 0, nullptr, false, false); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4045 | QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0); |
| 4046 | NamedDecl *TemplParamPtr = TemplParam; |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 4047 | FixedSizeTemplateParameterListStorage<1, false> TemplateParamsSt( |
| 4048 | Loc, Loc, TemplParamPtr, Loc, nullptr); |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 4049 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4050 | QualType FuncParam = |
| 4051 | SubstituteAutoTransform(*this, TemplArg, /*UseAutoSugar*/false) |
| 4052 | .Apply(Type); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4053 | assert(!FuncParam.isNull() && |
| 4054 | "substituting template parameter for 'auto' failed"); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4055 | |
| 4056 | // Deduce type of TemplParam in Func(Init) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4057 | SmallVector<DeducedTemplateArgument, 1> Deduced; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4058 | Deduced.resize(1); |
| 4059 | QualType InitType = Init->getType(); |
| 4060 | unsigned TDF = 0; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4061 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4062 | TemplateDeductionInfo Info(Loc, Depth); |
| 4063 | |
| 4064 | // If deduction failed, don't diagnose if the initializer is dependent; it |
| 4065 | // might acquire a matching type in the instantiation. |
| 4066 | auto DeductionFailed = [&]() -> DeduceAutoResult { |
| 4067 | if (Init->isTypeDependent()) { |
| 4068 | Result = SubstituteAutoTransform(*this, QualType()).Apply(Type); |
| 4069 | assert(!Result.isNull() && "substituting DependentTy can't fail"); |
| 4070 | return DAR_Succeeded; |
| 4071 | } |
| 4072 | return DAR_Failed; |
| 4073 | }; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4074 | |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 4075 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4076 | if (InitList) { |
| 4077 | for (unsigned i = 0, e = InitList->getNumInits(); i < e; ++i) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4078 | if (DeduceTemplateArgumentByListElement(*this, TemplateParamsSt.get(), |
| 4079 | TemplArg, InitList->getInit(i), |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 4080 | Info, Deduced, TDF)) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4081 | return DeductionFailed(); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4082 | } |
| 4083 | } else { |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4084 | if (!getLangOpts().CPlusPlus && Init->refersToBitField()) { |
| 4085 | Diag(Loc, diag::err_auto_bitfield); |
| 4086 | return DAR_FailedAlreadyDiagnosed; |
| 4087 | } |
| 4088 | |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4089 | if (AdjustFunctionParmAndArgTypesForDeduction( |
| 4090 | *this, TemplateParamsSt.get(), FuncParam, InitType, Init, TDF)) |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 4091 | return DAR_Failed; |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 4092 | |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4093 | if (DeduceTemplateArgumentsByTypeMatch(*this, TemplateParamsSt.get(), |
| 4094 | FuncParam, InitType, Info, Deduced, |
| 4095 | TDF)) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4096 | return DeductionFailed(); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4097 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4098 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4099 | // Could be null if somehow 'auto' appears in a non-deduced context. |
Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4100 | if (Deduced[0].getKind() != TemplateArgument::Type) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4101 | return DeductionFailed(); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4102 | |
Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4103 | QualType DeducedType = Deduced[0].getAsType(); |
| 4104 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4105 | if (InitList) { |
| 4106 | DeducedType = BuildStdInitializerList(DeducedType, Loc); |
| 4107 | if (DeducedType.isNull()) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4108 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4109 | } |
| 4110 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4111 | Result = SubstituteAutoTransform(*this, DeducedType).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4112 | if (Result.isNull()) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4113 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4114 | |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4115 | // Check that the deduced argument type is compatible with the original |
| 4116 | // argument type per C++ [temp.deduct.call]p4. |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4117 | if (!InitList && !Result.isNull() && |
| 4118 | CheckOriginalCallArgDeduction(*this, |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4119 | Sema::OriginalCallArg(FuncParam,0,InitType), |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4120 | Result)) { |
| 4121 | Result = QualType(); |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4122 | return DeductionFailed(); |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4123 | } |
| 4124 | |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4125 | return DAR_Succeeded; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4126 | } |
| 4127 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 4128 | QualType Sema::SubstAutoType(QualType TypeWithAuto, |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4129 | QualType TypeToReplaceAuto) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4130 | if (TypeToReplaceAuto->isDependentType()) |
| 4131 | TypeToReplaceAuto = QualType(); |
| 4132 | return SubstituteAutoTransform(*this, TypeToReplaceAuto) |
| 4133 | .TransformType(TypeWithAuto); |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4134 | } |
| 4135 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 4136 | TypeSourceInfo* Sema::SubstAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto, |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4137 | QualType TypeToReplaceAuto) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4138 | if (TypeToReplaceAuto->isDependentType()) |
| 4139 | TypeToReplaceAuto = QualType(); |
| 4140 | return SubstituteAutoTransform(*this, TypeToReplaceAuto) |
| 4141 | .TransformType(TypeWithAuto); |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 4142 | } |
| 4143 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4144 | void Sema::DiagnoseAutoDeductionFailure(VarDecl *VDecl, Expr *Init) { |
| 4145 | if (isa<InitListExpr>(Init)) |
| 4146 | Diag(VDecl->getLocation(), |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4147 | VDecl->isInitCapture() |
| 4148 | ? diag::err_init_capture_deduction_failure_from_init_list |
| 4149 | : diag::err_auto_var_deduction_failure_from_init_list) |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4150 | << VDecl->getDeclName() << VDecl->getType() << Init->getSourceRange(); |
| 4151 | else |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4152 | Diag(VDecl->getLocation(), |
| 4153 | VDecl->isInitCapture() ? diag::err_init_capture_deduction_failure |
| 4154 | : diag::err_auto_var_deduction_failure) |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4155 | << VDecl->getDeclName() << VDecl->getType() << Init->getType() |
| 4156 | << Init->getSourceRange(); |
| 4157 | } |
| 4158 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4159 | bool Sema::DeduceReturnType(FunctionDecl *FD, SourceLocation Loc, |
| 4160 | bool Diagnose) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4161 | assert(FD->getReturnType()->isUndeducedType()); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4162 | |
| 4163 | if (FD->getTemplateInstantiationPattern()) |
| 4164 | InstantiateFunctionDefinition(Loc, FD); |
| 4165 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4166 | bool StillUndeduced = FD->getReturnType()->isUndeducedType(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4167 | if (StillUndeduced && Diagnose && !FD->isInvalidDecl()) { |
| 4168 | Diag(Loc, diag::err_auto_fn_used_before_defined) << FD; |
| 4169 | Diag(FD->getLocation(), diag::note_callee_decl) << FD; |
| 4170 | } |
| 4171 | |
| 4172 | return StillUndeduced; |
| 4173 | } |
| 4174 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4175 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4176 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4177 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4178 | unsigned Level, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4179 | llvm::SmallBitVector &Deduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4180 | |
| 4181 | /// \brief If this is a non-static member function, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 4182 | static void |
| 4183 | AddImplicitObjectParameterType(ASTContext &Context, |
| 4184 | CXXMethodDecl *Method, |
| 4185 | SmallVectorImpl<QualType> &ArgTypes) { |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4186 | // C++11 [temp.func.order]p3: |
| 4187 | // [...] The new parameter is of type "reference to cv A," where cv are |
| 4188 | // the cv-qualifiers of the function template (if any) and A is |
| 4189 | // the class of which the function template is a member. |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4190 | // |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4191 | // The standard doesn't say explicitly, but we pick the appropriate kind of |
| 4192 | // reference type based on [over.match.funcs]p4. |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4193 | QualType ArgTy = Context.getTypeDeclType(Method->getParent()); |
| 4194 | ArgTy = Context.getQualifiedType(ArgTy, |
| 4195 | Qualifiers::fromCVRMask(Method->getTypeQualifiers())); |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4196 | if (Method->getRefQualifier() == RQ_RValue) |
| 4197 | ArgTy = Context.getRValueReferenceType(ArgTy); |
| 4198 | else |
| 4199 | ArgTy = Context.getLValueReferenceType(ArgTy); |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4200 | ArgTypes.push_back(ArgTy); |
| 4201 | } |
| 4202 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4203 | /// \brief Determine whether the function template \p FT1 is at least as |
| 4204 | /// specialized as \p FT2. |
| 4205 | static bool isAtLeastAsSpecializedAs(Sema &S, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4206 | SourceLocation Loc, |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4207 | FunctionTemplateDecl *FT1, |
| 4208 | FunctionTemplateDecl *FT2, |
| 4209 | TemplatePartialOrderingContext TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4210 | unsigned NumCallArguments1) { |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4211 | FunctionDecl *FD1 = FT1->getTemplatedDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4212 | FunctionDecl *FD2 = FT2->getTemplatedDecl(); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4213 | const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); |
| 4214 | const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4215 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4216 | assert(Proto1 && Proto2 && "Function templates must have prototypes"); |
| 4217 | TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4218 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4219 | Deduced.resize(TemplateParams->size()); |
| 4220 | |
| 4221 | // C++0x [temp.deduct.partial]p3: |
| 4222 | // The types used to determine the ordering depend on the context in which |
| 4223 | // the partial ordering is done: |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4224 | TemplateDeductionInfo Info(Loc); |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4225 | SmallVector<QualType, 4> Args2; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4226 | switch (TPOC) { |
| 4227 | case TPOC_Call: { |
| 4228 | // - In the context of a function call, the function parameter types are |
| 4229 | // used. |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4230 | CXXMethodDecl *Method1 = dyn_cast<CXXMethodDecl>(FD1); |
| 4231 | CXXMethodDecl *Method2 = dyn_cast<CXXMethodDecl>(FD2); |
Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4232 | |
Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4233 | // C++11 [temp.func.order]p3: |
Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4234 | // [...] If only one of the function templates is a non-static |
| 4235 | // member, that function template is considered to have a new |
| 4236 | // first parameter inserted in its function parameter list. The |
| 4237 | // new parameter is of type "reference to cv A," where cv are |
| 4238 | // the cv-qualifiers of the function template (if any) and A is |
| 4239 | // the class of which the function template is a member. |
| 4240 | // |
Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4241 | // Note that we interpret this to mean "if one of the function |
| 4242 | // templates is a non-static member and the other is a non-member"; |
| 4243 | // otherwise, the ordering rules for static functions against non-static |
| 4244 | // functions don't make any sense. |
| 4245 | // |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4246 | // C++98/03 doesn't have this provision but we've extended DR532 to cover |
| 4247 | // it as wording was broken prior to it. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4248 | SmallVector<QualType, 4> Args1; |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4249 | |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4250 | unsigned NumComparedArguments = NumCallArguments1; |
| 4251 | |
| 4252 | if (!Method2 && Method1 && !Method1->isStatic()) { |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4253 | // Compare 'this' from Method1 against first parameter from Method2. |
| 4254 | AddImplicitObjectParameterType(S.Context, Method1, Args1); |
| 4255 | ++NumComparedArguments; |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4256 | } else if (!Method1 && Method2 && !Method2->isStatic()) { |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4257 | // Compare 'this' from Method2 against first parameter from Method1. |
| 4258 | AddImplicitObjectParameterType(S.Context, Method2, Args2); |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4259 | } |
| 4260 | |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4261 | Args1.insert(Args1.end(), Proto1->param_type_begin(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4262 | Proto1->param_type_end()); |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4263 | Args2.insert(Args2.end(), Proto2->param_type_begin(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4264 | Proto2->param_type_end()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4265 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4266 | // C++ [temp.func.order]p5: |
| 4267 | // The presence of unused ellipsis and default arguments has no effect on |
| 4268 | // the partial ordering of function templates. |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4269 | if (Args1.size() > NumComparedArguments) |
| 4270 | Args1.resize(NumComparedArguments); |
| 4271 | if (Args2.size() > NumComparedArguments) |
| 4272 | Args2.resize(NumComparedArguments); |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4273 | if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(), |
| 4274 | Args1.data(), Args1.size(), Info, Deduced, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4275 | TDF_None, /*PartialOrdering=*/true)) |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 4276 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4277 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4278 | break; |
| 4279 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4280 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4281 | case TPOC_Conversion: |
| 4282 | // - In the context of a call to a conversion operator, the return types |
| 4283 | // of the conversion function templates are used. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4284 | if (DeduceTemplateArgumentsByTypeMatch( |
| 4285 | S, TemplateParams, Proto2->getReturnType(), Proto1->getReturnType(), |
| 4286 | Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4287 | /*PartialOrdering=*/true)) |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4288 | return false; |
| 4289 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4290 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4291 | case TPOC_Other: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4292 | // - 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] | 4293 | // is used. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4294 | if (DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 4295 | FD2->getType(), FD1->getType(), |
| 4296 | Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4297 | /*PartialOrdering=*/true)) |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4298 | return false; |
| 4299 | break; |
| 4300 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4301 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4302 | // C++0x [temp.deduct.partial]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4303 | // In most cases, all template parameters must have values in order for |
| 4304 | // deduction to succeed, but for partial ordering purposes a template |
| 4305 | // 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] | 4306 | // types being used for partial ordering. [ Note: a template parameter used |
| 4307 | // in a non-deduced context is considered used. -end note] |
| 4308 | unsigned ArgIdx = 0, NumArgs = Deduced.size(); |
| 4309 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4310 | if (Deduced[ArgIdx].isNull()) |
| 4311 | break; |
| 4312 | |
| 4313 | if (ArgIdx == NumArgs) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4314 | // All template arguments were deduced. FT1 is at least as specialized |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4315 | // as FT2. |
| 4316 | return true; |
| 4317 | } |
| 4318 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4319 | // Figure out which template parameters were used. |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4320 | llvm::SmallBitVector UsedParameters(TemplateParams->size()); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4321 | switch (TPOC) { |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4322 | case TPOC_Call: |
| 4323 | for (unsigned I = 0, N = Args2.size(); I != N; ++I) |
| 4324 | ::MarkUsedTemplateParameters(S.Context, Args2[I], false, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4325 | TemplateParams->getDepth(), |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4326 | UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4327 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4328 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4329 | case TPOC_Conversion: |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4330 | ::MarkUsedTemplateParameters(S.Context, Proto2->getReturnType(), false, |
| 4331 | TemplateParams->getDepth(), UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4332 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4333 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4334 | case TPOC_Other: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4335 | ::MarkUsedTemplateParameters(S.Context, FD2->getType(), false, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4336 | TemplateParams->getDepth(), |
| 4337 | UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4338 | break; |
| 4339 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4340 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4341 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4342 | // If this argument had no value deduced but was used in one of the types |
| 4343 | // used for partial ordering, then deduction fails. |
| 4344 | if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) |
| 4345 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4346 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4347 | return true; |
| 4348 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4349 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4350 | /// \brief Determine whether this a function template whose parameter-type-list |
| 4351 | /// ends with a function parameter pack. |
| 4352 | static bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) { |
| 4353 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
| 4354 | unsigned NumParams = Function->getNumParams(); |
| 4355 | if (NumParams == 0) |
| 4356 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4357 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4358 | ParmVarDecl *Last = Function->getParamDecl(NumParams - 1); |
| 4359 | if (!Last->isParameterPack()) |
| 4360 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4361 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4362 | // Make sure that no previous parameter is a parameter pack. |
| 4363 | while (--NumParams > 0) { |
| 4364 | if (Function->getParamDecl(NumParams - 1)->isParameterPack()) |
| 4365 | return false; |
| 4366 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4367 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4368 | return true; |
| 4369 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4370 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4371 | /// \brief Returns the more specialized function template according |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4372 | /// to the rules of function template partial ordering (C++ [temp.func.order]). |
| 4373 | /// |
| 4374 | /// \param FT1 the first function template |
| 4375 | /// |
| 4376 | /// \param FT2 the second function template |
| 4377 | /// |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4378 | /// \param TPOC the context in which we are performing partial ordering of |
| 4379 | /// function templates. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4380 | /// |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4381 | /// \param NumCallArguments1 The number of arguments in the call to FT1, used |
| 4382 | /// only when \c TPOC is \c TPOC_Call. |
| 4383 | /// |
| 4384 | /// \param NumCallArguments2 The number of arguments in the call to FT2, used |
| 4385 | /// only when \c TPOC is \c TPOC_Call. |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4386 | /// |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4387 | /// \returns the more specialized function template. If neither |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4388 | /// template is more specialized, returns NULL. |
| 4389 | FunctionTemplateDecl * |
| 4390 | Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, |
| 4391 | FunctionTemplateDecl *FT2, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4392 | SourceLocation Loc, |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4393 | TemplatePartialOrderingContext TPOC, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4394 | unsigned NumCallArguments1, |
| 4395 | unsigned NumCallArguments2) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4396 | bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4397 | NumCallArguments1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4398 | bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4399 | NumCallArguments2); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4400 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4401 | if (Better1 != Better2) // We have a clear winner |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4402 | return Better1 ? FT1 : FT2; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4403 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4404 | if (!Better1 && !Better2) // Neither is better than the other |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4405 | return nullptr; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4406 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4407 | // FIXME: This mimics what GCC implements, but doesn't match up with the |
| 4408 | // proposed resolution for core issue 692. This area needs to be sorted out, |
| 4409 | // but for now we attempt to maintain compatibility. |
| 4410 | bool Variadic1 = isVariadicFunctionTemplate(FT1); |
| 4411 | bool Variadic2 = isVariadicFunctionTemplate(FT2); |
| 4412 | if (Variadic1 != Variadic2) |
| 4413 | return Variadic1? FT2 : FT1; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4414 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4415 | return nullptr; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4416 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4417 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4418 | /// \brief Determine if the two templates are equivalent. |
| 4419 | static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) { |
| 4420 | if (T1 == T2) |
| 4421 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4422 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4423 | if (!T1 || !T2) |
| 4424 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4425 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4426 | return T1->getCanonicalDecl() == T2->getCanonicalDecl(); |
| 4427 | } |
| 4428 | |
| 4429 | /// \brief Retrieve the most specialized of the given function template |
| 4430 | /// specializations. |
| 4431 | /// |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4432 | /// \param SpecBegin the start iterator of the function template |
| 4433 | /// specializations that we will be comparing. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4434 | /// |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4435 | /// \param SpecEnd the end iterator of the function template |
| 4436 | /// specializations, paired with \p SpecBegin. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4437 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4438 | /// \param Loc the location where the ambiguity or no-specializations |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4439 | /// diagnostic should occur. |
| 4440 | /// |
| 4441 | /// \param NoneDiag partial diagnostic used to diagnose cases where there are |
| 4442 | /// no matching candidates. |
| 4443 | /// |
| 4444 | /// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one |
| 4445 | /// occurs. |
| 4446 | /// |
| 4447 | /// \param CandidateDiag partial diagnostic used for each function template |
| 4448 | /// specialization that is a candidate in the ambiguous ordering. One parameter |
| 4449 | /// in this diagnostic should be unbound, which will correspond to the string |
| 4450 | /// describing the template arguments for the function template specialization. |
| 4451 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4452 | /// \returns the most specialized function template specialization, if |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4453 | /// found. Otherwise, returns SpecEnd. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4454 | UnresolvedSetIterator Sema::getMostSpecialized( |
| 4455 | UnresolvedSetIterator SpecBegin, UnresolvedSetIterator SpecEnd, |
| 4456 | TemplateSpecCandidateSet &FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4457 | SourceLocation Loc, const PartialDiagnostic &NoneDiag, |
| 4458 | const PartialDiagnostic &AmbigDiag, const PartialDiagnostic &CandidateDiag, |
| 4459 | bool Complain, QualType TargetType) { |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4460 | if (SpecBegin == SpecEnd) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4461 | if (Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4462 | Diag(Loc, NoneDiag); |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4463 | FailedCandidates.NoteCandidates(*this, Loc); |
| 4464 | } |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4465 | return SpecEnd; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4466 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4467 | |
| 4468 | if (SpecBegin + 1 == SpecEnd) |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4469 | return SpecBegin; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4470 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4471 | // Find the function template that is better than all of the templates it |
| 4472 | // has been compared to. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4473 | UnresolvedSetIterator Best = SpecBegin; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4474 | FunctionTemplateDecl *BestTemplate |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4475 | = cast<FunctionDecl>(*Best)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4476 | assert(BestTemplate && "Not a function template specialization?"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4477 | for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) { |
| 4478 | FunctionTemplateDecl *Challenger |
| 4479 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4480 | assert(Challenger && "Not a function template specialization?"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4481 | if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4482 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4483 | Challenger)) { |
| 4484 | Best = I; |
| 4485 | BestTemplate = Challenger; |
| 4486 | } |
| 4487 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4488 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4489 | // Make sure that the "best" function template is more specialized than all |
| 4490 | // of the others. |
| 4491 | bool Ambiguous = false; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4492 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4493 | FunctionTemplateDecl *Challenger |
| 4494 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4495 | if (I != Best && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4496 | !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4497 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4498 | BestTemplate)) { |
| 4499 | Ambiguous = true; |
| 4500 | break; |
| 4501 | } |
| 4502 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4503 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4504 | if (!Ambiguous) { |
| 4505 | // We found an answer. Return it. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4506 | return Best; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4507 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4508 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4509 | // Diagnose the ambiguity. |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4510 | if (Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4511 | Diag(Loc, AmbigDiag); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4512 | |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4513 | // FIXME: Can we order the candidates in some sane way? |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4514 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4515 | PartialDiagnostic PD = CandidateDiag; |
Saleem Abdulrasool | 78704fb | 2016-12-22 04:26:57 +0000 | [diff] [blame] | 4516 | const auto *FD = cast<FunctionDecl>(*I); |
| 4517 | PD << FD << getTemplateArgumentBindingsText( |
| 4518 | FD->getPrimaryTemplate()->getTemplateParameters(), |
| 4519 | *FD->getTemplateSpecializationArgs()); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4520 | if (!TargetType.isNull()) |
Saleem Abdulrasool | 78704fb | 2016-12-22 04:26:57 +0000 | [diff] [blame] | 4521 | HandleFunctionTypeMismatch(PD, FD->getType(), TargetType); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4522 | Diag((*I)->getLocation(), PD); |
| 4523 | } |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4524 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4525 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4526 | return SpecEnd; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4527 | } |
| 4528 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4529 | /// Determine whether one partial specialization, P1, is at least as |
| 4530 | /// specialized than another, P2. |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4531 | /// |
Simon Pilgrim | 6f3e1ea | 2016-12-26 18:11:49 +0000 | [diff] [blame] | 4532 | /// \tparam PartialSpecializationDecl The kind of P2, which must be a |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4533 | /// {Class,Var}TemplatePartialSpecializationDecl. |
| 4534 | /// \param T1 The injected-class-name of P1 (faked for a variable template). |
| 4535 | /// \param T2 The injected-class-name of P2 (faked for a variable template). |
| 4536 | /// \param Loc The location at which the comparison is required. |
| 4537 | template<typename PartialSpecializationDecl> |
| 4538 | static bool isAtLeastAsSpecializedAs(Sema &S, QualType T1, QualType T2, |
| 4539 | PartialSpecializationDecl *P2, |
| 4540 | SourceLocation Loc) { |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4541 | // C++ [temp.class.order]p1: |
| 4542 | // For two class template partial specializations, the first is at least as |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4543 | // specialized as the second if, given the following rewrite to two |
| 4544 | // function templates, the first function template is at least as |
| 4545 | // specialized as the second according to the ordering rules for function |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4546 | // templates (14.6.6.2): |
| 4547 | // - the first function template has the same template parameters as the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4548 | // first partial specialization and has a single function parameter |
| 4549 | // whose type is a class template specialization with the template |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4550 | // arguments of the first partial specialization, and |
| 4551 | // - the second function template has the same template parameters as the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4552 | // second partial specialization and has a single function parameter |
| 4553 | // whose type is a class template specialization with the template |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4554 | // arguments of the second partial specialization. |
| 4555 | // |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 4556 | // Rather than synthesize function templates, we merely perform the |
| 4557 | // equivalent partial ordering by performing deduction directly on |
| 4558 | // the template arguments of the class template partial |
| 4559 | // specializations. This computation is slightly simpler than the |
| 4560 | // general problem of function template partial ordering, because |
| 4561 | // class template partial specializations are more constrained. We |
| 4562 | // know that every template parameter is deducible from the class |
| 4563 | // template partial specialization's template arguments, for |
| 4564 | // example. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4565 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4566 | TemplateDeductionInfo Info(Loc); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4567 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4568 | // Determine whether P1 is at least as specialized as P2. |
| 4569 | Deduced.resize(P2->getTemplateParameters()->size()); |
| 4570 | if (DeduceTemplateArgumentsByTypeMatch(S, P2->getTemplateParameters(), |
| 4571 | T2, T1, Info, Deduced, TDF_None, |
| 4572 | /*PartialOrdering=*/true)) |
| 4573 | return false; |
| 4574 | |
| 4575 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), |
| 4576 | Deduced.end()); |
| 4577 | Sema::InstantiatingTemplate Inst(S, Loc, P2, DeducedArgs, Info); |
| 4578 | auto *TST1 = T1->castAs<TemplateSpecializationType>(); |
| 4579 | if (FinishTemplateArgumentDeduction( |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4580 | S, P2, /*PartialOrdering=*/true, |
| 4581 | TemplateArgumentList(TemplateArgumentList::OnStack, |
| 4582 | TST1->template_arguments()), |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4583 | Deduced, Info)) |
| 4584 | return false; |
| 4585 | |
| 4586 | return true; |
| 4587 | } |
| 4588 | |
| 4589 | /// \brief Returns the more specialized class template partial specialization |
| 4590 | /// according to the rules of partial ordering of class template partial |
| 4591 | /// specializations (C++ [temp.class.order]). |
| 4592 | /// |
| 4593 | /// \param PS1 the first class template partial specialization |
| 4594 | /// |
| 4595 | /// \param PS2 the second class template partial specialization |
| 4596 | /// |
| 4597 | /// \returns the more specialized class template partial specialization. If |
| 4598 | /// neither partial specialization is more specialized, returns NULL. |
| 4599 | ClassTemplatePartialSpecializationDecl * |
| 4600 | Sema::getMoreSpecializedPartialSpecialization( |
| 4601 | ClassTemplatePartialSpecializationDecl *PS1, |
| 4602 | ClassTemplatePartialSpecializationDecl *PS2, |
| 4603 | SourceLocation Loc) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4604 | QualType PT1 = PS1->getInjectedSpecializationType(); |
| 4605 | QualType PT2 = PS2->getInjectedSpecializationType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4606 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4607 | bool Better1 = isAtLeastAsSpecializedAs(*this, PT1, PT2, PS2, Loc); |
| 4608 | bool Better2 = isAtLeastAsSpecializedAs(*this, PT2, PT1, PS1, Loc); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4609 | |
| 4610 | if (Better1 == Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4611 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4612 | |
| 4613 | return Better1 ? PS1 : PS2; |
| 4614 | } |
| 4615 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4616 | VarTemplatePartialSpecializationDecl * |
| 4617 | Sema::getMoreSpecializedPartialSpecialization( |
| 4618 | VarTemplatePartialSpecializationDecl *PS1, |
| 4619 | VarTemplatePartialSpecializationDecl *PS2, SourceLocation Loc) { |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4620 | // Pretend the variable template specializations are class template |
| 4621 | // specializations and form a fake injected class name type for comparison. |
Richard Smith | f04fd0b | 2013-12-12 23:14:16 +0000 | [diff] [blame] | 4622 | assert(PS1->getSpecializedTemplate() == PS2->getSpecializedTemplate() && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4623 | "the partial specializations being compared should specialize" |
| 4624 | " the same template."); |
| 4625 | TemplateName Name(PS1->getSpecializedTemplate()); |
| 4626 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4627 | QualType PT1 = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4628 | CanonTemplate, PS1->getTemplateArgs().asArray()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4629 | QualType PT2 = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4630 | CanonTemplate, PS2->getTemplateArgs().asArray()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4631 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4632 | bool Better1 = isAtLeastAsSpecializedAs(*this, PT1, PT2, PS2, Loc); |
| 4633 | bool Better2 = isAtLeastAsSpecializedAs(*this, PT2, PT1, PS1, Loc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4634 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4635 | if (Better1 == Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4636 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4637 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4638 | return Better1 ? PS1 : PS2; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4641 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4642 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4643 | const TemplateArgument &TemplateArg, |
| 4644 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4645 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4646 | llvm::SmallBitVector &Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4647 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4648 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4649 | /// expression. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4650 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4651 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4652 | const Expr *E, |
| 4653 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4654 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4655 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 4656 | // We can deduce from a pack expansion. |
| 4657 | if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E)) |
| 4658 | E = Expansion->getPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4659 | |
Richard Smith | 3434900 | 2012-07-09 03:07:20 +0000 | [diff] [blame] | 4660 | // Skip through any implicit casts we added while type-checking, and any |
| 4661 | // substitutions performed by template alias expansion. |
| 4662 | while (1) { |
| 4663 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 4664 | E = ICE->getSubExpr(); |
| 4665 | else if (const SubstNonTypeTemplateParmExpr *Subst = |
| 4666 | dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 4667 | E = Subst->getReplacement(); |
| 4668 | else |
| 4669 | break; |
| 4670 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4671 | |
| 4672 | // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4673 | // find other occurrences of template parameters. |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4674 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
Douglas Gregor | 04b1152 | 2010-01-14 18:13:22 +0000 | [diff] [blame] | 4675 | if (!DRE) |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4676 | return; |
| 4677 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4678 | const NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4679 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 4680 | if (!NTTP) |
| 4681 | return; |
| 4682 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4683 | if (NTTP->getDepth() == Depth) |
| 4684 | Used[NTTP->getIndex()] = true; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 4685 | |
| 4686 | // In C++1z mode, additional arguments may be deduced from the type of a |
| 4687 | // non-type argument. |
| 4688 | if (Ctx.getLangOpts().CPlusPlus1z) |
| 4689 | MarkUsedTemplateParameters(Ctx, NTTP->getType(), OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4690 | } |
| 4691 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4692 | /// \brief Mark the template parameters that are used by the given |
| 4693 | /// nested name specifier. |
| 4694 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4695 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4696 | NestedNameSpecifier *NNS, |
| 4697 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4698 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4699 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4700 | if (!NNS) |
| 4701 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4702 | |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4703 | MarkUsedTemplateParameters(Ctx, NNS->getPrefix(), OnlyDeduced, Depth, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4704 | Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4705 | MarkUsedTemplateParameters(Ctx, QualType(NNS->getAsType(), 0), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4706 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4707 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4708 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4709 | /// \brief Mark the template parameters that are used by the given |
| 4710 | /// template name. |
| 4711 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4712 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4713 | TemplateName Name, |
| 4714 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4715 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4716 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4717 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 4718 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4719 | = dyn_cast<TemplateTemplateParmDecl>(Template)) { |
| 4720 | if (TTP->getDepth() == Depth) |
| 4721 | Used[TTP->getIndex()] = true; |
| 4722 | } |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4723 | return; |
| 4724 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4725 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4726 | if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4727 | MarkUsedTemplateParameters(Ctx, QTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4728 | Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4729 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4730 | MarkUsedTemplateParameters(Ctx, DTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4731 | Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4732 | } |
| 4733 | |
| 4734 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4735 | /// type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4736 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4737 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4738 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4739 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4740 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4741 | if (T.isNull()) |
| 4742 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4743 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4744 | // Non-dependent types have nothing deducible |
| 4745 | if (!T->isDependentType()) |
| 4746 | return; |
| 4747 | |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4748 | T = Ctx.getCanonicalType(T); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4749 | switch (T->getTypeClass()) { |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4750 | case Type::Pointer: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4751 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4752 | cast<PointerType>(T)->getPointeeType(), |
| 4753 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4754 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4755 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4756 | break; |
| 4757 | |
| 4758 | case Type::BlockPointer: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4759 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4760 | cast<BlockPointerType>(T)->getPointeeType(), |
| 4761 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4762 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4763 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4764 | break; |
| 4765 | |
| 4766 | case Type::LValueReference: |
| 4767 | case Type::RValueReference: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4768 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4769 | cast<ReferenceType>(T)->getPointeeType(), |
| 4770 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4771 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4772 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4773 | break; |
| 4774 | |
| 4775 | case Type::MemberPointer: { |
| 4776 | const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4777 | MarkUsedTemplateParameters(Ctx, MemPtr->getPointeeType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4778 | Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4779 | MarkUsedTemplateParameters(Ctx, QualType(MemPtr->getClass(), 0), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4780 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4781 | break; |
| 4782 | } |
| 4783 | |
| 4784 | case Type::DependentSizedArray: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4785 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4786 | cast<DependentSizedArrayType>(T)->getSizeExpr(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4787 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4788 | // Fall through to check the element type |
| 4789 | |
| 4790 | case Type::ConstantArray: |
| 4791 | case Type::IncompleteArray: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4792 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4793 | cast<ArrayType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4794 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4795 | break; |
| 4796 | |
| 4797 | case Type::Vector: |
| 4798 | case Type::ExtVector: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4799 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4800 | cast<VectorType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4801 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4802 | break; |
| 4803 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4804 | case Type::DependentSizedExtVector: { |
| 4805 | const DependentSizedExtVectorType *VecType |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4806 | = cast<DependentSizedExtVectorType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4807 | MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4808 | Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4809 | MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4810 | Depth, Used); |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4811 | break; |
| 4812 | } |
| 4813 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4814 | case Type::FunctionProto: { |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4815 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4816 | MarkUsedTemplateParameters(Ctx, Proto->getReturnType(), OnlyDeduced, Depth, |
| 4817 | Used); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4818 | for (unsigned I = 0, N = Proto->getNumParams(); I != N; ++I) |
| 4819 | MarkUsedTemplateParameters(Ctx, Proto->getParamType(I), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4820 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4821 | break; |
| 4822 | } |
| 4823 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4824 | case Type::TemplateTypeParm: { |
| 4825 | const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T); |
| 4826 | if (TTP->getDepth() == Depth) |
| 4827 | Used[TTP->getIndex()] = true; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4828 | break; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4829 | } |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4830 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4831 | case Type::SubstTemplateTypeParmPack: { |
| 4832 | const SubstTemplateTypeParmPackType *Subst |
| 4833 | = cast<SubstTemplateTypeParmPackType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4834 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4835 | QualType(Subst->getReplacedParameter(), 0), |
| 4836 | OnlyDeduced, Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4837 | MarkUsedTemplateParameters(Ctx, Subst->getArgumentPack(), |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4838 | OnlyDeduced, Depth, Used); |
| 4839 | break; |
| 4840 | } |
| 4841 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4842 | case Type::InjectedClassName: |
| 4843 | T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); |
| 4844 | // fall through |
| 4845 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4846 | case Type::TemplateSpecialization: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4847 | const TemplateSpecializationType *Spec |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4848 | = cast<TemplateSpecializationType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4849 | MarkUsedTemplateParameters(Ctx, Spec->getTemplateName(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4850 | Depth, Used); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4851 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4852 | // C++0x [temp.deduct.type]p9: |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 4853 | // If the template argument list of P contains a pack expansion that is |
| 4854 | // not the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4855 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4856 | if (OnlyDeduced && |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 4857 | hasPackExpansionBeforeEnd(Spec->template_arguments())) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4858 | break; |
| 4859 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4860 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4861 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4862 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4863 | break; |
| 4864 | } |
| 4865 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4866 | case Type::Complex: |
| 4867 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4868 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4869 | cast<ComplexType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4870 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4871 | break; |
| 4872 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4873 | case Type::Atomic: |
| 4874 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4875 | MarkUsedTemplateParameters(Ctx, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4876 | cast<AtomicType>(T)->getValueType(), |
| 4877 | OnlyDeduced, Depth, Used); |
| 4878 | break; |
| 4879 | |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4880 | case Type::DependentName: |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4881 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4882 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4883 | cast<DependentNameType>(T)->getQualifier(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4884 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4885 | break; |
| 4886 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4887 | case Type::DependentTemplateSpecialization: { |
Richard Smith | 50d5b97 | 2015-12-30 20:56:05 +0000 | [diff] [blame] | 4888 | // C++14 [temp.deduct.type]p5: |
| 4889 | // The non-deduced contexts are: |
| 4890 | // -- The nested-name-specifier of a type that was specified using a |
| 4891 | // qualified-id |
| 4892 | // |
| 4893 | // C++14 [temp.deduct.type]p6: |
| 4894 | // When a type name is specified in a way that includes a non-deduced |
| 4895 | // context, all of the types that comprise that type name are also |
| 4896 | // non-deduced. |
| 4897 | if (OnlyDeduced) |
| 4898 | break; |
| 4899 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4900 | const DependentTemplateSpecializationType *Spec |
| 4901 | = cast<DependentTemplateSpecializationType>(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4902 | |
Richard Smith | 50d5b97 | 2015-12-30 20:56:05 +0000 | [diff] [blame] | 4903 | MarkUsedTemplateParameters(Ctx, Spec->getQualifier(), |
| 4904 | OnlyDeduced, Depth, Used); |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4905 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4906 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4907 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4908 | Used); |
| 4909 | break; |
| 4910 | } |
| 4911 | |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4912 | case Type::TypeOf: |
| 4913 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4914 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4915 | cast<TypeOfType>(T)->getUnderlyingType(), |
| 4916 | OnlyDeduced, Depth, Used); |
| 4917 | break; |
| 4918 | |
| 4919 | case Type::TypeOfExpr: |
| 4920 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4921 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4922 | cast<TypeOfExprType>(T)->getUnderlyingExpr(), |
| 4923 | OnlyDeduced, Depth, Used); |
| 4924 | break; |
| 4925 | |
| 4926 | case Type::Decltype: |
| 4927 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4928 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4929 | cast<DecltypeType>(T)->getUnderlyingExpr(), |
| 4930 | OnlyDeduced, Depth, Used); |
| 4931 | break; |
| 4932 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4933 | case Type::UnaryTransform: |
| 4934 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4935 | MarkUsedTemplateParameters(Ctx, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 4936 | cast<UnaryTransformType>(T)->getUnderlyingType(), |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4937 | OnlyDeduced, Depth, Used); |
| 4938 | break; |
| 4939 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4940 | case Type::PackExpansion: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4941 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4942 | cast<PackExpansionType>(T)->getPattern(), |
| 4943 | OnlyDeduced, Depth, Used); |
| 4944 | break; |
| 4945 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4946 | case Type::Auto: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4947 | MarkUsedTemplateParameters(Ctx, |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4948 | cast<AutoType>(T)->getDeducedType(), |
| 4949 | OnlyDeduced, Depth, Used); |
| 4950 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4951 | // None of these types have any template parameters in them. |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4952 | case Type::Builtin: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4953 | case Type::VariableArray: |
| 4954 | case Type::FunctionNoProto: |
| 4955 | case Type::Record: |
| 4956 | case Type::Enum: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4957 | case Type::ObjCInterface: |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4958 | case Type::ObjCObject: |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 4959 | case Type::ObjCObjectPointer: |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4960 | case Type::UnresolvedUsing: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 4961 | case Type::Pipe: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4962 | #define TYPE(Class, Base) |
| 4963 | #define ABSTRACT_TYPE(Class, Base) |
| 4964 | #define DEPENDENT_TYPE(Class, Base) |
| 4965 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 4966 | #include "clang/AST/TypeNodes.def" |
| 4967 | break; |
| 4968 | } |
| 4969 | } |
| 4970 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4971 | /// \brief Mark the template parameters that are used by this |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4972 | /// template argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4973 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4974 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4975 | const TemplateArgument &TemplateArg, |
| 4976 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4977 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4978 | llvm::SmallBitVector &Used) { |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4979 | switch (TemplateArg.getKind()) { |
| 4980 | case TemplateArgument::Null: |
| 4981 | case TemplateArgument::Integral: |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4982 | case TemplateArgument::Declaration: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4983 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4984 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4985 | case TemplateArgument::NullPtr: |
| 4986 | MarkUsedTemplateParameters(Ctx, TemplateArg.getNullPtrType(), OnlyDeduced, |
| 4987 | Depth, Used); |
| 4988 | break; |
| 4989 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4990 | case TemplateArgument::Type: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4991 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4992 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4993 | break; |
| 4994 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4995 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4996 | case TemplateArgument::TemplateExpansion: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4997 | MarkUsedTemplateParameters(Ctx, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4998 | TemplateArg.getAsTemplateOrTemplatePattern(), |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4999 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5000 | break; |
| 5001 | |
| 5002 | case TemplateArgument::Expression: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5003 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsExpr(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5004 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5005 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5006 | |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5007 | case TemplateArgument::Pack: |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 5008 | for (const auto &P : TemplateArg.pack_elements()) |
| 5009 | MarkUsedTemplateParameters(Ctx, P, OnlyDeduced, Depth, Used); |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5010 | break; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5011 | } |
| 5012 | } |
| 5013 | |
James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 5014 | /// \brief Mark which template parameters can be deduced from a given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5015 | /// template argument list. |
| 5016 | /// |
| 5017 | /// \param TemplateArgs the template argument list from which template |
| 5018 | /// parameters will be deduced. |
| 5019 | /// |
James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 5020 | /// \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] | 5021 | /// to indicate when the corresponding template parameter will be |
| 5022 | /// deduced. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5023 | void |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5024 | Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5025 | bool OnlyDeduced, unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5026 | llvm::SmallBitVector &Used) { |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5027 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5028 | // If the template argument list of P contains a pack expansion that is not |
| 5029 | // the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5030 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5031 | if (OnlyDeduced && |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 5032 | hasPackExpansionBeforeEnd(TemplateArgs.asArray())) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5033 | return; |
| 5034 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5035 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5036 | ::MarkUsedTemplateParameters(Context, TemplateArgs[I], OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5037 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5038 | } |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5039 | |
| 5040 | /// \brief Marks all of the template parameters that will be deduced by a |
| 5041 | /// call to the given function template. |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 5042 | void Sema::MarkDeducedTemplateParameters( |
| 5043 | ASTContext &Ctx, const FunctionTemplateDecl *FunctionTemplate, |
| 5044 | llvm::SmallBitVector &Deduced) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5045 | TemplateParameterList *TemplateParams |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5046 | = FunctionTemplate->getTemplateParameters(); |
| 5047 | Deduced.clear(); |
| 5048 | Deduced.resize(TemplateParams->size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5049 | |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5050 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 5051 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5052 | ::MarkUsedTemplateParameters(Ctx, Function->getParamDecl(I)->getType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5053 | true, TemplateParams->getDepth(), Deduced); |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5054 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5055 | |
| 5056 | bool hasDeducibleTemplateParameters(Sema &S, |
| 5057 | FunctionTemplateDecl *FunctionTemplate, |
| 5058 | QualType T) { |
| 5059 | if (!T->isDependentType()) |
| 5060 | return false; |
| 5061 | |
| 5062 | TemplateParameterList *TemplateParams |
| 5063 | = FunctionTemplate->getTemplateParameters(); |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5064 | llvm::SmallBitVector Deduced(TemplateParams->size()); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 5065 | ::MarkUsedTemplateParameters(S.Context, T, true, TemplateParams->getDepth(), |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5066 | Deduced); |
| 5067 | |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5068 | return Deduced.any(); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5069 | } |