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 | d92eddf | 2016-12-27 06:14:37 +0000 | [diff] [blame] | 330 | if (!S.getLangOpts().CPlusPlus1z) |
| 331 | return Sema::TDK_Success; |
| 332 | |
| 333 | // FIXME: It's not clear how deduction of a parameter of reference |
| 334 | // type from an argument (of non-reference type) should be performed. |
| 335 | // For now, we just remove reference types from both sides and let |
| 336 | // the final check for matching types sort out the mess. |
| 337 | return DeduceTemplateArgumentsByTypeMatch( |
| 338 | S, TemplateParams, NTTP->getType().getNonReferenceType(), |
| 339 | ValueType.getNonReferenceType(), Info, Deduced, TDF_SkipNonDependent, |
| 340 | /*PartialOrdering=*/false, |
| 341 | /*ArrayBound=*/NewDeduced.wasDeducedFromArrayBound()); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 344 | /// \brief Deduce the value of the given non-type template parameter |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 345 | /// from the given integral constant. |
| 346 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| 347 | Sema &S, TemplateParameterList *TemplateParams, |
| 348 | NonTypeTemplateParmDecl *NTTP, const llvm::APSInt &Value, |
| 349 | QualType ValueType, bool DeducedFromArrayBound, TemplateDeductionInfo &Info, |
| 350 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 351 | return DeduceNonTypeTemplateArgument( |
| 352 | S, TemplateParams, NTTP, |
| 353 | DeducedTemplateArgument(S.Context, Value, ValueType, |
| 354 | DeducedFromArrayBound), |
| 355 | ValueType, Info, Deduced); |
| 356 | } |
| 357 | |
| 358 | /// \brief Deduce the value of the given non-type template parameter |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 359 | /// from the given null pointer template argument type. |
| 360 | static Sema::TemplateDeductionResult DeduceNullPtrTemplateArgument( |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 361 | Sema &S, TemplateParameterList *TemplateParams, |
| 362 | NonTypeTemplateParmDecl *NTTP, QualType NullPtrType, |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 363 | TemplateDeductionInfo &Info, |
| 364 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 365 | Expr *Value = |
| 366 | S.ImpCastExprToType(new (S.Context) CXXNullPtrLiteralExpr( |
| 367 | S.Context.NullPtrTy, NTTP->getLocation()), |
| 368 | NullPtrType, CK_NullToPointer) |
| 369 | .get(); |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 370 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 371 | DeducedTemplateArgument(Value), |
| 372 | Value->getType(), Info, Deduced); |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /// \brief Deduce the value of the given non-type template parameter |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 376 | /// from the given type- or value-dependent expression. |
| 377 | /// |
| 378 | /// \returns true if deduction succeeded, false otherwise. |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 379 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| 380 | Sema &S, TemplateParameterList *TemplateParams, |
| 381 | NonTypeTemplateParmDecl *NTTP, Expr *Value, TemplateDeductionInfo &Info, |
| 382 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 383 | assert((Value->isTypeDependent() || Value->isValueDependent()) && |
| 384 | "Expression template argument must be type- or value-dependent."); |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 385 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 386 | DeducedTemplateArgument(Value), |
| 387 | Value->getType(), Info, Deduced); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 390 | /// \brief Deduce the value of the given non-type template parameter |
| 391 | /// from the given declaration. |
| 392 | /// |
| 393 | /// \returns true if deduction succeeded, false otherwise. |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 394 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| 395 | Sema &S, TemplateParameterList *TemplateParams, |
| 396 | NonTypeTemplateParmDecl *NTTP, ValueDecl *D, QualType T, |
| 397 | TemplateDeductionInfo &Info, |
| 398 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 399 | D = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 400 | TemplateArgument New(D, T); |
Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 401 | return DeduceNonTypeTemplateArgument( |
| 402 | S, TemplateParams, NTTP, DeducedTemplateArgument(New), T, Info, Deduced); |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 405 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 406 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 407 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 408 | TemplateName Param, |
| 409 | TemplateName Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 410 | TemplateDeductionInfo &Info, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 411 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 412 | TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 413 | if (!ParamDecl) { |
| 414 | // The parameter type is dependent and is not a template template parameter, |
| 415 | // so there is nothing that we can deduce. |
| 416 | return Sema::TDK_Success; |
| 417 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 418 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 419 | if (TemplateTemplateParmDecl *TempParam |
| 420 | = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 421 | // If we're not deducing at this depth, there's nothing to deduce. |
| 422 | if (TempParam->getDepth() != Info.getDeducedDepth()) |
| 423 | return Sema::TDK_Success; |
| 424 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 425 | DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 426 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 427 | Deduced[TempParam->getIndex()], |
| 428 | NewDeduced); |
| 429 | if (Result.isNull()) { |
| 430 | Info.Param = TempParam; |
| 431 | Info.FirstArg = Deduced[TempParam->getIndex()]; |
| 432 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 433 | return Sema::TDK_Inconsistent; |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 434 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 435 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 436 | Deduced[TempParam->getIndex()] = Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 437 | return Sema::TDK_Success; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 438 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 439 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 440 | // Verify that the two template names are equivalent. |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 441 | if (S.Context.hasSameTemplateName(Param, Arg)) |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 442 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 443 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 444 | // Mismatch of non-dependent template parameter to argument. |
| 445 | Info.FirstArg = TemplateArgument(Param); |
| 446 | Info.SecondArg = TemplateArgument(Arg); |
| 447 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 450 | /// \brief Deduce the template arguments by comparing the template parameter |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 451 | /// type (which is a template-id) with the template argument type. |
| 452 | /// |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 453 | /// \param S the Sema |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 454 | /// |
| 455 | /// \param TemplateParams the template parameters that we are deducing |
| 456 | /// |
| 457 | /// \param Param the parameter type |
| 458 | /// |
| 459 | /// \param Arg the argument type |
| 460 | /// |
| 461 | /// \param Info information about the template argument deduction itself |
| 462 | /// |
| 463 | /// \param Deduced the deduced template arguments |
| 464 | /// |
| 465 | /// \returns the result of template argument deduction so far. Note that a |
| 466 | /// "success" result means that template argument deduction has not yet failed, |
| 467 | /// but it may still fail, later, for other reasons. |
| 468 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 469 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 470 | TemplateParameterList *TemplateParams, |
| 471 | const TemplateSpecializationType *Param, |
| 472 | QualType Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 473 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 474 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 475 | assert(Arg.isCanonical() && "Argument type must be canonical"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 476 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 477 | // Check whether the template argument is a dependent template-id. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | if (const TemplateSpecializationType *SpecArg |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 479 | = dyn_cast<TemplateSpecializationType>(Arg)) { |
| 480 | // Perform template argument deduction for the template name. |
| 481 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 482 | = DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 483 | Param->getTemplateName(), |
| 484 | SpecArg->getTemplateName(), |
| 485 | Info, Deduced)) |
| 486 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 487 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 488 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 489 | // Perform template argument deduction on each template |
Douglas Gregor | d80ea20 | 2010-12-22 18:55:49 +0000 | [diff] [blame] | 490 | // argument. Ignore any missing/extra arguments, since they could be |
| 491 | // filled in by default arguments. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 492 | return DeduceTemplateArguments(S, TemplateParams, |
| 493 | Param->template_arguments(), |
| 494 | SpecArg->template_arguments(), Info, Deduced, |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 495 | /*NumberOfArgumentsMustMatch=*/false); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 496 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 497 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 498 | // If the argument type is a class template specialization, we |
| 499 | // perform template argument deduction using its template |
| 500 | // arguments. |
| 501 | const RecordType *RecordArg = dyn_cast<RecordType>(Arg); |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 502 | if (!RecordArg) { |
| 503 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 504 | Info.SecondArg = TemplateArgument(Arg); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 505 | return Sema::TDK_NonDeducedMismatch; |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 506 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 507 | |
| 508 | ClassTemplateSpecializationDecl *SpecArg |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 509 | = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 510 | if (!SpecArg) { |
| 511 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 512 | Info.SecondArg = TemplateArgument(Arg); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 513 | return Sema::TDK_NonDeducedMismatch; |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 514 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 515 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 516 | // Perform template argument deduction for the template name. |
| 517 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 518 | = DeduceTemplateArguments(S, |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 519 | TemplateParams, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 520 | Param->getTemplateName(), |
| 521 | TemplateName(SpecArg->getSpecializedTemplate()), |
| 522 | Info, Deduced)) |
| 523 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 524 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 525 | // Perform template argument deduction for the template arguments. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 526 | return DeduceTemplateArguments(S, TemplateParams, Param->template_arguments(), |
| 527 | SpecArg->getTemplateArgs().asArray(), Info, |
| 528 | Deduced, /*NumberOfArgumentsMustMatch=*/true); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 529 | } |
| 530 | |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 531 | /// \brief Determines whether the given type is an opaque type that |
| 532 | /// might be more qualified when instantiated. |
| 533 | static bool IsPossiblyOpaquelyQualifiedType(QualType T) { |
| 534 | switch (T->getTypeClass()) { |
| 535 | case Type::TypeOfExpr: |
| 536 | case Type::TypeOf: |
| 537 | case Type::DependentName: |
| 538 | case Type::Decltype: |
| 539 | case Type::UnresolvedUsing: |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 540 | case Type::TemplateTypeParm: |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 541 | return true; |
| 542 | |
| 543 | case Type::ConstantArray: |
| 544 | case Type::IncompleteArray: |
| 545 | case Type::VariableArray: |
| 546 | case Type::DependentSizedArray: |
| 547 | return IsPossiblyOpaquelyQualifiedType( |
| 548 | cast<ArrayType>(T)->getElementType()); |
| 549 | |
| 550 | default: |
| 551 | return false; |
| 552 | } |
| 553 | } |
| 554 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 555 | /// \brief Retrieve the depth and index of a template parameter. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 556 | static std::pair<unsigned, unsigned> |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 557 | getDepthAndIndex(NamedDecl *ND) { |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 558 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND)) |
| 559 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 560 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 561 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND)) |
| 562 | return std::make_pair(NTTP->getDepth(), NTTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 563 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 564 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND); |
| 565 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| 566 | } |
| 567 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 568 | /// \brief Retrieve the depth and index of an unexpanded parameter pack. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 569 | static std::pair<unsigned, unsigned> |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 570 | getDepthAndIndex(UnexpandedParameterPack UPP) { |
| 571 | if (const TemplateTypeParmType *TTP |
| 572 | = UPP.first.dyn_cast<const TemplateTypeParmType *>()) |
| 573 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 574 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 575 | return getDepthAndIndex(UPP.first.get<NamedDecl *>()); |
| 576 | } |
| 577 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 578 | /// \brief Helper function to build a TemplateParameter when we don't |
| 579 | /// know its type statically. |
| 580 | static TemplateParameter makeTemplateParameter(Decl *D) { |
| 581 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) |
| 582 | return TemplateParameter(TTP); |
Craig Topper | 4b482ee | 2013-07-08 04:24:47 +0000 | [diff] [blame] | 583 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 584 | return TemplateParameter(NTTP); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 585 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 586 | return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); |
| 587 | } |
| 588 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 589 | /// A pack that we're currently deducing. |
| 590 | struct clang::DeducedPack { |
| 591 | DeducedPack(unsigned Index) : Index(Index), Outer(nullptr) {} |
Craig Topper | 0a4e1f5 | 2013-07-08 04:44:01 +0000 | [diff] [blame] | 592 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 593 | // The index of the pack. |
| 594 | unsigned Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 595 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 596 | // The old value of the pack before we started deducing it. |
| 597 | DeducedTemplateArgument Saved; |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 598 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 599 | // A deferred value of this pack from an inner deduction, that couldn't be |
| 600 | // deduced because this deduction hadn't happened yet. |
| 601 | DeducedTemplateArgument DeferredDeduction; |
| 602 | |
| 603 | // The new value of the pack. |
| 604 | SmallVector<DeducedTemplateArgument, 4> New; |
| 605 | |
| 606 | // The outer deduction for this pack, if any. |
| 607 | DeducedPack *Outer; |
| 608 | }; |
| 609 | |
Benjamin Kramer | d5748c7 | 2015-03-23 12:31:05 +0000 | [diff] [blame] | 610 | namespace { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 611 | /// A scope in which we're performing pack deduction. |
| 612 | class PackDeductionScope { |
| 613 | public: |
| 614 | PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, |
| 615 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 616 | TemplateDeductionInfo &Info, TemplateArgument Pattern) |
| 617 | : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { |
| 618 | // Compute the set of template parameter indices that correspond to |
| 619 | // parameter packs expanded by the pack expansion. |
| 620 | { |
| 621 | llvm::SmallBitVector SawIndices(TemplateParams->size()); |
| 622 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 623 | S.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
| 624 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 625 | unsigned Depth, Index; |
| 626 | std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 627 | if (Depth == Info.getDeducedDepth() && !SawIndices[Index]) { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 628 | SawIndices[Index] = true; |
| 629 | |
| 630 | // Save the deduced template argument for the parameter pack expanded |
| 631 | // by this pack expansion, then clear out the deduction. |
| 632 | DeducedPack Pack(Index); |
| 633 | Pack.Saved = Deduced[Index]; |
| 634 | Deduced[Index] = TemplateArgument(); |
| 635 | |
| 636 | Packs.push_back(Pack); |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | assert(!Packs.empty() && "Pack expansion without unexpanded packs?"); |
| 641 | |
| 642 | for (auto &Pack : Packs) { |
| 643 | if (Info.PendingDeducedPacks.size() > Pack.Index) |
| 644 | Pack.Outer = Info.PendingDeducedPacks[Pack.Index]; |
| 645 | else |
| 646 | Info.PendingDeducedPacks.resize(Pack.Index + 1); |
| 647 | Info.PendingDeducedPacks[Pack.Index] = &Pack; |
| 648 | |
| 649 | if (S.CurrentInstantiationScope) { |
| 650 | // If the template argument pack was explicitly specified, add that to |
| 651 | // the set of deduced arguments. |
| 652 | const TemplateArgument *ExplicitArgs; |
| 653 | unsigned NumExplicitArgs; |
| 654 | NamedDecl *PartiallySubstitutedPack = |
| 655 | S.CurrentInstantiationScope->getPartiallySubstitutedPack( |
| 656 | &ExplicitArgs, &NumExplicitArgs); |
| 657 | if (PartiallySubstitutedPack && |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 658 | getDepthAndIndex(PartiallySubstitutedPack) == |
| 659 | std::make_pair(Info.getDeducedDepth(), Pack.Index)) |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 660 | Pack.New.append(ExplicitArgs, ExplicitArgs + NumExplicitArgs); |
| 661 | } |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 662 | } |
| 663 | } |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 664 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 665 | ~PackDeductionScope() { |
| 666 | for (auto &Pack : Packs) |
| 667 | Info.PendingDeducedPacks[Pack.Index] = Pack.Outer; |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 668 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 669 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 670 | /// Move to deducing the next element in each pack that is being deduced. |
| 671 | void nextPackElement() { |
| 672 | // Capture the deduced template arguments for each parameter pack expanded |
| 673 | // by this pack expansion, add them to the list of arguments we've deduced |
| 674 | // for that pack, then clear out the deduced argument. |
| 675 | for (auto &Pack : Packs) { |
| 676 | DeducedTemplateArgument &DeducedArg = Deduced[Pack.Index]; |
| 677 | if (!DeducedArg.isNull()) { |
| 678 | Pack.New.push_back(DeducedArg); |
| 679 | DeducedArg = DeducedTemplateArgument(); |
| 680 | } |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | /// \brief Finish template argument deduction for a set of argument packs, |
| 685 | /// producing the argument packs and checking for consistency with prior |
| 686 | /// deductions. |
| 687 | Sema::TemplateDeductionResult finish(bool HasAnyArguments) { |
| 688 | // Build argument packs for each of the parameter packs expanded by this |
| 689 | // pack expansion. |
| 690 | for (auto &Pack : Packs) { |
| 691 | // Put back the old value for this pack. |
| 692 | Deduced[Pack.Index] = Pack.Saved; |
| 693 | |
| 694 | // Build or find a new value for this pack. |
| 695 | DeducedTemplateArgument NewPack; |
| 696 | if (HasAnyArguments && Pack.New.empty()) { |
| 697 | if (Pack.DeferredDeduction.isNull()) { |
| 698 | // We were not able to deduce anything for this parameter pack |
| 699 | // (because it only appeared in non-deduced contexts), so just |
| 700 | // restore the saved argument pack. |
| 701 | continue; |
| 702 | } |
| 703 | |
| 704 | NewPack = Pack.DeferredDeduction; |
| 705 | Pack.DeferredDeduction = TemplateArgument(); |
| 706 | } else if (Pack.New.empty()) { |
| 707 | // If we deduced an empty argument pack, create it now. |
| 708 | NewPack = DeducedTemplateArgument(TemplateArgument::getEmptyPack()); |
| 709 | } else { |
| 710 | TemplateArgument *ArgumentPack = |
| 711 | new (S.Context) TemplateArgument[Pack.New.size()]; |
| 712 | std::copy(Pack.New.begin(), Pack.New.end(), ArgumentPack); |
| 713 | NewPack = DeducedTemplateArgument( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 714 | TemplateArgument(llvm::makeArrayRef(ArgumentPack, Pack.New.size())), |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 715 | Pack.New[0].wasDeducedFromArrayBound()); |
| 716 | } |
| 717 | |
| 718 | // Pick where we're going to put the merged pack. |
| 719 | DeducedTemplateArgument *Loc; |
| 720 | if (Pack.Outer) { |
| 721 | if (Pack.Outer->DeferredDeduction.isNull()) { |
| 722 | // Defer checking this pack until we have a complete pack to compare |
| 723 | // it against. |
| 724 | Pack.Outer->DeferredDeduction = NewPack; |
| 725 | continue; |
| 726 | } |
| 727 | Loc = &Pack.Outer->DeferredDeduction; |
| 728 | } else { |
| 729 | Loc = &Deduced[Pack.Index]; |
| 730 | } |
| 731 | |
| 732 | // Check the new pack matches any previous value. |
| 733 | DeducedTemplateArgument OldPack = *Loc; |
| 734 | DeducedTemplateArgument Result = |
| 735 | checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 736 | |
| 737 | // If we deferred a deduction of this pack, check that one now too. |
| 738 | if (!Result.isNull() && !Pack.DeferredDeduction.isNull()) { |
| 739 | OldPack = Result; |
| 740 | NewPack = Pack.DeferredDeduction; |
| 741 | Result = checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 742 | } |
| 743 | |
| 744 | if (Result.isNull()) { |
| 745 | Info.Param = |
| 746 | makeTemplateParameter(TemplateParams->getParam(Pack.Index)); |
| 747 | Info.FirstArg = OldPack; |
| 748 | Info.SecondArg = NewPack; |
| 749 | return Sema::TDK_Inconsistent; |
| 750 | } |
| 751 | |
| 752 | *Loc = Result; |
| 753 | } |
| 754 | |
| 755 | return Sema::TDK_Success; |
| 756 | } |
| 757 | |
| 758 | private: |
| 759 | Sema &S; |
| 760 | TemplateParameterList *TemplateParams; |
| 761 | SmallVectorImpl<DeducedTemplateArgument> &Deduced; |
| 762 | TemplateDeductionInfo &Info; |
| 763 | |
| 764 | SmallVector<DeducedPack, 2> Packs; |
| 765 | }; |
Benjamin Kramer | d5748c7 | 2015-03-23 12:31:05 +0000 | [diff] [blame] | 766 | } // namespace |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 767 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 768 | /// \brief Deduce the template arguments by comparing the list of parameter |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 769 | /// types to the list of argument types, as in the parameter-type-lists of |
| 770 | /// function types (C++ [temp.deduct.type]p10). |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 771 | /// |
| 772 | /// \param S The semantic analysis object within which we are deducing |
| 773 | /// |
| 774 | /// \param TemplateParams The template parameters that we are deducing |
| 775 | /// |
| 776 | /// \param Params The list of parameter types |
| 777 | /// |
| 778 | /// \param NumParams The number of types in \c Params |
| 779 | /// |
| 780 | /// \param Args The list of argument types |
| 781 | /// |
| 782 | /// \param NumArgs The number of types in \c Args |
| 783 | /// |
| 784 | /// \param Info information about the template argument deduction itself |
| 785 | /// |
| 786 | /// \param Deduced the deduced template arguments |
| 787 | /// |
| 788 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
| 789 | /// how template argument deduction is performed. |
| 790 | /// |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 791 | /// \param PartialOrdering If true, we are performing template argument |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 792 | /// deduction for during partial ordering for a call |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 793 | /// (C++0x [temp.deduct.partial]). |
| 794 | /// |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 795 | /// \returns the result of template argument deduction so far. Note that a |
| 796 | /// "success" result means that template argument deduction has not yet failed, |
| 797 | /// but it may still fail, later, for other reasons. |
| 798 | static Sema::TemplateDeductionResult |
| 799 | DeduceTemplateArguments(Sema &S, |
| 800 | TemplateParameterList *TemplateParams, |
| 801 | const QualType *Params, unsigned NumParams, |
| 802 | const QualType *Args, unsigned NumArgs, |
| 803 | TemplateDeductionInfo &Info, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 804 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 805 | unsigned TDF, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 806 | bool PartialOrdering = false) { |
Douglas Gregor | 86bea35 | 2011-01-05 23:23:17 +0000 | [diff] [blame] | 807 | // Fast-path check to see if we have too many/too few arguments. |
| 808 | if (NumParams != NumArgs && |
| 809 | !(NumParams && isa<PackExpansionType>(Params[NumParams - 1])) && |
| 810 | !(NumArgs && isa<PackExpansionType>(Args[NumArgs - 1]))) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 811 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 812 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 813 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 814 | // Similarly, if P has a form that contains (T), then each parameter type |
| 815 | // Pi of the respective parameter-type- list of P is compared with the |
| 816 | // corresponding parameter type Ai of the corresponding parameter-type-list |
| 817 | // of A. [...] |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 818 | unsigned ArgIdx = 0, ParamIdx = 0; |
| 819 | for (; ParamIdx != NumParams; ++ParamIdx) { |
| 820 | // Check argument types. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 821 | const PackExpansionType *Expansion |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 822 | = dyn_cast<PackExpansionType>(Params[ParamIdx]); |
| 823 | if (!Expansion) { |
| 824 | // Simple case: compare the parameter and argument types at this point. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 825 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 826 | // Make sure we have an argument. |
| 827 | if (ArgIdx >= NumArgs) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 828 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 829 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 830 | if (isa<PackExpansionType>(Args[ArgIdx])) { |
| 831 | // C++0x [temp.deduct.type]p22: |
| 832 | // If the original function parameter associated with A is a function |
| 833 | // parameter pack and the function parameter associated with P is not |
| 834 | // a function parameter pack, then template argument deduction fails. |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 835 | return Sema::TDK_MiscellaneousDeductionFailure; |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 836 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 837 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 838 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 839 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 840 | Params[ParamIdx], Args[ArgIdx], |
| 841 | Info, Deduced, TDF, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 842 | PartialOrdering)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 843 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 844 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 845 | ++ArgIdx; |
| 846 | continue; |
| 847 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 848 | |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 849 | // C++0x [temp.deduct.type]p5: |
| 850 | // The non-deduced contexts are: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 851 | // - 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] | 852 | // parameter-declaration-clause. |
| 853 | if (ParamIdx + 1 < NumParams) |
| 854 | return Sema::TDK_Success; |
| 855 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 856 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 857 | // If the parameter-declaration corresponding to Pi is a function |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 858 | // parameter pack, then the type of its declarator- id is compared with |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 859 | // each remaining parameter type in the parameter-type-list of A. Each |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 860 | // comparison deduces template arguments for subsequent positions in the |
| 861 | // template parameter packs expanded by the function parameter pack. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 862 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 863 | QualType Pattern = Expansion->getPattern(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 864 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 865 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 866 | bool HasAnyArguments = false; |
| 867 | for (; ArgIdx < NumArgs; ++ArgIdx) { |
| 868 | HasAnyArguments = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 869 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 870 | // Deduce template arguments from the pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 871 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 872 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, Pattern, |
| 873 | Args[ArgIdx], Info, Deduced, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 874 | TDF, PartialOrdering)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 875 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 876 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 877 | PackScope.nextPackElement(); |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 878 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 879 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 880 | // Build argument packs for each of the parameter packs expanded by this |
| 881 | // pack expansion. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 882 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 883 | return Result; |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 884 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 885 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 886 | // Make sure we don't have any extra arguments. |
| 887 | if (ArgIdx < NumArgs) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 888 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 889 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 890 | return Sema::TDK_Success; |
| 891 | } |
| 892 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 893 | /// \brief Determine whether the parameter has qualifiers that are either |
| 894 | /// inconsistent with or a superset of the argument's qualifiers. |
| 895 | static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType, |
| 896 | QualType ArgType) { |
| 897 | Qualifiers ParamQs = ParamType.getQualifiers(); |
| 898 | Qualifiers ArgQs = ArgType.getQualifiers(); |
| 899 | |
| 900 | if (ParamQs == ArgQs) |
| 901 | return false; |
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) Objective-C GC attributes. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 904 | if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() && |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 905 | ParamQs.hasObjCGCAttr()) |
| 906 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 907 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 908 | // Mismatched (but not missing) address spaces. |
| 909 | if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() && |
| 910 | ParamQs.hasAddressSpace()) |
| 911 | return true; |
| 912 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 913 | // Mismatched (but not missing) Objective-C lifetime qualifiers. |
| 914 | if (ParamQs.getObjCLifetime() != ArgQs.getObjCLifetime() && |
| 915 | ParamQs.hasObjCLifetime()) |
| 916 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 917 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 918 | // CVR qualifier superset. |
| 919 | return (ParamQs.getCVRQualifiers() != ArgQs.getCVRQualifiers()) && |
| 920 | ((ParamQs.getCVRQualifiers() | ArgQs.getCVRQualifiers()) |
| 921 | == ParamQs.getCVRQualifiers()); |
| 922 | } |
| 923 | |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 924 | /// \brief Compare types for equality with respect to possibly compatible |
| 925 | /// function types (noreturn adjustment, implicit calling conventions). If any |
| 926 | /// of parameter and argument is not a function, just perform type comparison. |
| 927 | /// |
| 928 | /// \param Param the template parameter type. |
| 929 | /// |
| 930 | /// \param Arg the argument type. |
| 931 | bool Sema::isSameOrCompatibleFunctionType(CanQualType Param, |
| 932 | CanQualType Arg) { |
| 933 | const FunctionType *ParamFunction = Param->getAs<FunctionType>(), |
| 934 | *ArgFunction = Arg->getAs<FunctionType>(); |
| 935 | |
| 936 | // Just compare if not functions. |
| 937 | if (!ParamFunction || !ArgFunction) |
| 938 | return Param == Arg; |
| 939 | |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 940 | // Noreturn and noexcept adjustment. |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 941 | QualType AdjustedParam; |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 942 | if (IsFunctionConversion(Param, Arg, AdjustedParam)) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 943 | return Arg == Context.getCanonicalType(AdjustedParam); |
| 944 | |
| 945 | // FIXME: Compatible calling conventions. |
| 946 | |
| 947 | return Param == Arg; |
| 948 | } |
| 949 | |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 950 | /// \brief Deduce the template arguments by comparing the parameter type and |
| 951 | /// the argument type (C++ [temp.deduct.type]). |
| 952 | /// |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 953 | /// \param S the semantic analysis object within which we are deducing |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 954 | /// |
| 955 | /// \param TemplateParams the template parameters that we are deducing |
| 956 | /// |
| 957 | /// \param ParamIn the parameter type |
| 958 | /// |
| 959 | /// \param ArgIn the argument type |
| 960 | /// |
| 961 | /// \param Info information about the template argument deduction itself |
| 962 | /// |
| 963 | /// \param Deduced the deduced template arguments |
| 964 | /// |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 965 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 966 | /// how template argument deduction is performed. |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 967 | /// |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 968 | /// \param PartialOrdering Whether we're performing template argument deduction |
| 969 | /// in the context of partial ordering (C++0x [temp.deduct.partial]). |
| 970 | /// |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 971 | /// \returns the result of template argument deduction so far. Note that a |
| 972 | /// "success" result means that template argument deduction has not yet failed, |
| 973 | /// but it may still fail, later, for other reasons. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 974 | static Sema::TemplateDeductionResult |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 975 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 976 | TemplateParameterList *TemplateParams, |
| 977 | QualType ParamIn, QualType ArgIn, |
| 978 | TemplateDeductionInfo &Info, |
| 979 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 980 | unsigned TDF, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 981 | bool PartialOrdering, |
| 982 | bool DeducedFromArrayBound) { |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 983 | // We only want to look at the canonical types, since typedefs and |
| 984 | // sugar are not part of template argument deduction. |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 985 | QualType Param = S.Context.getCanonicalType(ParamIn); |
| 986 | QualType Arg = S.Context.getCanonicalType(ArgIn); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 987 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 988 | // If the argument type is a pack expansion, look at its pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 989 | // This isn't explicitly called out |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 990 | if (const PackExpansionType *ArgExpansion |
| 991 | = dyn_cast<PackExpansionType>(Arg)) |
| 992 | Arg = ArgExpansion->getPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 993 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 994 | if (PartialOrdering) { |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 995 | // C++11 [temp.deduct.partial]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 996 | // Before the partial ordering is done, certain transformations are |
| 997 | // performed on the types used for partial ordering: |
| 998 | // - 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] | 999 | const ReferenceType *ParamRef = Param->getAs<ReferenceType>(); |
| 1000 | if (ParamRef) |
| 1001 | Param = ParamRef->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1002 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1003 | // - If A is a reference type, A is replaced by the type referred to. |
| 1004 | const ReferenceType *ArgRef = Arg->getAs<ReferenceType>(); |
| 1005 | if (ArgRef) |
| 1006 | Arg = ArgRef->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1007 | |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1008 | if (ParamRef && ArgRef && S.Context.hasSameUnqualifiedType(Param, Arg)) { |
| 1009 | // C++11 [temp.deduct.partial]p9: |
| 1010 | // If, for a given type, deduction succeeds in both directions (i.e., |
| 1011 | // the types are identical after the transformations above) and both |
| 1012 | // P and A were reference types [...]: |
| 1013 | // - if [one type] was an lvalue reference and [the other type] was |
| 1014 | // not, [the other type] is not considered to be at least as |
| 1015 | // specialized as [the first type] |
| 1016 | // - if [one type] is more cv-qualified than [the other type], |
| 1017 | // [the other type] is not considered to be at least as specialized |
| 1018 | // as [the first type] |
| 1019 | // Objective-C ARC adds: |
| 1020 | // - [one type] has non-trivial lifetime, [the other type] has |
| 1021 | // __unsafe_unretained lifetime, and the types are otherwise |
| 1022 | // identical |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1023 | // |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1024 | // A is "considered to be at least as specialized" as P iff deduction |
| 1025 | // succeeds, so we model this as a deduction failure. Note that |
| 1026 | // [the first type] is P and [the other type] is A here; the standard |
| 1027 | // gets this backwards. |
Douglas Gregor | 85894a8 | 2011-04-30 17:07:52 +0000 | [diff] [blame] | 1028 | Qualifiers ParamQuals = Param.getQualifiers(); |
| 1029 | Qualifiers ArgQuals = Arg.getQualifiers(); |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1030 | if ((ParamRef->isLValueReferenceType() && |
| 1031 | !ArgRef->isLValueReferenceType()) || |
| 1032 | ParamQuals.isStrictSupersetOf(ArgQuals) || |
| 1033 | (ParamQuals.hasNonTrivialObjCLifetime() && |
| 1034 | ArgQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone && |
| 1035 | ParamQuals.withoutObjCLifetime() == |
| 1036 | ArgQuals.withoutObjCLifetime())) { |
| 1037 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1038 | Info.SecondArg = TemplateArgument(ArgIn); |
| 1039 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 6beabee | 2014-01-02 19:42:02 +0000 | [diff] [blame] | 1040 | } |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1041 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1042 | |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1043 | // C++11 [temp.deduct.partial]p7: |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1044 | // Remove any top-level cv-qualifiers: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1045 | // - 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] | 1046 | // version of P. |
| 1047 | Param = Param.getUnqualifiedType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1048 | // - 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] | 1049 | // version of A. |
| 1050 | Arg = Arg.getUnqualifiedType(); |
| 1051 | } else { |
| 1052 | // C++0x [temp.deduct.call]p4 bullet 1: |
| 1053 | // - If the original P is a reference type, the deduced A (i.e., the type |
| 1054 | // referred to by the reference) can be more cv-qualified than the |
| 1055 | // transformed A. |
| 1056 | if (TDF & TDF_ParamWithReferenceType) { |
| 1057 | Qualifiers Quals; |
| 1058 | QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals); |
| 1059 | Quals.setCVRQualifiers(Quals.getCVRQualifiers() & |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 1060 | Arg.getCVRQualifiers()); |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1061 | Param = S.Context.getQualifiedType(UnqualParam, Quals); |
| 1062 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1063 | |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1064 | if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) { |
| 1065 | // C++0x [temp.deduct.type]p10: |
| 1066 | // If P and A are function types that originated from deduction when |
| 1067 | // taking the address of a function template (14.8.2.2) or when deducing |
| 1068 | // 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] | 1069 | // Ai are parameters of the top-level parameter-type-list of P and A, |
| 1070 | // respectively, Pi is adjusted if it is an rvalue reference to a |
| 1071 | // cv-unqualified template parameter and Ai is an lvalue reference, in |
| 1072 | // 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] | 1073 | // type (i.e., T&& is changed to simply T). [ Note: As a result, when |
| 1074 | // 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] | 1075 | // deduced as X&. - end note ] |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1076 | TDF &= ~TDF_TopLevelParameterTypeList; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1077 | |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1078 | if (const RValueReferenceType *ParamRef |
| 1079 | = Param->getAs<RValueReferenceType>()) { |
| 1080 | if (isa<TemplateTypeParmType>(ParamRef->getPointeeType()) && |
| 1081 | !ParamRef->getPointeeType().getQualifiers()) |
| 1082 | if (Arg->isLValueReferenceType()) |
| 1083 | Param = ParamRef->getPointeeType(); |
| 1084 | } |
| 1085 | } |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1086 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1087 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1088 | // C++ [temp.deduct.type]p9: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1089 | // A template type argument T, a template template argument TT or a |
| 1090 | // 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] | 1091 | // the following forms: |
| 1092 | // |
| 1093 | // T |
| 1094 | // cv-list T |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1095 | if (const TemplateTypeParmType *TemplateTypeParm |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1096 | = Param->getAs<TemplateTypeParmType>()) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1097 | // Just skip any attempts to deduce from a placeholder type or a parameter |
| 1098 | // at a different depth. |
| 1099 | if (Arg->isPlaceholderType() || |
| 1100 | Info.getDeducedDepth() != TemplateTypeParm->getDepth()) |
Douglas Gregor | 4ea5dec | 2011-09-22 15:57:07 +0000 | [diff] [blame] | 1101 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1102 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1103 | unsigned Index = TemplateTypeParm->getIndex(); |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1104 | bool RecanonicalizeArg = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1105 | |
Douglas Gregor | 6045482 | 2009-07-22 20:02:25 +0000 | [diff] [blame] | 1106 | // If the argument type is an array type, move the qualifiers up to the |
| 1107 | // 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] | 1108 | if (isa<ArrayType>(Arg)) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1109 | Qualifiers Quals; |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1110 | Arg = S.Context.getUnqualifiedArrayType(Arg, Quals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1111 | if (Quals) { |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1112 | Arg = S.Context.getQualifiedType(Arg, Quals); |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1113 | RecanonicalizeArg = true; |
| 1114 | } |
| 1115 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1116 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1117 | // The argument type can not be less qualified than the parameter |
| 1118 | // type. |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1119 | if (!(TDF & TDF_IgnoreQualifiers) && |
| 1120 | hasInconsistentOrSupersetQualifiersOf(Param, Arg)) { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1121 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1122 | Info.FirstArg = TemplateArgument(Param); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1123 | Info.SecondArg = TemplateArgument(Arg); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1124 | return Sema::TDK_Underqualified; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1125 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1126 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1127 | assert(TemplateTypeParm->getDepth() == Info.getDeducedDepth() && |
| 1128 | "saw template type parameter with wrong depth"); |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1129 | assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function"); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1130 | QualType DeducedType = Arg; |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1131 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1132 | // Remove any qualifiers on the parameter from the deduced type. |
| 1133 | // We checked the qualifiers for consistency above. |
| 1134 | Qualifiers DeducedQs = DeducedType.getQualifiers(); |
| 1135 | Qualifiers ParamQs = Param.getQualifiers(); |
| 1136 | DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers()); |
| 1137 | if (ParamQs.hasObjCGCAttr()) |
| 1138 | DeducedQs.removeObjCGCAttr(); |
| 1139 | if (ParamQs.hasAddressSpace()) |
| 1140 | DeducedQs.removeAddressSpace(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1141 | if (ParamQs.hasObjCLifetime()) |
| 1142 | DeducedQs.removeObjCLifetime(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1143 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1144 | // Objective-C ARC: |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1145 | // If template deduction would produce a lifetime qualifier on a type |
| 1146 | // that is not a lifetime type, template argument deduction fails. |
| 1147 | if (ParamQs.hasObjCLifetime() && !DeducedType->isObjCLifetimeType() && |
| 1148 | !DeducedType->isDependentType()) { |
| 1149 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1150 | Info.FirstArg = TemplateArgument(Param); |
| 1151 | Info.SecondArg = TemplateArgument(Arg); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1152 | return Sema::TDK_Underqualified; |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1153 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1154 | |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1155 | // Objective-C ARC: |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1156 | // If template deduction would produce an argument type with lifetime type |
| 1157 | // but no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1158 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1159 | DeducedType->isObjCLifetimeType() && |
| 1160 | !DeducedQs.hasObjCLifetime()) |
| 1161 | DeducedQs.setObjCLifetime(Qualifiers::OCL_Strong); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1162 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1163 | DeducedType = S.Context.getQualifiedType(DeducedType.getUnqualifiedType(), |
| 1164 | DeducedQs); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1165 | |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1166 | if (RecanonicalizeArg) |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1167 | DeducedType = S.Context.getCanonicalType(DeducedType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1168 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1169 | DeducedTemplateArgument NewDeduced(DeducedType, DeducedFromArrayBound); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1170 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1171 | Deduced[Index], |
| 1172 | NewDeduced); |
| 1173 | if (Result.isNull()) { |
| 1174 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1175 | Info.FirstArg = Deduced[Index]; |
| 1176 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1177 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1178 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1179 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1180 | Deduced[Index] = Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1181 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1184 | // Set up the template argument deduction information for a failure. |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1185 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1186 | Info.SecondArg = TemplateArgument(ArgIn); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1187 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 1188 | // If the parameter is an already-substituted template parameter |
| 1189 | // pack, do nothing: we don't know which of its arguments to look |
| 1190 | // at, so we have to wait until all of the parameter packs in this |
| 1191 | // expansion have arguments. |
| 1192 | if (isa<SubstTemplateTypeParmPackType>(Param)) |
| 1193 | return Sema::TDK_Success; |
| 1194 | |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1195 | // Check the cv-qualifiers on the parameter and argument types. |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1196 | CanQualType CanParam = S.Context.getCanonicalType(Param); |
| 1197 | CanQualType CanArg = S.Context.getCanonicalType(Arg); |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1198 | if (!(TDF & TDF_IgnoreQualifiers)) { |
| 1199 | if (TDF & TDF_ParamWithReferenceType) { |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1200 | if (hasInconsistentOrSupersetQualifiersOf(Param, Arg)) |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1201 | return Sema::TDK_NonDeducedMismatch; |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 1202 | } else if (!IsPossiblyOpaquelyQualifiedType(Param)) { |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1203 | if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1204 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1205 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1206 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1207 | // If the parameter type is not dependent, there is nothing to deduce. |
| 1208 | if (!Param->isDependentType()) { |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1209 | if (!(TDF & TDF_SkipNonDependent)) { |
| 1210 | bool NonDeduced = (TDF & TDF_InOverloadResolution)? |
| 1211 | !S.isSameOrCompatibleFunctionType(CanParam, CanArg) : |
| 1212 | Param != Arg; |
| 1213 | if (NonDeduced) { |
| 1214 | return Sema::TDK_NonDeducedMismatch; |
| 1215 | } |
| 1216 | } |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1217 | return Sema::TDK_Success; |
| 1218 | } |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1219 | } else if (!Param->isDependentType()) { |
| 1220 | CanQualType ParamUnqualType = CanParam.getUnqualifiedType(), |
| 1221 | ArgUnqualType = CanArg.getUnqualifiedType(); |
| 1222 | bool Success = (TDF & TDF_InOverloadResolution)? |
| 1223 | S.isSameOrCompatibleFunctionType(ParamUnqualType, |
| 1224 | ArgUnqualType) : |
| 1225 | ParamUnqualType == ArgUnqualType; |
| 1226 | if (Success) |
| 1227 | return Sema::TDK_Success; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1228 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1229 | |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1230 | switch (Param->getTypeClass()) { |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1231 | // Non-canonical types cannot appear here. |
| 1232 | #define NON_CANONICAL_TYPE(Class, Base) \ |
| 1233 | case Type::Class: llvm_unreachable("deducing non-canonical type: " #Class); |
| 1234 | #define TYPE(Class, Base) |
| 1235 | #include "clang/AST/TypeNodes.def" |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1236 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1237 | case Type::TemplateTypeParm: |
| 1238 | case Type::SubstTemplateTypeParmPack: |
| 1239 | llvm_unreachable("Type nodes handled above"); |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1240 | |
| 1241 | // These types cannot be dependent, so simply check whether the types are |
| 1242 | // the same. |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1243 | case Type::Builtin: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1244 | case Type::VariableArray: |
| 1245 | case Type::Vector: |
| 1246 | case Type::FunctionNoProto: |
| 1247 | case Type::Record: |
| 1248 | case Type::Enum: |
| 1249 | case Type::ObjCObject: |
| 1250 | case Type::ObjCInterface: |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1251 | case Type::ObjCObjectPointer: { |
| 1252 | if (TDF & TDF_SkipNonDependent) |
| 1253 | return Sema::TDK_Success; |
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 | if (TDF & TDF_IgnoreQualifiers) { |
| 1256 | Param = Param.getUnqualifiedType(); |
| 1257 | Arg = Arg.getUnqualifiedType(); |
| 1258 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1259 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1260 | return Param == Arg? Sema::TDK_Success : Sema::TDK_NonDeducedMismatch; |
| 1261 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1262 | |
| 1263 | // _Complex T [placeholder extension] |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1264 | case Type::Complex: |
| 1265 | if (const ComplexType *ComplexArg = Arg->getAs<ComplexType>()) |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1266 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1267 | cast<ComplexType>(Param)->getElementType(), |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1268 | ComplexArg->getElementType(), |
| 1269 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1270 | |
| 1271 | return Sema::TDK_NonDeducedMismatch; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1272 | |
| 1273 | // _Atomic T [extension] |
| 1274 | case Type::Atomic: |
| 1275 | if (const AtomicType *AtomicArg = Arg->getAs<AtomicType>()) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1276 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1277 | cast<AtomicType>(Param)->getValueType(), |
| 1278 | AtomicArg->getValueType(), |
| 1279 | Info, Deduced, TDF); |
| 1280 | |
| 1281 | return Sema::TDK_NonDeducedMismatch; |
| 1282 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1283 | // T * |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1284 | case Type::Pointer: { |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1285 | QualType PointeeType; |
| 1286 | if (const PointerType *PointerArg = Arg->getAs<PointerType>()) { |
| 1287 | PointeeType = PointerArg->getPointeeType(); |
| 1288 | } else if (const ObjCObjectPointerType *PointerArg |
| 1289 | = Arg->getAs<ObjCObjectPointerType>()) { |
| 1290 | PointeeType = PointerArg->getPointeeType(); |
| 1291 | } else { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1292 | return Sema::TDK_NonDeducedMismatch; |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1293 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1294 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1295 | unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1296 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1297 | cast<PointerType>(Param)->getPointeeType(), |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1298 | PointeeType, |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1299 | Info, Deduced, SubTDF); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1300 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1301 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1302 | // T & |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1303 | case Type::LValueReference: { |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 1304 | const LValueReferenceType *ReferenceArg = |
| 1305 | Arg->getAs<LValueReferenceType>(); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1306 | if (!ReferenceArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1307 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1308 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1309 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1310 | cast<LValueReferenceType>(Param)->getPointeeType(), |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1311 | ReferenceArg->getPointeeType(), Info, Deduced, 0); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1312 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1313 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1314 | // T && [C++0x] |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1315 | case Type::RValueReference: { |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 1316 | const RValueReferenceType *ReferenceArg = |
| 1317 | Arg->getAs<RValueReferenceType>(); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1318 | if (!ReferenceArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1319 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1320 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1321 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1322 | cast<RValueReferenceType>(Param)->getPointeeType(), |
| 1323 | ReferenceArg->getPointeeType(), |
| 1324 | Info, Deduced, 0); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1325 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1326 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1327 | // T [] (implied, but not stated explicitly) |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1328 | case Type::IncompleteArray: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1329 | const IncompleteArrayType *IncompleteArrayArg = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1330 | S.Context.getAsIncompleteArrayType(Arg); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1331 | if (!IncompleteArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1332 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1333 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1334 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1335 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1336 | S.Context.getAsIncompleteArrayType(Param)->getElementType(), |
| 1337 | IncompleteArrayArg->getElementType(), |
| 1338 | Info, Deduced, SubTDF); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1339 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1340 | |
| 1341 | // T [integer-constant] |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1342 | case Type::ConstantArray: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1343 | const ConstantArrayType *ConstantArrayArg = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1344 | S.Context.getAsConstantArrayType(Arg); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1345 | if (!ConstantArrayArg) |
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 | |
| 1348 | const ConstantArrayType *ConstantArrayParm = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1349 | S.Context.getAsConstantArrayType(Param); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1350 | if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1351 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1352 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1353 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1354 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1355 | ConstantArrayParm->getElementType(), |
| 1356 | ConstantArrayArg->getElementType(), |
| 1357 | Info, Deduced, SubTDF); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1360 | // type [i] |
| 1361 | case Type::DependentSizedArray: { |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1362 | const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1363 | if (!ArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1364 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1365 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1366 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
| 1367 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1368 | // Check the element type of the arrays |
| 1369 | const DependentSizedArrayType *DependentArrayParm |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1370 | = S.Context.getAsDependentSizedArrayType(Param); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1371 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1372 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1373 | DependentArrayParm->getElementType(), |
| 1374 | ArrayArg->getElementType(), |
| 1375 | Info, Deduced, SubTDF)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1376 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1377 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1378 | // Determine the array bound is something we can deduce. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1379 | NonTypeTemplateParmDecl *NTTP |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1380 | = getDeducedParameterFromExpr(Info, DependentArrayParm->getSizeExpr()); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1381 | if (!NTTP) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1382 | return Sema::TDK_Success; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1383 | |
| 1384 | // We can perform template argument deduction for the given non-type |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1385 | // template parameter. |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1386 | assert(NTTP->getDepth() == Info.getDeducedDepth() && |
| 1387 | "saw non-type template parameter with wrong depth"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1388 | if (const ConstantArrayType *ConstantArrayArg |
Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1389 | = dyn_cast<ConstantArrayType>(ArrayArg)) { |
| 1390 | llvm::APSInt Size(ConstantArrayArg->getSize()); |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1391 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, Size, |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1392 | S.Context.getSizeType(), |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1393 | /*ArrayBound=*/true, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1394 | Info, Deduced); |
Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1395 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1396 | if (const DependentSizedArrayType *DependentArrayArg |
| 1397 | = dyn_cast<DependentSizedArrayType>(ArrayArg)) |
Douglas Gregor | 7a49ead | 2010-12-22 23:15:38 +0000 | [diff] [blame] | 1398 | if (DependentArrayArg->getSizeExpr()) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1399 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
Douglas Gregor | 7a49ead | 2010-12-22 23:15:38 +0000 | [diff] [blame] | 1400 | DependentArrayArg->getSizeExpr(), |
| 1401 | Info, Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1402 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1403 | // Incomplete type does not match a dependently-sized array type |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1404 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1405 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1406 | |
| 1407 | // type(*)(T) |
| 1408 | // T(*)() |
| 1409 | // T(*)(T) |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1410 | case Type::FunctionProto: { |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1411 | unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | const FunctionProtoType *FunctionProtoArg = |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1413 | dyn_cast<FunctionProtoType>(Arg); |
| 1414 | if (!FunctionProtoArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1415 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1416 | |
| 1417 | const FunctionProtoType *FunctionProtoParam = |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1418 | cast<FunctionProtoType>(Param); |
Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1419 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1420 | if (FunctionProtoParam->getTypeQuals() |
Douglas Gregor | 54e462a | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1421 | != FunctionProtoArg->getTypeQuals() || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1422 | FunctionProtoParam->getRefQualifier() |
Douglas Gregor | 54e462a | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1423 | != FunctionProtoArg->getRefQualifier() || |
| 1424 | FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1425 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1426 | |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1427 | // Check return types. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1428 | if (Sema::TemplateDeductionResult Result = |
| 1429 | DeduceTemplateArgumentsByTypeMatch( |
| 1430 | S, TemplateParams, FunctionProtoParam->getReturnType(), |
| 1431 | FunctionProtoArg->getReturnType(), Info, Deduced, 0)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1432 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1433 | |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1434 | return DeduceTemplateArguments( |
| 1435 | S, TemplateParams, FunctionProtoParam->param_type_begin(), |
| 1436 | FunctionProtoParam->getNumParams(), |
| 1437 | FunctionProtoArg->param_type_begin(), |
| 1438 | FunctionProtoArg->getNumParams(), Info, Deduced, SubTDF); |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1439 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1440 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1441 | case Type::InjectedClassName: { |
| 1442 | // Treat a template's injected-class-name as if the template |
| 1443 | // specialization type had been used. |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1444 | Param = cast<InjectedClassNameType>(Param) |
| 1445 | ->getInjectedSpecializationType(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1446 | assert(isa<TemplateSpecializationType>(Param) && |
| 1447 | "injected class name is not a template specialization type"); |
| 1448 | // fall through |
| 1449 | } |
| 1450 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1451 | // template-name<T> (where template-name refers to a class template) |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1452 | // template-name<i> |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1453 | // TT<T> |
| 1454 | // TT<i> |
| 1455 | // TT<> |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1456 | case Type::TemplateSpecialization: { |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1457 | const TemplateSpecializationType *SpecParam = |
| 1458 | cast<TemplateSpecializationType>(Param); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1459 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1460 | // When Arg cannot be a derived class, we can just try to deduce template |
| 1461 | // arguments from the template-id. |
| 1462 | const RecordType *RecordT = Arg->getAs<RecordType>(); |
| 1463 | if (!(TDF & TDF_DerivedClass) || !RecordT) |
| 1464 | return DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg, Info, |
| 1465 | Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1466 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1467 | SmallVector<DeducedTemplateArgument, 8> DeducedOrig(Deduced.begin(), |
| 1468 | Deduced.end()); |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1469 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1470 | Sema::TemplateDeductionResult Result = DeduceTemplateArguments( |
| 1471 | S, TemplateParams, SpecParam, Arg, Info, Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1472 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1473 | if (Result == Sema::TDK_Success) |
| 1474 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1475 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1476 | // We cannot inspect base classes as part of deduction when the type |
| 1477 | // is incomplete, so either instantiate any templates necessary to |
| 1478 | // complete the type, or skip over it if it cannot be completed. |
| 1479 | if (!S.isCompleteType(Info.getLocation(), Arg)) |
| 1480 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1481 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1482 | // C++14 [temp.deduct.call] p4b3: |
| 1483 | // If P is a class and P has the form simple-template-id, then the |
| 1484 | // transformed A can be a derived class of the deduced A. Likewise if |
| 1485 | // P is a pointer to a class of the form simple-template-id, the |
| 1486 | // transformed A can be a pointer to a derived class pointed to by the |
| 1487 | // deduced A. |
| 1488 | // |
| 1489 | // These alternatives are considered only if type deduction would |
| 1490 | // otherwise fail. If they yield more than one possible deduced A, the |
| 1491 | // type deduction fails. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1492 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1493 | // Reset the incorrectly deduced argument from above. |
| 1494 | Deduced = DeducedOrig; |
| 1495 | |
| 1496 | // Use data recursion to crawl through the list of base classes. |
| 1497 | // Visited contains the set of nodes we have already visited, while |
| 1498 | // ToVisit is our stack of records that we still need to visit. |
| 1499 | llvm::SmallPtrSet<const RecordType *, 8> Visited; |
| 1500 | SmallVector<const RecordType *, 8> ToVisit; |
| 1501 | ToVisit.push_back(RecordT); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1502 | bool Successful = false; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1503 | SmallVector<DeducedTemplateArgument, 8> SuccessfulDeduced; |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1504 | while (!ToVisit.empty()) { |
| 1505 | // Retrieve the next class in the inheritance hierarchy. |
| 1506 | const RecordType *NextT = ToVisit.pop_back_val(); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1507 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1508 | // If we have already seen this type, skip it. |
| 1509 | if (!Visited.insert(NextT).second) |
| 1510 | continue; |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1511 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1512 | // If this is a base class, try to perform template argument |
| 1513 | // deduction from it. |
| 1514 | if (NextT != RecordT) { |
| 1515 | TemplateDeductionInfo BaseInfo(Info.getLocation()); |
| 1516 | Sema::TemplateDeductionResult BaseResult = |
| 1517 | DeduceTemplateArguments(S, TemplateParams, SpecParam, |
| 1518 | QualType(NextT, 0), BaseInfo, Deduced); |
| 1519 | |
| 1520 | // If template argument deduction for this base was successful, |
| 1521 | // note that we had some success. Otherwise, ignore any deductions |
| 1522 | // from this base class. |
| 1523 | if (BaseResult == Sema::TDK_Success) { |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1524 | // If we've already seen some success, then deduction fails due to |
| 1525 | // an ambiguity (temp.deduct.call p5). |
| 1526 | if (Successful) |
| 1527 | return Sema::TDK_MiscellaneousDeductionFailure; |
| 1528 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1529 | Successful = true; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1530 | std::swap(SuccessfulDeduced, Deduced); |
| 1531 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1532 | Info.Param = BaseInfo.Param; |
| 1533 | Info.FirstArg = BaseInfo.FirstArg; |
| 1534 | Info.SecondArg = BaseInfo.SecondArg; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | Deduced = DeducedOrig; |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1538 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1539 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1540 | // Visit base classes |
| 1541 | CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl()); |
| 1542 | for (const auto &Base : Next->bases()) { |
| 1543 | assert(Base.getType()->isRecordType() && |
| 1544 | "Base class that isn't a record?"); |
| 1545 | ToVisit.push_back(Base.getType()->getAs<RecordType>()); |
| 1546 | } |
| 1547 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1548 | |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1549 | if (Successful) { |
| 1550 | std::swap(SuccessfulDeduced, Deduced); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1551 | return Sema::TDK_Success; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1552 | } |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1553 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1554 | return Result; |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1557 | // T type::* |
| 1558 | // T T::* |
| 1559 | // T (type::*)() |
| 1560 | // type (T::*)() |
| 1561 | // type (type::*)(T) |
| 1562 | // type (T::*)(T) |
| 1563 | // T (type::*)(T) |
| 1564 | // T (T::*)() |
| 1565 | // T (T::*)(T) |
| 1566 | case Type::MemberPointer: { |
| 1567 | const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); |
| 1568 | const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); |
| 1569 | if (!MemPtrArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1570 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1571 | |
David Majnemer | a381cda | 2015-11-30 20:34:28 +0000 | [diff] [blame] | 1572 | QualType ParamPointeeType = MemPtrParam->getPointeeType(); |
| 1573 | if (ParamPointeeType->isFunctionType()) |
| 1574 | S.adjustMemberFunctionCC(ParamPointeeType, /*IsStatic=*/true, |
| 1575 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
| 1576 | QualType ArgPointeeType = MemPtrArg->getPointeeType(); |
| 1577 | if (ArgPointeeType->isFunctionType()) |
| 1578 | S.adjustMemberFunctionCC(ArgPointeeType, /*IsStatic=*/true, |
| 1579 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
| 1580 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1581 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1582 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
David Majnemer | a381cda | 2015-11-30 20:34:28 +0000 | [diff] [blame] | 1583 | ParamPointeeType, |
| 1584 | ArgPointeeType, |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1585 | Info, Deduced, |
| 1586 | TDF & TDF_IgnoreQualifiers)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1587 | return Result; |
| 1588 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1589 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1590 | QualType(MemPtrParam->getClass(), 0), |
| 1591 | QualType(MemPtrArg->getClass(), 0), |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1592 | Info, Deduced, |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1593 | TDF & TDF_IgnoreQualifiers); |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
Anders Carlsson | 15f1dd1 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1596 | // (clang extension) |
| 1597 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1598 | // type(^)(T) |
| 1599 | // T(^)() |
| 1600 | // T(^)(T) |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1601 | case Type::BlockPointer: { |
| 1602 | const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); |
| 1603 | const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1604 | |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1605 | if (!BlockPtrArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1606 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1607 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1608 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1609 | BlockPtrParam->getPointeeType(), |
| 1610 | BlockPtrArg->getPointeeType(), |
| 1611 | Info, Deduced, 0); |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1614 | // (clang extension) |
| 1615 | // |
| 1616 | // T __attribute__(((ext_vector_type(<integral constant>)))) |
| 1617 | case Type::ExtVector: { |
| 1618 | const ExtVectorType *VectorParam = cast<ExtVectorType>(Param); |
| 1619 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1620 | // Make sure that the vectors have the same number of elements. |
| 1621 | if (VectorParam->getNumElements() != VectorArg->getNumElements()) |
| 1622 | return Sema::TDK_NonDeducedMismatch; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1623 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1624 | // Perform deduction on the element types. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1625 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1626 | VectorParam->getElementType(), |
| 1627 | VectorArg->getElementType(), |
| 1628 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1629 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1630 | |
| 1631 | if (const DependentSizedExtVectorType *VectorArg |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1632 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1633 | // We can't check the number of elements, since the argument has a |
| 1634 | // dependent number of elements. This can only occur during partial |
| 1635 | // ordering. |
| 1636 | |
| 1637 | // Perform deduction on the element types. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1638 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1639 | VectorParam->getElementType(), |
| 1640 | VectorArg->getElementType(), |
| 1641 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1642 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1643 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1644 | return Sema::TDK_NonDeducedMismatch; |
| 1645 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1646 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1647 | // (clang extension) |
| 1648 | // |
| 1649 | // T __attribute__(((ext_vector_type(N)))) |
| 1650 | case Type::DependentSizedExtVector: { |
| 1651 | const DependentSizedExtVectorType *VectorParam |
| 1652 | = cast<DependentSizedExtVectorType>(Param); |
| 1653 | |
| 1654 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1655 | // Perform deduction on the element types. |
| 1656 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1657 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1658 | VectorParam->getElementType(), |
| 1659 | VectorArg->getElementType(), |
| 1660 | Info, Deduced, TDF)) |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1661 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1662 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1663 | // Perform deduction on the vector size, if we can. |
| 1664 | NonTypeTemplateParmDecl *NTTP |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1665 | = getDeducedParameterFromExpr(Info, VectorParam->getSizeExpr()); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1666 | if (!NTTP) |
| 1667 | return Sema::TDK_Success; |
| 1668 | |
| 1669 | llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); |
| 1670 | ArgSize = VectorArg->getNumElements(); |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1671 | // Note that we use the "array bound" rules here; just like in that |
| 1672 | // case, we don't have any particular type for the vector size, but |
| 1673 | // we can provide one if necessary. |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1674 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1675 | S.Context.IntTy, true, Info, |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 1676 | Deduced); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1677 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1678 | |
| 1679 | if (const DependentSizedExtVectorType *VectorArg |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1680 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1681 | // Perform deduction on the element types. |
| 1682 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1683 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1684 | VectorParam->getElementType(), |
| 1685 | VectorArg->getElementType(), |
| 1686 | Info, Deduced, TDF)) |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1687 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1688 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1689 | // Perform deduction on the vector size, if we can. |
| 1690 | NonTypeTemplateParmDecl *NTTP |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1691 | = getDeducedParameterFromExpr(Info, VectorParam->getSizeExpr()); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1692 | if (!NTTP) |
| 1693 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1694 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1695 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 1696 | VectorArg->getSizeExpr(), |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1697 | Info, Deduced); |
| 1698 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1699 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1700 | return Sema::TDK_NonDeducedMismatch; |
| 1701 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1702 | |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1703 | case Type::TypeOfExpr: |
| 1704 | case Type::TypeOf: |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 1705 | case Type::DependentName: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1706 | case Type::UnresolvedUsing: |
| 1707 | case Type::Decltype: |
| 1708 | case Type::UnaryTransform: |
| 1709 | case Type::Auto: |
| 1710 | case Type::DependentTemplateSpecialization: |
| 1711 | case Type::PackExpansion: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 1712 | case Type::Pipe: |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1713 | // No template argument deduction for these types |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1714 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1717 | llvm_unreachable("Invalid Type Class!"); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1720 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1721 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1722 | TemplateParameterList *TemplateParams, |
| 1723 | const TemplateArgument &Param, |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1724 | TemplateArgument Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1725 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 1726 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1727 | // If the template argument is a pack expansion, perform template argument |
| 1728 | // deduction against the pattern of that expansion. This only occurs during |
| 1729 | // partial ordering. |
| 1730 | if (Arg.isPackExpansion()) |
| 1731 | Arg = Arg.getPackExpansionPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1732 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1733 | switch (Param.getKind()) { |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1734 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1735 | llvm_unreachable("Null template argument in parameter list"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1736 | |
| 1737 | case TemplateArgument::Type: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1738 | if (Arg.getKind() == TemplateArgument::Type) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1739 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1740 | Param.getAsType(), |
| 1741 | Arg.getAsType(), |
| 1742 | Info, Deduced, 0); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1743 | Info.FirstArg = Param; |
| 1744 | Info.SecondArg = Arg; |
| 1745 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1746 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1747 | case TemplateArgument::Template: |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1748 | if (Arg.getKind() == TemplateArgument::Template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1749 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1750 | Param.getAsTemplate(), |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1751 | Arg.getAsTemplate(), Info, Deduced); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1752 | Info.FirstArg = Param; |
| 1753 | Info.SecondArg = Arg; |
| 1754 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1755 | |
| 1756 | case TemplateArgument::TemplateExpansion: |
| 1757 | llvm_unreachable("caller should handle pack expansions"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1758 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1759 | case TemplateArgument::Declaration: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1760 | if (Arg.getKind() == TemplateArgument::Declaration && |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 1761 | isSameDeclaration(Param.getAsDecl(), Arg.getAsDecl())) |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 1762 | return Sema::TDK_Success; |
| 1763 | |
| 1764 | Info.FirstArg = Param; |
| 1765 | Info.SecondArg = Arg; |
| 1766 | return Sema::TDK_NonDeducedMismatch; |
| 1767 | |
| 1768 | case TemplateArgument::NullPtr: |
| 1769 | if (Arg.getKind() == TemplateArgument::NullPtr && |
| 1770 | S.Context.hasSameType(Param.getNullPtrType(), Arg.getNullPtrType())) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1771 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1772 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1773 | Info.FirstArg = Param; |
| 1774 | Info.SecondArg = Arg; |
| 1775 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1776 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1777 | case TemplateArgument::Integral: |
| 1778 | if (Arg.getKind() == TemplateArgument::Integral) { |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1779 | if (hasSameExtendedValue(Param.getAsIntegral(), Arg.getAsIntegral())) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1780 | return Sema::TDK_Success; |
| 1781 | |
| 1782 | Info.FirstArg = Param; |
| 1783 | Info.SecondArg = Arg; |
| 1784 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1785 | } |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1786 | |
| 1787 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 1788 | Info.FirstArg = Param; |
| 1789 | Info.SecondArg = Arg; |
| 1790 | return Sema::TDK_NonDeducedMismatch; |
| 1791 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1792 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1793 | Info.FirstArg = Param; |
| 1794 | Info.SecondArg = Arg; |
| 1795 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1796 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1797 | case TemplateArgument::Expression: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1798 | if (NonTypeTemplateParmDecl *NTTP |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1799 | = getDeducedParameterFromExpr(Info, Param.getAsExpr())) { |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1800 | if (Arg.getKind() == TemplateArgument::Integral) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1801 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1802 | Arg.getAsIntegral(), |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1803 | Arg.getIntegralType(), |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1804 | /*ArrayBound=*/false, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1805 | Info, Deduced); |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 1806 | if (Arg.getKind() == TemplateArgument::NullPtr) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1807 | return DeduceNullPtrTemplateArgument(S, TemplateParams, NTTP, |
| 1808 | Arg.getNullPtrType(), |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 1809 | Info, Deduced); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1810 | if (Arg.getKind() == TemplateArgument::Expression) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1811 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 1812 | Arg.getAsExpr(), Info, Deduced); |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 1813 | if (Arg.getKind() == TemplateArgument::Declaration) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1814 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 1815 | Arg.getAsDecl(), |
| 1816 | Arg.getParamTypeForDecl(), |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 1817 | Info, Deduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1818 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1819 | Info.FirstArg = Param; |
| 1820 | Info.SecondArg = Arg; |
| 1821 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1822 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1824 | // Can't deduce anything, but that's okay. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1825 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1826 | } |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 1827 | case TemplateArgument::Pack: |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1828 | llvm_unreachable("Argument packs should be expanded by the caller!"); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1829 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1830 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1831 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1834 | /// \brief Determine whether there is a template argument to be used for |
| 1835 | /// deduction. |
| 1836 | /// |
| 1837 | /// This routine "expands" argument packs in-place, overriding its input |
| 1838 | /// parameters so that \c Args[ArgIdx] will be the available template argument. |
| 1839 | /// |
| 1840 | /// \returns true if there is another template argument (which will be at |
| 1841 | /// \c Args[ArgIdx]), false otherwise. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1842 | static bool hasTemplateArgumentForDeduction(ArrayRef<TemplateArgument> &Args, |
| 1843 | unsigned &ArgIdx) { |
| 1844 | if (ArgIdx == Args.size()) |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1845 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1846 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1847 | const TemplateArgument &Arg = Args[ArgIdx]; |
| 1848 | if (Arg.getKind() != TemplateArgument::Pack) |
| 1849 | return true; |
| 1850 | |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1851 | assert(ArgIdx == Args.size() - 1 && "Pack not at the end of argument list?"); |
| 1852 | Args = Arg.pack_elements(); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1853 | ArgIdx = 0; |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1854 | return ArgIdx < Args.size(); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1857 | /// \brief Determine whether the given set of template arguments has a pack |
| 1858 | /// expansion that is not the last template argument. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1859 | static bool hasPackExpansionBeforeEnd(ArrayRef<TemplateArgument> Args) { |
| 1860 | bool FoundPackExpansion = false; |
| 1861 | for (const auto &A : Args) { |
| 1862 | if (FoundPackExpansion) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1863 | return true; |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1864 | |
| 1865 | if (A.getKind() == TemplateArgument::Pack) |
| 1866 | return hasPackExpansionBeforeEnd(A.pack_elements()); |
| 1867 | |
| 1868 | if (A.isPackExpansion()) |
| 1869 | FoundPackExpansion = true; |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1870 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1871 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1872 | return false; |
| 1873 | } |
| 1874 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1875 | static Sema::TemplateDeductionResult |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1876 | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1877 | ArrayRef<TemplateArgument> Params, |
| 1878 | ArrayRef<TemplateArgument> Args, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1879 | TemplateDeductionInfo &Info, |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1880 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 1881 | bool NumberOfArgumentsMustMatch) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1882 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1883 | // If the template argument list of P contains a pack expansion that is not |
| 1884 | // the last template argument, the entire template argument list is a |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1885 | // non-deduced context. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1886 | if (hasPackExpansionBeforeEnd(Params)) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1887 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1888 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1889 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1890 | // If P has a form that contains <T> or <i>, then each argument Pi of the |
| 1891 | // respective template argument list P is compared with the corresponding |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1892 | // argument Ai of the corresponding template argument list of A. |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1893 | unsigned ArgIdx = 0, ParamIdx = 0; |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1894 | for (; hasTemplateArgumentForDeduction(Params, ParamIdx); ++ParamIdx) { |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1895 | if (!Params[ParamIdx].isPackExpansion()) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1896 | // The simple case: deduce template arguments by matching Pi and Ai. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1897 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1898 | // Check whether we have enough arguments. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1899 | if (!hasTemplateArgumentForDeduction(Args, ArgIdx)) |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1900 | return NumberOfArgumentsMustMatch ? Sema::TDK_TooFewArguments |
| 1901 | : Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1902 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1903 | if (Args[ArgIdx].isPackExpansion()) { |
| 1904 | // FIXME: We follow the logic of C++0x [temp.deduct.type]p22 here, |
| 1905 | // but applied to pack expansions that are template arguments. |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 1906 | return Sema::TDK_MiscellaneousDeductionFailure; |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1907 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1908 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1909 | // Perform deduction for this Pi/Ai pair. |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1910 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1911 | = DeduceTemplateArguments(S, TemplateParams, |
| 1912 | Params[ParamIdx], Args[ArgIdx], |
| 1913 | Info, Deduced)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1914 | return Result; |
| 1915 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1916 | // Move to the next argument. |
| 1917 | ++ArgIdx; |
| 1918 | continue; |
| 1919 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1920 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1921 | // The parameter is a pack expansion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1922 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1923 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1924 | // If Pi is a pack expansion, then the pattern of Pi is compared with |
| 1925 | // each remaining argument in the template argument list of A. Each |
| 1926 | // comparison deduces template arguments for subsequent positions in the |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1927 | // template parameter packs expanded by Pi. |
| 1928 | TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1929 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1930 | // FIXME: If there are no remaining arguments, we can bail out early |
| 1931 | // and set any deduced parameter packs to an empty argument pack. |
| 1932 | // The latter part of this is a (minor) correctness issue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1933 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1934 | // Prepare to deduce the packs within the pattern. |
| 1935 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1936 | |
| 1937 | // Keep track of the deduced template arguments for each parameter pack |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1938 | // expanded by this pack expansion (the outer index) and for each |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1939 | // template argument (the inner SmallVectors). |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1940 | bool HasAnyArguments = false; |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1941 | for (; hasTemplateArgumentForDeduction(Args, ArgIdx); ++ArgIdx) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1942 | HasAnyArguments = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1943 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1944 | // Deduce template arguments from the pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1945 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1946 | = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx], |
| 1947 | Info, Deduced)) |
| 1948 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1949 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1950 | PackScope.nextPackElement(); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1951 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1952 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1953 | // Build argument packs for each of the parameter packs expanded by this |
| 1954 | // pack expansion. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1955 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1956 | return Result; |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1957 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1958 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1959 | return Sema::TDK_Success; |
| 1960 | } |
| 1961 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1962 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1963 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1964 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1965 | const TemplateArgumentList &ParamList, |
| 1966 | const TemplateArgumentList &ArgList, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1967 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 1968 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1969 | return DeduceTemplateArguments(S, TemplateParams, ParamList.asArray(), |
| 1970 | ArgList.asArray(), Info, Deduced, false); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1973 | /// \brief Determine whether two template arguments are the same. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1974 | static bool isSameTemplateArg(ASTContext &Context, |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1975 | const TemplateArgument &X, |
| 1976 | const TemplateArgument &Y) { |
| 1977 | if (X.getKind() != Y.getKind()) |
| 1978 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1979 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1980 | switch (X.getKind()) { |
| 1981 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1982 | llvm_unreachable("Comparing NULL template argument"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1983 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1984 | case TemplateArgument::Type: |
| 1985 | return Context.getCanonicalType(X.getAsType()) == |
| 1986 | Context.getCanonicalType(Y.getAsType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1987 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1988 | case TemplateArgument::Declaration: |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 1989 | return isSameDeclaration(X.getAsDecl(), Y.getAsDecl()); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 1990 | |
| 1991 | case TemplateArgument::NullPtr: |
| 1992 | return Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1993 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1994 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1995 | case TemplateArgument::TemplateExpansion: |
| 1996 | return Context.getCanonicalTemplateName( |
| 1997 | X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() == |
| 1998 | Context.getCanonicalTemplateName( |
| 1999 | Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2000 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2001 | case TemplateArgument::Integral: |
Richard Smith | 993f203 | 2016-12-25 20:21:12 +0000 | [diff] [blame] | 2002 | return hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2003 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2004 | case TemplateArgument::Expression: { |
| 2005 | llvm::FoldingSetNodeID XID, YID; |
| 2006 | X.getAsExpr()->Profile(XID, Context, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2007 | Y.getAsExpr()->Profile(YID, Context, true); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2008 | return XID == YID; |
| 2009 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2010 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2011 | case TemplateArgument::Pack: |
| 2012 | if (X.pack_size() != Y.pack_size()) |
| 2013 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2014 | |
| 2015 | for (TemplateArgument::pack_iterator XP = X.pack_begin(), |
| 2016 | XPEnd = X.pack_end(), |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2017 | YP = Y.pack_begin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2018 | XP != XPEnd; ++XP, ++YP) |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2019 | if (!isSameTemplateArg(Context, *XP, *YP)) |
| 2020 | return false; |
| 2021 | |
| 2022 | return true; |
| 2023 | } |
| 2024 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2025 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2028 | /// \brief Allocate a TemplateArgumentLoc where all locations have |
| 2029 | /// been initialized to the given location. |
| 2030 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2031 | /// \param Arg The template argument we are producing template argument |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2032 | /// location information for. |
| 2033 | /// |
| 2034 | /// \param NTTPType For a declaration template argument, the type of |
| 2035 | /// the non-type template parameter that corresponds to this template |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2036 | /// argument. Can be null if no type sugar is available to add to the |
| 2037 | /// type from the template argument. |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2038 | /// |
| 2039 | /// \param Loc The source location to use for the resulting template |
| 2040 | /// argument. |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2041 | TemplateArgumentLoc |
| 2042 | Sema::getTrivialTemplateArgumentLoc(const TemplateArgument &Arg, |
| 2043 | QualType NTTPType, SourceLocation Loc) { |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2044 | switch (Arg.getKind()) { |
| 2045 | case TemplateArgument::Null: |
| 2046 | llvm_unreachable("Can't get a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2047 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2048 | case TemplateArgument::Type: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2049 | return TemplateArgumentLoc( |
| 2050 | Arg, Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2051 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2052 | case TemplateArgument::Declaration: { |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2053 | if (NTTPType.isNull()) |
| 2054 | NTTPType = Arg.getParamTypeForDecl(); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2055 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
| 2056 | .getAs<Expr>(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2057 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2058 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2059 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2060 | case TemplateArgument::NullPtr: { |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2061 | if (NTTPType.isNull()) |
| 2062 | NTTPType = Arg.getNullPtrType(); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2063 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
| 2064 | .getAs<Expr>(); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2065 | return TemplateArgumentLoc(TemplateArgument(NTTPType, /*isNullPtr*/true), |
| 2066 | E); |
| 2067 | } |
| 2068 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2069 | case TemplateArgument::Integral: { |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2070 | Expr *E = |
| 2071 | BuildExpressionFromIntegralTemplateArgument(Arg, Loc).getAs<Expr>(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2072 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2073 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2074 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2075 | case TemplateArgument::Template: |
| 2076 | case TemplateArgument::TemplateExpansion: { |
| 2077 | NestedNameSpecifierLocBuilder Builder; |
| 2078 | TemplateName Template = Arg.getAsTemplate(); |
| 2079 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2080 | Builder.MakeTrivial(Context, DTN->getQualifier(), Loc); |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 2081 | else if (QualifiedTemplateName *QTN = |
| 2082 | Template.getAsQualifiedTemplateName()) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2083 | Builder.MakeTrivial(Context, QTN->getQualifier(), Loc); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2084 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2085 | if (Arg.getKind() == TemplateArgument::Template) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2086 | return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(Context), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2087 | Loc); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2088 | |
| 2089 | return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(Context), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2090 | Loc, Loc); |
| 2091 | } |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2092 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2093 | case TemplateArgument::Expression: |
| 2094 | return TemplateArgumentLoc(Arg, Arg.getAsExpr()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2095 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2096 | case TemplateArgument::Pack: |
| 2097 | return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo()); |
| 2098 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2099 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2100 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2101 | } |
| 2102 | |
| 2103 | |
| 2104 | /// \brief Convert the given deduced template argument and add it to the set of |
| 2105 | /// fully-converted template arguments. |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2106 | static bool |
| 2107 | ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param, |
| 2108 | DeducedTemplateArgument Arg, |
| 2109 | NamedDecl *Template, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2110 | TemplateDeductionInfo &Info, |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2111 | bool IsDeduced, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2112 | SmallVectorImpl<TemplateArgument> &Output) { |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2113 | auto ConvertArg = [&](DeducedTemplateArgument Arg, |
| 2114 | unsigned ArgumentPackIndex) { |
| 2115 | // Convert the deduced template argument into a template |
| 2116 | // argument that we can check, almost as if the user had written |
| 2117 | // the template argument explicitly. |
| 2118 | TemplateArgumentLoc ArgLoc = |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2119 | S.getTrivialTemplateArgumentLoc(Arg, QualType(), Info.getLocation()); |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2120 | |
| 2121 | // Check the template argument, converting it as necessary. |
| 2122 | return S.CheckTemplateArgument( |
| 2123 | Param, ArgLoc, Template, Template->getLocation(), |
| 2124 | Template->getSourceRange().getEnd(), ArgumentPackIndex, Output, |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2125 | IsDeduced |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2126 | ? (Arg.wasDeducedFromArrayBound() ? Sema::CTAK_DeducedFromArrayBound |
| 2127 | : Sema::CTAK_Deduced) |
| 2128 | : Sema::CTAK_Specified); |
| 2129 | }; |
| 2130 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2131 | if (Arg.getKind() == TemplateArgument::Pack) { |
| 2132 | // This is a template argument pack, so check each of its arguments against |
| 2133 | // the template parameter. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2134 | SmallVector<TemplateArgument, 2> PackedArgsBuilder; |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 2135 | for (const auto &P : Arg.pack_elements()) { |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2136 | // When converting the deduced template argument, append it to the |
| 2137 | // general output list. We need to do this so that the template argument |
| 2138 | // checking logic has all of the prior template arguments available. |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 2139 | DeducedTemplateArgument InnerArg(P); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2140 | InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound()); |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2141 | assert(InnerArg.getKind() != TemplateArgument::Pack && |
| 2142 | "deduced nested pack"); |
| 2143 | if (ConvertArg(InnerArg, PackedArgsBuilder.size())) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2144 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2145 | |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2146 | // Move the converted template argument into our argument pack. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 2147 | PackedArgsBuilder.push_back(Output.pop_back_val()); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2148 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2149 | |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2150 | // 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] | 2151 | // itself, in case that substitution fails. |
| 2152 | if (PackedArgsBuilder.empty()) { |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2153 | LocalInstantiationScope Scope(S); |
Richard Smith | e824775 | 2016-12-22 07:24:39 +0000 | [diff] [blame] | 2154 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Output); |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2155 | MultiLevelTemplateArgumentList Args(TemplateArgs); |
| 2156 | |
| 2157 | if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2158 | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
| 2159 | NTTP, Output, |
| 2160 | Template->getSourceRange()); |
Simon Pilgrim | 6f3e1ea | 2016-12-26 18:11:49 +0000 | [diff] [blame] | 2161 | if (Inst.isInvalid() || |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2162 | S.SubstType(NTTP->getType(), Args, NTTP->getLocation(), |
| 2163 | NTTP->getDeclName()).isNull()) |
| 2164 | return true; |
| 2165 | } else if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 2166 | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
| 2167 | TTP, Output, |
| 2168 | Template->getSourceRange()); |
| 2169 | if (Inst.isInvalid() || !S.SubstDecl(TTP, S.CurContext, Args)) |
| 2170 | return true; |
| 2171 | } |
| 2172 | // For type parameters, no substitution is ever required. |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2173 | } |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2174 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2175 | // Create the resulting argument pack. |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 2176 | Output.push_back( |
| 2177 | TemplateArgument::CreatePackCopy(S.Context, PackedArgsBuilder)); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2178 | return false; |
| 2179 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2180 | |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2181 | return ConvertArg(Arg, 0); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2182 | } |
| 2183 | |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2184 | // FIXME: This should not be a template, but |
| 2185 | // ClassTemplatePartialSpecializationDecl sadly does not derive from |
| 2186 | // TemplateDecl. |
| 2187 | template<typename TemplateDeclT> |
| 2188 | static Sema::TemplateDeductionResult ConvertDeducedTemplateArguments( |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2189 | Sema &S, TemplateDeclT *Template, bool IsDeduced, |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2190 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2191 | TemplateDeductionInfo &Info, SmallVectorImpl<TemplateArgument> &Builder, |
| 2192 | LocalInstantiationScope *CurrentInstantiationScope = nullptr, |
| 2193 | unsigned NumAlreadyConverted = 0, bool PartialOverloading = false) { |
| 2194 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| 2195 | |
| 2196 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 2197 | NamedDecl *Param = TemplateParams->getParam(I); |
| 2198 | |
| 2199 | if (!Deduced[I].isNull()) { |
| 2200 | if (I < NumAlreadyConverted) { |
| 2201 | // We have already fully type-checked and converted this |
| 2202 | // argument, because it was explicitly-specified. Just record the |
| 2203 | // presence of this argument. |
| 2204 | Builder.push_back(Deduced[I]); |
| 2205 | // We may have had explicitly-specified template arguments for a |
| 2206 | // template parameter pack (that may or may not have been extended |
| 2207 | // via additional deduced arguments). |
| 2208 | if (Param->isParameterPack() && CurrentInstantiationScope) { |
| 2209 | if (CurrentInstantiationScope->getPartiallySubstitutedPack() == |
| 2210 | Param) { |
| 2211 | // Forget the partially-substituted pack; its substitution is now |
| 2212 | // complete. |
| 2213 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2214 | } |
| 2215 | } |
| 2216 | continue; |
| 2217 | } |
| 2218 | |
| 2219 | // We have deduced this argument, so it still needs to be |
| 2220 | // checked and converted. |
| 2221 | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Template, Info, |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2222 | IsDeduced, Builder)) { |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2223 | Info.Param = makeTemplateParameter(Param); |
| 2224 | // FIXME: These template arguments are temporary. Free them! |
| 2225 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2226 | return Sema::TDK_SubstitutionFailure; |
| 2227 | } |
| 2228 | |
| 2229 | continue; |
| 2230 | } |
| 2231 | |
| 2232 | // C++0x [temp.arg.explicit]p3: |
| 2233 | // A trailing template parameter pack (14.5.3) not otherwise deduced will |
| 2234 | // be deduced to an empty sequence of template arguments. |
| 2235 | // FIXME: Where did the word "trailing" come from? |
| 2236 | if (Param->isTemplateParameterPack()) { |
| 2237 | // We may have had explicitly-specified template arguments for this |
| 2238 | // template parameter pack. If so, our empty deduction extends the |
| 2239 | // explicitly-specified set (C++0x [temp.arg.explicit]p9). |
| 2240 | const TemplateArgument *ExplicitArgs; |
| 2241 | unsigned NumExplicitArgs; |
| 2242 | if (CurrentInstantiationScope && |
| 2243 | CurrentInstantiationScope->getPartiallySubstitutedPack( |
| 2244 | &ExplicitArgs, &NumExplicitArgs) == Param) { |
| 2245 | Builder.push_back(TemplateArgument( |
| 2246 | llvm::makeArrayRef(ExplicitArgs, NumExplicitArgs))); |
| 2247 | |
| 2248 | // Forget the partially-substituted pack; its substitution is now |
| 2249 | // complete. |
| 2250 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2251 | } else { |
| 2252 | // Go through the motions of checking the empty argument pack against |
| 2253 | // the parameter pack. |
| 2254 | DeducedTemplateArgument DeducedPack(TemplateArgument::getEmptyPack()); |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2255 | if (ConvertDeducedTemplateArgument(S, Param, DeducedPack, Template, |
| 2256 | Info, IsDeduced, Builder)) { |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2257 | Info.Param = makeTemplateParameter(Param); |
| 2258 | // FIXME: These template arguments are temporary. Free them! |
| 2259 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2260 | return Sema::TDK_SubstitutionFailure; |
| 2261 | } |
| 2262 | } |
| 2263 | continue; |
| 2264 | } |
| 2265 | |
| 2266 | // Substitute into the default template argument, if available. |
| 2267 | bool HasDefaultArg = false; |
| 2268 | TemplateDecl *TD = dyn_cast<TemplateDecl>(Template); |
| 2269 | if (!TD) { |
| 2270 | assert(isa<ClassTemplatePartialSpecializationDecl>(Template)); |
| 2271 | return Sema::TDK_Incomplete; |
| 2272 | } |
| 2273 | |
| 2274 | TemplateArgumentLoc DefArg = S.SubstDefaultTemplateArgumentIfAvailable( |
| 2275 | TD, TD->getLocation(), TD->getSourceRange().getEnd(), Param, Builder, |
| 2276 | HasDefaultArg); |
| 2277 | |
| 2278 | // If there was no default argument, deduction is incomplete. |
| 2279 | if (DefArg.getArgument().isNull()) { |
| 2280 | Info.Param = makeTemplateParameter( |
| 2281 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
| 2282 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2283 | if (PartialOverloading) break; |
| 2284 | |
| 2285 | return HasDefaultArg ? Sema::TDK_SubstitutionFailure |
| 2286 | : Sema::TDK_Incomplete; |
| 2287 | } |
| 2288 | |
| 2289 | // Check whether we can actually use the default argument. |
| 2290 | if (S.CheckTemplateArgument(Param, DefArg, TD, TD->getLocation(), |
| 2291 | TD->getSourceRange().getEnd(), 0, Builder, |
| 2292 | Sema::CTAK_Specified)) { |
| 2293 | Info.Param = makeTemplateParameter( |
| 2294 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
| 2295 | // FIXME: These template arguments are temporary. Free them! |
| 2296 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2297 | return Sema::TDK_SubstitutionFailure; |
| 2298 | } |
| 2299 | |
| 2300 | // If we get here, we successfully used the default template argument. |
| 2301 | } |
| 2302 | |
| 2303 | return Sema::TDK_Success; |
| 2304 | } |
| 2305 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2306 | DeclContext *getAsDeclContextOrEnclosing(Decl *D) { |
| 2307 | if (auto *DC = dyn_cast<DeclContext>(D)) |
| 2308 | return DC; |
| 2309 | return D->getDeclContext(); |
| 2310 | } |
| 2311 | |
| 2312 | template<typename T> struct IsPartialSpecialization { |
| 2313 | static constexpr bool value = false; |
| 2314 | }; |
| 2315 | template<> |
| 2316 | struct IsPartialSpecialization<ClassTemplatePartialSpecializationDecl> { |
| 2317 | static constexpr bool value = true; |
| 2318 | }; |
| 2319 | template<> |
| 2320 | struct IsPartialSpecialization<VarTemplatePartialSpecializationDecl> { |
| 2321 | static constexpr bool value = true; |
| 2322 | }; |
| 2323 | |
| 2324 | /// Complete template argument deduction for a partial specialization. |
| 2325 | template <typename T> |
| 2326 | static typename std::enable_if<IsPartialSpecialization<T>::value, |
| 2327 | Sema::TemplateDeductionResult>::type |
| 2328 | FinishTemplateArgumentDeduction( |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2329 | Sema &S, T *Partial, bool IsPartialOrdering, |
| 2330 | const TemplateArgumentList &TemplateArgs, |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2331 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2332 | TemplateDeductionInfo &Info) { |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2333 | // Unevaluated SFINAE context. |
| 2334 | EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2335 | Sema::SFINAETrap Trap(S); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2336 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2337 | Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Partial)); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2338 | |
| 2339 | // C++ [temp.deduct.type]p2: |
| 2340 | // [...] or if any template argument remains neither deduced nor |
| 2341 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2342 | SmallVector<TemplateArgument, 4> Builder; |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2343 | if (auto Result = ConvertDeducedTemplateArguments( |
| 2344 | S, Partial, IsPartialOrdering, Deduced, Info, Builder)) |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2345 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2346 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2347 | // Form the template argument list from the deduced template arguments. |
| 2348 | TemplateArgumentList *DeducedArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2349 | = TemplateArgumentList::CreateCopy(S.Context, Builder); |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2350 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2351 | Info.reset(DeducedArgumentList); |
| 2352 | |
| 2353 | // Substitute the deduced template arguments into the template |
| 2354 | // arguments of the class template partial specialization, and |
| 2355 | // verify that the instantiated template arguments are both valid |
| 2356 | // and are equivalent to the template arguments originally provided |
| 2357 | // to the class template. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2358 | LocalInstantiationScope InstScope(S); |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2359 | auto *Template = Partial->getSpecializedTemplate(); |
| 2360 | const ASTTemplateArgumentListInfo *PartialTemplArgInfo = |
| 2361 | Partial->getTemplateArgsAsWritten(); |
| 2362 | const TemplateArgumentLoc *PartialTemplateArgs = |
| 2363 | PartialTemplArgInfo->getTemplateArgs(); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2364 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2365 | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, |
| 2366 | PartialTemplArgInfo->RAngleLoc); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2367 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2368 | if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs, |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2369 | InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { |
| 2370 | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; |
| 2371 | if (ParamIdx >= Partial->getTemplateParameters()->size()) |
| 2372 | ParamIdx = Partial->getTemplateParameters()->size() - 1; |
| 2373 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2374 | Decl *Param = const_cast<NamedDecl *>( |
| 2375 | Partial->getTemplateParameters()->getParam(ParamIdx)); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2376 | Info.Param = makeTemplateParameter(Param); |
| 2377 | Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); |
| 2378 | return Sema::TDK_SubstitutionFailure; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2379 | } |
| 2380 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2381 | SmallVector<TemplateArgument, 4> ConvertedInstArgs; |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2382 | if (S.CheckTemplateArgumentList(Template, Partial->getLocation(), InstArgs, |
| 2383 | false, ConvertedInstArgs)) |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2384 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2385 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2386 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2387 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2388 | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2389 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 2390 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2391 | Info.FirstArg = TemplateArgs[I]; |
| 2392 | Info.SecondArg = InstArg; |
| 2393 | return Sema::TDK_NonDeducedMismatch; |
| 2394 | } |
| 2395 | } |
| 2396 | |
| 2397 | if (Trap.hasErrorOccurred()) |
| 2398 | return Sema::TDK_SubstitutionFailure; |
| 2399 | |
| 2400 | return Sema::TDK_Success; |
| 2401 | } |
| 2402 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2403 | /// \brief Perform template argument deduction to determine whether |
Larisse Voufo | 833b05a | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 2404 | /// the given template arguments match the given class template |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2405 | /// partial specialization per C++ [temp.class.spec.match]. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2406 | Sema::TemplateDeductionResult |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2407 | Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2408 | const TemplateArgumentList &TemplateArgs, |
| 2409 | TemplateDeductionInfo &Info) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 2410 | if (Partial->isInvalidDecl()) |
| 2411 | return TDK_Invalid; |
| 2412 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2413 | // C++ [temp.class.spec.match]p2: |
| 2414 | // A partial specialization matches a given actual template |
| 2415 | // argument list if the template arguments of the partial |
| 2416 | // specialization can be deduced from the actual template argument |
| 2417 | // list (14.8.2). |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2418 | |
| 2419 | // Unevaluated SFINAE context. |
| 2420 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2421 | SFINAETrap Trap(*this); |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2422 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2423 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2424 | Deduced.resize(Partial->getTemplateParameters()->size()); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2425 | if (TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2426 | = ::DeduceTemplateArguments(*this, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2427 | Partial->getTemplateParameters(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2428 | Partial->getTemplateArgs(), |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2429 | TemplateArgs, Info, Deduced)) |
| 2430 | return Result; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 2431 | |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2432 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2433 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2434 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2435 | if (Inst.isInvalid()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2436 | return TDK_InstantiationDepth; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2437 | |
Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2438 | if (Trap.hasErrorOccurred()) |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2439 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2440 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2441 | return ::FinishTemplateArgumentDeduction( |
| 2442 | *this, Partial, /*PartialOrdering=*/false, TemplateArgs, Deduced, Info); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2443 | } |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2444 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2445 | /// \brief Perform template argument deduction to determine whether |
| 2446 | /// the given template arguments match the given variable template |
| 2447 | /// partial specialization per C++ [temp.class.spec.match]. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2448 | Sema::TemplateDeductionResult |
| 2449 | Sema::DeduceTemplateArguments(VarTemplatePartialSpecializationDecl *Partial, |
| 2450 | const TemplateArgumentList &TemplateArgs, |
| 2451 | TemplateDeductionInfo &Info) { |
| 2452 | if (Partial->isInvalidDecl()) |
| 2453 | return TDK_Invalid; |
| 2454 | |
| 2455 | // C++ [temp.class.spec.match]p2: |
| 2456 | // A partial specialization matches a given actual template |
| 2457 | // argument list if the template arguments of the partial |
| 2458 | // specialization can be deduced from the actual template argument |
| 2459 | // list (14.8.2). |
| 2460 | |
| 2461 | // Unevaluated SFINAE context. |
| 2462 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
| 2463 | SFINAETrap Trap(*this); |
| 2464 | |
| 2465 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 2466 | Deduced.resize(Partial->getTemplateParameters()->size()); |
| 2467 | if (TemplateDeductionResult Result = ::DeduceTemplateArguments( |
| 2468 | *this, Partial->getTemplateParameters(), Partial->getTemplateArgs(), |
| 2469 | TemplateArgs, Info, Deduced)) |
| 2470 | return Result; |
| 2471 | |
| 2472 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2473 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2474 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2475 | if (Inst.isInvalid()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2476 | return TDK_InstantiationDepth; |
| 2477 | |
| 2478 | if (Trap.hasErrorOccurred()) |
| 2479 | return Sema::TDK_SubstitutionFailure; |
| 2480 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2481 | return ::FinishTemplateArgumentDeduction( |
| 2482 | *this, Partial, /*PartialOrdering=*/false, TemplateArgs, Deduced, Info); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2483 | } |
| 2484 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2485 | /// \brief Determine whether the given type T is a simple-template-id type. |
| 2486 | static bool isSimpleTemplateIdType(QualType T) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2487 | if (const TemplateSpecializationType *Spec |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2488 | = T->getAs<TemplateSpecializationType>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2489 | return Spec->getTemplateName().getAsTemplateDecl() != nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2490 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2491 | return false; |
| 2492 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2493 | |
| 2494 | /// \brief Substitute the explicitly-provided template arguments into the |
| 2495 | /// given function template according to C++ [temp.arg.explicit]. |
| 2496 | /// |
| 2497 | /// \param FunctionTemplate the function template into which the explicit |
| 2498 | /// template arguments will be substituted. |
| 2499 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2500 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2501 | /// arguments. |
| 2502 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2503 | /// \param Deduced the deduced template arguments, which will be populated |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2504 | /// with the converted and checked explicit template arguments. |
| 2505 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2506 | /// \param ParamTypes will be populated with the instantiated function |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2507 | /// parameters. |
| 2508 | /// |
| 2509 | /// \param FunctionType if non-NULL, the result type of the function template |
| 2510 | /// will also be instantiated and the pointed-to value will be updated with |
| 2511 | /// the instantiated function type. |
| 2512 | /// |
| 2513 | /// \param Info if substitution fails for any reason, this object will be |
| 2514 | /// populated with more information about the failure. |
| 2515 | /// |
| 2516 | /// \returns TDK_Success if substitution was successful, or some failure |
| 2517 | /// condition. |
| 2518 | Sema::TemplateDeductionResult |
| 2519 | Sema::SubstituteExplicitTemplateArguments( |
| 2520 | FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2521 | TemplateArgumentListInfo &ExplicitTemplateArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2522 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2523 | SmallVectorImpl<QualType> &ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2524 | QualType *FunctionType, |
| 2525 | TemplateDeductionInfo &Info) { |
| 2526 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 2527 | TemplateParameterList *TemplateParams |
| 2528 | = FunctionTemplate->getTemplateParameters(); |
| 2529 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2530 | if (ExplicitTemplateArgs.size() == 0) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2531 | // No arguments to substitute; just copy over the parameter types and |
| 2532 | // fill in the function type. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2533 | for (auto P : Function->parameters()) |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 2534 | ParamTypes.push_back(P->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2535 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2536 | if (FunctionType) |
| 2537 | *FunctionType = Function->getType(); |
| 2538 | return TDK_Success; |
| 2539 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2540 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2541 | // Unevaluated SFINAE context. |
| 2542 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2543 | SFINAETrap Trap(*this); |
| 2544 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2545 | // C++ [temp.arg.explicit]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2546 | // Template arguments that are present shall be specified in the |
| 2547 | // declaration order of their corresponding template-parameters. The |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2548 | // template argument list shall not specify more template-arguments than |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2549 | // there are corresponding template-parameters. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2550 | SmallVector<TemplateArgument, 4> Builder; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2551 | |
| 2552 | // Enter a new template instantiation context where we check the |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2553 | // explicitly-specified template arguments against this function template, |
| 2554 | // and then substitute them into the function parameter types. |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2555 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2556 | InstantiatingTemplate Inst(*this, Info.getLocation(), FunctionTemplate, |
| 2557 | DeducedArgs, |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2558 | ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution, |
| 2559 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2560 | if (Inst.isInvalid()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2561 | return TDK_InstantiationDepth; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2562 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2563 | if (CheckTemplateArgumentList(FunctionTemplate, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2564 | SourceLocation(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2565 | ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2566 | true, |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 2567 | Builder) || Trap.hasErrorOccurred()) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2568 | unsigned Index = Builder.size(); |
Douglas Gregor | 62c281a | 2010-05-09 01:26:06 +0000 | [diff] [blame] | 2569 | if (Index >= TemplateParams->size()) |
| 2570 | Index = TemplateParams->size() - 1; |
| 2571 | Info.Param = makeTemplateParameter(TemplateParams->getParam(Index)); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2572 | return TDK_InvalidExplicitArguments; |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 2573 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2574 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2575 | // Form the template argument list from the explicitly-specified |
| 2576 | // template arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2577 | TemplateArgumentList *ExplicitArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2578 | = TemplateArgumentList::CreateCopy(Context, Builder); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2579 | Info.reset(ExplicitArgumentList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2580 | |
John McCall | 036855a | 2010-10-12 19:40:14 +0000 | [diff] [blame] | 2581 | // Template argument deduction and the final substitution should be |
| 2582 | // done in the context of the templated declaration. Explicit |
| 2583 | // argument substitution, on the other hand, needs to happen in the |
| 2584 | // calling context. |
| 2585 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
| 2586 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2587 | // If we deduced template arguments for a template parameter pack, |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2588 | // note that the template argument pack is partially substituted and record |
| 2589 | // the explicit template arguments. They'll be used as part of deduction |
| 2590 | // for this template parameter pack. |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2591 | for (unsigned I = 0, N = Builder.size(); I != N; ++I) { |
| 2592 | const TemplateArgument &Arg = Builder[I]; |
| 2593 | if (Arg.getKind() == TemplateArgument::Pack) { |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2594 | CurrentInstantiationScope->SetPartiallySubstitutedPack( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2595 | TemplateParams->getParam(I), |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2596 | Arg.pack_begin(), |
| 2597 | Arg.pack_size()); |
| 2598 | break; |
| 2599 | } |
| 2600 | } |
| 2601 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2602 | const FunctionProtoType *Proto |
| 2603 | = Function->getType()->getAs<FunctionProtoType>(); |
| 2604 | assert(Proto && "Function template does not have a prototype?"); |
| 2605 | |
Richard Smith | 70b1304 | 2015-01-09 01:19:56 +0000 | [diff] [blame] | 2606 | // Isolate our substituted parameters from our caller. |
| 2607 | LocalInstantiationScope InstScope(*this, /*MergeWithOuterScope*/true); |
| 2608 | |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2609 | ExtParameterInfoBuilder ExtParamInfos; |
| 2610 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2611 | // Instantiate the types of each of the function parameters given the |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2612 | // explicitly-specified template arguments. If the function has a trailing |
| 2613 | // return type, substitute it after the arguments to ensure we substitute |
| 2614 | // in lexical order. |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2615 | if (Proto->hasTrailingReturn()) { |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2616 | if (SubstParmTypes(Function->getLocation(), Function->parameters(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2617 | Proto->getExtParameterInfosOrNull(), |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2618 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2619 | ParamTypes, /*params*/ nullptr, ExtParamInfos)) |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2620 | return TDK_SubstitutionFailure; |
| 2621 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2622 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2623 | // Instantiate the return type. |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2624 | QualType ResultType; |
| 2625 | { |
| 2626 | // C++11 [expr.prim.general]p3: |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2627 | // If a declaration declares a member function or member function |
| 2628 | // 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] | 2629 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2630 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2631 | // declarator. |
| 2632 | unsigned ThisTypeQuals = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2633 | CXXRecordDecl *ThisContext = nullptr; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2634 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
| 2635 | ThisContext = Method->getParent(); |
| 2636 | ThisTypeQuals = Method->getTypeQualifiers(); |
| 2637 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2638 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2639 | CXXThisScopeRAII ThisScope(*this, ThisContext, ThisTypeQuals, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2640 | getLangOpts().CPlusPlus11); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2641 | |
| 2642 | ResultType = |
| 2643 | SubstType(Proto->getReturnType(), |
| 2644 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 2645 | Function->getTypeSpecStartLoc(), Function->getDeclName()); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2646 | if (ResultType.isNull() || Trap.hasErrorOccurred()) |
| 2647 | return TDK_SubstitutionFailure; |
| 2648 | } |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2649 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2650 | // Instantiate the types of each of the function parameters given the |
| 2651 | // explicitly-specified template arguments if we didn't do so earlier. |
| 2652 | if (!Proto->hasTrailingReturn() && |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2653 | SubstParmTypes(Function->getLocation(), Function->parameters(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2654 | Proto->getExtParameterInfosOrNull(), |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2655 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2656 | ParamTypes, /*params*/ nullptr, ExtParamInfos)) |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2657 | return TDK_SubstitutionFailure; |
| 2658 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2659 | if (FunctionType) { |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2660 | auto EPI = Proto->getExtProtoInfo(); |
| 2661 | EPI.ExtParameterInfos = ExtParamInfos.getPointerOrNull(ParamTypes.size()); |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2662 | *FunctionType = BuildFunctionType(ResultType, ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2663 | Function->getLocation(), |
Eli Friedman | d8725a9 | 2010-08-05 02:54:05 +0000 | [diff] [blame] | 2664 | Function->getDeclName(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2665 | EPI); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2666 | if (FunctionType->isNull() || Trap.hasErrorOccurred()) |
| 2667 | return TDK_SubstitutionFailure; |
| 2668 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2669 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2670 | // C++ [temp.arg.explicit]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2671 | // Trailing template arguments that can be deduced (14.8.2) may be |
| 2672 | // omitted from the list of explicit template-arguments. If all of the |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2673 | // template arguments can be deduced, they may all be omitted; in this |
| 2674 | // case, the empty template argument list <> itself may also be omitted. |
| 2675 | // |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2676 | // Take all of the explicitly-specified arguments and put them into |
| 2677 | // the set of deduced template arguments. Explicitly-specified |
| 2678 | // parameter packs, however, will be set to NULL since the deduction |
| 2679 | // mechanisms handle explicitly-specified argument packs directly. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2680 | Deduced.reserve(TemplateParams->size()); |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2681 | for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) { |
| 2682 | const TemplateArgument &Arg = ExplicitArgumentList->get(I); |
| 2683 | if (Arg.getKind() == TemplateArgument::Pack) |
| 2684 | Deduced.push_back(DeducedTemplateArgument()); |
| 2685 | else |
| 2686 | Deduced.push_back(Arg); |
| 2687 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2688 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2689 | return TDK_Success; |
| 2690 | } |
| 2691 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2692 | /// \brief Check whether the deduced argument type for a call to a function |
| 2693 | /// template matches the actual argument type per C++ [temp.deduct.call]p4. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2694 | static bool |
| 2695 | CheckOriginalCallArgDeduction(Sema &S, Sema::OriginalCallArg OriginalArg, |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2696 | QualType DeducedA) { |
| 2697 | ASTContext &Context = S.Context; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2698 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2699 | QualType A = OriginalArg.OriginalArgType; |
| 2700 | QualType OriginalParamType = OriginalArg.OriginalParamType; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2701 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2702 | // Check for type equality (top-level cv-qualifiers are ignored). |
| 2703 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| 2704 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2705 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2706 | // Strip off references on the argument types; they aren't needed for |
| 2707 | // the following checks. |
| 2708 | if (const ReferenceType *DeducedARef = DeducedA->getAs<ReferenceType>()) |
| 2709 | DeducedA = DeducedARef->getPointeeType(); |
| 2710 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
| 2711 | A = ARef->getPointeeType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2712 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2713 | // C++ [temp.deduct.call]p4: |
| 2714 | // [...] However, there are three cases that allow a difference: |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2715 | // - If the original P is a reference type, the deduced A (i.e., the |
| 2716 | // type referred to by the reference) can be more cv-qualified than |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2717 | // the transformed A. |
| 2718 | if (const ReferenceType *OriginalParamRef |
| 2719 | = OriginalParamType->getAs<ReferenceType>()) { |
| 2720 | // We don't want to keep the reference around any more. |
| 2721 | OriginalParamType = OriginalParamRef->getPointeeType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2722 | |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 2723 | // FIXME: Resolve core issue (no number yet): if the original P is a |
| 2724 | // reference type and the transformed A is function type "noexcept F", |
| 2725 | // the deduced A can be F. |
| 2726 | QualType Tmp; |
| 2727 | if (A->isFunctionType() && S.IsFunctionConversion(A, DeducedA, Tmp)) |
| 2728 | return false; |
| 2729 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2730 | Qualifiers AQuals = A.getQualifiers(); |
| 2731 | Qualifiers DeducedAQuals = DeducedA.getQualifiers(); |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2732 | |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2733 | // Under Objective-C++ ARC, the deduced type may have implicitly |
| 2734 | // been given strong or (when dealing with a const reference) |
| 2735 | // unsafe_unretained lifetime. If so, update the original |
| 2736 | // qualifiers to include this lifetime. |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2737 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2738 | ((DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_Strong && |
| 2739 | AQuals.getObjCLifetime() == Qualifiers::OCL_None) || |
| 2740 | (DeducedAQuals.hasConst() && |
| 2741 | DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone))) { |
| 2742 | AQuals.setObjCLifetime(DeducedAQuals.getObjCLifetime()); |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2743 | } |
| 2744 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2745 | if (AQuals == DeducedAQuals) { |
| 2746 | // Qualifiers match; there's nothing to do. |
| 2747 | } else if (!DeducedAQuals.compatiblyIncludes(AQuals)) { |
Douglas Gregor | ddaae52 | 2011-06-17 14:36:00 +0000 | [diff] [blame] | 2748 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2749 | } else { |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2750 | // Qualifiers are compatible, so have the argument type adopt the |
| 2751 | // deduced argument type's qualifiers as if we had performed the |
| 2752 | // qualification conversion. |
| 2753 | A = Context.getQualifiedType(A.getUnqualifiedType(), DeducedAQuals); |
| 2754 | } |
| 2755 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2756 | |
| 2757 | // - The transformed A can be another pointer or pointer to member |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 2758 | // type that can be converted to the deduced A via a function pointer |
| 2759 | // conversion and/or a qualification conversion. |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2760 | // |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 2761 | // Also allow conversions which merely strip __attribute__((noreturn)) from |
| 2762 | // function types (recursively). |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2763 | bool ObjCLifetimeConversion = false; |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2764 | QualType ResultTy; |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2765 | if ((A->isAnyPointerType() || A->isMemberPointerType()) && |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2766 | (S.IsQualificationConversion(A, DeducedA, false, |
| 2767 | ObjCLifetimeConversion) || |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 2768 | S.IsFunctionConversion(A, DeducedA, ResultTy))) |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2769 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2770 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2771 | // - 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] | 2772 | // transformed A can be a derived class of the deduced A. [...] |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2773 | // [...] Likewise, if P is a pointer to a class of the form |
| 2774 | // simple-template-id, the transformed A can be a pointer to a |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2775 | // derived class pointed to by the deduced A. |
| 2776 | if (const PointerType *OriginalParamPtr |
| 2777 | = OriginalParamType->getAs<PointerType>()) { |
| 2778 | if (const PointerType *DeducedAPtr = DeducedA->getAs<PointerType>()) { |
| 2779 | if (const PointerType *APtr = A->getAs<PointerType>()) { |
| 2780 | if (A->getPointeeType()->isRecordType()) { |
| 2781 | OriginalParamType = OriginalParamPtr->getPointeeType(); |
| 2782 | DeducedA = DeducedAPtr->getPointeeType(); |
| 2783 | A = APtr->getPointeeType(); |
| 2784 | } |
| 2785 | } |
| 2786 | } |
| 2787 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2788 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2789 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| 2790 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2791 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2792 | if (A->isRecordType() && isSimpleTemplateIdType(OriginalParamType) && |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 2793 | S.IsDerivedFrom(SourceLocation(), A, DeducedA)) |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2794 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2795 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2796 | return true; |
| 2797 | } |
| 2798 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2799 | /// \brief Finish template argument deduction for a function template, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2800 | /// checking the deduced template arguments for completeness and forming |
| 2801 | /// the function template specialization. |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2802 | /// |
| 2803 | /// \param OriginalCallArgs If non-NULL, the original call arguments against |
| 2804 | /// which the deduced argument types should be compared. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2805 | Sema::TemplateDeductionResult |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2806 | Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2807 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2808 | unsigned NumExplicitlySpecified, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2809 | FunctionDecl *&Specialization, |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2810 | TemplateDeductionInfo &Info, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 2811 | SmallVectorImpl<OriginalCallArg> const *OriginalCallArgs, |
| 2812 | bool PartialOverloading) { |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2813 | // Unevaluated SFINAE context. |
| 2814 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2815 | SFINAETrap Trap(*this); |
| 2816 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2817 | // Enter a new template instantiation context while we instantiate the |
| 2818 | // actual function declaration. |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2819 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2820 | InstantiatingTemplate Inst(*this, Info.getLocation(), FunctionTemplate, |
| 2821 | DeducedArgs, |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2822 | ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution, |
| 2823 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2824 | if (Inst.isInvalid()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2825 | return TDK_InstantiationDepth; |
| 2826 | |
John McCall | e23b871 | 2010-04-29 01:18:58 +0000 | [diff] [blame] | 2827 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
John McCall | 80e58cd | 2010-04-29 00:35:03 +0000 | [diff] [blame] | 2828 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2829 | // C++ [temp.deduct.type]p2: |
| 2830 | // [...] or if any template argument remains neither deduced nor |
| 2831 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2832 | SmallVector<TemplateArgument, 4> Builder; |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2833 | if (auto Result = ConvertDeducedTemplateArguments( |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2834 | *this, FunctionTemplate, /*IsDeduced*/true, Deduced, Info, Builder, |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2835 | CurrentInstantiationScope, NumExplicitlySpecified, |
| 2836 | PartialOverloading)) |
| 2837 | return Result; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2838 | |
| 2839 | // Form the template argument list from the deduced template arguments. |
| 2840 | TemplateArgumentList *DeducedArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2841 | = TemplateArgumentList::CreateCopy(Context, Builder); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2842 | Info.reset(DeducedArgumentList); |
| 2843 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2844 | // Substitute the deduced template arguments into the function template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2845 | // declaration to produce the function template specialization. |
Douglas Gregor | 1614237 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 2846 | DeclContext *Owner = FunctionTemplate->getDeclContext(); |
| 2847 | if (FunctionTemplate->getFriendObjectKind()) |
| 2848 | Owner = FunctionTemplate->getLexicalDeclContext(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2849 | Specialization = cast_or_null<FunctionDecl>( |
Douglas Gregor | 1614237 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 2850 | SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner, |
Douglas Gregor | 39cacdb | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 2851 | MultiLevelTemplateArgumentList(*DeducedArgumentList))); |
Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 2852 | if (!Specialization || Specialization->isInvalidDecl()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2853 | return TDK_SubstitutionFailure; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2854 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2855 | assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == |
Douglas Gregor | 31fae89 | 2009-09-15 18:26:13 +0000 | [diff] [blame] | 2856 | FunctionTemplate->getCanonicalDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2857 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2858 | // If the template argument list is owned by the function template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2859 | // specialization, release it. |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 2860 | if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList && |
| 2861 | !Trap.hasErrorOccurred()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2862 | Info.take(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2863 | |
Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 2864 | // There may have been an error that did not prevent us from constructing a |
| 2865 | // declaration. Mark the declaration invalid and return with a substitution |
| 2866 | // failure. |
| 2867 | if (Trap.hasErrorOccurred()) { |
| 2868 | Specialization->setInvalidDecl(true); |
| 2869 | return TDK_SubstitutionFailure; |
| 2870 | } |
| 2871 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2872 | if (OriginalCallArgs) { |
| 2873 | // C++ [temp.deduct.call]p4: |
| 2874 | // In general, the deduction process attempts to find template argument |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2875 | // 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] | 2876 | // is transformed as described above). [...] |
| 2877 | for (unsigned I = 0, N = OriginalCallArgs->size(); I != N; ++I) { |
| 2878 | OriginalCallArg OriginalArg = (*OriginalCallArgs)[I]; |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2879 | unsigned ParamIdx = OriginalArg.ArgIdx; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2880 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2881 | if (ParamIdx >= Specialization->getNumParams()) |
| 2882 | continue; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2883 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2884 | QualType DeducedA = Specialization->getParamDecl(ParamIdx)->getType(); |
Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 2885 | if (CheckOriginalCallArgDeduction(*this, OriginalArg, DeducedA)) { |
| 2886 | Info.FirstArg = TemplateArgument(DeducedA); |
| 2887 | Info.SecondArg = TemplateArgument(OriginalArg.OriginalArgType); |
| 2888 | Info.CallArgIndex = OriginalArg.ArgIdx; |
| 2889 | return TDK_DeducedMismatch; |
| 2890 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2891 | } |
| 2892 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2893 | |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2894 | // If we suppressed any diagnostics while performing template argument |
| 2895 | // deduction, and if we haven't already instantiated this declaration, |
| 2896 | // keep track of these diagnostics. They'll be emitted if this specialization |
| 2897 | // is actually used. |
| 2898 | if (Info.diag_begin() != Info.diag_end()) { |
Craig Topper | 79be4cd | 2013-07-05 04:33:53 +0000 | [diff] [blame] | 2899 | SuppressedDiagnosticsMap::iterator |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2900 | Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl()); |
| 2901 | if (Pos == SuppressedDiagnostics.end()) |
| 2902 | SuppressedDiagnostics[Specialization->getCanonicalDecl()] |
| 2903 | .append(Info.diag_begin(), Info.diag_end()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2904 | } |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2905 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2906 | return TDK_Success; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2907 | } |
| 2908 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2909 | /// Gets the type of a function for template-argument-deducton |
| 2910 | /// purposes when it's considered as part of an overload set. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2911 | static QualType GetTypeOfFunction(Sema &S, const OverloadExpr::FindResult &R, |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2912 | FunctionDecl *Fn) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2913 | // We may need to deduce the return type of the function now. |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 2914 | if (S.getLangOpts().CPlusPlus14 && Fn->getReturnType()->isUndeducedType() && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2915 | S.DeduceReturnType(Fn, R.Expression->getExprLoc(), /*Diagnose*/ false)) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2916 | return QualType(); |
| 2917 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2918 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2919 | if (Method->isInstance()) { |
| 2920 | // An instance method that's referenced in a form that doesn't |
| 2921 | // look like a member pointer is just invalid. |
| 2922 | if (!R.HasFormOfMemberPointer) return QualType(); |
| 2923 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2924 | return S.Context.getMemberPointerType(Fn->getType(), |
| 2925 | S.Context.getTypeDeclType(Method->getParent()).getTypePtr()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2926 | } |
| 2927 | |
| 2928 | if (!R.IsAddressOfOperand) return Fn->getType(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2929 | return S.Context.getPointerType(Fn->getType()); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2930 | } |
| 2931 | |
| 2932 | /// Apply the deduction rules for overload sets. |
| 2933 | /// |
| 2934 | /// \return the null type if this argument should be treated as an |
| 2935 | /// undeduced context |
| 2936 | static QualType |
| 2937 | ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams, |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 2938 | Expr *Arg, QualType ParamType, |
| 2939 | bool ParamWasReference) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2940 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2941 | OverloadExpr::FindResult R = OverloadExpr::find(Arg); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2942 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2943 | OverloadExpr *Ovl = R.Expression; |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2944 | |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 2945 | // C++0x [temp.deduct.call]p4 |
| 2946 | unsigned TDF = 0; |
| 2947 | if (ParamWasReference) |
| 2948 | TDF |= TDF_ParamWithReferenceType; |
| 2949 | if (R.IsAddressOfOperand) |
| 2950 | TDF |= TDF_IgnoreQualifiers; |
| 2951 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2952 | // C++0x [temp.deduct.call]p6: |
| 2953 | // When P is a function type, pointer to function type, or pointer |
| 2954 | // to member function type: |
| 2955 | |
| 2956 | if (!ParamType->isFunctionType() && |
| 2957 | !ParamType->isFunctionPointerType() && |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 2958 | !ParamType->isMemberFunctionPointerType()) { |
| 2959 | if (Ovl->hasExplicitTemplateArgs()) { |
| 2960 | // But we can still look for an explicit specialization. |
| 2961 | if (FunctionDecl *ExplicitSpec |
| 2962 | = S.ResolveSingleFunctionTemplateSpecialization(Ovl)) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2963 | return GetTypeOfFunction(S, R, ExplicitSpec); |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 2964 | } |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2965 | |
George Burgess IV | cc2f355 | 2016-03-19 21:51:45 +0000 | [diff] [blame] | 2966 | DeclAccessPair DAP; |
| 2967 | if (FunctionDecl *Viable = |
| 2968 | S.resolveAddressOfOnlyViableOverloadCandidate(Arg, DAP)) |
| 2969 | return GetTypeOfFunction(S, R, Viable); |
| 2970 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 2971 | return QualType(); |
| 2972 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2973 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 2974 | // Gather the explicit template arguments, if any. |
| 2975 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 2976 | if (Ovl->hasExplicitTemplateArgs()) |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 2977 | Ovl->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2978 | QualType Match; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 2979 | for (UnresolvedSetIterator I = Ovl->decls_begin(), |
| 2980 | E = Ovl->decls_end(); I != E; ++I) { |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2981 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 2982 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 2983 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) { |
| 2984 | // - If the argument is an overload set containing one or more |
| 2985 | // function templates, the parameter is treated as a |
| 2986 | // non-deduced context. |
| 2987 | if (!Ovl->hasExplicitTemplateArgs()) |
| 2988 | return QualType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2989 | |
| 2990 | // Otherwise, see if we can resolve a function type |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2991 | FunctionDecl *Specialization = nullptr; |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 2992 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 2993 | if (S.DeduceTemplateArguments(FunTmpl, &ExplicitTemplateArgs, |
| 2994 | Specialization, Info)) |
| 2995 | continue; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2996 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 2997 | D = Specialization; |
| 2998 | } |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2999 | |
| 3000 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3001 | QualType ArgType = GetTypeOfFunction(S, R, Fn); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3002 | if (ArgType.isNull()) continue; |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3003 | |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3004 | // Function-to-pointer conversion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3005 | if (!ParamWasReference && ParamType->isPointerType() && |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3006 | ArgType->isFunctionType()) |
| 3007 | ArgType = S.Context.getPointerType(ArgType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3008 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3009 | // - If the argument is an overload set (not containing function |
| 3010 | // templates), trial argument deduction is attempted using each |
| 3011 | // of the members of the set. If deduction succeeds for only one |
| 3012 | // of the overload set members, that member is used as the |
| 3013 | // argument value for the deduction. If deduction succeeds for |
| 3014 | // more than one member of the overload set the parameter is |
| 3015 | // treated as a non-deduced context. |
| 3016 | |
| 3017 | // We do all of this in a fresh context per C++0x [temp.deduct.type]p2: |
| 3018 | // Type deduction is done independently for each P/A pair, and |
| 3019 | // the deduced template argument values are then combined. |
| 3020 | // So we do not reject deductions which were made elsewhere. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3021 | SmallVector<DeducedTemplateArgument, 8> |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3022 | Deduced(TemplateParams->size()); |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3023 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3024 | Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3025 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
| 3026 | ArgType, Info, Deduced, TDF); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3027 | if (Result) continue; |
| 3028 | if (!Match.isNull()) return QualType(); |
| 3029 | Match = ArgType; |
| 3030 | } |
| 3031 | |
| 3032 | return Match; |
| 3033 | } |
| 3034 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3035 | /// \brief Perform the adjustments to the parameter and argument types |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3036 | /// described in C++ [temp.deduct.call]. |
| 3037 | /// |
| 3038 | /// \returns true if the caller should not attempt to perform any template |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3039 | /// argument deduction based on this P/A pair because the argument is an |
| 3040 | /// overloaded function set that could not be resolved. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3041 | static bool AdjustFunctionParmAndArgTypesForDeduction(Sema &S, |
| 3042 | TemplateParameterList *TemplateParams, |
| 3043 | QualType &ParamType, |
| 3044 | QualType &ArgType, |
| 3045 | Expr *Arg, |
| 3046 | unsigned &TDF) { |
| 3047 | // C++0x [temp.deduct.call]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3048 | // 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] | 3049 | // are ignored for type deduction. |
Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3050 | if (ParamType.hasQualifiers()) |
| 3051 | ParamType = ParamType.getUnqualifiedType(); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3052 | |
| 3053 | // [...] If P is a reference type, the type referred to by P is |
| 3054 | // used for type deduction. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3055 | const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>(); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3056 | if (ParamRefType) |
| 3057 | ParamType = ParamRefType->getPointeeType(); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3058 | |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3059 | // Overload sets usually make this parameter an undeduced context, |
| 3060 | // but there are sometimes special circumstances. Typically |
| 3061 | // involving a template-id-expr. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3062 | if (ArgType == S.Context.OverloadTy) { |
| 3063 | ArgType = ResolveOverloadForDeduction(S, TemplateParams, |
| 3064 | Arg, ParamType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3065 | ParamRefType != nullptr); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3066 | if (ArgType.isNull()) |
| 3067 | return true; |
| 3068 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3069 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3070 | if (ParamRefType) { |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3071 | // If the argument has incomplete array type, try to complete its type. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3072 | if (ArgType->isIncompleteArrayType()) { |
| 3073 | S.completeExprArrayBound(Arg); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3074 | ArgType = Arg->getType(); |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3075 | } |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3076 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3077 | // C++0x [temp.deduct.call]p3: |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3078 | // If P is an rvalue reference to a cv-unqualified template |
| 3079 | // parameter and the argument is an lvalue, the type "lvalue |
| 3080 | // reference to A" is used in place of A for type deduction. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3081 | if (ParamRefType->isRValueReferenceType() && |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3082 | !ParamType.getQualifiers() && |
| 3083 | isa<TemplateTypeParmType>(ParamType) && |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3084 | Arg->isLValue()) |
| 3085 | ArgType = S.Context.getLValueReferenceType(ArgType); |
| 3086 | } else { |
| 3087 | // C++ [temp.deduct.call]p2: |
| 3088 | // If P is not a reference type: |
| 3089 | // - If A is an array type, the pointer type produced by the |
| 3090 | // array-to-pointer standard conversion (4.2) is used in place of |
| 3091 | // A for type deduction; otherwise, |
| 3092 | if (ArgType->isArrayType()) |
| 3093 | ArgType = S.Context.getArrayDecayedType(ArgType); |
| 3094 | // - If A is a function type, the pointer type produced by the |
| 3095 | // function-to-pointer standard conversion (4.3) is used in place |
| 3096 | // of A for type deduction; otherwise, |
| 3097 | else if (ArgType->isFunctionType()) |
| 3098 | ArgType = S.Context.getPointerType(ArgType); |
| 3099 | else { |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3100 | // - 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] | 3101 | // type are ignored for type deduction. |
Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3102 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3103 | } |
| 3104 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3105 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3106 | // C++0x [temp.deduct.call]p4: |
| 3107 | // In general, the deduction process attempts to find template argument |
| 3108 | // values that will make the deduced A identical to A (after the type A |
| 3109 | // is transformed as described above). [...] |
| 3110 | TDF = TDF_SkipNonDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3111 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3112 | // - If the original P is a reference type, the deduced A (i.e., the |
| 3113 | // type referred to by the reference) can be more cv-qualified than |
| 3114 | // the transformed A. |
| 3115 | if (ParamRefType) |
| 3116 | TDF |= TDF_ParamWithReferenceType; |
| 3117 | // - The transformed A can be another pointer or pointer to member |
| 3118 | // type that can be converted to the deduced A via a qualification |
| 3119 | // conversion (4.4). |
| 3120 | if (ArgType->isPointerType() || ArgType->isMemberPointerType() || |
| 3121 | ArgType->isObjCObjectPointerType()) |
| 3122 | TDF |= TDF_IgnoreQualifiers; |
| 3123 | // - If P is a class and P has the form simple-template-id, then the |
| 3124 | // transformed A can be a derived class of the deduced A. Likewise, |
| 3125 | // if P is a pointer to a class of the form simple-template-id, the |
| 3126 | // transformed A can be a pointer to a derived class pointed to by |
| 3127 | // the deduced A. |
| 3128 | if (isSimpleTemplateIdType(ParamType) || |
| 3129 | (isa<PointerType>(ParamType) && |
| 3130 | isSimpleTemplateIdType( |
| 3131 | ParamType->getAs<PointerType>()->getPointeeType()))) |
| 3132 | TDF |= TDF_DerivedClass; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3133 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3134 | return false; |
| 3135 | } |
| 3136 | |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3137 | static bool |
| 3138 | hasDeducibleTemplateParameters(Sema &S, FunctionTemplateDecl *FunctionTemplate, |
| 3139 | QualType T); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3140 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3141 | static Sema::TemplateDeductionResult DeduceTemplateArgumentByListElement( |
| 3142 | Sema &S, TemplateParameterList *TemplateParams, QualType ParamType, |
| 3143 | Expr *Arg, TemplateDeductionInfo &Info, |
| 3144 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, unsigned TDF); |
| 3145 | |
| 3146 | /// \brief Attempt template argument deduction from an initializer list |
| 3147 | /// deemed to be an argument in a function call. |
| 3148 | static bool |
| 3149 | DeduceFromInitializerList(Sema &S, TemplateParameterList *TemplateParams, |
| 3150 | QualType AdjustedParamType, InitListExpr *ILE, |
| 3151 | TemplateDeductionInfo &Info, |
| 3152 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3153 | unsigned TDF, Sema::TemplateDeductionResult &Result) { |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3154 | |
| 3155 | // [temp.deduct.call] p1 (post CWG-1591) |
| 3156 | // If removing references and cv-qualifiers from P gives |
| 3157 | // std::initializer_list<P0> or P0[N] for some P0 and N and the argument is a |
| 3158 | // non-empty initializer list (8.5.4), then deduction is performed instead for |
| 3159 | // each element of the initializer list, taking P0 as a function template |
| 3160 | // parameter type and the initializer element as its argument, and in the |
| 3161 | // P0[N] case, if N is a non-type template parameter, N is deduced from the |
| 3162 | // length of the initializer list. Otherwise, an initializer list argument |
| 3163 | // causes the parameter to be considered a non-deduced context |
| 3164 | |
| 3165 | const bool IsConstSizedArray = AdjustedParamType->isConstantArrayType(); |
| 3166 | |
| 3167 | const bool IsDependentSizedArray = |
| 3168 | !IsConstSizedArray && AdjustedParamType->isDependentSizedArrayType(); |
| 3169 | |
Faisal Vali | dd76cc1 | 2015-12-10 12:29:11 +0000 | [diff] [blame] | 3170 | 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] | 3171 | |
| 3172 | const bool IsSTDList = !IsConstSizedArray && !IsDependentSizedArray && |
| 3173 | S.isStdInitializerList(AdjustedParamType, &ElTy); |
| 3174 | |
| 3175 | if (!IsConstSizedArray && !IsDependentSizedArray && !IsSTDList) |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3176 | return false; |
| 3177 | |
| 3178 | Result = Sema::TDK_Success; |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3179 | // If we are not deducing against the 'T' in a std::initializer_list<T> then |
| 3180 | // deduce against the 'T' in T[N]. |
| 3181 | if (ElTy.isNull()) { |
| 3182 | assert(!IsSTDList); |
| 3183 | ElTy = S.Context.getAsArrayType(AdjustedParamType)->getElementType(); |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3184 | } |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3185 | // Deduction only needs to be done for dependent types. |
| 3186 | if (ElTy->isDependentType()) { |
| 3187 | for (Expr *E : ILE->inits()) { |
Craig Topper | 0852953 | 2015-12-10 08:49:55 +0000 | [diff] [blame] | 3188 | if ((Result = DeduceTemplateArgumentByListElement(S, TemplateParams, ElTy, |
| 3189 | E, Info, Deduced, TDF))) |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3190 | return true; |
| 3191 | } |
| 3192 | } |
| 3193 | if (IsDependentSizedArray) { |
| 3194 | const DependentSizedArrayType *ArrTy = |
| 3195 | S.Context.getAsDependentSizedArrayType(AdjustedParamType); |
| 3196 | // Determine the array bound is something we can deduce. |
| 3197 | if (NonTypeTemplateParmDecl *NTTP = |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 3198 | getDeducedParameterFromExpr(Info, ArrTy->getSizeExpr())) { |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3199 | // We can perform template argument deduction for the given non-type |
| 3200 | // template parameter. |
| 3201 | assert(NTTP->getDepth() == 0 && |
| 3202 | "Cannot deduce non-type template argument at depth > 0"); |
| 3203 | llvm::APInt Size(S.Context.getIntWidth(NTTP->getType()), |
| 3204 | ILE->getNumInits()); |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3205 | |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3206 | Result = DeduceNonTypeTemplateArgument( |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 3207 | S, TemplateParams, NTTP, llvm::APSInt(Size), NTTP->getType(), |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3208 | /*ArrayBound=*/true, Info, Deduced); |
| 3209 | } |
| 3210 | } |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3211 | return true; |
| 3212 | } |
| 3213 | |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3214 | /// \brief Perform template argument deduction by matching a parameter type |
| 3215 | /// against a single expression, where the expression is an element of |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3216 | /// an initializer list that was originally matched against a parameter |
| 3217 | /// of type \c initializer_list\<ParamType\>. |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3218 | static Sema::TemplateDeductionResult |
| 3219 | DeduceTemplateArgumentByListElement(Sema &S, |
| 3220 | TemplateParameterList *TemplateParams, |
| 3221 | QualType ParamType, Expr *Arg, |
| 3222 | TemplateDeductionInfo &Info, |
| 3223 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3224 | unsigned TDF) { |
| 3225 | // Handle the case where an init list contains another init list as the |
| 3226 | // element. |
| 3227 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3228 | Sema::TemplateDeductionResult Result; |
| 3229 | if (!DeduceFromInitializerList(S, TemplateParams, |
| 3230 | ParamType.getNonReferenceType(), ILE, Info, |
| 3231 | Deduced, TDF, Result)) |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3232 | return Sema::TDK_Success; // Just ignore this expression. |
| 3233 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3234 | return Result; |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
| 3237 | // For all other cases, just match by type. |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3238 | QualType ArgType = Arg->getType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3239 | if (AdjustFunctionParmAndArgTypesForDeduction(S, TemplateParams, ParamType, |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3240 | ArgType, Arg, TDF)) { |
| 3241 | Info.Expression = Arg; |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3242 | return Sema::TDK_FailedOverloadResolution; |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3243 | } |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3244 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3245 | ArgType, Info, Deduced, TDF); |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3246 | } |
| 3247 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3248 | /// \brief Perform template argument deduction from a function call |
| 3249 | /// (C++ [temp.deduct.call]). |
| 3250 | /// |
| 3251 | /// \param FunctionTemplate the function template for which we are performing |
| 3252 | /// template argument deduction. |
| 3253 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3254 | /// \param ExplicitTemplateArgs the explicit template arguments provided |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 3255 | /// for this call. |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3256 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3257 | /// \param Args the function call arguments |
| 3258 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3259 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3260 | /// this will be set to the function template specialization produced by |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3261 | /// template argument deduction. |
| 3262 | /// |
| 3263 | /// \param Info the argument will be updated to provide additional information |
| 3264 | /// about template argument deduction. |
| 3265 | /// |
| 3266 | /// \returns the result of template argument deduction. |
Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 3267 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3268 | FunctionTemplateDecl *FunctionTemplate, |
| 3269 | TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3270 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 3271 | bool PartialOverloading) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3272 | if (FunctionTemplate->isInvalidDecl()) |
| 3273 | return TDK_Invalid; |
| 3274 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3275 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3276 | unsigned NumParams = Function->getNumParams(); |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3277 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3278 | // C++ [temp.deduct.call]p1: |
| 3279 | // Template argument deduction is done by comparing each function template |
| 3280 | // parameter type (call it P) with the type of the corresponding argument |
| 3281 | // of the call (call it A) as described below. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3282 | unsigned CheckArgs = Args.size(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3283 | if (Args.size() < Function->getMinRequiredArguments() && !PartialOverloading) |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3284 | return TDK_TooFewArguments; |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3285 | else if (TooManyArguments(NumParams, Args.size(), PartialOverloading)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3286 | const FunctionProtoType *Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3287 | = Function->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3288 | if (Proto->isTemplateVariadic()) |
| 3289 | /* Do nothing */; |
| 3290 | else if (Proto->isVariadic()) |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3291 | CheckArgs = NumParams; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3292 | else |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3293 | return TDK_TooManyArguments; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3294 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3295 | |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3296 | // The types of the parameters from which we will perform template argument |
| 3297 | // deduction. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3298 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3299 | TemplateParameterList *TemplateParams |
| 3300 | = FunctionTemplate->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3301 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 3302 | SmallVector<QualType, 4> ParamTypes; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3303 | unsigned NumExplicitlySpecified = 0; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3304 | if (ExplicitTemplateArgs) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3305 | TemplateDeductionResult Result = |
| 3306 | SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3307 | *ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3308 | Deduced, |
| 3309 | ParamTypes, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3310 | nullptr, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3311 | Info); |
| 3312 | if (Result) |
| 3313 | return Result; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3314 | |
| 3315 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3316 | } else { |
| 3317 | // Just fill in the parameter types from the function declaration. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3318 | for (unsigned I = 0; I != NumParams; ++I) |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3319 | ParamTypes.push_back(Function->getParamDecl(I)->getType()); |
| 3320 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3321 | |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3322 | // Deduce template arguments from the function parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3323 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3324 | unsigned ArgIdx = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3325 | SmallVector<OriginalCallArg, 4> OriginalCallArgs; |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3326 | for (unsigned ParamIdx = 0, NumParamTypes = ParamTypes.size(); |
| 3327 | ParamIdx != NumParamTypes; ++ParamIdx) { |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3328 | QualType OrigParamType = ParamTypes[ParamIdx]; |
| 3329 | QualType ParamType = OrigParamType; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3330 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3331 | const PackExpansionType *ParamExpansion |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3332 | = dyn_cast<PackExpansionType>(ParamType); |
| 3333 | if (!ParamExpansion) { |
| 3334 | // Simple case: matching a function parameter to a function argument. |
| 3335 | if (ArgIdx >= CheckArgs) |
| 3336 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3337 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3338 | Expr *Arg = Args[ArgIdx++]; |
| 3339 | QualType ArgType = Arg->getType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3340 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3341 | unsigned TDF = 0; |
| 3342 | if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams, |
| 3343 | ParamType, ArgType, Arg, |
| 3344 | TDF)) |
| 3345 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3346 | |
Douglas Gregor | 0c83c81 | 2011-10-09 22:06:46 +0000 | [diff] [blame] | 3347 | // If we have nothing to deduce, we're done. |
| 3348 | if (!hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
| 3349 | continue; |
| 3350 | |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3351 | // If the argument is an initializer list ... |
| 3352 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3353 | TemplateDeductionResult Result; |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3354 | // Removing references was already done. |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3355 | if (!DeduceFromInitializerList(*this, TemplateParams, ParamType, ILE, |
| 3356 | Info, Deduced, TDF, Result)) |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3357 | continue; |
| 3358 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3359 | if (Result) |
| 3360 | return Result; |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3361 | // Don't track the argument type, since an initializer list has none. |
| 3362 | continue; |
| 3363 | } |
| 3364 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3365 | // Keep track of the argument type and corresponding parameter index, |
| 3366 | // so we can check for compatibility between the deduced A and A. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3367 | OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx-1, |
Douglas Gregor | 0c83c81 | 2011-10-09 22:06:46 +0000 | [diff] [blame] | 3368 | ArgType)); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3369 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3370 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3371 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3372 | ParamType, ArgType, |
| 3373 | Info, Deduced, TDF)) |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3374 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3375 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3376 | continue; |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3377 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3378 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3379 | // C++0x [temp.deduct.call]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3380 | // For a function parameter pack that occurs at the end of the |
| 3381 | // parameter-declaration-list, the type A of each remaining argument of |
| 3382 | // the call is compared with the type P of the declarator-id of the |
| 3383 | // function parameter pack. Each comparison deduces template arguments |
| 3384 | // for subsequent positions in the template parameter packs expanded by |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3385 | // the function parameter pack. For a function parameter pack that does |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3386 | // 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] | 3387 | // the parameter pack is a non-deduced context. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3388 | if (ParamIdx + 1 < NumParamTypes) |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3389 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3390 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3391 | QualType ParamPattern = ParamExpansion->getPattern(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3392 | PackDeductionScope PackScope(*this, TemplateParams, Deduced, Info, |
| 3393 | ParamPattern); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3394 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3395 | bool HasAnyArguments = false; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3396 | for (; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3397 | HasAnyArguments = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3398 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3399 | QualType OrigParamType = ParamPattern; |
| 3400 | ParamType = OrigParamType; |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3401 | Expr *Arg = Args[ArgIdx]; |
| 3402 | QualType ArgType = Arg->getType(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3403 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3404 | unsigned TDF = 0; |
| 3405 | if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams, |
| 3406 | ParamType, ArgType, Arg, |
| 3407 | TDF)) { |
| 3408 | // We can't actually perform any deduction for this argument, so stop |
| 3409 | // deduction at this point. |
| 3410 | ++ArgIdx; |
| 3411 | break; |
| 3412 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3413 | |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3414 | // As above, initializer lists need special handling. |
| 3415 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3416 | TemplateDeductionResult Result; |
| 3417 | if (!DeduceFromInitializerList(*this, TemplateParams, ParamType, ILE, |
| 3418 | Info, Deduced, TDF, Result)) { |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3419 | ++ArgIdx; |
| 3420 | break; |
| 3421 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3422 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3423 | if (Result) |
| 3424 | return Result; |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3425 | } else { |
| 3426 | |
| 3427 | // Keep track of the argument type and corresponding argument index, |
| 3428 | // so we can check for compatibility between the deduced A and A. |
| 3429 | if (hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3430 | OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx, |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3431 | ArgType)); |
| 3432 | |
| 3433 | if (TemplateDeductionResult Result |
| 3434 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3435 | ParamType, ArgType, Info, |
| 3436 | Deduced, TDF)) |
| 3437 | return Result; |
| 3438 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3439 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3440 | PackScope.nextPackElement(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3441 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3442 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3443 | // Build argument packs for each of the parameter packs expanded by this |
| 3444 | // pack expansion. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3445 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3446 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3447 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3448 | // After we've matching against a parameter pack, we're done. |
| 3449 | break; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3450 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3451 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3452 | return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3453 | NumExplicitlySpecified, Specialization, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3454 | Info, &OriginalCallArgs, |
| 3455 | PartialOverloading); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3456 | } |
| 3457 | |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3458 | QualType Sema::adjustCCAndNoReturn(QualType ArgFunctionType, |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3459 | QualType FunctionType, |
| 3460 | bool AdjustExceptionSpec) { |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3461 | if (ArgFunctionType.isNull()) |
| 3462 | return ArgFunctionType; |
| 3463 | |
| 3464 | const FunctionProtoType *FunctionTypeP = |
| 3465 | FunctionType->castAs<FunctionProtoType>(); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3466 | const FunctionProtoType *ArgFunctionTypeP = |
| 3467 | ArgFunctionType->getAs<FunctionProtoType>(); |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3468 | |
| 3469 | FunctionProtoType::ExtProtoInfo EPI = ArgFunctionTypeP->getExtProtoInfo(); |
| 3470 | bool Rebuild = false; |
| 3471 | |
| 3472 | CallingConv CC = FunctionTypeP->getCallConv(); |
| 3473 | if (EPI.ExtInfo.getCC() != CC) { |
| 3474 | EPI.ExtInfo = EPI.ExtInfo.withCallingConv(CC); |
| 3475 | Rebuild = true; |
| 3476 | } |
| 3477 | |
| 3478 | bool NoReturn = FunctionTypeP->getNoReturnAttr(); |
| 3479 | if (EPI.ExtInfo.getNoReturn() != NoReturn) { |
| 3480 | EPI.ExtInfo = EPI.ExtInfo.withNoReturn(NoReturn); |
| 3481 | Rebuild = true; |
| 3482 | } |
| 3483 | |
| 3484 | if (AdjustExceptionSpec && (FunctionTypeP->hasExceptionSpec() || |
| 3485 | ArgFunctionTypeP->hasExceptionSpec())) { |
| 3486 | EPI.ExceptionSpec = FunctionTypeP->getExtProtoInfo().ExceptionSpec; |
| 3487 | Rebuild = true; |
| 3488 | } |
| 3489 | |
| 3490 | if (!Rebuild) |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3491 | return ArgFunctionType; |
| 3492 | |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3493 | return Context.getFunctionType(ArgFunctionTypeP->getReturnType(), |
| 3494 | ArgFunctionTypeP->getParamTypes(), EPI); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3495 | } |
| 3496 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3497 | /// \brief Deduce template arguments when taking the address of a function |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3498 | /// template (C++ [temp.deduct.funcaddr]) or matching a specialization to |
| 3499 | /// a template. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3500 | /// |
| 3501 | /// \param FunctionTemplate the function template for which we are performing |
| 3502 | /// template argument deduction. |
| 3503 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3504 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3505 | /// arguments. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3506 | /// |
| 3507 | /// \param ArgFunctionType the function type that will be used as the |
| 3508 | /// "argument" type (A) when performing template argument deduction from the |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3509 | /// function template's function type. This type may be NULL, if there is no |
| 3510 | /// argument type to compare against, in C++0x [temp.arg.explicit]p3. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3511 | /// |
| 3512 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3513 | /// this will be set to the function template specialization produced by |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3514 | /// template argument deduction. |
| 3515 | /// |
| 3516 | /// \param Info the argument will be updated to provide additional information |
| 3517 | /// about template argument deduction. |
| 3518 | /// |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3519 | /// \param IsAddressOfFunction If \c true, we are deducing as part of taking |
| 3520 | /// the address of a function template per [temp.deduct.funcaddr] and |
| 3521 | /// [over.over]. If \c false, we are looking up a function template |
| 3522 | /// specialization based on its signature, per [temp.deduct.decl]. |
| 3523 | /// |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3524 | /// \returns the result of template argument deduction. |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3525 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3526 | FunctionTemplateDecl *FunctionTemplate, |
| 3527 | TemplateArgumentListInfo *ExplicitTemplateArgs, QualType ArgFunctionType, |
| 3528 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 3529 | bool IsAddressOfFunction) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3530 | if (FunctionTemplate->isInvalidDecl()) |
| 3531 | return TDK_Invalid; |
| 3532 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3533 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 3534 | TemplateParameterList *TemplateParams |
| 3535 | = FunctionTemplate->getTemplateParameters(); |
| 3536 | QualType FunctionType = Function->getType(); |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3537 | |
| 3538 | // When taking the address of a function, we require convertibility of |
| 3539 | // the resulting function type. Otherwise, we allow arbitrary mismatches |
| 3540 | // of calling convention, noreturn, and noexcept. |
| 3541 | if (!IsAddressOfFunction) |
| 3542 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, FunctionType, |
| 3543 | /*AdjustExceptionSpec*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3544 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3545 | // Substitute any explicit template arguments. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3546 | LocalInstantiationScope InstScope(*this); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3547 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3548 | unsigned NumExplicitlySpecified = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3549 | SmallVector<QualType, 4> ParamTypes; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3550 | if (ExplicitTemplateArgs) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3551 | if (TemplateDeductionResult Result |
| 3552 | = SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3553 | *ExplicitTemplateArgs, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3554 | Deduced, ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3555 | &FunctionType, Info)) |
| 3556 | return Result; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3557 | |
| 3558 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3559 | } |
| 3560 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3561 | // Unevaluated SFINAE context. |
| 3562 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3563 | SFINAETrap Trap(*this); |
| 3564 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3565 | Deduced.resize(TemplateParams->size()); |
| 3566 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3567 | // 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] | 3568 | // type so that we treat it as a non-deduced context in what follows. If we |
| 3569 | // are looking up by signature, the signature type should also have a deduced |
| 3570 | // return type, which we instead expect to exactly match. |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3571 | bool HasDeducedReturnType = false; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3572 | if (getLangOpts().CPlusPlus14 && IsAddressOfFunction && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3573 | Function->getReturnType()->getContainedAutoType()) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3574 | FunctionType = SubstAutoType(FunctionType, Context.DependentTy); |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3575 | HasDeducedReturnType = true; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3576 | } |
| 3577 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3578 | if (!ArgFunctionType.isNull()) { |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3579 | unsigned TDF = TDF_TopLevelParameterTypeList; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3580 | if (IsAddressOfFunction) |
| 3581 | TDF |= TDF_InOverloadResolution; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3582 | // Deduce template arguments from the function type. |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3583 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3584 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3585 | FunctionType, ArgFunctionType, |
| 3586 | Info, Deduced, TDF)) |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3587 | return Result; |
| 3588 | } |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3589 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3590 | if (TemplateDeductionResult Result |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3591 | = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
| 3592 | NumExplicitlySpecified, |
| 3593 | Specialization, Info)) |
| 3594 | return Result; |
| 3595 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3596 | // If the function has a deduced return type, deduce it now, so we can check |
| 3597 | // that the deduced function type matches the requested type. |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3598 | if (HasDeducedReturnType && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3599 | Specialization->getReturnType()->isUndeducedType() && |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3600 | DeduceReturnType(Specialization, Info.getLocation(), false)) |
| 3601 | return TDK_MiscellaneousDeductionFailure; |
| 3602 | |
Richard Smith | 9095e5b | 2016-11-01 01:31:23 +0000 | [diff] [blame] | 3603 | // If the function has a dependent exception specification, resolve it now, |
| 3604 | // so we can check that the exception specification matches. |
| 3605 | auto *SpecializationFPT = |
| 3606 | Specialization->getType()->castAs<FunctionProtoType>(); |
| 3607 | if (getLangOpts().CPlusPlus1z && |
| 3608 | isUnresolvedExceptionSpec(SpecializationFPT->getExceptionSpecType()) && |
| 3609 | !ResolveExceptionSpec(Info.getLocation(), SpecializationFPT)) |
| 3610 | return TDK_MiscellaneousDeductionFailure; |
| 3611 | |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3612 | // Adjust the exception specification of the argument again to match the |
| 3613 | // substituted and resolved type we just formed. (Calling convention and |
| 3614 | // noreturn can't be dependent, so we don't actually need this for them |
| 3615 | // right now.) |
| 3616 | QualType SpecializationType = Specialization->getType(); |
| 3617 | if (!IsAddressOfFunction) |
| 3618 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, SpecializationType, |
| 3619 | /*AdjustExceptionSpec*/true); |
| 3620 | |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3621 | // 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] | 3622 | // specialization with respect to arguments of compatible pointer to function |
| 3623 | // types, template argument deduction fails. |
| 3624 | if (!ArgFunctionType.isNull()) { |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3625 | if (IsAddressOfFunction && |
| 3626 | !isSameOrCompatibleFunctionType( |
| 3627 | Context.getCanonicalType(SpecializationType), |
| 3628 | Context.getCanonicalType(ArgFunctionType))) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3629 | return TDK_MiscellaneousDeductionFailure; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3630 | |
| 3631 | if (!IsAddressOfFunction && |
| 3632 | !Context.hasSameType(SpecializationType, ArgFunctionType)) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3633 | return TDK_MiscellaneousDeductionFailure; |
| 3634 | } |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3635 | |
| 3636 | return TDK_Success; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3637 | } |
| 3638 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3639 | /// \brief Given a function declaration (e.g. a generic lambda conversion |
| 3640 | /// function) that contains an 'auto' in its result type, substitute it |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3641 | /// with TypeToReplaceAutoWith. Be careful to pass in the type you want |
| 3642 | /// to replace 'auto' with and not the actual result type you want |
| 3643 | /// to set the function to. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3644 | static inline void |
| 3645 | SubstAutoWithinFunctionReturnType(FunctionDecl *F, |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3646 | QualType TypeToReplaceAutoWith, Sema &S) { |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3647 | assert(!TypeToReplaceAutoWith->getContainedAutoType()); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3648 | QualType AutoResultType = F->getReturnType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3649 | assert(AutoResultType->getContainedAutoType()); |
| 3650 | QualType DeducedResultType = S.SubstAutoType(AutoResultType, |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3651 | TypeToReplaceAutoWith); |
| 3652 | S.Context.adjustDeducedFunctionResultType(F, DeducedResultType); |
| 3653 | } |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3654 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3655 | /// \brief Given a specialized conversion operator of a generic lambda |
| 3656 | /// create the corresponding specializations of the call operator and |
| 3657 | /// the static-invoker. If the return type of the call operator is auto, |
| 3658 | /// deduce its return type and check if that matches the |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3659 | /// return type of the destination function ptr. |
| 3660 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3661 | static inline Sema::TemplateDeductionResult |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3662 | SpecializeCorrespondingLambdaCallOperatorAndInvoker( |
| 3663 | CXXConversionDecl *ConversionSpecialized, |
| 3664 | SmallVectorImpl<DeducedTemplateArgument> &DeducedArguments, |
| 3665 | QualType ReturnTypeOfDestFunctionPtr, |
| 3666 | TemplateDeductionInfo &TDInfo, |
| 3667 | Sema &S) { |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3668 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3669 | CXXRecordDecl *LambdaClass = ConversionSpecialized->getParent(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3670 | assert(LambdaClass && LambdaClass->isGenericLambda()); |
| 3671 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3672 | CXXMethodDecl *CallOpGeneric = LambdaClass->getLambdaCallOperator(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3673 | QualType CallOpResultType = CallOpGeneric->getReturnType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3674 | const bool GenericLambdaCallOperatorHasDeducedReturnType = |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3675 | CallOpResultType->getContainedAutoType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3676 | |
| 3677 | FunctionTemplateDecl *CallOpTemplate = |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3678 | CallOpGeneric->getDescribedFunctionTemplate(); |
| 3679 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3680 | FunctionDecl *CallOpSpecialized = nullptr; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3681 | // Use the deduced arguments of the conversion function, to specialize our |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3682 | // generic lambda's call operator. |
| 3683 | if (Sema::TemplateDeductionResult Result |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3684 | = S.FinishTemplateArgumentDeduction(CallOpTemplate, |
| 3685 | DeducedArguments, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3686 | 0, CallOpSpecialized, TDInfo)) |
| 3687 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3688 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3689 | // 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] | 3690 | if (GenericLambdaCallOperatorHasDeducedReturnType && |
| 3691 | CallOpSpecialized->getReturnType()->isUndeducedType()) |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3692 | S.DeduceReturnType(CallOpSpecialized, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3693 | CallOpSpecialized->getPointOfInstantiation(), |
| 3694 | /*Diagnose*/ true); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3695 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3696 | // Check to see if the return type of the destination ptr-to-function |
| 3697 | // matches the return type of the call operator. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3698 | if (!S.Context.hasSameType(CallOpSpecialized->getReturnType(), |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3699 | ReturnTypeOfDestFunctionPtr)) |
| 3700 | return Sema::TDK_NonDeducedMismatch; |
| 3701 | // Since we have succeeded in matching the source and destination |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3702 | // ptr-to-functions (now including return type), and have successfully |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3703 | // specialized our corresponding call operator, we are ready to |
| 3704 | // specialize the static invoker with the deduced arguments of our |
| 3705 | // ptr-to-function. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3706 | FunctionDecl *InvokerSpecialized = nullptr; |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3707 | FunctionTemplateDecl *InvokerTemplate = LambdaClass-> |
| 3708 | getLambdaStaticInvoker()->getDescribedFunctionTemplate(); |
| 3709 | |
Yaron Keren | f428fcf | 2015-05-13 17:56:46 +0000 | [diff] [blame] | 3710 | #ifndef NDEBUG |
| 3711 | Sema::TemplateDeductionResult LLVM_ATTRIBUTE_UNUSED Result = |
| 3712 | #endif |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3713 | S.FinishTemplateArgumentDeduction(InvokerTemplate, DeducedArguments, 0, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3714 | InvokerSpecialized, TDInfo); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3715 | assert(Result == Sema::TDK_Success && |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3716 | "If the call operator succeeded so should the invoker!"); |
| 3717 | // Set the result type to match the corresponding call operator |
| 3718 | // specialization's result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3719 | if (GenericLambdaCallOperatorHasDeducedReturnType && |
| 3720 | InvokerSpecialized->getReturnType()->isUndeducedType()) { |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3721 | // Be sure to get the type to replace 'auto' with and not |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3722 | // the full result type of the call op specialization |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3723 | // to substitute into the 'auto' of the invoker and conversion |
| 3724 | // function. |
| 3725 | // For e.g. |
| 3726 | // int* (*fp)(int*) = [](auto* a) -> auto* { return a; }; |
| 3727 | // We don't want to subst 'int*' into 'auto' to get int**. |
| 3728 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3729 | QualType TypeToReplaceAutoWith = CallOpSpecialized->getReturnType() |
| 3730 | ->getContainedAutoType() |
| 3731 | ->getDeducedType(); |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3732 | SubstAutoWithinFunctionReturnType(InvokerSpecialized, |
| 3733 | TypeToReplaceAutoWith, S); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3734 | SubstAutoWithinFunctionReturnType(ConversionSpecialized, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3735 | TypeToReplaceAutoWith, S); |
| 3736 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3737 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3738 | // Ensure that static invoker doesn't have a const qualifier. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3739 | // FIXME: When creating the InvokerTemplate in SemaLambda.cpp |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3740 | // do not use the CallOperator's TypeSourceInfo which allows |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3741 | // the const qualifier to leak through. |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3742 | const FunctionProtoType *InvokerFPT = InvokerSpecialized-> |
| 3743 | getType().getTypePtr()->castAs<FunctionProtoType>(); |
| 3744 | FunctionProtoType::ExtProtoInfo EPI = InvokerFPT->getExtProtoInfo(); |
| 3745 | EPI.TypeQuals = 0; |
| 3746 | InvokerSpecialized->setType(S.Context.getFunctionType( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3747 | InvokerFPT->getReturnType(), InvokerFPT->getParamTypes(), EPI)); |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3748 | return Sema::TDK_Success; |
| 3749 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3750 | /// \brief Deduce template arguments for a templated conversion |
| 3751 | /// function (C++ [temp.deduct.conv]) and, if successful, produce a |
| 3752 | /// conversion function template specialization. |
| 3753 | Sema::TemplateDeductionResult |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3754 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *ConversionTemplate, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3755 | QualType ToType, |
| 3756 | CXXConversionDecl *&Specialization, |
| 3757 | TemplateDeductionInfo &Info) { |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3758 | if (ConversionTemplate->isInvalidDecl()) |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3759 | return TDK_Invalid; |
| 3760 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3761 | CXXConversionDecl *ConversionGeneric |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3762 | = cast<CXXConversionDecl>(ConversionTemplate->getTemplatedDecl()); |
| 3763 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3764 | QualType FromType = ConversionGeneric->getConversionType(); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3765 | |
| 3766 | // Canonicalize the types for deduction. |
| 3767 | QualType P = Context.getCanonicalType(FromType); |
| 3768 | QualType A = Context.getCanonicalType(ToType); |
| 3769 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3770 | // C++0x [temp.deduct.conv]p2: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3771 | // If P is a reference type, the type referred to by P is used for |
| 3772 | // type deduction. |
| 3773 | if (const ReferenceType *PRef = P->getAs<ReferenceType>()) |
| 3774 | P = PRef->getPointeeType(); |
| 3775 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3776 | // C++0x [temp.deduct.conv]p4: |
| 3777 | // [...] 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] | 3778 | // for type deduction. |
| 3779 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3780 | A = ARef->getPointeeType().getUnqualifiedType(); |
| 3781 | // C++ [temp.deduct.conv]p3: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3782 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3783 | // If A is not a reference type: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3784 | else { |
| 3785 | assert(!A->isReferenceType() && "Reference types were handled above"); |
| 3786 | |
| 3787 | // - If P is an array type, the pointer type produced by the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3788 | // array-to-pointer standard conversion (4.2) is used in place |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3789 | // of P for type deduction; otherwise, |
| 3790 | if (P->isArrayType()) |
| 3791 | P = Context.getArrayDecayedType(P); |
| 3792 | // - If P is a function type, the pointer type produced by the |
| 3793 | // function-to-pointer standard conversion (4.3) is used in |
| 3794 | // place of P for type deduction; otherwise, |
| 3795 | else if (P->isFunctionType()) |
| 3796 | P = Context.getPointerType(P); |
| 3797 | // - 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] | 3798 | // P's type are ignored for type deduction. |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3799 | else |
| 3800 | P = P.getUnqualifiedType(); |
| 3801 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3802 | // C++0x [temp.deduct.conv]p4: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3803 | // 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] | 3804 | // 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] | 3805 | // referred to by A is used for type deduction. |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3806 | A = A.getUnqualifiedType(); |
| 3807 | } |
| 3808 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3809 | // Unevaluated SFINAE context. |
| 3810 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3811 | SFINAETrap Trap(*this); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3812 | |
| 3813 | // C++ [temp.deduct.conv]p1: |
| 3814 | // Template argument deduction is done by comparing the return |
| 3815 | // type of the template conversion function (call it P) with the |
| 3816 | // type that is required as the result of the conversion (call it |
| 3817 | // A) as described in 14.8.2.4. |
| 3818 | TemplateParameterList *TemplateParams |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3819 | = ConversionTemplate->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3820 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3821 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3822 | |
| 3823 | // C++0x [temp.deduct.conv]p4: |
| 3824 | // In general, the deduction process attempts to find template |
| 3825 | // argument values that will make the deduced A identical to |
| 3826 | // A. However, there are two cases that allow a difference: |
| 3827 | unsigned TDF = 0; |
| 3828 | // - If the original A is a reference type, A can be more |
| 3829 | // cv-qualified than the deduced A (i.e., the type referred to |
| 3830 | // by the reference) |
| 3831 | if (ToType->isReferenceType()) |
| 3832 | TDF |= TDF_ParamWithReferenceType; |
| 3833 | // - The deduced A can be another pointer or pointer to member |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3834 | // type that can be converted to A via a qualification |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3835 | // conversion. |
| 3836 | // |
| 3837 | // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when |
| 3838 | // both P and A are pointers or member pointers. In this case, we |
| 3839 | // just ignore cv-qualifiers completely). |
| 3840 | if ((P->isPointerType() && A->isPointerType()) || |
Douglas Gregor | 9f05ed5 | 2011-08-30 00:37:54 +0000 | [diff] [blame] | 3841 | (P->isMemberPointerType() && A->isMemberPointerType())) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3842 | TDF |= TDF_IgnoreQualifiers; |
| 3843 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3844 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3845 | P, A, Info, Deduced, TDF)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3846 | return Result; |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3847 | |
| 3848 | // Create an Instantiation Scope for finalizing the operator. |
| 3849 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3850 | // Finish template argument deduction. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3851 | FunctionDecl *ConversionSpecialized = nullptr; |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3852 | TemplateDeductionResult Result |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3853 | = FinishTemplateArgumentDeduction(ConversionTemplate, Deduced, 0, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3854 | ConversionSpecialized, Info); |
| 3855 | Specialization = cast_or_null<CXXConversionDecl>(ConversionSpecialized); |
| 3856 | |
| 3857 | // 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] | 3858 | // to a ptr-to-function, use the deduced arguments from the conversion |
| 3859 | // function to specialize the corresponding call operator. |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3860 | // e.g., int (*fp)(int) = [](auto a) { return a; }; |
| 3861 | if (Result == TDK_Success && isLambdaConversionOperator(ConversionGeneric)) { |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3862 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3863 | // 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] | 3864 | // to. This is necessary for matching the lambda call operator's return |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3865 | // type to that of the destination ptr-to-function's return type. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3866 | assert(A->isPointerType() && |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3867 | "Can only convert from lambda to ptr-to-function"); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3868 | const FunctionType *ToFunType = |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3869 | A->getPointeeType().getTypePtr()->getAs<FunctionType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3870 | const QualType DestFunctionPtrReturnType = ToFunType->getReturnType(); |
| 3871 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3872 | // Create the corresponding specializations of the call operator and |
| 3873 | // the static-invoker; and if the return type is auto, |
| 3874 | // deduce the return type and check if it matches the |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3875 | // DestFunctionPtrReturnType. |
| 3876 | // For instance: |
| 3877 | // auto L = [](auto a) { return f(a); }; |
| 3878 | // int (*fp)(int) = L; |
| 3879 | // char (*fp2)(int) = L; <-- Not OK. |
| 3880 | |
| 3881 | Result = SpecializeCorrespondingLambdaCallOperatorAndInvoker( |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3882 | Specialization, Deduced, DestFunctionPtrReturnType, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3883 | Info, *this); |
| 3884 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3885 | return Result; |
| 3886 | } |
| 3887 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3888 | /// \brief Deduce template arguments for a function template when there is |
| 3889 | /// nothing to deduce against (C++0x [temp.arg.explicit]p3). |
| 3890 | /// |
| 3891 | /// \param FunctionTemplate the function template for which we are performing |
| 3892 | /// template argument deduction. |
| 3893 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3894 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3895 | /// arguments. |
| 3896 | /// |
| 3897 | /// \param Specialization if template argument deduction was successful, |
| 3898 | /// this will be set to the function template specialization produced by |
| 3899 | /// template argument deduction. |
| 3900 | /// |
| 3901 | /// \param Info the argument will be updated to provide additional information |
| 3902 | /// about template argument deduction. |
| 3903 | /// |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3904 | /// \param IsAddressOfFunction If \c true, we are deducing as part of taking |
| 3905 | /// the address of a function template in a context where we do not have a |
| 3906 | /// target type, per [over.over]. If \c false, we are looking up a function |
| 3907 | /// template specialization based on its signature, which only happens when |
| 3908 | /// deducing a function parameter type from an argument that is a template-id |
| 3909 | /// naming a function template specialization. |
| 3910 | /// |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3911 | /// \returns the result of template argument deduction. |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3912 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3913 | FunctionTemplateDecl *FunctionTemplate, |
| 3914 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 3915 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 3916 | bool IsAddressOfFunction) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3917 | return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3918 | QualType(), Specialization, Info, |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3919 | IsAddressOfFunction); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3920 | } |
| 3921 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3922 | namespace { |
| 3923 | /// Substitute the 'auto' type specifier within a type for a given replacement |
| 3924 | /// type. |
| 3925 | class SubstituteAutoTransform : |
| 3926 | public TreeTransform<SubstituteAutoTransform> { |
| 3927 | QualType Replacement; |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 3928 | bool UseAutoSugar; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3929 | public: |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 3930 | SubstituteAutoTransform(Sema &SemaRef, QualType Replacement, |
| 3931 | bool UseAutoSugar = true) |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3932 | : TreeTransform<SubstituteAutoTransform>(SemaRef), |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 3933 | Replacement(Replacement), UseAutoSugar(UseAutoSugar) {} |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3934 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3935 | QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) { |
| 3936 | // If we're building the type pattern to deduce against, don't wrap the |
| 3937 | // substituted type in an AutoType. Certain template deduction rules |
| 3938 | // apply only when a template type parameter appears directly (and not if |
| 3939 | // the parameter is found through desugaring). For instance: |
| 3940 | // auto &&lref = lvalue; |
| 3941 | // must transform into "rvalue reference to T" not "rvalue reference to |
| 3942 | // 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] | 3943 | if (!UseAutoSugar) { |
| 3944 | assert(isa<TemplateTypeParmType>(Replacement) && |
| 3945 | "unexpected unsugared replacement kind"); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3946 | QualType Result = Replacement; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 3947 | TemplateTypeParmTypeLoc NewTL = |
| 3948 | TLB.push<TemplateTypeParmTypeLoc>(Result); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3949 | NewTL.setNameLoc(TL.getNameLoc()); |
| 3950 | return Result; |
| 3951 | } else { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 3952 | QualType Result = SemaRef.Context.getAutoType( |
| 3953 | Replacement, TL.getTypePtr()->getKeyword(), Replacement.isNull()); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3954 | AutoTypeLoc NewTL = TLB.push<AutoTypeLoc>(Result); |
| 3955 | NewTL.setNameLoc(TL.getNameLoc()); |
| 3956 | return Result; |
| 3957 | } |
| 3958 | } |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 3959 | |
| 3960 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 3961 | // Lambdas never need to be transformed. |
| 3962 | return E; |
| 3963 | } |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3964 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3965 | QualType Apply(TypeLoc TL) { |
| 3966 | // Create some scratch storage for the transformed type locations. |
| 3967 | // FIXME: We're just going to throw this information away. Don't build it. |
| 3968 | TypeLocBuilder TLB; |
| 3969 | TLB.reserve(TL.getFullDataSize()); |
| 3970 | return TransformType(TLB, TL); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3971 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3972 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3973 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3974 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3975 | Sema::DeduceAutoResult |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 3976 | Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *&Init, QualType &Result, |
| 3977 | Optional<unsigned> DependentDeductionDepth) { |
| 3978 | return DeduceAutoType(Type->getTypeLoc(), Init, Result, |
| 3979 | DependentDeductionDepth); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3980 | } |
| 3981 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3982 | /// \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] | 3983 | /// |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 3984 | /// Note that this is done even if the initializer is dependent. (This is |
| 3985 | /// necessary to support partial ordering of templates using 'auto'.) |
| 3986 | /// A dependent type will be produced when deducing from a dependent type. |
| 3987 | /// |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3988 | /// \param Type the type pattern using the auto type-specifier. |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3989 | /// \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] | 3990 | /// \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] | 3991 | /// deduced type. |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 3992 | /// \param DependentDeductionDepth Set if we should permit deduction in |
| 3993 | /// dependent cases. This is necessary for template partial ordering with |
| 3994 | /// 'auto' template parameters. The value specified is the template |
| 3995 | /// parameter depth at which we should perform 'auto' deduction. |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 3996 | Sema::DeduceAutoResult |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 3997 | Sema::DeduceAutoType(TypeLoc Type, Expr *&Init, QualType &Result, |
| 3998 | Optional<unsigned> DependentDeductionDepth) { |
John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 3999 | if (Init->getType()->isNonOverloadPlaceholderType()) { |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4000 | ExprResult NonPlaceholder = CheckPlaceholderExpr(Init); |
| 4001 | if (NonPlaceholder.isInvalid()) |
| 4002 | return DAR_FailedAlreadyDiagnosed; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4003 | Init = NonPlaceholder.get(); |
John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4004 | } |
| 4005 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4006 | if (!DependentDeductionDepth && |
| 4007 | (Type.getType()->isDependentType() || Init->isTypeDependent())) { |
| 4008 | Result = SubstituteAutoTransform(*this, QualType()).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4009 | assert(!Result.isNull() && "substituting DependentTy can't fail"); |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4010 | return DAR_Succeeded; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4011 | } |
| 4012 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4013 | // Find the depth of template parameter to synthesize. |
| 4014 | unsigned Depth = DependentDeductionDepth.getValueOr(0); |
| 4015 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4016 | // If this is a 'decltype(auto)' specifier, do the decltype dance. |
| 4017 | // Since 'decltype(auto)' can only occur at the top of the type, we |
| 4018 | // don't need to go digging for it. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4019 | if (const AutoType *AT = Type.getType()->getAs<AutoType>()) { |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4020 | if (AT->isDecltypeAuto()) { |
| 4021 | if (isa<InitListExpr>(Init)) { |
| 4022 | Diag(Init->getLocStart(), diag::err_decltype_auto_initializer_list); |
| 4023 | return DAR_FailedAlreadyDiagnosed; |
| 4024 | } |
| 4025 | |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 4026 | QualType Deduced = BuildDecltypeType(Init, Init->getLocStart(), false); |
David Majnemer | 3c20ab2 | 2015-07-01 00:29:28 +0000 | [diff] [blame] | 4027 | if (Deduced.isNull()) |
| 4028 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4029 | // FIXME: Support a non-canonical deduced type for 'auto'. |
| 4030 | Deduced = Context.getCanonicalType(Deduced); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4031 | Result = SubstituteAutoTransform(*this, Deduced).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4032 | if (Result.isNull()) |
| 4033 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4034 | return DAR_Succeeded; |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4035 | } else if (!getLangOpts().CPlusPlus) { |
| 4036 | if (isa<InitListExpr>(Init)) { |
| 4037 | Diag(Init->getLocStart(), diag::err_auto_init_list_from_c); |
| 4038 | return DAR_FailedAlreadyDiagnosed; |
| 4039 | } |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4040 | } |
| 4041 | } |
| 4042 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4043 | SourceLocation Loc = Init->getExprLoc(); |
| 4044 | |
| 4045 | LocalInstantiationScope InstScope(*this); |
| 4046 | |
| 4047 | // Build template<class TemplParam> void Func(FuncParam); |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4048 | TemplateTypeParmDecl *TemplParam = TemplateTypeParmDecl::Create( |
| 4049 | Context, nullptr, SourceLocation(), Loc, Depth, 0, nullptr, false, false); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4050 | QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0); |
| 4051 | NamedDecl *TemplParamPtr = TemplParam; |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 4052 | FixedSizeTemplateParameterListStorage<1, false> TemplateParamsSt( |
| 4053 | Loc, Loc, TemplParamPtr, Loc, nullptr); |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 4054 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4055 | QualType FuncParam = |
| 4056 | SubstituteAutoTransform(*this, TemplArg, /*UseAutoSugar*/false) |
| 4057 | .Apply(Type); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4058 | assert(!FuncParam.isNull() && |
| 4059 | "substituting template parameter for 'auto' failed"); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4060 | |
| 4061 | // Deduce type of TemplParam in Func(Init) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4062 | SmallVector<DeducedTemplateArgument, 1> Deduced; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4063 | Deduced.resize(1); |
| 4064 | QualType InitType = Init->getType(); |
| 4065 | unsigned TDF = 0; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4066 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4067 | TemplateDeductionInfo Info(Loc, Depth); |
| 4068 | |
| 4069 | // If deduction failed, don't diagnose if the initializer is dependent; it |
| 4070 | // might acquire a matching type in the instantiation. |
| 4071 | auto DeductionFailed = [&]() -> DeduceAutoResult { |
| 4072 | if (Init->isTypeDependent()) { |
| 4073 | Result = SubstituteAutoTransform(*this, QualType()).Apply(Type); |
| 4074 | assert(!Result.isNull() && "substituting DependentTy can't fail"); |
| 4075 | return DAR_Succeeded; |
| 4076 | } |
| 4077 | return DAR_Failed; |
| 4078 | }; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4079 | |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 4080 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4081 | if (InitList) { |
| 4082 | for (unsigned i = 0, e = InitList->getNumInits(); i < e; ++i) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4083 | if (DeduceTemplateArgumentByListElement(*this, TemplateParamsSt.get(), |
| 4084 | TemplArg, InitList->getInit(i), |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 4085 | Info, Deduced, TDF)) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4086 | return DeductionFailed(); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4087 | } |
| 4088 | } else { |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4089 | if (!getLangOpts().CPlusPlus && Init->refersToBitField()) { |
| 4090 | Diag(Loc, diag::err_auto_bitfield); |
| 4091 | return DAR_FailedAlreadyDiagnosed; |
| 4092 | } |
| 4093 | |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4094 | if (AdjustFunctionParmAndArgTypesForDeduction( |
| 4095 | *this, TemplateParamsSt.get(), FuncParam, InitType, Init, TDF)) |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 4096 | return DAR_Failed; |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 4097 | |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4098 | if (DeduceTemplateArgumentsByTypeMatch(*this, TemplateParamsSt.get(), |
| 4099 | FuncParam, InitType, Info, Deduced, |
| 4100 | TDF)) |
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 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4103 | |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4104 | // Could be null if somehow 'auto' appears in a non-deduced context. |
Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4105 | if (Deduced[0].getKind() != TemplateArgument::Type) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4106 | return DeductionFailed(); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4107 | |
Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4108 | QualType DeducedType = Deduced[0].getAsType(); |
| 4109 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4110 | if (InitList) { |
| 4111 | DeducedType = BuildStdInitializerList(DeducedType, Loc); |
| 4112 | if (DeducedType.isNull()) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4113 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4114 | } |
| 4115 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4116 | Result = SubstituteAutoTransform(*this, DeducedType).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4117 | if (Result.isNull()) |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4118 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4119 | |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4120 | // Check that the deduced argument type is compatible with the original |
| 4121 | // argument type per C++ [temp.deduct.call]p4. |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4122 | if (!InitList && !Result.isNull() && |
| 4123 | CheckOriginalCallArgDeduction(*this, |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4124 | Sema::OriginalCallArg(FuncParam,0,InitType), |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4125 | Result)) { |
| 4126 | Result = QualType(); |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4127 | return DeductionFailed(); |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4128 | } |
| 4129 | |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4130 | return DAR_Succeeded; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4131 | } |
| 4132 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 4133 | QualType Sema::SubstAutoType(QualType TypeWithAuto, |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4134 | QualType TypeToReplaceAuto) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4135 | if (TypeToReplaceAuto->isDependentType()) |
| 4136 | TypeToReplaceAuto = QualType(); |
| 4137 | return SubstituteAutoTransform(*this, TypeToReplaceAuto) |
| 4138 | .TransformType(TypeWithAuto); |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4139 | } |
| 4140 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 4141 | TypeSourceInfo* Sema::SubstAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto, |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4142 | QualType TypeToReplaceAuto) { |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4143 | if (TypeToReplaceAuto->isDependentType()) |
| 4144 | TypeToReplaceAuto = QualType(); |
| 4145 | return SubstituteAutoTransform(*this, TypeToReplaceAuto) |
| 4146 | .TransformType(TypeWithAuto); |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4149 | void Sema::DiagnoseAutoDeductionFailure(VarDecl *VDecl, Expr *Init) { |
| 4150 | if (isa<InitListExpr>(Init)) |
| 4151 | Diag(VDecl->getLocation(), |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4152 | VDecl->isInitCapture() |
| 4153 | ? diag::err_init_capture_deduction_failure_from_init_list |
| 4154 | : diag::err_auto_var_deduction_failure_from_init_list) |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4155 | << VDecl->getDeclName() << VDecl->getType() << Init->getSourceRange(); |
| 4156 | else |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4157 | Diag(VDecl->getLocation(), |
| 4158 | VDecl->isInitCapture() ? diag::err_init_capture_deduction_failure |
| 4159 | : diag::err_auto_var_deduction_failure) |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4160 | << VDecl->getDeclName() << VDecl->getType() << Init->getType() |
| 4161 | << Init->getSourceRange(); |
| 4162 | } |
| 4163 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4164 | bool Sema::DeduceReturnType(FunctionDecl *FD, SourceLocation Loc, |
| 4165 | bool Diagnose) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4166 | assert(FD->getReturnType()->isUndeducedType()); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4167 | |
| 4168 | if (FD->getTemplateInstantiationPattern()) |
| 4169 | InstantiateFunctionDefinition(Loc, FD); |
| 4170 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4171 | bool StillUndeduced = FD->getReturnType()->isUndeducedType(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4172 | if (StillUndeduced && Diagnose && !FD->isInvalidDecl()) { |
| 4173 | Diag(Loc, diag::err_auto_fn_used_before_defined) << FD; |
| 4174 | Diag(FD->getLocation(), diag::note_callee_decl) << FD; |
| 4175 | } |
| 4176 | |
| 4177 | return StillUndeduced; |
| 4178 | } |
| 4179 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4180 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4181 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4182 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4183 | unsigned Level, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4184 | llvm::SmallBitVector &Deduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4185 | |
| 4186 | /// \brief If this is a non-static member function, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 4187 | static void |
| 4188 | AddImplicitObjectParameterType(ASTContext &Context, |
| 4189 | CXXMethodDecl *Method, |
| 4190 | SmallVectorImpl<QualType> &ArgTypes) { |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4191 | // C++11 [temp.func.order]p3: |
| 4192 | // [...] The new parameter is of type "reference to cv A," where cv are |
| 4193 | // the cv-qualifiers of the function template (if any) and A is |
| 4194 | // the class of which the function template is a member. |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4195 | // |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4196 | // The standard doesn't say explicitly, but we pick the appropriate kind of |
| 4197 | // reference type based on [over.match.funcs]p4. |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4198 | QualType ArgTy = Context.getTypeDeclType(Method->getParent()); |
| 4199 | ArgTy = Context.getQualifiedType(ArgTy, |
| 4200 | Qualifiers::fromCVRMask(Method->getTypeQualifiers())); |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4201 | if (Method->getRefQualifier() == RQ_RValue) |
| 4202 | ArgTy = Context.getRValueReferenceType(ArgTy); |
| 4203 | else |
| 4204 | ArgTy = Context.getLValueReferenceType(ArgTy); |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4205 | ArgTypes.push_back(ArgTy); |
| 4206 | } |
| 4207 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4208 | /// \brief Determine whether the function template \p FT1 is at least as |
| 4209 | /// specialized as \p FT2. |
| 4210 | static bool isAtLeastAsSpecializedAs(Sema &S, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4211 | SourceLocation Loc, |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4212 | FunctionTemplateDecl *FT1, |
| 4213 | FunctionTemplateDecl *FT2, |
| 4214 | TemplatePartialOrderingContext TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4215 | unsigned NumCallArguments1) { |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4216 | FunctionDecl *FD1 = FT1->getTemplatedDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4217 | FunctionDecl *FD2 = FT2->getTemplatedDecl(); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4218 | const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); |
| 4219 | const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4220 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4221 | assert(Proto1 && Proto2 && "Function templates must have prototypes"); |
| 4222 | TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4223 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4224 | Deduced.resize(TemplateParams->size()); |
| 4225 | |
| 4226 | // C++0x [temp.deduct.partial]p3: |
| 4227 | // The types used to determine the ordering depend on the context in which |
| 4228 | // the partial ordering is done: |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4229 | TemplateDeductionInfo Info(Loc); |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4230 | SmallVector<QualType, 4> Args2; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4231 | switch (TPOC) { |
| 4232 | case TPOC_Call: { |
| 4233 | // - In the context of a function call, the function parameter types are |
| 4234 | // used. |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4235 | CXXMethodDecl *Method1 = dyn_cast<CXXMethodDecl>(FD1); |
| 4236 | CXXMethodDecl *Method2 = dyn_cast<CXXMethodDecl>(FD2); |
Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4237 | |
Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4238 | // C++11 [temp.func.order]p3: |
Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4239 | // [...] If only one of the function templates is a non-static |
| 4240 | // member, that function template is considered to have a new |
| 4241 | // first parameter inserted in its function parameter list. The |
| 4242 | // new parameter is of type "reference to cv A," where cv are |
| 4243 | // the cv-qualifiers of the function template (if any) and A is |
| 4244 | // the class of which the function template is a member. |
| 4245 | // |
Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4246 | // Note that we interpret this to mean "if one of the function |
| 4247 | // templates is a non-static member and the other is a non-member"; |
| 4248 | // otherwise, the ordering rules for static functions against non-static |
| 4249 | // functions don't make any sense. |
| 4250 | // |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4251 | // C++98/03 doesn't have this provision but we've extended DR532 to cover |
| 4252 | // it as wording was broken prior to it. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4253 | SmallVector<QualType, 4> Args1; |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4254 | |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4255 | unsigned NumComparedArguments = NumCallArguments1; |
| 4256 | |
| 4257 | if (!Method2 && Method1 && !Method1->isStatic()) { |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4258 | // Compare 'this' from Method1 against first parameter from Method2. |
| 4259 | AddImplicitObjectParameterType(S.Context, Method1, Args1); |
| 4260 | ++NumComparedArguments; |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4261 | } else if (!Method1 && Method2 && !Method2->isStatic()) { |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4262 | // Compare 'this' from Method2 against first parameter from Method1. |
| 4263 | AddImplicitObjectParameterType(S.Context, Method2, Args2); |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4264 | } |
| 4265 | |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4266 | Args1.insert(Args1.end(), Proto1->param_type_begin(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4267 | Proto1->param_type_end()); |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4268 | Args2.insert(Args2.end(), Proto2->param_type_begin(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4269 | Proto2->param_type_end()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4270 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4271 | // C++ [temp.func.order]p5: |
| 4272 | // The presence of unused ellipsis and default arguments has no effect on |
| 4273 | // the partial ordering of function templates. |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4274 | if (Args1.size() > NumComparedArguments) |
| 4275 | Args1.resize(NumComparedArguments); |
| 4276 | if (Args2.size() > NumComparedArguments) |
| 4277 | Args2.resize(NumComparedArguments); |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4278 | if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(), |
| 4279 | Args1.data(), Args1.size(), Info, Deduced, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4280 | TDF_None, /*PartialOrdering=*/true)) |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 4281 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4282 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4283 | break; |
| 4284 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4285 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4286 | case TPOC_Conversion: |
| 4287 | // - In the context of a call to a conversion operator, the return types |
| 4288 | // of the conversion function templates are used. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4289 | if (DeduceTemplateArgumentsByTypeMatch( |
| 4290 | S, TemplateParams, Proto2->getReturnType(), Proto1->getReturnType(), |
| 4291 | Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4292 | /*PartialOrdering=*/true)) |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4293 | return false; |
| 4294 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4295 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4296 | case TPOC_Other: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4297 | // - 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] | 4298 | // is used. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4299 | if (DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 4300 | FD2->getType(), FD1->getType(), |
| 4301 | Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4302 | /*PartialOrdering=*/true)) |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4303 | return false; |
| 4304 | break; |
| 4305 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4306 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4307 | // C++0x [temp.deduct.partial]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4308 | // In most cases, all template parameters must have values in order for |
| 4309 | // deduction to succeed, but for partial ordering purposes a template |
| 4310 | // 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] | 4311 | // types being used for partial ordering. [ Note: a template parameter used |
| 4312 | // in a non-deduced context is considered used. -end note] |
| 4313 | unsigned ArgIdx = 0, NumArgs = Deduced.size(); |
| 4314 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4315 | if (Deduced[ArgIdx].isNull()) |
| 4316 | break; |
| 4317 | |
| 4318 | if (ArgIdx == NumArgs) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4319 | // All template arguments were deduced. FT1 is at least as specialized |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4320 | // as FT2. |
| 4321 | return true; |
| 4322 | } |
| 4323 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4324 | // Figure out which template parameters were used. |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4325 | llvm::SmallBitVector UsedParameters(TemplateParams->size()); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4326 | switch (TPOC) { |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4327 | case TPOC_Call: |
| 4328 | for (unsigned I = 0, N = Args2.size(); I != N; ++I) |
| 4329 | ::MarkUsedTemplateParameters(S.Context, Args2[I], false, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4330 | TemplateParams->getDepth(), |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4331 | 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_Conversion: |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4335 | ::MarkUsedTemplateParameters(S.Context, Proto2->getReturnType(), false, |
| 4336 | TemplateParams->getDepth(), UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4337 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4338 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4339 | case TPOC_Other: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4340 | ::MarkUsedTemplateParameters(S.Context, FD2->getType(), false, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4341 | TemplateParams->getDepth(), |
| 4342 | UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4343 | break; |
| 4344 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4345 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4346 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4347 | // If this argument had no value deduced but was used in one of the types |
| 4348 | // used for partial ordering, then deduction fails. |
| 4349 | if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) |
| 4350 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4351 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4352 | return true; |
| 4353 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4354 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4355 | /// \brief Determine whether this a function template whose parameter-type-list |
| 4356 | /// ends with a function parameter pack. |
| 4357 | static bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) { |
| 4358 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
| 4359 | unsigned NumParams = Function->getNumParams(); |
| 4360 | if (NumParams == 0) |
| 4361 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4362 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4363 | ParmVarDecl *Last = Function->getParamDecl(NumParams - 1); |
| 4364 | if (!Last->isParameterPack()) |
| 4365 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4366 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4367 | // Make sure that no previous parameter is a parameter pack. |
| 4368 | while (--NumParams > 0) { |
| 4369 | if (Function->getParamDecl(NumParams - 1)->isParameterPack()) |
| 4370 | return false; |
| 4371 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4372 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4373 | return true; |
| 4374 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4375 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4376 | /// \brief Returns the more specialized function template according |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4377 | /// to the rules of function template partial ordering (C++ [temp.func.order]). |
| 4378 | /// |
| 4379 | /// \param FT1 the first function template |
| 4380 | /// |
| 4381 | /// \param FT2 the second function template |
| 4382 | /// |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4383 | /// \param TPOC the context in which we are performing partial ordering of |
| 4384 | /// function templates. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4385 | /// |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4386 | /// \param NumCallArguments1 The number of arguments in the call to FT1, used |
| 4387 | /// only when \c TPOC is \c TPOC_Call. |
| 4388 | /// |
| 4389 | /// \param NumCallArguments2 The number of arguments in the call to FT2, used |
| 4390 | /// only when \c TPOC is \c TPOC_Call. |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4391 | /// |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4392 | /// \returns the more specialized function template. If neither |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4393 | /// template is more specialized, returns NULL. |
| 4394 | FunctionTemplateDecl * |
| 4395 | Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, |
| 4396 | FunctionTemplateDecl *FT2, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4397 | SourceLocation Loc, |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4398 | TemplatePartialOrderingContext TPOC, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4399 | unsigned NumCallArguments1, |
| 4400 | unsigned NumCallArguments2) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4401 | bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4402 | NumCallArguments1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4403 | bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4404 | NumCallArguments2); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4405 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4406 | if (Better1 != Better2) // We have a clear winner |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4407 | return Better1 ? FT1 : FT2; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4408 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4409 | if (!Better1 && !Better2) // Neither is better than the other |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4410 | return nullptr; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4411 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4412 | // FIXME: This mimics what GCC implements, but doesn't match up with the |
| 4413 | // proposed resolution for core issue 692. This area needs to be sorted out, |
| 4414 | // but for now we attempt to maintain compatibility. |
| 4415 | bool Variadic1 = isVariadicFunctionTemplate(FT1); |
| 4416 | bool Variadic2 = isVariadicFunctionTemplate(FT2); |
| 4417 | if (Variadic1 != Variadic2) |
| 4418 | return Variadic1? FT2 : FT1; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4419 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4420 | return nullptr; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4421 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4422 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4423 | /// \brief Determine if the two templates are equivalent. |
| 4424 | static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) { |
| 4425 | if (T1 == T2) |
| 4426 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4427 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4428 | if (!T1 || !T2) |
| 4429 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4430 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4431 | return T1->getCanonicalDecl() == T2->getCanonicalDecl(); |
| 4432 | } |
| 4433 | |
| 4434 | /// \brief Retrieve the most specialized of the given function template |
| 4435 | /// specializations. |
| 4436 | /// |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4437 | /// \param SpecBegin the start iterator of the function template |
| 4438 | /// specializations that we will be comparing. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4439 | /// |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4440 | /// \param SpecEnd the end iterator of the function template |
| 4441 | /// specializations, paired with \p SpecBegin. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4442 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4443 | /// \param Loc the location where the ambiguity or no-specializations |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4444 | /// diagnostic should occur. |
| 4445 | /// |
| 4446 | /// \param NoneDiag partial diagnostic used to diagnose cases where there are |
| 4447 | /// no matching candidates. |
| 4448 | /// |
| 4449 | /// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one |
| 4450 | /// occurs. |
| 4451 | /// |
| 4452 | /// \param CandidateDiag partial diagnostic used for each function template |
| 4453 | /// specialization that is a candidate in the ambiguous ordering. One parameter |
| 4454 | /// in this diagnostic should be unbound, which will correspond to the string |
| 4455 | /// describing the template arguments for the function template specialization. |
| 4456 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4457 | /// \returns the most specialized function template specialization, if |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4458 | /// found. Otherwise, returns SpecEnd. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4459 | UnresolvedSetIterator Sema::getMostSpecialized( |
| 4460 | UnresolvedSetIterator SpecBegin, UnresolvedSetIterator SpecEnd, |
| 4461 | TemplateSpecCandidateSet &FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4462 | SourceLocation Loc, const PartialDiagnostic &NoneDiag, |
| 4463 | const PartialDiagnostic &AmbigDiag, const PartialDiagnostic &CandidateDiag, |
| 4464 | bool Complain, QualType TargetType) { |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4465 | if (SpecBegin == SpecEnd) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4466 | if (Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4467 | Diag(Loc, NoneDiag); |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4468 | FailedCandidates.NoteCandidates(*this, Loc); |
| 4469 | } |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4470 | return SpecEnd; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4471 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4472 | |
| 4473 | if (SpecBegin + 1 == SpecEnd) |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4474 | return SpecBegin; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4475 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4476 | // Find the function template that is better than all of the templates it |
| 4477 | // has been compared to. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4478 | UnresolvedSetIterator Best = SpecBegin; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4479 | FunctionTemplateDecl *BestTemplate |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4480 | = cast<FunctionDecl>(*Best)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4481 | assert(BestTemplate && "Not a function template specialization?"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4482 | for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) { |
| 4483 | FunctionTemplateDecl *Challenger |
| 4484 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4485 | assert(Challenger && "Not a function template specialization?"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4486 | if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4487 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4488 | Challenger)) { |
| 4489 | Best = I; |
| 4490 | BestTemplate = Challenger; |
| 4491 | } |
| 4492 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4493 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4494 | // Make sure that the "best" function template is more specialized than all |
| 4495 | // of the others. |
| 4496 | bool Ambiguous = false; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4497 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4498 | FunctionTemplateDecl *Challenger |
| 4499 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4500 | if (I != Best && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4501 | !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4502 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4503 | BestTemplate)) { |
| 4504 | Ambiguous = true; |
| 4505 | break; |
| 4506 | } |
| 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 | if (!Ambiguous) { |
| 4510 | // We found an answer. Return it. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4511 | return Best; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4512 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4513 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4514 | // Diagnose the ambiguity. |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4515 | if (Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4516 | Diag(Loc, AmbigDiag); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4517 | |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4518 | // FIXME: Can we order the candidates in some sane way? |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4519 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4520 | PartialDiagnostic PD = CandidateDiag; |
Saleem Abdulrasool | 78704fb | 2016-12-22 04:26:57 +0000 | [diff] [blame] | 4521 | const auto *FD = cast<FunctionDecl>(*I); |
| 4522 | PD << FD << getTemplateArgumentBindingsText( |
| 4523 | FD->getPrimaryTemplate()->getTemplateParameters(), |
| 4524 | *FD->getTemplateSpecializationArgs()); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4525 | if (!TargetType.isNull()) |
Saleem Abdulrasool | 78704fb | 2016-12-22 04:26:57 +0000 | [diff] [blame] | 4526 | HandleFunctionTypeMismatch(PD, FD->getType(), TargetType); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4527 | Diag((*I)->getLocation(), PD); |
| 4528 | } |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4529 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4530 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4531 | return SpecEnd; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4532 | } |
| 4533 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4534 | /// Determine whether one partial specialization, P1, is at least as |
| 4535 | /// specialized than another, P2. |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4536 | /// |
Simon Pilgrim | 6f3e1ea | 2016-12-26 18:11:49 +0000 | [diff] [blame] | 4537 | /// \tparam PartialSpecializationDecl The kind of P2, which must be a |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4538 | /// {Class,Var}TemplatePartialSpecializationDecl. |
| 4539 | /// \param T1 The injected-class-name of P1 (faked for a variable template). |
| 4540 | /// \param T2 The injected-class-name of P2 (faked for a variable template). |
| 4541 | /// \param Loc The location at which the comparison is required. |
| 4542 | template<typename PartialSpecializationDecl> |
| 4543 | static bool isAtLeastAsSpecializedAs(Sema &S, QualType T1, QualType T2, |
| 4544 | PartialSpecializationDecl *P2, |
| 4545 | SourceLocation Loc) { |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4546 | // C++ [temp.class.order]p1: |
| 4547 | // For two class template partial specializations, the first is at least as |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4548 | // specialized as the second if, given the following rewrite to two |
| 4549 | // function templates, the first function template is at least as |
| 4550 | // specialized as the second according to the ordering rules for function |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4551 | // templates (14.6.6.2): |
| 4552 | // - the first function template has the same template parameters as the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4553 | // first partial specialization and has a single function parameter |
| 4554 | // whose type is a class template specialization with the template |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4555 | // arguments of the first partial specialization, and |
| 4556 | // - the second function template has the same template parameters as the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4557 | // second partial specialization and has a single function parameter |
| 4558 | // whose type is a class template specialization with the template |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4559 | // arguments of the second partial specialization. |
| 4560 | // |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 4561 | // Rather than synthesize function templates, we merely perform the |
| 4562 | // equivalent partial ordering by performing deduction directly on |
| 4563 | // the template arguments of the class template partial |
| 4564 | // specializations. This computation is slightly simpler than the |
| 4565 | // general problem of function template partial ordering, because |
| 4566 | // class template partial specializations are more constrained. We |
| 4567 | // know that every template parameter is deducible from the class |
| 4568 | // template partial specialization's template arguments, for |
| 4569 | // example. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4570 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4571 | TemplateDeductionInfo Info(Loc); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4572 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4573 | // Determine whether P1 is at least as specialized as P2. |
| 4574 | Deduced.resize(P2->getTemplateParameters()->size()); |
| 4575 | if (DeduceTemplateArgumentsByTypeMatch(S, P2->getTemplateParameters(), |
| 4576 | T2, T1, Info, Deduced, TDF_None, |
| 4577 | /*PartialOrdering=*/true)) |
| 4578 | return false; |
| 4579 | |
| 4580 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), |
| 4581 | Deduced.end()); |
| 4582 | Sema::InstantiatingTemplate Inst(S, Loc, P2, DeducedArgs, Info); |
| 4583 | auto *TST1 = T1->castAs<TemplateSpecializationType>(); |
| 4584 | if (FinishTemplateArgumentDeduction( |
Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4585 | S, P2, /*PartialOrdering=*/true, |
| 4586 | TemplateArgumentList(TemplateArgumentList::OnStack, |
| 4587 | TST1->template_arguments()), |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4588 | Deduced, Info)) |
| 4589 | return false; |
| 4590 | |
| 4591 | return true; |
| 4592 | } |
| 4593 | |
| 4594 | /// \brief Returns the more specialized class template partial specialization |
| 4595 | /// according to the rules of partial ordering of class template partial |
| 4596 | /// specializations (C++ [temp.class.order]). |
| 4597 | /// |
| 4598 | /// \param PS1 the first class template partial specialization |
| 4599 | /// |
| 4600 | /// \param PS2 the second class template partial specialization |
| 4601 | /// |
| 4602 | /// \returns the more specialized class template partial specialization. If |
| 4603 | /// neither partial specialization is more specialized, returns NULL. |
| 4604 | ClassTemplatePartialSpecializationDecl * |
| 4605 | Sema::getMoreSpecializedPartialSpecialization( |
| 4606 | ClassTemplatePartialSpecializationDecl *PS1, |
| 4607 | ClassTemplatePartialSpecializationDecl *PS2, |
| 4608 | SourceLocation Loc) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4609 | QualType PT1 = PS1->getInjectedSpecializationType(); |
| 4610 | QualType PT2 = PS2->getInjectedSpecializationType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4611 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4612 | bool Better1 = isAtLeastAsSpecializedAs(*this, PT1, PT2, PS2, Loc); |
| 4613 | bool Better2 = isAtLeastAsSpecializedAs(*this, PT2, PT1, PS1, Loc); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4614 | |
| 4615 | if (Better1 == Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4616 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4617 | |
| 4618 | return Better1 ? PS1 : PS2; |
| 4619 | } |
| 4620 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4621 | VarTemplatePartialSpecializationDecl * |
| 4622 | Sema::getMoreSpecializedPartialSpecialization( |
| 4623 | VarTemplatePartialSpecializationDecl *PS1, |
| 4624 | VarTemplatePartialSpecializationDecl *PS2, SourceLocation Loc) { |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4625 | // Pretend the variable template specializations are class template |
| 4626 | // specializations and form a fake injected class name type for comparison. |
Richard Smith | f04fd0b | 2013-12-12 23:14:16 +0000 | [diff] [blame] | 4627 | assert(PS1->getSpecializedTemplate() == PS2->getSpecializedTemplate() && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4628 | "the partial specializations being compared should specialize" |
| 4629 | " the same template."); |
| 4630 | TemplateName Name(PS1->getSpecializedTemplate()); |
| 4631 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4632 | QualType PT1 = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4633 | CanonTemplate, PS1->getTemplateArgs().asArray()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4634 | QualType PT2 = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4635 | CanonTemplate, PS2->getTemplateArgs().asArray()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4636 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4637 | bool Better1 = isAtLeastAsSpecializedAs(*this, PT1, PT2, PS2, Loc); |
| 4638 | bool Better2 = isAtLeastAsSpecializedAs(*this, PT2, PT1, PS1, Loc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4639 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4640 | if (Better1 == Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4641 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4642 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4643 | return Better1 ? PS1 : PS2; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4644 | } |
| 4645 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4646 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4647 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4648 | const TemplateArgument &TemplateArg, |
| 4649 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4650 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4651 | llvm::SmallBitVector &Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4652 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4653 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4654 | /// expression. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4655 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4656 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4657 | const Expr *E, |
| 4658 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4659 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4660 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 4661 | // We can deduce from a pack expansion. |
| 4662 | if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E)) |
| 4663 | E = Expansion->getPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4664 | |
Richard Smith | 3434900 | 2012-07-09 03:07:20 +0000 | [diff] [blame] | 4665 | // Skip through any implicit casts we added while type-checking, and any |
| 4666 | // substitutions performed by template alias expansion. |
| 4667 | while (1) { |
| 4668 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 4669 | E = ICE->getSubExpr(); |
| 4670 | else if (const SubstNonTypeTemplateParmExpr *Subst = |
| 4671 | dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 4672 | E = Subst->getReplacement(); |
| 4673 | else |
| 4674 | break; |
| 4675 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4676 | |
| 4677 | // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4678 | // find other occurrences of template parameters. |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4679 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
Douglas Gregor | 04b1152 | 2010-01-14 18:13:22 +0000 | [diff] [blame] | 4680 | if (!DRE) |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4681 | return; |
| 4682 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4683 | const NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4684 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 4685 | if (!NTTP) |
| 4686 | return; |
| 4687 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4688 | if (NTTP->getDepth() == Depth) |
| 4689 | Used[NTTP->getIndex()] = true; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 4690 | |
| 4691 | // In C++1z mode, additional arguments may be deduced from the type of a |
| 4692 | // non-type argument. |
| 4693 | if (Ctx.getLangOpts().CPlusPlus1z) |
| 4694 | MarkUsedTemplateParameters(Ctx, NTTP->getType(), OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4695 | } |
| 4696 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4697 | /// \brief Mark the template parameters that are used by the given |
| 4698 | /// nested name specifier. |
| 4699 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4700 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4701 | NestedNameSpecifier *NNS, |
| 4702 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4703 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4704 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4705 | if (!NNS) |
| 4706 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4707 | |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4708 | MarkUsedTemplateParameters(Ctx, NNS->getPrefix(), OnlyDeduced, Depth, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4709 | Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4710 | MarkUsedTemplateParameters(Ctx, QualType(NNS->getAsType(), 0), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4711 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4712 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4713 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4714 | /// \brief Mark the template parameters that are used by the given |
| 4715 | /// template name. |
| 4716 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4717 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4718 | TemplateName Name, |
| 4719 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4720 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4721 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4722 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 4723 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4724 | = dyn_cast<TemplateTemplateParmDecl>(Template)) { |
| 4725 | if (TTP->getDepth() == Depth) |
| 4726 | Used[TTP->getIndex()] = true; |
| 4727 | } |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4728 | return; |
| 4729 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4730 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4731 | if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4732 | MarkUsedTemplateParameters(Ctx, QTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4733 | Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4734 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4735 | MarkUsedTemplateParameters(Ctx, DTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4736 | Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4737 | } |
| 4738 | |
| 4739 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4740 | /// type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4741 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4742 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4743 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4744 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4745 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4746 | if (T.isNull()) |
| 4747 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4748 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4749 | // Non-dependent types have nothing deducible |
| 4750 | if (!T->isDependentType()) |
| 4751 | return; |
| 4752 | |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4753 | T = Ctx.getCanonicalType(T); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4754 | switch (T->getTypeClass()) { |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4755 | case Type::Pointer: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4756 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4757 | cast<PointerType>(T)->getPointeeType(), |
| 4758 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4759 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4760 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4761 | break; |
| 4762 | |
| 4763 | case Type::BlockPointer: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4764 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4765 | cast<BlockPointerType>(T)->getPointeeType(), |
| 4766 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4767 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4768 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4769 | break; |
| 4770 | |
| 4771 | case Type::LValueReference: |
| 4772 | case Type::RValueReference: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4773 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4774 | cast<ReferenceType>(T)->getPointeeType(), |
| 4775 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4776 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4777 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4778 | break; |
| 4779 | |
| 4780 | case Type::MemberPointer: { |
| 4781 | const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4782 | MarkUsedTemplateParameters(Ctx, MemPtr->getPointeeType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4783 | Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4784 | MarkUsedTemplateParameters(Ctx, QualType(MemPtr->getClass(), 0), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4785 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4786 | break; |
| 4787 | } |
| 4788 | |
| 4789 | case Type::DependentSizedArray: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4790 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4791 | cast<DependentSizedArrayType>(T)->getSizeExpr(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4792 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4793 | // Fall through to check the element type |
| 4794 | |
| 4795 | case Type::ConstantArray: |
| 4796 | case Type::IncompleteArray: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4797 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4798 | cast<ArrayType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4799 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4800 | break; |
| 4801 | |
| 4802 | case Type::Vector: |
| 4803 | case Type::ExtVector: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4804 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4805 | cast<VectorType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4806 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4807 | break; |
| 4808 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4809 | case Type::DependentSizedExtVector: { |
| 4810 | const DependentSizedExtVectorType *VecType |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4811 | = cast<DependentSizedExtVectorType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4812 | MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4813 | Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4814 | MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4815 | Depth, Used); |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4816 | break; |
| 4817 | } |
| 4818 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4819 | case Type::FunctionProto: { |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4820 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4821 | MarkUsedTemplateParameters(Ctx, Proto->getReturnType(), OnlyDeduced, Depth, |
| 4822 | Used); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4823 | for (unsigned I = 0, N = Proto->getNumParams(); I != N; ++I) |
| 4824 | MarkUsedTemplateParameters(Ctx, Proto->getParamType(I), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4825 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4826 | break; |
| 4827 | } |
| 4828 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4829 | case Type::TemplateTypeParm: { |
| 4830 | const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T); |
| 4831 | if (TTP->getDepth() == Depth) |
| 4832 | Used[TTP->getIndex()] = true; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4833 | break; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4834 | } |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4835 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4836 | case Type::SubstTemplateTypeParmPack: { |
| 4837 | const SubstTemplateTypeParmPackType *Subst |
| 4838 | = cast<SubstTemplateTypeParmPackType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4839 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4840 | QualType(Subst->getReplacedParameter(), 0), |
| 4841 | OnlyDeduced, Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4842 | MarkUsedTemplateParameters(Ctx, Subst->getArgumentPack(), |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4843 | OnlyDeduced, Depth, Used); |
| 4844 | break; |
| 4845 | } |
| 4846 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4847 | case Type::InjectedClassName: |
| 4848 | T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); |
| 4849 | // fall through |
| 4850 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4851 | case Type::TemplateSpecialization: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4852 | const TemplateSpecializationType *Spec |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4853 | = cast<TemplateSpecializationType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4854 | MarkUsedTemplateParameters(Ctx, Spec->getTemplateName(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4855 | Depth, Used); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4856 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4857 | // C++0x [temp.deduct.type]p9: |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 4858 | // If the template argument list of P contains a pack expansion that is |
| 4859 | // not the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4860 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4861 | if (OnlyDeduced && |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 4862 | hasPackExpansionBeforeEnd(Spec->template_arguments())) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4863 | break; |
| 4864 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4865 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4866 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4867 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4868 | break; |
| 4869 | } |
| 4870 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4871 | case Type::Complex: |
| 4872 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4873 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4874 | cast<ComplexType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4875 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4876 | break; |
| 4877 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4878 | case Type::Atomic: |
| 4879 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4880 | MarkUsedTemplateParameters(Ctx, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4881 | cast<AtomicType>(T)->getValueType(), |
| 4882 | OnlyDeduced, Depth, Used); |
| 4883 | break; |
| 4884 | |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4885 | case Type::DependentName: |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4886 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4887 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4888 | cast<DependentNameType>(T)->getQualifier(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4889 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4890 | break; |
| 4891 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4892 | case Type::DependentTemplateSpecialization: { |
Richard Smith | 50d5b97 | 2015-12-30 20:56:05 +0000 | [diff] [blame] | 4893 | // C++14 [temp.deduct.type]p5: |
| 4894 | // The non-deduced contexts are: |
| 4895 | // -- The nested-name-specifier of a type that was specified using a |
| 4896 | // qualified-id |
| 4897 | // |
| 4898 | // C++14 [temp.deduct.type]p6: |
| 4899 | // When a type name is specified in a way that includes a non-deduced |
| 4900 | // context, all of the types that comprise that type name are also |
| 4901 | // non-deduced. |
| 4902 | if (OnlyDeduced) |
| 4903 | break; |
| 4904 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4905 | const DependentTemplateSpecializationType *Spec |
| 4906 | = cast<DependentTemplateSpecializationType>(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4907 | |
Richard Smith | 50d5b97 | 2015-12-30 20:56:05 +0000 | [diff] [blame] | 4908 | MarkUsedTemplateParameters(Ctx, Spec->getQualifier(), |
| 4909 | OnlyDeduced, Depth, Used); |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4910 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4911 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4912 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4913 | Used); |
| 4914 | break; |
| 4915 | } |
| 4916 | |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4917 | case Type::TypeOf: |
| 4918 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4919 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4920 | cast<TypeOfType>(T)->getUnderlyingType(), |
| 4921 | OnlyDeduced, Depth, Used); |
| 4922 | break; |
| 4923 | |
| 4924 | case Type::TypeOfExpr: |
| 4925 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4926 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4927 | cast<TypeOfExprType>(T)->getUnderlyingExpr(), |
| 4928 | OnlyDeduced, Depth, Used); |
| 4929 | break; |
| 4930 | |
| 4931 | case Type::Decltype: |
| 4932 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4933 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4934 | cast<DecltypeType>(T)->getUnderlyingExpr(), |
| 4935 | OnlyDeduced, Depth, Used); |
| 4936 | break; |
| 4937 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4938 | case Type::UnaryTransform: |
| 4939 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4940 | MarkUsedTemplateParameters(Ctx, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 4941 | cast<UnaryTransformType>(T)->getUnderlyingType(), |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4942 | OnlyDeduced, Depth, Used); |
| 4943 | break; |
| 4944 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4945 | case Type::PackExpansion: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4946 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4947 | cast<PackExpansionType>(T)->getPattern(), |
| 4948 | OnlyDeduced, Depth, Used); |
| 4949 | break; |
| 4950 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4951 | case Type::Auto: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4952 | MarkUsedTemplateParameters(Ctx, |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4953 | cast<AutoType>(T)->getDeducedType(), |
| 4954 | OnlyDeduced, Depth, Used); |
| 4955 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4956 | // None of these types have any template parameters in them. |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4957 | case Type::Builtin: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4958 | case Type::VariableArray: |
| 4959 | case Type::FunctionNoProto: |
| 4960 | case Type::Record: |
| 4961 | case Type::Enum: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4962 | case Type::ObjCInterface: |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4963 | case Type::ObjCObject: |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 4964 | case Type::ObjCObjectPointer: |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4965 | case Type::UnresolvedUsing: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 4966 | case Type::Pipe: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4967 | #define TYPE(Class, Base) |
| 4968 | #define ABSTRACT_TYPE(Class, Base) |
| 4969 | #define DEPENDENT_TYPE(Class, Base) |
| 4970 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 4971 | #include "clang/AST/TypeNodes.def" |
| 4972 | break; |
| 4973 | } |
| 4974 | } |
| 4975 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4976 | /// \brief Mark the template parameters that are used by this |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4977 | /// template argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4978 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4979 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4980 | const TemplateArgument &TemplateArg, |
| 4981 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4982 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4983 | llvm::SmallBitVector &Used) { |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4984 | switch (TemplateArg.getKind()) { |
| 4985 | case TemplateArgument::Null: |
| 4986 | case TemplateArgument::Integral: |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4987 | case TemplateArgument::Declaration: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4988 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4989 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4990 | case TemplateArgument::NullPtr: |
| 4991 | MarkUsedTemplateParameters(Ctx, TemplateArg.getNullPtrType(), OnlyDeduced, |
| 4992 | Depth, Used); |
| 4993 | break; |
| 4994 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4995 | case TemplateArgument::Type: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4996 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4997 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4998 | break; |
| 4999 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5000 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 5001 | case TemplateArgument::TemplateExpansion: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5002 | MarkUsedTemplateParameters(Ctx, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5003 | TemplateArg.getAsTemplateOrTemplatePattern(), |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5004 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5005 | break; |
| 5006 | |
| 5007 | case TemplateArgument::Expression: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5008 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsExpr(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5009 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5010 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5011 | |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5012 | case TemplateArgument::Pack: |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 5013 | for (const auto &P : TemplateArg.pack_elements()) |
| 5014 | MarkUsedTemplateParameters(Ctx, P, OnlyDeduced, Depth, Used); |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5015 | break; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5016 | } |
| 5017 | } |
| 5018 | |
James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 5019 | /// \brief Mark which template parameters can be deduced from a given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5020 | /// template argument list. |
| 5021 | /// |
| 5022 | /// \param TemplateArgs the template argument list from which template |
| 5023 | /// parameters will be deduced. |
| 5024 | /// |
James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 5025 | /// \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] | 5026 | /// to indicate when the corresponding template parameter will be |
| 5027 | /// deduced. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5028 | void |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5029 | Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5030 | bool OnlyDeduced, unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5031 | llvm::SmallBitVector &Used) { |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5032 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5033 | // If the template argument list of P contains a pack expansion that is not |
| 5034 | // the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5035 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5036 | if (OnlyDeduced && |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 5037 | hasPackExpansionBeforeEnd(TemplateArgs.asArray())) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5038 | return; |
| 5039 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5040 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5041 | ::MarkUsedTemplateParameters(Context, TemplateArgs[I], OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5042 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5043 | } |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5044 | |
| 5045 | /// \brief Marks all of the template parameters that will be deduced by a |
| 5046 | /// call to the given function template. |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 5047 | void Sema::MarkDeducedTemplateParameters( |
| 5048 | ASTContext &Ctx, const FunctionTemplateDecl *FunctionTemplate, |
| 5049 | llvm::SmallBitVector &Deduced) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5050 | TemplateParameterList *TemplateParams |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5051 | = FunctionTemplate->getTemplateParameters(); |
| 5052 | Deduced.clear(); |
| 5053 | Deduced.resize(TemplateParams->size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5054 | |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5055 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 5056 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5057 | ::MarkUsedTemplateParameters(Ctx, Function->getParamDecl(I)->getType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5058 | true, TemplateParams->getDepth(), Deduced); |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5059 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5060 | |
| 5061 | bool hasDeducibleTemplateParameters(Sema &S, |
| 5062 | FunctionTemplateDecl *FunctionTemplate, |
| 5063 | QualType T) { |
| 5064 | if (!T->isDependentType()) |
| 5065 | return false; |
| 5066 | |
| 5067 | TemplateParameterList *TemplateParams |
| 5068 | = FunctionTemplate->getTemplateParameters(); |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5069 | llvm::SmallBitVector Deduced(TemplateParams->size()); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 5070 | ::MarkUsedTemplateParameters(S.Context, T, true, TemplateParams->getDepth(), |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5071 | Deduced); |
| 5072 | |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5073 | return Deduced.any(); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5074 | } |