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, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 108 | const TemplateArgument *Params, unsigned NumParams, |
| 109 | const TemplateArgument *Args, unsigned NumArgs, |
| 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. |
| 117 | static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) { |
Richard Smith | 7ebb07c | 2012-07-08 04:37:51 +0000 | [diff] [blame] | 118 | // If we are within an alias template, the expression may have undergone |
| 119 | // any number of parameter substitutions already. |
| 120 | while (1) { |
| 121 | if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E)) |
| 122 | E = IC->getSubExpr(); |
| 123 | else if (SubstNonTypeTemplateParmExpr *Subst = |
| 124 | dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 125 | E = Subst->getReplacement(); |
| 126 | else |
| 127 | break; |
| 128 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 130 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 131 | return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 133 | return nullptr; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 136 | /// \brief Determine whether two declaration pointers refer to the same |
| 137 | /// declaration. |
| 138 | static bool isSameDeclaration(Decl *X, Decl *Y) { |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 139 | if (NamedDecl *NX = dyn_cast<NamedDecl>(X)) |
| 140 | X = NX->getUnderlyingDecl(); |
| 141 | if (NamedDecl *NY = dyn_cast<NamedDecl>(Y)) |
| 142 | Y = NY->getUnderlyingDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 143 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 144 | return X->getCanonicalDecl() == Y->getCanonicalDecl(); |
| 145 | } |
| 146 | |
| 147 | /// \brief Verify that the given, deduced template arguments are compatible. |
| 148 | /// |
| 149 | /// \returns The deduced template argument, or a NULL template argument if |
| 150 | /// the deduced template arguments were incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 151 | static DeducedTemplateArgument |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 152 | checkDeducedTemplateArguments(ASTContext &Context, |
| 153 | const DeducedTemplateArgument &X, |
| 154 | const DeducedTemplateArgument &Y) { |
| 155 | // We have no deduction for one or both of the arguments; they're compatible. |
| 156 | if (X.isNull()) |
| 157 | return Y; |
| 158 | if (Y.isNull()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 159 | return X; |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 160 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 161 | // If we have two non-type template argument values deduced for the same |
| 162 | // parameter, they must both match the type of the parameter, and thus must |
| 163 | // match each other's type. As we're only keeping one of them, we must check |
| 164 | // for that now. The exception is that if either was deduced from an array |
| 165 | // bound, the type is permitted to differ. |
| 166 | if (!X.wasDeducedFromArrayBound() && !Y.wasDeducedFromArrayBound()) { |
| 167 | QualType XType = X.getNonTypeTemplateArgumentType(); |
| 168 | if (!XType.isNull()) { |
| 169 | QualType YType = Y.getNonTypeTemplateArgumentType(); |
| 170 | if (YType.isNull() || !Context.hasSameType(XType, YType)) |
| 171 | return DeducedTemplateArgument(); |
| 172 | } |
| 173 | } |
| 174 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 175 | switch (X.getKind()) { |
| 176 | case TemplateArgument::Null: |
| 177 | llvm_unreachable("Non-deduced template arguments handled above"); |
| 178 | |
| 179 | case TemplateArgument::Type: |
| 180 | // If two template type arguments have the same type, they're compatible. |
| 181 | if (Y.getKind() == TemplateArgument::Type && |
| 182 | Context.hasSameType(X.getAsType(), Y.getAsType())) |
| 183 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 184 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 185 | // If one of the two arguments was deduced from an array bound, the other |
| 186 | // supersedes it. |
| 187 | if (X.wasDeducedFromArrayBound() != Y.wasDeducedFromArrayBound()) |
| 188 | return X.wasDeducedFromArrayBound() ? Y : X; |
| 189 | |
| 190 | // The arguments are not compatible. |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 191 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 192 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 193 | case TemplateArgument::Integral: |
| 194 | // If we deduced a constant in one case and either a dependent expression or |
| 195 | // declaration in another case, keep the integral constant. |
| 196 | // If both are integral constants with the same value, keep that value. |
| 197 | if (Y.getKind() == TemplateArgument::Expression || |
| 198 | Y.getKind() == TemplateArgument::Declaration || |
| 199 | (Y.getKind() == TemplateArgument::Integral && |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 200 | hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()))) |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 201 | return X.wasDeducedFromArrayBound() ? Y : X; |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 202 | |
| 203 | // All other combinations are incompatible. |
| 204 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 205 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 206 | case TemplateArgument::Template: |
| 207 | if (Y.getKind() == TemplateArgument::Template && |
| 208 | Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate())) |
| 209 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 210 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 211 | // All other combinations are incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 212 | return DeducedTemplateArgument(); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 213 | |
| 214 | case TemplateArgument::TemplateExpansion: |
| 215 | if (Y.getKind() == TemplateArgument::TemplateExpansion && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 216 | Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(), |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 217 | Y.getAsTemplateOrTemplatePattern())) |
| 218 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 219 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 220 | // All other combinations are incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 221 | return DeducedTemplateArgument(); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 222 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 223 | case TemplateArgument::Expression: { |
| 224 | if (Y.getKind() != TemplateArgument::Expression) |
| 225 | return checkDeducedTemplateArguments(Context, Y, X); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 226 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 227 | // Compare the expressions for equality |
| 228 | llvm::FoldingSetNodeID ID1, ID2; |
| 229 | X.getAsExpr()->Profile(ID1, Context, true); |
| 230 | Y.getAsExpr()->Profile(ID2, Context, true); |
| 231 | if (ID1 == ID2) |
| 232 | return X.wasDeducedFromArrayBound() ? Y : X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 233 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 234 | // Differing dependent expressions are incompatible. |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 235 | return DeducedTemplateArgument(); |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 236 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 237 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 238 | case TemplateArgument::Declaration: |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 239 | assert(!X.wasDeducedFromArrayBound()); |
| 240 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 241 | // If we deduced a declaration and a dependent expression, keep the |
| 242 | // declaration. |
| 243 | if (Y.getKind() == TemplateArgument::Expression) |
| 244 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 245 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 246 | // If we deduced a declaration and an integral constant, keep the |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 247 | // integral constant and whichever type did not come from an array |
| 248 | // bound. |
| 249 | if (Y.getKind() == TemplateArgument::Integral) { |
| 250 | if (Y.wasDeducedFromArrayBound()) |
| 251 | return TemplateArgument(Context, Y.getAsIntegral(), |
| 252 | X.getParamTypeForDecl()); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 253 | return Y; |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 254 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 255 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 256 | // If we deduced two declarations, make sure they they refer to the |
| 257 | // same declaration. |
| 258 | if (Y.getKind() == TemplateArgument::Declaration && |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 259 | isSameDeclaration(X.getAsDecl(), Y.getAsDecl())) |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 260 | return X; |
| 261 | |
| 262 | // All other combinations are incompatible. |
| 263 | return DeducedTemplateArgument(); |
| 264 | |
| 265 | case TemplateArgument::NullPtr: |
| 266 | // If we deduced a null pointer and a dependent expression, keep the |
| 267 | // null pointer. |
| 268 | if (Y.getKind() == TemplateArgument::Expression) |
| 269 | return X; |
| 270 | |
| 271 | // If we deduced a null pointer and an integral constant, keep the |
| 272 | // integral constant. |
| 273 | if (Y.getKind() == TemplateArgument::Integral) |
| 274 | return Y; |
| 275 | |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 276 | // If we deduced two null pointers, they are the same. |
| 277 | if (Y.getKind() == TemplateArgument::NullPtr) |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 278 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 279 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 280 | // All other combinations are incompatible. |
| 281 | return DeducedTemplateArgument(); |
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 | case TemplateArgument::Pack: |
| 284 | if (Y.getKind() != TemplateArgument::Pack || |
| 285 | X.pack_size() != Y.pack_size()) |
| 286 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 287 | |
| 288 | for (TemplateArgument::pack_iterator XA = X.pack_begin(), |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 289 | XAEnd = X.pack_end(), |
| 290 | YA = Y.pack_begin(); |
| 291 | XA != XAEnd; ++XA, ++YA) { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 292 | // FIXME: Do we need to merge the results together here? |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 293 | if (checkDeducedTemplateArguments(Context, |
| 294 | DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()), |
Douglas Gregor | f491ee2 | 2011-01-05 21:00:53 +0000 | [diff] [blame] | 295 | DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound())) |
| 296 | .isNull()) |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 297 | return DeducedTemplateArgument(); |
| 298 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 299 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 300 | return X; |
| 301 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 302 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 303 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | /// \brief Deduce the value of the given non-type template parameter |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 307 | /// from the given integral constant. |
Benjamin Kramer | 7320b99 | 2016-06-15 14:20:56 +0000 | [diff] [blame] | 308 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 309 | Sema &S, TemplateParameterList *TemplateParams, |
| 310 | NonTypeTemplateParmDecl *NTTP, const llvm::APSInt &Value, |
Benjamin Kramer | 7320b99 | 2016-06-15 14:20:56 +0000 | [diff] [blame] | 311 | QualType ValueType, bool DeducedFromArrayBound, TemplateDeductionInfo &Info, |
| 312 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 313 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 314 | "Cannot deduce non-type template argument with depth > 0"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 316 | DeducedTemplateArgument NewDeduced(S.Context, Value, ValueType, |
| 317 | DeducedFromArrayBound); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 318 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 319 | Deduced[NTTP->getIndex()], |
| 320 | NewDeduced); |
| 321 | if (Result.isNull()) { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 322 | Info.Param = NTTP; |
| 323 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 324 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 325 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 326 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 327 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 328 | Deduced[NTTP->getIndex()] = Result; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 329 | return S.getLangOpts().CPlusPlus1z |
| 330 | ? DeduceTemplateArgumentsByTypeMatch( |
| 331 | S, TemplateParams, NTTP->getType(), ValueType, Info, Deduced, |
| 332 | TDF_ParamWithReferenceType | TDF_SkipNonDependent, |
| 333 | /*PartialOrdering=*/false, |
| 334 | /*ArrayBound=*/DeducedFromArrayBound) |
| 335 | : Sema::TDK_Success; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 338 | /// \brief Deduce the value of the given non-type template parameter |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 339 | /// from the given null pointer template argument type. |
| 340 | static Sema::TemplateDeductionResult DeduceNullPtrTemplateArgument( |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 341 | Sema &S, TemplateParameterList *TemplateParams, |
| 342 | NonTypeTemplateParmDecl *NTTP, QualType NullPtrType, |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 343 | TemplateDeductionInfo &Info, |
| 344 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 345 | Expr *Value = |
| 346 | S.ImpCastExprToType(new (S.Context) CXXNullPtrLiteralExpr( |
| 347 | S.Context.NullPtrTy, NTTP->getLocation()), |
| 348 | NullPtrType, CK_NullToPointer) |
| 349 | .get(); |
| 350 | DeducedTemplateArgument NewDeduced(Value); |
| 351 | DeducedTemplateArgument Result = checkDeducedTemplateArguments( |
| 352 | S.Context, Deduced[NTTP->getIndex()], NewDeduced); |
| 353 | |
| 354 | if (Result.isNull()) { |
| 355 | Info.Param = NTTP; |
| 356 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
| 357 | Info.SecondArg = NewDeduced; |
| 358 | return Sema::TDK_Inconsistent; |
| 359 | } |
| 360 | |
| 361 | Deduced[NTTP->getIndex()] = Result; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 362 | return S.getLangOpts().CPlusPlus1z |
| 363 | ? DeduceTemplateArgumentsByTypeMatch( |
| 364 | S, TemplateParams, NTTP->getType(), Value->getType(), Info, |
| 365 | Deduced, TDF_ParamWithReferenceType | TDF_SkipNonDependent) |
| 366 | : Sema::TDK_Success; |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | /// \brief Deduce the value of the given non-type template parameter |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 370 | /// from the given type- or value-dependent expression. |
| 371 | /// |
| 372 | /// \returns true if deduction succeeded, false otherwise. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 373 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 374 | DeduceNonTypeTemplateArgument(Sema &S, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 375 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 376 | NonTypeTemplateParmDecl *NTTP, |
| 377 | Expr *Value, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 378 | TemplateDeductionInfo &Info, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 379 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 380 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 381 | "Cannot deduce non-type template argument with depth > 0"); |
| 382 | assert((Value->isTypeDependent() || Value->isValueDependent()) && |
| 383 | "Expression template argument must be type- or value-dependent."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 384 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 385 | DeducedTemplateArgument NewDeduced(Value); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 386 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
| 387 | Deduced[NTTP->getIndex()], |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 388 | NewDeduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 389 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 390 | if (Result.isNull()) { |
| 391 | Info.Param = NTTP; |
| 392 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
| 393 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 394 | return Sema::TDK_Inconsistent; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 395 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 396 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 397 | Deduced[NTTP->getIndex()] = Result; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 398 | return S.getLangOpts().CPlusPlus1z |
| 399 | ? DeduceTemplateArgumentsByTypeMatch( |
| 400 | S, TemplateParams, NTTP->getType(), Value->getType(), Info, |
| 401 | Deduced, TDF_ParamWithReferenceType | TDF_SkipNonDependent) |
| 402 | : Sema::TDK_Success; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 405 | /// \brief Deduce the value of the given non-type template parameter |
| 406 | /// from the given declaration. |
| 407 | /// |
| 408 | /// \returns true if deduction succeeded, false otherwise. |
| 409 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 410 | DeduceNonTypeTemplateArgument(Sema &S, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 411 | TemplateParameterList *TemplateParams, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 412 | NonTypeTemplateParmDecl *NTTP, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 413 | ValueDecl *D, QualType T, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 414 | TemplateDeductionInfo &Info, |
| 415 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 416 | assert(NTTP->getDepth() == 0 && |
| 417 | "Cannot deduce non-type template argument with depth > 0"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 418 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 419 | D = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 420 | TemplateArgument New(D, T); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 421 | DeducedTemplateArgument NewDeduced(New); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 422 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 423 | Deduced[NTTP->getIndex()], |
| 424 | NewDeduced); |
| 425 | if (Result.isNull()) { |
| 426 | Info.Param = NTTP; |
| 427 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
| 428 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 429 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 430 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 431 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 432 | Deduced[NTTP->getIndex()] = Result; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 433 | return S.getLangOpts().CPlusPlus1z |
| 434 | ? DeduceTemplateArgumentsByTypeMatch( |
| 435 | S, TemplateParams, NTTP->getType(), T, Info, Deduced, |
| 436 | TDF_ParamWithReferenceType | TDF_SkipNonDependent) |
| 437 | : Sema::TDK_Success; |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 440 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 441 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 442 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 443 | TemplateName Param, |
| 444 | TemplateName Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 445 | TemplateDeductionInfo &Info, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 446 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 447 | TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 448 | if (!ParamDecl) { |
| 449 | // The parameter type is dependent and is not a template template parameter, |
| 450 | // so there is nothing that we can deduce. |
| 451 | return Sema::TDK_Success; |
| 452 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 453 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 454 | if (TemplateTemplateParmDecl *TempParam |
| 455 | = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) { |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 456 | DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 457 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 458 | Deduced[TempParam->getIndex()], |
| 459 | NewDeduced); |
| 460 | if (Result.isNull()) { |
| 461 | Info.Param = TempParam; |
| 462 | Info.FirstArg = Deduced[TempParam->getIndex()]; |
| 463 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 464 | return Sema::TDK_Inconsistent; |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 465 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 466 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 467 | Deduced[TempParam->getIndex()] = Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 468 | return Sema::TDK_Success; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 469 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 470 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 471 | // Verify that the two template names are equivalent. |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 472 | if (S.Context.hasSameTemplateName(Param, Arg)) |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 473 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 474 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 475 | // Mismatch of non-dependent template parameter to argument. |
| 476 | Info.FirstArg = TemplateArgument(Param); |
| 477 | Info.SecondArg = TemplateArgument(Arg); |
| 478 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 481 | /// \brief Deduce the template arguments by comparing the template parameter |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 482 | /// type (which is a template-id) with the template argument type. |
| 483 | /// |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 484 | /// \param S the Sema |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 485 | /// |
| 486 | /// \param TemplateParams the template parameters that we are deducing |
| 487 | /// |
| 488 | /// \param Param the parameter type |
| 489 | /// |
| 490 | /// \param Arg the argument type |
| 491 | /// |
| 492 | /// \param Info information about the template argument deduction itself |
| 493 | /// |
| 494 | /// \param Deduced the deduced template arguments |
| 495 | /// |
| 496 | /// \returns the result of template argument deduction so far. Note that a |
| 497 | /// "success" result means that template argument deduction has not yet failed, |
| 498 | /// but it may still fail, later, for other reasons. |
| 499 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 500 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 501 | TemplateParameterList *TemplateParams, |
| 502 | const TemplateSpecializationType *Param, |
| 503 | QualType Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 504 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 505 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 506 | assert(Arg.isCanonical() && "Argument type must be canonical"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 507 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 508 | // Check whether the template argument is a dependent template-id. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 509 | if (const TemplateSpecializationType *SpecArg |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 510 | = dyn_cast<TemplateSpecializationType>(Arg)) { |
| 511 | // Perform template argument deduction for the template name. |
| 512 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 513 | = DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 514 | Param->getTemplateName(), |
| 515 | SpecArg->getTemplateName(), |
| 516 | Info, Deduced)) |
| 517 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 518 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 519 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 520 | // Perform template argument deduction on each template |
Douglas Gregor | d80ea20 | 2010-12-22 18:55:49 +0000 | [diff] [blame] | 521 | // argument. Ignore any missing/extra arguments, since they could be |
| 522 | // filled in by default arguments. |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 523 | return DeduceTemplateArguments(S, TemplateParams, Param->getArgs(), |
| 524 | Param->getNumArgs(), SpecArg->getArgs(), |
| 525 | SpecArg->getNumArgs(), Info, Deduced, |
| 526 | /*NumberOfArgumentsMustMatch=*/false); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 527 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 528 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 529 | // If the argument type is a class template specialization, we |
| 530 | // perform template argument deduction using its template |
| 531 | // arguments. |
| 532 | const RecordType *RecordArg = dyn_cast<RecordType>(Arg); |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 533 | if (!RecordArg) { |
| 534 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 535 | Info.SecondArg = TemplateArgument(Arg); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 536 | return Sema::TDK_NonDeducedMismatch; |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 537 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 538 | |
| 539 | ClassTemplateSpecializationDecl *SpecArg |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 540 | = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 541 | if (!SpecArg) { |
| 542 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 543 | Info.SecondArg = TemplateArgument(Arg); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 544 | return Sema::TDK_NonDeducedMismatch; |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 545 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 546 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 547 | // Perform template argument deduction for the template name. |
| 548 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 549 | = DeduceTemplateArguments(S, |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 550 | TemplateParams, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 551 | Param->getTemplateName(), |
| 552 | TemplateName(SpecArg->getSpecializedTemplate()), |
| 553 | Info, Deduced)) |
| 554 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 555 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 556 | // Perform template argument deduction for the template arguments. |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 557 | return DeduceTemplateArguments( |
| 558 | S, TemplateParams, Param->getArgs(), Param->getNumArgs(), |
| 559 | SpecArg->getTemplateArgs().data(), SpecArg->getTemplateArgs().size(), |
| 560 | Info, Deduced, /*NumberOfArgumentsMustMatch=*/true); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 561 | } |
| 562 | |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 563 | /// \brief Determines whether the given type is an opaque type that |
| 564 | /// might be more qualified when instantiated. |
| 565 | static bool IsPossiblyOpaquelyQualifiedType(QualType T) { |
| 566 | switch (T->getTypeClass()) { |
| 567 | case Type::TypeOfExpr: |
| 568 | case Type::TypeOf: |
| 569 | case Type::DependentName: |
| 570 | case Type::Decltype: |
| 571 | case Type::UnresolvedUsing: |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 572 | case Type::TemplateTypeParm: |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 573 | return true; |
| 574 | |
| 575 | case Type::ConstantArray: |
| 576 | case Type::IncompleteArray: |
| 577 | case Type::VariableArray: |
| 578 | case Type::DependentSizedArray: |
| 579 | return IsPossiblyOpaquelyQualifiedType( |
| 580 | cast<ArrayType>(T)->getElementType()); |
| 581 | |
| 582 | default: |
| 583 | return false; |
| 584 | } |
| 585 | } |
| 586 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 587 | /// \brief Retrieve the depth and index of a template parameter. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 588 | static std::pair<unsigned, unsigned> |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 589 | getDepthAndIndex(NamedDecl *ND) { |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 590 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND)) |
| 591 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 592 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 593 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND)) |
| 594 | return std::make_pair(NTTP->getDepth(), NTTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 595 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 596 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND); |
| 597 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| 598 | } |
| 599 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 600 | /// \brief Retrieve the depth and index of an unexpanded parameter pack. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 601 | static std::pair<unsigned, unsigned> |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 602 | getDepthAndIndex(UnexpandedParameterPack UPP) { |
| 603 | if (const TemplateTypeParmType *TTP |
| 604 | = UPP.first.dyn_cast<const TemplateTypeParmType *>()) |
| 605 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 606 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 607 | return getDepthAndIndex(UPP.first.get<NamedDecl *>()); |
| 608 | } |
| 609 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 610 | /// \brief Helper function to build a TemplateParameter when we don't |
| 611 | /// know its type statically. |
| 612 | static TemplateParameter makeTemplateParameter(Decl *D) { |
| 613 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) |
| 614 | return TemplateParameter(TTP); |
Craig Topper | 4b482ee | 2013-07-08 04:24:47 +0000 | [diff] [blame] | 615 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 616 | return TemplateParameter(NTTP); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 617 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 618 | return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); |
| 619 | } |
| 620 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 621 | /// A pack that we're currently deducing. |
| 622 | struct clang::DeducedPack { |
| 623 | DeducedPack(unsigned Index) : Index(Index), Outer(nullptr) {} |
Craig Topper | 0a4e1f5 | 2013-07-08 04:44:01 +0000 | [diff] [blame] | 624 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 625 | // The index of the pack. |
| 626 | unsigned Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 627 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 628 | // The old value of the pack before we started deducing it. |
| 629 | DeducedTemplateArgument Saved; |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 630 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 631 | // A deferred value of this pack from an inner deduction, that couldn't be |
| 632 | // deduced because this deduction hadn't happened yet. |
| 633 | DeducedTemplateArgument DeferredDeduction; |
| 634 | |
| 635 | // The new value of the pack. |
| 636 | SmallVector<DeducedTemplateArgument, 4> New; |
| 637 | |
| 638 | // The outer deduction for this pack, if any. |
| 639 | DeducedPack *Outer; |
| 640 | }; |
| 641 | |
Benjamin Kramer | d5748c7 | 2015-03-23 12:31:05 +0000 | [diff] [blame] | 642 | namespace { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 643 | /// A scope in which we're performing pack deduction. |
| 644 | class PackDeductionScope { |
| 645 | public: |
| 646 | PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, |
| 647 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 648 | TemplateDeductionInfo &Info, TemplateArgument Pattern) |
| 649 | : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { |
| 650 | // Compute the set of template parameter indices that correspond to |
| 651 | // parameter packs expanded by the pack expansion. |
| 652 | { |
| 653 | llvm::SmallBitVector SawIndices(TemplateParams->size()); |
| 654 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 655 | S.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
| 656 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 657 | unsigned Depth, Index; |
| 658 | std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); |
| 659 | if (Depth == 0 && !SawIndices[Index]) { |
| 660 | SawIndices[Index] = true; |
| 661 | |
| 662 | // Save the deduced template argument for the parameter pack expanded |
| 663 | // by this pack expansion, then clear out the deduction. |
| 664 | DeducedPack Pack(Index); |
| 665 | Pack.Saved = Deduced[Index]; |
| 666 | Deduced[Index] = TemplateArgument(); |
| 667 | |
| 668 | Packs.push_back(Pack); |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | assert(!Packs.empty() && "Pack expansion without unexpanded packs?"); |
| 673 | |
| 674 | for (auto &Pack : Packs) { |
| 675 | if (Info.PendingDeducedPacks.size() > Pack.Index) |
| 676 | Pack.Outer = Info.PendingDeducedPacks[Pack.Index]; |
| 677 | else |
| 678 | Info.PendingDeducedPacks.resize(Pack.Index + 1); |
| 679 | Info.PendingDeducedPacks[Pack.Index] = &Pack; |
| 680 | |
| 681 | if (S.CurrentInstantiationScope) { |
| 682 | // If the template argument pack was explicitly specified, add that to |
| 683 | // the set of deduced arguments. |
| 684 | const TemplateArgument *ExplicitArgs; |
| 685 | unsigned NumExplicitArgs; |
| 686 | NamedDecl *PartiallySubstitutedPack = |
| 687 | S.CurrentInstantiationScope->getPartiallySubstitutedPack( |
| 688 | &ExplicitArgs, &NumExplicitArgs); |
| 689 | if (PartiallySubstitutedPack && |
| 690 | getDepthAndIndex(PartiallySubstitutedPack).second == Pack.Index) |
| 691 | Pack.New.append(ExplicitArgs, ExplicitArgs + NumExplicitArgs); |
| 692 | } |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 693 | } |
| 694 | } |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 695 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 696 | ~PackDeductionScope() { |
| 697 | for (auto &Pack : Packs) |
| 698 | Info.PendingDeducedPacks[Pack.Index] = Pack.Outer; |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 699 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 700 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 701 | /// Move to deducing the next element in each pack that is being deduced. |
| 702 | void nextPackElement() { |
| 703 | // Capture the deduced template arguments for each parameter pack expanded |
| 704 | // by this pack expansion, add them to the list of arguments we've deduced |
| 705 | // for that pack, then clear out the deduced argument. |
| 706 | for (auto &Pack : Packs) { |
| 707 | DeducedTemplateArgument &DeducedArg = Deduced[Pack.Index]; |
| 708 | if (!DeducedArg.isNull()) { |
| 709 | Pack.New.push_back(DeducedArg); |
| 710 | DeducedArg = DeducedTemplateArgument(); |
| 711 | } |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | /// \brief Finish template argument deduction for a set of argument packs, |
| 716 | /// producing the argument packs and checking for consistency with prior |
| 717 | /// deductions. |
| 718 | Sema::TemplateDeductionResult finish(bool HasAnyArguments) { |
| 719 | // Build argument packs for each of the parameter packs expanded by this |
| 720 | // pack expansion. |
| 721 | for (auto &Pack : Packs) { |
| 722 | // Put back the old value for this pack. |
| 723 | Deduced[Pack.Index] = Pack.Saved; |
| 724 | |
| 725 | // Build or find a new value for this pack. |
| 726 | DeducedTemplateArgument NewPack; |
| 727 | if (HasAnyArguments && Pack.New.empty()) { |
| 728 | if (Pack.DeferredDeduction.isNull()) { |
| 729 | // We were not able to deduce anything for this parameter pack |
| 730 | // (because it only appeared in non-deduced contexts), so just |
| 731 | // restore the saved argument pack. |
| 732 | continue; |
| 733 | } |
| 734 | |
| 735 | NewPack = Pack.DeferredDeduction; |
| 736 | Pack.DeferredDeduction = TemplateArgument(); |
| 737 | } else if (Pack.New.empty()) { |
| 738 | // If we deduced an empty argument pack, create it now. |
| 739 | NewPack = DeducedTemplateArgument(TemplateArgument::getEmptyPack()); |
| 740 | } else { |
| 741 | TemplateArgument *ArgumentPack = |
| 742 | new (S.Context) TemplateArgument[Pack.New.size()]; |
| 743 | std::copy(Pack.New.begin(), Pack.New.end(), ArgumentPack); |
| 744 | NewPack = DeducedTemplateArgument( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 745 | TemplateArgument(llvm::makeArrayRef(ArgumentPack, Pack.New.size())), |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 746 | Pack.New[0].wasDeducedFromArrayBound()); |
| 747 | } |
| 748 | |
| 749 | // Pick where we're going to put the merged pack. |
| 750 | DeducedTemplateArgument *Loc; |
| 751 | if (Pack.Outer) { |
| 752 | if (Pack.Outer->DeferredDeduction.isNull()) { |
| 753 | // Defer checking this pack until we have a complete pack to compare |
| 754 | // it against. |
| 755 | Pack.Outer->DeferredDeduction = NewPack; |
| 756 | continue; |
| 757 | } |
| 758 | Loc = &Pack.Outer->DeferredDeduction; |
| 759 | } else { |
| 760 | Loc = &Deduced[Pack.Index]; |
| 761 | } |
| 762 | |
| 763 | // Check the new pack matches any previous value. |
| 764 | DeducedTemplateArgument OldPack = *Loc; |
| 765 | DeducedTemplateArgument Result = |
| 766 | checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 767 | |
| 768 | // If we deferred a deduction of this pack, check that one now too. |
| 769 | if (!Result.isNull() && !Pack.DeferredDeduction.isNull()) { |
| 770 | OldPack = Result; |
| 771 | NewPack = Pack.DeferredDeduction; |
| 772 | Result = checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 773 | } |
| 774 | |
| 775 | if (Result.isNull()) { |
| 776 | Info.Param = |
| 777 | makeTemplateParameter(TemplateParams->getParam(Pack.Index)); |
| 778 | Info.FirstArg = OldPack; |
| 779 | Info.SecondArg = NewPack; |
| 780 | return Sema::TDK_Inconsistent; |
| 781 | } |
| 782 | |
| 783 | *Loc = Result; |
| 784 | } |
| 785 | |
| 786 | return Sema::TDK_Success; |
| 787 | } |
| 788 | |
| 789 | private: |
| 790 | Sema &S; |
| 791 | TemplateParameterList *TemplateParams; |
| 792 | SmallVectorImpl<DeducedTemplateArgument> &Deduced; |
| 793 | TemplateDeductionInfo &Info; |
| 794 | |
| 795 | SmallVector<DeducedPack, 2> Packs; |
| 796 | }; |
Benjamin Kramer | d5748c7 | 2015-03-23 12:31:05 +0000 | [diff] [blame] | 797 | } // namespace |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 798 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 799 | /// \brief Deduce the template arguments by comparing the list of parameter |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 800 | /// types to the list of argument types, as in the parameter-type-lists of |
| 801 | /// function types (C++ [temp.deduct.type]p10). |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 802 | /// |
| 803 | /// \param S The semantic analysis object within which we are deducing |
| 804 | /// |
| 805 | /// \param TemplateParams The template parameters that we are deducing |
| 806 | /// |
| 807 | /// \param Params The list of parameter types |
| 808 | /// |
| 809 | /// \param NumParams The number of types in \c Params |
| 810 | /// |
| 811 | /// \param Args The list of argument types |
| 812 | /// |
| 813 | /// \param NumArgs The number of types in \c Args |
| 814 | /// |
| 815 | /// \param Info information about the template argument deduction itself |
| 816 | /// |
| 817 | /// \param Deduced the deduced template arguments |
| 818 | /// |
| 819 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
| 820 | /// how template argument deduction is performed. |
| 821 | /// |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 822 | /// \param PartialOrdering If true, we are performing template argument |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 823 | /// deduction for during partial ordering for a call |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 824 | /// (C++0x [temp.deduct.partial]). |
| 825 | /// |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 826 | /// \returns the result of template argument deduction so far. Note that a |
| 827 | /// "success" result means that template argument deduction has not yet failed, |
| 828 | /// but it may still fail, later, for other reasons. |
| 829 | static Sema::TemplateDeductionResult |
| 830 | DeduceTemplateArguments(Sema &S, |
| 831 | TemplateParameterList *TemplateParams, |
| 832 | const QualType *Params, unsigned NumParams, |
| 833 | const QualType *Args, unsigned NumArgs, |
| 834 | TemplateDeductionInfo &Info, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 835 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 836 | unsigned TDF, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 837 | bool PartialOrdering = false) { |
Douglas Gregor | 86bea35 | 2011-01-05 23:23:17 +0000 | [diff] [blame] | 838 | // Fast-path check to see if we have too many/too few arguments. |
| 839 | if (NumParams != NumArgs && |
| 840 | !(NumParams && isa<PackExpansionType>(Params[NumParams - 1])) && |
| 841 | !(NumArgs && isa<PackExpansionType>(Args[NumArgs - 1]))) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 842 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 843 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 844 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 845 | // Similarly, if P has a form that contains (T), then each parameter type |
| 846 | // Pi of the respective parameter-type- list of P is compared with the |
| 847 | // corresponding parameter type Ai of the corresponding parameter-type-list |
| 848 | // of A. [...] |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 849 | unsigned ArgIdx = 0, ParamIdx = 0; |
| 850 | for (; ParamIdx != NumParams; ++ParamIdx) { |
| 851 | // Check argument types. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 852 | const PackExpansionType *Expansion |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 853 | = dyn_cast<PackExpansionType>(Params[ParamIdx]); |
| 854 | if (!Expansion) { |
| 855 | // Simple case: compare the parameter and argument types at this point. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 856 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 857 | // Make sure we have an argument. |
| 858 | if (ArgIdx >= NumArgs) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 859 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 860 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 861 | if (isa<PackExpansionType>(Args[ArgIdx])) { |
| 862 | // C++0x [temp.deduct.type]p22: |
| 863 | // If the original function parameter associated with A is a function |
| 864 | // parameter pack and the function parameter associated with P is not |
| 865 | // a function parameter pack, then template argument deduction fails. |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 866 | return Sema::TDK_MiscellaneousDeductionFailure; |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 867 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 868 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 869 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 870 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 871 | Params[ParamIdx], Args[ArgIdx], |
| 872 | Info, Deduced, TDF, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 873 | PartialOrdering)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 874 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 875 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 876 | ++ArgIdx; |
| 877 | continue; |
| 878 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 879 | |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 880 | // C++0x [temp.deduct.type]p5: |
| 881 | // The non-deduced contexts are: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 882 | // - 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] | 883 | // parameter-declaration-clause. |
| 884 | if (ParamIdx + 1 < NumParams) |
| 885 | return Sema::TDK_Success; |
| 886 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 887 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 888 | // If the parameter-declaration corresponding to Pi is a function |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 889 | // parameter pack, then the type of its declarator- id is compared with |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 890 | // each remaining parameter type in the parameter-type-list of A. Each |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 891 | // comparison deduces template arguments for subsequent positions in the |
| 892 | // template parameter packs expanded by the function parameter pack. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 893 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 894 | QualType Pattern = Expansion->getPattern(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 895 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 896 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 897 | bool HasAnyArguments = false; |
| 898 | for (; ArgIdx < NumArgs; ++ArgIdx) { |
| 899 | HasAnyArguments = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 900 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 901 | // Deduce template arguments from the pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 902 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 903 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, Pattern, |
| 904 | Args[ArgIdx], Info, Deduced, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 905 | TDF, PartialOrdering)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 906 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 907 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 908 | PackScope.nextPackElement(); |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 909 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 910 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 911 | // Build argument packs for each of the parameter packs expanded by this |
| 912 | // pack expansion. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 913 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 914 | return Result; |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 915 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 916 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 917 | // Make sure we don't have any extra arguments. |
| 918 | if (ArgIdx < NumArgs) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 919 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 920 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 921 | return Sema::TDK_Success; |
| 922 | } |
| 923 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 924 | /// \brief Determine whether the parameter has qualifiers that are either |
| 925 | /// inconsistent with or a superset of the argument's qualifiers. |
| 926 | static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType, |
| 927 | QualType ArgType) { |
| 928 | Qualifiers ParamQs = ParamType.getQualifiers(); |
| 929 | Qualifiers ArgQs = ArgType.getQualifiers(); |
| 930 | |
| 931 | if (ParamQs == ArgQs) |
| 932 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 933 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 934 | // Mismatched (but not missing) Objective-C GC attributes. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 935 | if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() && |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 936 | ParamQs.hasObjCGCAttr()) |
| 937 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 938 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 939 | // Mismatched (but not missing) address spaces. |
| 940 | if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() && |
| 941 | ParamQs.hasAddressSpace()) |
| 942 | return true; |
| 943 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 944 | // Mismatched (but not missing) Objective-C lifetime qualifiers. |
| 945 | if (ParamQs.getObjCLifetime() != ArgQs.getObjCLifetime() && |
| 946 | ParamQs.hasObjCLifetime()) |
| 947 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 948 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 949 | // CVR qualifier superset. |
| 950 | return (ParamQs.getCVRQualifiers() != ArgQs.getCVRQualifiers()) && |
| 951 | ((ParamQs.getCVRQualifiers() | ArgQs.getCVRQualifiers()) |
| 952 | == ParamQs.getCVRQualifiers()); |
| 953 | } |
| 954 | |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 955 | /// \brief Compare types for equality with respect to possibly compatible |
| 956 | /// function types (noreturn adjustment, implicit calling conventions). If any |
| 957 | /// of parameter and argument is not a function, just perform type comparison. |
| 958 | /// |
| 959 | /// \param Param the template parameter type. |
| 960 | /// |
| 961 | /// \param Arg the argument type. |
| 962 | bool Sema::isSameOrCompatibleFunctionType(CanQualType Param, |
| 963 | CanQualType Arg) { |
| 964 | const FunctionType *ParamFunction = Param->getAs<FunctionType>(), |
| 965 | *ArgFunction = Arg->getAs<FunctionType>(); |
| 966 | |
| 967 | // Just compare if not functions. |
| 968 | if (!ParamFunction || !ArgFunction) |
| 969 | return Param == Arg; |
| 970 | |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 971 | // Noreturn and noexcept adjustment. |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 972 | QualType AdjustedParam; |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 973 | if (IsFunctionConversion(Param, Arg, AdjustedParam)) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 974 | return Arg == Context.getCanonicalType(AdjustedParam); |
| 975 | |
| 976 | // FIXME: Compatible calling conventions. |
| 977 | |
| 978 | return Param == Arg; |
| 979 | } |
| 980 | |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 981 | /// \brief Deduce the template arguments by comparing the parameter type and |
| 982 | /// the argument type (C++ [temp.deduct.type]). |
| 983 | /// |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 984 | /// \param S the semantic analysis object within which we are deducing |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 985 | /// |
| 986 | /// \param TemplateParams the template parameters that we are deducing |
| 987 | /// |
| 988 | /// \param ParamIn the parameter type |
| 989 | /// |
| 990 | /// \param ArgIn the argument type |
| 991 | /// |
| 992 | /// \param Info information about the template argument deduction itself |
| 993 | /// |
| 994 | /// \param Deduced the deduced template arguments |
| 995 | /// |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 996 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 997 | /// how template argument deduction is performed. |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 998 | /// |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 999 | /// \param PartialOrdering Whether we're performing template argument deduction |
| 1000 | /// in the context of partial ordering (C++0x [temp.deduct.partial]). |
| 1001 | /// |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1002 | /// \returns the result of template argument deduction so far. Note that a |
| 1003 | /// "success" result means that template argument deduction has not yet failed, |
| 1004 | /// but it may still fail, later, for other reasons. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1005 | static Sema::TemplateDeductionResult |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1006 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 1007 | TemplateParameterList *TemplateParams, |
| 1008 | QualType ParamIn, QualType ArgIn, |
| 1009 | TemplateDeductionInfo &Info, |
| 1010 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 1011 | unsigned TDF, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1012 | bool PartialOrdering, |
| 1013 | bool DeducedFromArrayBound) { |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1014 | // We only want to look at the canonical types, since typedefs and |
| 1015 | // sugar are not part of template argument deduction. |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1016 | QualType Param = S.Context.getCanonicalType(ParamIn); |
| 1017 | QualType Arg = S.Context.getCanonicalType(ArgIn); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1018 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1019 | // If the argument type is a pack expansion, look at its pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1020 | // This isn't explicitly called out |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1021 | if (const PackExpansionType *ArgExpansion |
| 1022 | = dyn_cast<PackExpansionType>(Arg)) |
| 1023 | Arg = ArgExpansion->getPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1024 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1025 | if (PartialOrdering) { |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1026 | // C++11 [temp.deduct.partial]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1027 | // Before the partial ordering is done, certain transformations are |
| 1028 | // performed on the types used for partial ordering: |
| 1029 | // - 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] | 1030 | const ReferenceType *ParamRef = Param->getAs<ReferenceType>(); |
| 1031 | if (ParamRef) |
| 1032 | Param = ParamRef->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1033 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1034 | // - If A is a reference type, A is replaced by the type referred to. |
| 1035 | const ReferenceType *ArgRef = Arg->getAs<ReferenceType>(); |
| 1036 | if (ArgRef) |
| 1037 | Arg = ArgRef->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1038 | |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1039 | if (ParamRef && ArgRef && S.Context.hasSameUnqualifiedType(Param, Arg)) { |
| 1040 | // C++11 [temp.deduct.partial]p9: |
| 1041 | // If, for a given type, deduction succeeds in both directions (i.e., |
| 1042 | // the types are identical after the transformations above) and both |
| 1043 | // P and A were reference types [...]: |
| 1044 | // - if [one type] was an lvalue reference and [the other type] was |
| 1045 | // not, [the other type] is not considered to be at least as |
| 1046 | // specialized as [the first type] |
| 1047 | // - if [one type] is more cv-qualified than [the other type], |
| 1048 | // [the other type] is not considered to be at least as specialized |
| 1049 | // as [the first type] |
| 1050 | // Objective-C ARC adds: |
| 1051 | // - [one type] has non-trivial lifetime, [the other type] has |
| 1052 | // __unsafe_unretained lifetime, and the types are otherwise |
| 1053 | // identical |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1054 | // |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1055 | // A is "considered to be at least as specialized" as P iff deduction |
| 1056 | // succeeds, so we model this as a deduction failure. Note that |
| 1057 | // [the first type] is P and [the other type] is A here; the standard |
| 1058 | // gets this backwards. |
Douglas Gregor | 85894a8 | 2011-04-30 17:07:52 +0000 | [diff] [blame] | 1059 | Qualifiers ParamQuals = Param.getQualifiers(); |
| 1060 | Qualifiers ArgQuals = Arg.getQualifiers(); |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1061 | if ((ParamRef->isLValueReferenceType() && |
| 1062 | !ArgRef->isLValueReferenceType()) || |
| 1063 | ParamQuals.isStrictSupersetOf(ArgQuals) || |
| 1064 | (ParamQuals.hasNonTrivialObjCLifetime() && |
| 1065 | ArgQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone && |
| 1066 | ParamQuals.withoutObjCLifetime() == |
| 1067 | ArgQuals.withoutObjCLifetime())) { |
| 1068 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1069 | Info.SecondArg = TemplateArgument(ArgIn); |
| 1070 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 6beabee | 2014-01-02 19:42:02 +0000 | [diff] [blame] | 1071 | } |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1072 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1073 | |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1074 | // C++11 [temp.deduct.partial]p7: |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1075 | // Remove any top-level cv-qualifiers: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1076 | // - 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] | 1077 | // version of P. |
| 1078 | Param = Param.getUnqualifiedType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1079 | // - 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] | 1080 | // version of A. |
| 1081 | Arg = Arg.getUnqualifiedType(); |
| 1082 | } else { |
| 1083 | // C++0x [temp.deduct.call]p4 bullet 1: |
| 1084 | // - If the original P is a reference type, the deduced A (i.e., the type |
| 1085 | // referred to by the reference) can be more cv-qualified than the |
| 1086 | // transformed A. |
| 1087 | if (TDF & TDF_ParamWithReferenceType) { |
| 1088 | Qualifiers Quals; |
| 1089 | QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals); |
| 1090 | Quals.setCVRQualifiers(Quals.getCVRQualifiers() & |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 1091 | Arg.getCVRQualifiers()); |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1092 | Param = S.Context.getQualifiedType(UnqualParam, Quals); |
| 1093 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1094 | |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1095 | if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) { |
| 1096 | // C++0x [temp.deduct.type]p10: |
| 1097 | // If P and A are function types that originated from deduction when |
| 1098 | // taking the address of a function template (14.8.2.2) or when deducing |
| 1099 | // 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] | 1100 | // Ai are parameters of the top-level parameter-type-list of P and A, |
| 1101 | // respectively, Pi is adjusted if it is an rvalue reference to a |
| 1102 | // cv-unqualified template parameter and Ai is an lvalue reference, in |
| 1103 | // 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] | 1104 | // type (i.e., T&& is changed to simply T). [ Note: As a result, when |
| 1105 | // 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] | 1106 | // deduced as X&. - end note ] |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1107 | TDF &= ~TDF_TopLevelParameterTypeList; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1108 | |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1109 | if (const RValueReferenceType *ParamRef |
| 1110 | = Param->getAs<RValueReferenceType>()) { |
| 1111 | if (isa<TemplateTypeParmType>(ParamRef->getPointeeType()) && |
| 1112 | !ParamRef->getPointeeType().getQualifiers()) |
| 1113 | if (Arg->isLValueReferenceType()) |
| 1114 | Param = ParamRef->getPointeeType(); |
| 1115 | } |
| 1116 | } |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1117 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1118 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1119 | // C++ [temp.deduct.type]p9: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1120 | // A template type argument T, a template template argument TT or a |
| 1121 | // 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] | 1122 | // the following forms: |
| 1123 | // |
| 1124 | // T |
| 1125 | // cv-list T |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1126 | if (const TemplateTypeParmType *TemplateTypeParm |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1127 | = Param->getAs<TemplateTypeParmType>()) { |
Douglas Gregor | 4ea5dec | 2011-09-22 15:57:07 +0000 | [diff] [blame] | 1128 | // Just skip any attempts to deduce from a placeholder type. |
| 1129 | if (Arg->isPlaceholderType()) |
| 1130 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1131 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1132 | unsigned Index = TemplateTypeParm->getIndex(); |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1133 | bool RecanonicalizeArg = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1134 | |
Douglas Gregor | 6045482 | 2009-07-22 20:02:25 +0000 | [diff] [blame] | 1135 | // If the argument type is an array type, move the qualifiers up to the |
| 1136 | // 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] | 1137 | if (isa<ArrayType>(Arg)) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1138 | Qualifiers Quals; |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1139 | Arg = S.Context.getUnqualifiedArrayType(Arg, Quals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1140 | if (Quals) { |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1141 | Arg = S.Context.getQualifiedType(Arg, Quals); |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1142 | RecanonicalizeArg = true; |
| 1143 | } |
| 1144 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1145 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1146 | // The argument type can not be less qualified than the parameter |
| 1147 | // type. |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1148 | if (!(TDF & TDF_IgnoreQualifiers) && |
| 1149 | hasInconsistentOrSupersetQualifiersOf(Param, Arg)) { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1150 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1151 | Info.FirstArg = TemplateArgument(Param); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1152 | Info.SecondArg = TemplateArgument(Arg); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1153 | return Sema::TDK_Underqualified; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1154 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1155 | |
| 1156 | assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0"); |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1157 | assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function"); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1158 | QualType DeducedType = Arg; |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1159 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1160 | // Remove any qualifiers on the parameter from the deduced type. |
| 1161 | // We checked the qualifiers for consistency above. |
| 1162 | Qualifiers DeducedQs = DeducedType.getQualifiers(); |
| 1163 | Qualifiers ParamQs = Param.getQualifiers(); |
| 1164 | DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers()); |
| 1165 | if (ParamQs.hasObjCGCAttr()) |
| 1166 | DeducedQs.removeObjCGCAttr(); |
| 1167 | if (ParamQs.hasAddressSpace()) |
| 1168 | DeducedQs.removeAddressSpace(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1169 | if (ParamQs.hasObjCLifetime()) |
| 1170 | DeducedQs.removeObjCLifetime(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1171 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1172 | // Objective-C ARC: |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1173 | // If template deduction would produce a lifetime qualifier on a type |
| 1174 | // that is not a lifetime type, template argument deduction fails. |
| 1175 | if (ParamQs.hasObjCLifetime() && !DeducedType->isObjCLifetimeType() && |
| 1176 | !DeducedType->isDependentType()) { |
| 1177 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1178 | Info.FirstArg = TemplateArgument(Param); |
| 1179 | Info.SecondArg = TemplateArgument(Arg); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1180 | return Sema::TDK_Underqualified; |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1181 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1182 | |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1183 | // Objective-C ARC: |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1184 | // If template deduction would produce an argument type with lifetime type |
| 1185 | // but no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1186 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1187 | DeducedType->isObjCLifetimeType() && |
| 1188 | !DeducedQs.hasObjCLifetime()) |
| 1189 | DeducedQs.setObjCLifetime(Qualifiers::OCL_Strong); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1190 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1191 | DeducedType = S.Context.getQualifiedType(DeducedType.getUnqualifiedType(), |
| 1192 | DeducedQs); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1193 | |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1194 | if (RecanonicalizeArg) |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1195 | DeducedType = S.Context.getCanonicalType(DeducedType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1196 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1197 | DeducedTemplateArgument NewDeduced(DeducedType, DeducedFromArrayBound); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1198 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1199 | Deduced[Index], |
| 1200 | NewDeduced); |
| 1201 | if (Result.isNull()) { |
| 1202 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1203 | Info.FirstArg = Deduced[Index]; |
| 1204 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1205 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1206 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1207 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1208 | Deduced[Index] = Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1209 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1212 | // Set up the template argument deduction information for a failure. |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1213 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1214 | Info.SecondArg = TemplateArgument(ArgIn); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1215 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 1216 | // If the parameter is an already-substituted template parameter |
| 1217 | // pack, do nothing: we don't know which of its arguments to look |
| 1218 | // at, so we have to wait until all of the parameter packs in this |
| 1219 | // expansion have arguments. |
| 1220 | if (isa<SubstTemplateTypeParmPackType>(Param)) |
| 1221 | return Sema::TDK_Success; |
| 1222 | |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1223 | // Check the cv-qualifiers on the parameter and argument types. |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1224 | CanQualType CanParam = S.Context.getCanonicalType(Param); |
| 1225 | CanQualType CanArg = S.Context.getCanonicalType(Arg); |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1226 | if (!(TDF & TDF_IgnoreQualifiers)) { |
| 1227 | if (TDF & TDF_ParamWithReferenceType) { |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1228 | if (hasInconsistentOrSupersetQualifiersOf(Param, Arg)) |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1229 | return Sema::TDK_NonDeducedMismatch; |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 1230 | } else if (!IsPossiblyOpaquelyQualifiedType(Param)) { |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1231 | if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1232 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1233 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1234 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1235 | // If the parameter type is not dependent, there is nothing to deduce. |
| 1236 | if (!Param->isDependentType()) { |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1237 | if (!(TDF & TDF_SkipNonDependent)) { |
| 1238 | bool NonDeduced = (TDF & TDF_InOverloadResolution)? |
| 1239 | !S.isSameOrCompatibleFunctionType(CanParam, CanArg) : |
| 1240 | Param != Arg; |
| 1241 | if (NonDeduced) { |
| 1242 | return Sema::TDK_NonDeducedMismatch; |
| 1243 | } |
| 1244 | } |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1245 | return Sema::TDK_Success; |
| 1246 | } |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1247 | } else if (!Param->isDependentType()) { |
| 1248 | CanQualType ParamUnqualType = CanParam.getUnqualifiedType(), |
| 1249 | ArgUnqualType = CanArg.getUnqualifiedType(); |
| 1250 | bool Success = (TDF & TDF_InOverloadResolution)? |
| 1251 | S.isSameOrCompatibleFunctionType(ParamUnqualType, |
| 1252 | ArgUnqualType) : |
| 1253 | ParamUnqualType == ArgUnqualType; |
| 1254 | if (Success) |
| 1255 | return Sema::TDK_Success; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1256 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1257 | |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1258 | switch (Param->getTypeClass()) { |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1259 | // Non-canonical types cannot appear here. |
| 1260 | #define NON_CANONICAL_TYPE(Class, Base) \ |
| 1261 | case Type::Class: llvm_unreachable("deducing non-canonical type: " #Class); |
| 1262 | #define TYPE(Class, Base) |
| 1263 | #include "clang/AST/TypeNodes.def" |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1264 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1265 | case Type::TemplateTypeParm: |
| 1266 | case Type::SubstTemplateTypeParmPack: |
| 1267 | llvm_unreachable("Type nodes handled above"); |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1268 | |
| 1269 | // These types cannot be dependent, so simply check whether the types are |
| 1270 | // the same. |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1271 | case Type::Builtin: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1272 | case Type::VariableArray: |
| 1273 | case Type::Vector: |
| 1274 | case Type::FunctionNoProto: |
| 1275 | case Type::Record: |
| 1276 | case Type::Enum: |
| 1277 | case Type::ObjCObject: |
| 1278 | case Type::ObjCInterface: |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1279 | case Type::ObjCObjectPointer: { |
| 1280 | if (TDF & TDF_SkipNonDependent) |
| 1281 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1282 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1283 | if (TDF & TDF_IgnoreQualifiers) { |
| 1284 | Param = Param.getUnqualifiedType(); |
| 1285 | Arg = Arg.getUnqualifiedType(); |
| 1286 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1287 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1288 | return Param == Arg? Sema::TDK_Success : Sema::TDK_NonDeducedMismatch; |
| 1289 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1290 | |
| 1291 | // _Complex T [placeholder extension] |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1292 | case Type::Complex: |
| 1293 | if (const ComplexType *ComplexArg = Arg->getAs<ComplexType>()) |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1294 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1295 | cast<ComplexType>(Param)->getElementType(), |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1296 | ComplexArg->getElementType(), |
| 1297 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1298 | |
| 1299 | return Sema::TDK_NonDeducedMismatch; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1300 | |
| 1301 | // _Atomic T [extension] |
| 1302 | case Type::Atomic: |
| 1303 | if (const AtomicType *AtomicArg = Arg->getAs<AtomicType>()) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1304 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1305 | cast<AtomicType>(Param)->getValueType(), |
| 1306 | AtomicArg->getValueType(), |
| 1307 | Info, Deduced, TDF); |
| 1308 | |
| 1309 | return Sema::TDK_NonDeducedMismatch; |
| 1310 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1311 | // T * |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1312 | case Type::Pointer: { |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1313 | QualType PointeeType; |
| 1314 | if (const PointerType *PointerArg = Arg->getAs<PointerType>()) { |
| 1315 | PointeeType = PointerArg->getPointeeType(); |
| 1316 | } else if (const ObjCObjectPointerType *PointerArg |
| 1317 | = Arg->getAs<ObjCObjectPointerType>()) { |
| 1318 | PointeeType = PointerArg->getPointeeType(); |
| 1319 | } else { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1320 | return Sema::TDK_NonDeducedMismatch; |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1321 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1322 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1323 | unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1324 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1325 | cast<PointerType>(Param)->getPointeeType(), |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1326 | PointeeType, |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1327 | Info, Deduced, SubTDF); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1328 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1329 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1330 | // T & |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1331 | case Type::LValueReference: { |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 1332 | const LValueReferenceType *ReferenceArg = |
| 1333 | Arg->getAs<LValueReferenceType>(); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1334 | if (!ReferenceArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1335 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1336 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1337 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1338 | cast<LValueReferenceType>(Param)->getPointeeType(), |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1339 | ReferenceArg->getPointeeType(), Info, Deduced, 0); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1340 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1341 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1342 | // T && [C++0x] |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1343 | case Type::RValueReference: { |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 1344 | const RValueReferenceType *ReferenceArg = |
| 1345 | Arg->getAs<RValueReferenceType>(); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1346 | if (!ReferenceArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1347 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1348 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1349 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1350 | cast<RValueReferenceType>(Param)->getPointeeType(), |
| 1351 | ReferenceArg->getPointeeType(), |
| 1352 | Info, Deduced, 0); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1353 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1354 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1355 | // T [] (implied, but not stated explicitly) |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1356 | case Type::IncompleteArray: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1357 | const IncompleteArrayType *IncompleteArrayArg = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1358 | S.Context.getAsIncompleteArrayType(Arg); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1359 | if (!IncompleteArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1360 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1361 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1362 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1363 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1364 | S.Context.getAsIncompleteArrayType(Param)->getElementType(), |
| 1365 | IncompleteArrayArg->getElementType(), |
| 1366 | Info, Deduced, SubTDF); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1367 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1368 | |
| 1369 | // T [integer-constant] |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1370 | case Type::ConstantArray: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1371 | const ConstantArrayType *ConstantArrayArg = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1372 | S.Context.getAsConstantArrayType(Arg); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1373 | if (!ConstantArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1374 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1375 | |
| 1376 | const ConstantArrayType *ConstantArrayParm = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1377 | S.Context.getAsConstantArrayType(Param); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1378 | if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1379 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1380 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1381 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1382 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1383 | ConstantArrayParm->getElementType(), |
| 1384 | ConstantArrayArg->getElementType(), |
| 1385 | Info, Deduced, SubTDF); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1388 | // type [i] |
| 1389 | case Type::DependentSizedArray: { |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1390 | const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1391 | if (!ArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1392 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1393 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1394 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
| 1395 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1396 | // Check the element type of the arrays |
| 1397 | const DependentSizedArrayType *DependentArrayParm |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1398 | = S.Context.getAsDependentSizedArrayType(Param); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1399 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1400 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1401 | DependentArrayParm->getElementType(), |
| 1402 | ArrayArg->getElementType(), |
| 1403 | Info, Deduced, SubTDF)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1404 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1405 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1406 | // Determine the array bound is something we can deduce. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1407 | NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1408 | = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr()); |
| 1409 | if (!NTTP) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1410 | return Sema::TDK_Success; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1411 | |
| 1412 | // We can perform template argument deduction for the given non-type |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1413 | // template parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1414 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1415 | "Cannot deduce non-type template argument at depth > 0"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1416 | if (const ConstantArrayType *ConstantArrayArg |
Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1417 | = dyn_cast<ConstantArrayType>(ArrayArg)) { |
| 1418 | llvm::APSInt Size(ConstantArrayArg->getSize()); |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1419 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, Size, |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1420 | S.Context.getSizeType(), |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1421 | /*ArrayBound=*/true, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1422 | Info, Deduced); |
Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1423 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1424 | if (const DependentSizedArrayType *DependentArrayArg |
| 1425 | = dyn_cast<DependentSizedArrayType>(ArrayArg)) |
Douglas Gregor | 7a49ead | 2010-12-22 23:15:38 +0000 | [diff] [blame] | 1426 | if (DependentArrayArg->getSizeExpr()) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1427 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
Douglas Gregor | 7a49ead | 2010-12-22 23:15:38 +0000 | [diff] [blame] | 1428 | DependentArrayArg->getSizeExpr(), |
| 1429 | Info, Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1430 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1431 | // Incomplete type does not match a dependently-sized array type |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1432 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1433 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1434 | |
| 1435 | // type(*)(T) |
| 1436 | // T(*)() |
| 1437 | // T(*)(T) |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1438 | case Type::FunctionProto: { |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1439 | unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1440 | const FunctionProtoType *FunctionProtoArg = |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1441 | dyn_cast<FunctionProtoType>(Arg); |
| 1442 | if (!FunctionProtoArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1443 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1444 | |
| 1445 | const FunctionProtoType *FunctionProtoParam = |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1446 | cast<FunctionProtoType>(Param); |
Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1447 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1448 | if (FunctionProtoParam->getTypeQuals() |
Douglas Gregor | 54e462a | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1449 | != FunctionProtoArg->getTypeQuals() || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1450 | FunctionProtoParam->getRefQualifier() |
Douglas Gregor | 54e462a | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1451 | != FunctionProtoArg->getRefQualifier() || |
| 1452 | FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1453 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1454 | |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1455 | // Check return types. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1456 | if (Sema::TemplateDeductionResult Result = |
| 1457 | DeduceTemplateArgumentsByTypeMatch( |
| 1458 | S, TemplateParams, FunctionProtoParam->getReturnType(), |
| 1459 | FunctionProtoArg->getReturnType(), Info, Deduced, 0)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1460 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1461 | |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1462 | return DeduceTemplateArguments( |
| 1463 | S, TemplateParams, FunctionProtoParam->param_type_begin(), |
| 1464 | FunctionProtoParam->getNumParams(), |
| 1465 | FunctionProtoArg->param_type_begin(), |
| 1466 | FunctionProtoArg->getNumParams(), Info, Deduced, SubTDF); |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1467 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1468 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1469 | case Type::InjectedClassName: { |
| 1470 | // Treat a template's injected-class-name as if the template |
| 1471 | // specialization type had been used. |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1472 | Param = cast<InjectedClassNameType>(Param) |
| 1473 | ->getInjectedSpecializationType(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1474 | assert(isa<TemplateSpecializationType>(Param) && |
| 1475 | "injected class name is not a template specialization type"); |
| 1476 | // fall through |
| 1477 | } |
| 1478 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1479 | // template-name<T> (where template-name refers to a class template) |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1480 | // template-name<i> |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1481 | // TT<T> |
| 1482 | // TT<i> |
| 1483 | // TT<> |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1484 | case Type::TemplateSpecialization: { |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1485 | const TemplateSpecializationType *SpecParam = |
| 1486 | cast<TemplateSpecializationType>(Param); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1488 | // When Arg cannot be a derived class, we can just try to deduce template |
| 1489 | // arguments from the template-id. |
| 1490 | const RecordType *RecordT = Arg->getAs<RecordType>(); |
| 1491 | if (!(TDF & TDF_DerivedClass) || !RecordT) |
| 1492 | return DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg, Info, |
| 1493 | Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1494 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1495 | SmallVector<DeducedTemplateArgument, 8> DeducedOrig(Deduced.begin(), |
| 1496 | Deduced.end()); |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1497 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1498 | Sema::TemplateDeductionResult Result = DeduceTemplateArguments( |
| 1499 | S, TemplateParams, SpecParam, Arg, Info, Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1500 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1501 | if (Result == Sema::TDK_Success) |
| 1502 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1503 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1504 | // We cannot inspect base classes as part of deduction when the type |
| 1505 | // is incomplete, so either instantiate any templates necessary to |
| 1506 | // complete the type, or skip over it if it cannot be completed. |
| 1507 | if (!S.isCompleteType(Info.getLocation(), Arg)) |
| 1508 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1509 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1510 | // C++14 [temp.deduct.call] p4b3: |
| 1511 | // If P is a class and P has the form simple-template-id, then the |
| 1512 | // transformed A can be a derived class of the deduced A. Likewise if |
| 1513 | // P is a pointer to a class of the form simple-template-id, the |
| 1514 | // transformed A can be a pointer to a derived class pointed to by the |
| 1515 | // deduced A. |
| 1516 | // |
| 1517 | // These alternatives are considered only if type deduction would |
| 1518 | // otherwise fail. If they yield more than one possible deduced A, the |
| 1519 | // type deduction fails. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1520 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1521 | // Reset the incorrectly deduced argument from above. |
| 1522 | Deduced = DeducedOrig; |
| 1523 | |
| 1524 | // Use data recursion to crawl through the list of base classes. |
| 1525 | // Visited contains the set of nodes we have already visited, while |
| 1526 | // ToVisit is our stack of records that we still need to visit. |
| 1527 | llvm::SmallPtrSet<const RecordType *, 8> Visited; |
| 1528 | SmallVector<const RecordType *, 8> ToVisit; |
| 1529 | ToVisit.push_back(RecordT); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1530 | bool Successful = false; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1531 | SmallVector<DeducedTemplateArgument, 8> SuccessfulDeduced; |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1532 | while (!ToVisit.empty()) { |
| 1533 | // Retrieve the next class in the inheritance hierarchy. |
| 1534 | const RecordType *NextT = ToVisit.pop_back_val(); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1535 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1536 | // If we have already seen this type, skip it. |
| 1537 | if (!Visited.insert(NextT).second) |
| 1538 | continue; |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1539 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1540 | // If this is a base class, try to perform template argument |
| 1541 | // deduction from it. |
| 1542 | if (NextT != RecordT) { |
| 1543 | TemplateDeductionInfo BaseInfo(Info.getLocation()); |
| 1544 | Sema::TemplateDeductionResult BaseResult = |
| 1545 | DeduceTemplateArguments(S, TemplateParams, SpecParam, |
| 1546 | QualType(NextT, 0), BaseInfo, Deduced); |
| 1547 | |
| 1548 | // If template argument deduction for this base was successful, |
| 1549 | // note that we had some success. Otherwise, ignore any deductions |
| 1550 | // from this base class. |
| 1551 | if (BaseResult == Sema::TDK_Success) { |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1552 | // If we've already seen some success, then deduction fails due to |
| 1553 | // an ambiguity (temp.deduct.call p5). |
| 1554 | if (Successful) |
| 1555 | return Sema::TDK_MiscellaneousDeductionFailure; |
| 1556 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1557 | Successful = true; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1558 | std::swap(SuccessfulDeduced, Deduced); |
| 1559 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1560 | Info.Param = BaseInfo.Param; |
| 1561 | Info.FirstArg = BaseInfo.FirstArg; |
| 1562 | Info.SecondArg = BaseInfo.SecondArg; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | Deduced = DeducedOrig; |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1566 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1567 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1568 | // Visit base classes |
| 1569 | CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl()); |
| 1570 | for (const auto &Base : Next->bases()) { |
| 1571 | assert(Base.getType()->isRecordType() && |
| 1572 | "Base class that isn't a record?"); |
| 1573 | ToVisit.push_back(Base.getType()->getAs<RecordType>()); |
| 1574 | } |
| 1575 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1576 | |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1577 | if (Successful) { |
| 1578 | std::swap(SuccessfulDeduced, Deduced); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1579 | return Sema::TDK_Success; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1580 | } |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1581 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1582 | return Result; |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1585 | // T type::* |
| 1586 | // T T::* |
| 1587 | // T (type::*)() |
| 1588 | // type (T::*)() |
| 1589 | // type (type::*)(T) |
| 1590 | // type (T::*)(T) |
| 1591 | // T (type::*)(T) |
| 1592 | // T (T::*)() |
| 1593 | // T (T::*)(T) |
| 1594 | case Type::MemberPointer: { |
| 1595 | const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); |
| 1596 | const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); |
| 1597 | if (!MemPtrArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1598 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1599 | |
David Majnemer | a381cda | 2015-11-30 20:34:28 +0000 | [diff] [blame] | 1600 | QualType ParamPointeeType = MemPtrParam->getPointeeType(); |
| 1601 | if (ParamPointeeType->isFunctionType()) |
| 1602 | S.adjustMemberFunctionCC(ParamPointeeType, /*IsStatic=*/true, |
| 1603 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
| 1604 | QualType ArgPointeeType = MemPtrArg->getPointeeType(); |
| 1605 | if (ArgPointeeType->isFunctionType()) |
| 1606 | S.adjustMemberFunctionCC(ArgPointeeType, /*IsStatic=*/true, |
| 1607 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
| 1608 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1609 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1610 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
David Majnemer | a381cda | 2015-11-30 20:34:28 +0000 | [diff] [blame] | 1611 | ParamPointeeType, |
| 1612 | ArgPointeeType, |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1613 | Info, Deduced, |
| 1614 | TDF & TDF_IgnoreQualifiers)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1615 | return Result; |
| 1616 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1617 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1618 | QualType(MemPtrParam->getClass(), 0), |
| 1619 | QualType(MemPtrArg->getClass(), 0), |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1620 | Info, Deduced, |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1621 | TDF & TDF_IgnoreQualifiers); |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
Anders Carlsson | 15f1dd1 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1624 | // (clang extension) |
| 1625 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1626 | // type(^)(T) |
| 1627 | // T(^)() |
| 1628 | // T(^)(T) |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1629 | case Type::BlockPointer: { |
| 1630 | const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); |
| 1631 | const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1632 | |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1633 | if (!BlockPtrArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1634 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1635 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1636 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1637 | BlockPtrParam->getPointeeType(), |
| 1638 | BlockPtrArg->getPointeeType(), |
| 1639 | Info, Deduced, 0); |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1642 | // (clang extension) |
| 1643 | // |
| 1644 | // T __attribute__(((ext_vector_type(<integral constant>)))) |
| 1645 | case Type::ExtVector: { |
| 1646 | const ExtVectorType *VectorParam = cast<ExtVectorType>(Param); |
| 1647 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1648 | // Make sure that the vectors have the same number of elements. |
| 1649 | if (VectorParam->getNumElements() != VectorArg->getNumElements()) |
| 1650 | return Sema::TDK_NonDeducedMismatch; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1651 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1652 | // Perform deduction on the element types. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1653 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1654 | VectorParam->getElementType(), |
| 1655 | VectorArg->getElementType(), |
| 1656 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1657 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1658 | |
| 1659 | if (const DependentSizedExtVectorType *VectorArg |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1660 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1661 | // We can't check the number of elements, since the argument has a |
| 1662 | // dependent number of elements. This can only occur during partial |
| 1663 | // ordering. |
| 1664 | |
| 1665 | // Perform deduction on the element types. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1666 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1667 | VectorParam->getElementType(), |
| 1668 | VectorArg->getElementType(), |
| 1669 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1670 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1671 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1672 | return Sema::TDK_NonDeducedMismatch; |
| 1673 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1674 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1675 | // (clang extension) |
| 1676 | // |
| 1677 | // T __attribute__(((ext_vector_type(N)))) |
| 1678 | case Type::DependentSizedExtVector: { |
| 1679 | const DependentSizedExtVectorType *VectorParam |
| 1680 | = cast<DependentSizedExtVectorType>(Param); |
| 1681 | |
| 1682 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1683 | // Perform deduction on the element types. |
| 1684 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1685 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1686 | VectorParam->getElementType(), |
| 1687 | VectorArg->getElementType(), |
| 1688 | Info, Deduced, TDF)) |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1689 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1690 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1691 | // Perform deduction on the vector size, if we can. |
| 1692 | NonTypeTemplateParmDecl *NTTP |
| 1693 | = getDeducedParameterFromExpr(VectorParam->getSizeExpr()); |
| 1694 | if (!NTTP) |
| 1695 | return Sema::TDK_Success; |
| 1696 | |
| 1697 | llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); |
| 1698 | ArgSize = VectorArg->getNumElements(); |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1699 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 1700 | S.Context.IntTy, false, Info, |
| 1701 | Deduced); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1702 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1703 | |
| 1704 | if (const DependentSizedExtVectorType *VectorArg |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1705 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1706 | // Perform deduction on the element types. |
| 1707 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1708 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1709 | VectorParam->getElementType(), |
| 1710 | VectorArg->getElementType(), |
| 1711 | Info, Deduced, TDF)) |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1712 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1713 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1714 | // Perform deduction on the vector size, if we can. |
| 1715 | NonTypeTemplateParmDecl *NTTP |
| 1716 | = getDeducedParameterFromExpr(VectorParam->getSizeExpr()); |
| 1717 | if (!NTTP) |
| 1718 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1719 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1720 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 1721 | VectorArg->getSizeExpr(), |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1722 | Info, Deduced); |
| 1723 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1724 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1725 | return Sema::TDK_NonDeducedMismatch; |
| 1726 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1727 | |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1728 | case Type::TypeOfExpr: |
| 1729 | case Type::TypeOf: |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 1730 | case Type::DependentName: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1731 | case Type::UnresolvedUsing: |
| 1732 | case Type::Decltype: |
| 1733 | case Type::UnaryTransform: |
| 1734 | case Type::Auto: |
| 1735 | case Type::DependentTemplateSpecialization: |
| 1736 | case Type::PackExpansion: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 1737 | case Type::Pipe: |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1738 | // No template argument deduction for these types |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1739 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1742 | llvm_unreachable("Invalid Type Class!"); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1745 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1746 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1747 | TemplateParameterList *TemplateParams, |
| 1748 | const TemplateArgument &Param, |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1749 | TemplateArgument Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1750 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 1751 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1752 | // If the template argument is a pack expansion, perform template argument |
| 1753 | // deduction against the pattern of that expansion. This only occurs during |
| 1754 | // partial ordering. |
| 1755 | if (Arg.isPackExpansion()) |
| 1756 | Arg = Arg.getPackExpansionPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1757 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1758 | switch (Param.getKind()) { |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1759 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1760 | llvm_unreachable("Null template argument in parameter list"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1761 | |
| 1762 | case TemplateArgument::Type: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1763 | if (Arg.getKind() == TemplateArgument::Type) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1764 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1765 | Param.getAsType(), |
| 1766 | Arg.getAsType(), |
| 1767 | Info, Deduced, 0); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1768 | Info.FirstArg = Param; |
| 1769 | Info.SecondArg = Arg; |
| 1770 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1771 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1772 | case TemplateArgument::Template: |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1773 | if (Arg.getKind() == TemplateArgument::Template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1774 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1775 | Param.getAsTemplate(), |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1776 | Arg.getAsTemplate(), Info, Deduced); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1777 | Info.FirstArg = Param; |
| 1778 | Info.SecondArg = Arg; |
| 1779 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1780 | |
| 1781 | case TemplateArgument::TemplateExpansion: |
| 1782 | llvm_unreachable("caller should handle pack expansions"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1783 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1784 | case TemplateArgument::Declaration: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1785 | if (Arg.getKind() == TemplateArgument::Declaration && |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 1786 | isSameDeclaration(Param.getAsDecl(), Arg.getAsDecl())) |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 1787 | return Sema::TDK_Success; |
| 1788 | |
| 1789 | Info.FirstArg = Param; |
| 1790 | Info.SecondArg = Arg; |
| 1791 | return Sema::TDK_NonDeducedMismatch; |
| 1792 | |
| 1793 | case TemplateArgument::NullPtr: |
| 1794 | if (Arg.getKind() == TemplateArgument::NullPtr && |
| 1795 | S.Context.hasSameType(Param.getNullPtrType(), Arg.getNullPtrType())) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1796 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1797 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1798 | Info.FirstArg = Param; |
| 1799 | Info.SecondArg = Arg; |
| 1800 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1801 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1802 | case TemplateArgument::Integral: |
| 1803 | if (Arg.getKind() == TemplateArgument::Integral) { |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1804 | if (hasSameExtendedValue(Param.getAsIntegral(), Arg.getAsIntegral())) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1805 | return Sema::TDK_Success; |
| 1806 | |
| 1807 | Info.FirstArg = Param; |
| 1808 | Info.SecondArg = Arg; |
| 1809 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1810 | } |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1811 | |
| 1812 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 1813 | Info.FirstArg = Param; |
| 1814 | Info.SecondArg = Arg; |
| 1815 | return Sema::TDK_NonDeducedMismatch; |
| 1816 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1817 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1818 | Info.FirstArg = Param; |
| 1819 | Info.SecondArg = Arg; |
| 1820 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1821 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1822 | case TemplateArgument::Expression: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1824 | = getDeducedParameterFromExpr(Param.getAsExpr())) { |
| 1825 | if (Arg.getKind() == TemplateArgument::Integral) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1826 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1827 | Arg.getAsIntegral(), |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1828 | Arg.getIntegralType(), |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1829 | /*ArrayBound=*/false, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1830 | Info, Deduced); |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 1831 | if (Arg.getKind() == TemplateArgument::NullPtr) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1832 | return DeduceNullPtrTemplateArgument(S, TemplateParams, NTTP, |
| 1833 | Arg.getNullPtrType(), |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 1834 | Info, Deduced); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1835 | if (Arg.getKind() == TemplateArgument::Expression) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1836 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 1837 | Arg.getAsExpr(), Info, Deduced); |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 1838 | if (Arg.getKind() == TemplateArgument::Declaration) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1839 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 1840 | Arg.getAsDecl(), |
| 1841 | Arg.getParamTypeForDecl(), |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 1842 | Info, Deduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1843 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1844 | Info.FirstArg = Param; |
| 1845 | Info.SecondArg = Arg; |
| 1846 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1847 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1848 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1849 | // Can't deduce anything, but that's okay. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1850 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1851 | } |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 1852 | case TemplateArgument::Pack: |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1853 | llvm_unreachable("Argument packs should be expanded by the caller!"); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1854 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1855 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1856 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1859 | /// \brief Determine whether there is a template argument to be used for |
| 1860 | /// deduction. |
| 1861 | /// |
| 1862 | /// This routine "expands" argument packs in-place, overriding its input |
| 1863 | /// parameters so that \c Args[ArgIdx] will be the available template argument. |
| 1864 | /// |
| 1865 | /// \returns true if there is another template argument (which will be at |
| 1866 | /// \c Args[ArgIdx]), false otherwise. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1867 | static bool hasTemplateArgumentForDeduction(const TemplateArgument *&Args, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1868 | unsigned &ArgIdx, |
| 1869 | unsigned &NumArgs) { |
| 1870 | if (ArgIdx == NumArgs) |
| 1871 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1872 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1873 | const TemplateArgument &Arg = Args[ArgIdx]; |
| 1874 | if (Arg.getKind() != TemplateArgument::Pack) |
| 1875 | return true; |
| 1876 | |
| 1877 | assert(ArgIdx == NumArgs - 1 && "Pack not at the end of argument list?"); |
| 1878 | Args = Arg.pack_begin(); |
| 1879 | NumArgs = Arg.pack_size(); |
| 1880 | ArgIdx = 0; |
| 1881 | return ArgIdx < NumArgs; |
| 1882 | } |
| 1883 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1884 | /// \brief Determine whether the given set of template arguments has a pack |
| 1885 | /// expansion that is not the last template argument. |
| 1886 | static bool hasPackExpansionBeforeEnd(const TemplateArgument *Args, |
| 1887 | unsigned NumArgs) { |
| 1888 | unsigned ArgIdx = 0; |
| 1889 | while (ArgIdx < NumArgs) { |
| 1890 | const TemplateArgument &Arg = Args[ArgIdx]; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1891 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1892 | // Unwrap argument packs. |
| 1893 | if (Args[ArgIdx].getKind() == TemplateArgument::Pack) { |
| 1894 | Args = Arg.pack_begin(); |
| 1895 | NumArgs = Arg.pack_size(); |
| 1896 | ArgIdx = 0; |
| 1897 | continue; |
| 1898 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1899 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1900 | ++ArgIdx; |
| 1901 | if (ArgIdx == NumArgs) |
| 1902 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1903 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1904 | if (Arg.isPackExpansion()) |
| 1905 | return true; |
| 1906 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1907 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1908 | return false; |
| 1909 | } |
| 1910 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1911 | static Sema::TemplateDeductionResult |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1912 | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1913 | const TemplateArgument *Params, unsigned NumParams, |
| 1914 | const TemplateArgument *Args, unsigned NumArgs, |
| 1915 | TemplateDeductionInfo &Info, |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1916 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 1917 | bool NumberOfArgumentsMustMatch) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1918 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1919 | // If the template argument list of P contains a pack expansion that is not |
| 1920 | // the last template argument, the entire template argument list is a |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1921 | // non-deduced context. |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1922 | if (hasPackExpansionBeforeEnd(Params, NumParams)) |
| 1923 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1924 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1925 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1926 | // If P has a form that contains <T> or <i>, then each argument Pi of the |
| 1927 | // respective template argument list P is compared with the corresponding |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1928 | // argument Ai of the corresponding template argument list of A. |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1929 | unsigned ArgIdx = 0, ParamIdx = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1930 | for (; hasTemplateArgumentForDeduction(Params, ParamIdx, NumParams); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1931 | ++ParamIdx) { |
| 1932 | if (!Params[ParamIdx].isPackExpansion()) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1933 | // The simple case: deduce template arguments by matching Pi and Ai. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1934 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1935 | // Check whether we have enough arguments. |
| 1936 | if (!hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs)) |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1937 | return NumberOfArgumentsMustMatch ? Sema::TDK_TooFewArguments |
| 1938 | : Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1939 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1940 | if (Args[ArgIdx].isPackExpansion()) { |
| 1941 | // FIXME: We follow the logic of C++0x [temp.deduct.type]p22 here, |
| 1942 | // but applied to pack expansions that are template arguments. |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 1943 | return Sema::TDK_MiscellaneousDeductionFailure; |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1944 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1945 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1946 | // Perform deduction for this Pi/Ai pair. |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1947 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1948 | = DeduceTemplateArguments(S, TemplateParams, |
| 1949 | Params[ParamIdx], Args[ArgIdx], |
| 1950 | Info, Deduced)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1951 | return Result; |
| 1952 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1953 | // Move to the next argument. |
| 1954 | ++ArgIdx; |
| 1955 | continue; |
| 1956 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1957 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1958 | // The parameter is a pack expansion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1959 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1960 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1961 | // If Pi is a pack expansion, then the pattern of Pi is compared with |
| 1962 | // each remaining argument in the template argument list of A. Each |
| 1963 | // comparison deduces template arguments for subsequent positions in the |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1964 | // template parameter packs expanded by Pi. |
| 1965 | TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1966 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1967 | // FIXME: If there are no remaining arguments, we can bail out early |
| 1968 | // and set any deduced parameter packs to an empty argument pack. |
| 1969 | // The latter part of this is a (minor) correctness issue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1970 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1971 | // Prepare to deduce the packs within the pattern. |
| 1972 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1973 | |
| 1974 | // Keep track of the deduced template arguments for each parameter pack |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1975 | // expanded by this pack expansion (the outer index) and for each |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1976 | // template argument (the inner SmallVectors). |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1977 | bool HasAnyArguments = false; |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1978 | for (; hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs); ++ArgIdx) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1979 | HasAnyArguments = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1980 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1981 | // Deduce template arguments from the pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1982 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1983 | = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx], |
| 1984 | Info, Deduced)) |
| 1985 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1986 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1987 | PackScope.nextPackElement(); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1988 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1989 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1990 | // Build argument packs for each of the parameter packs expanded by this |
| 1991 | // pack expansion. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1992 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1993 | return Result; |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1994 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1995 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1996 | return Sema::TDK_Success; |
| 1997 | } |
| 1998 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1999 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2000 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2001 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2002 | const TemplateArgumentList &ParamList, |
| 2003 | const TemplateArgumentList &ArgList, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2004 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2005 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2006 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2007 | ParamList.data(), ParamList.size(), |
| 2008 | ArgList.data(), ArgList.size(), |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 2009 | Info, Deduced, false); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2012 | /// \brief Determine whether two template arguments are the same. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2013 | static bool isSameTemplateArg(ASTContext &Context, |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2014 | const TemplateArgument &X, |
| 2015 | const TemplateArgument &Y) { |
| 2016 | if (X.getKind() != Y.getKind()) |
| 2017 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2018 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2019 | switch (X.getKind()) { |
| 2020 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2021 | llvm_unreachable("Comparing NULL template argument"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2022 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2023 | case TemplateArgument::Type: |
| 2024 | return Context.getCanonicalType(X.getAsType()) == |
| 2025 | Context.getCanonicalType(Y.getAsType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2026 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2027 | case TemplateArgument::Declaration: |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 2028 | return isSameDeclaration(X.getAsDecl(), Y.getAsDecl()); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2029 | |
| 2030 | case TemplateArgument::NullPtr: |
| 2031 | return Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2032 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2033 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2034 | case TemplateArgument::TemplateExpansion: |
| 2035 | return Context.getCanonicalTemplateName( |
| 2036 | X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() == |
| 2037 | Context.getCanonicalTemplateName( |
| 2038 | Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2039 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2040 | case TemplateArgument::Integral: |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 2041 | return X.getAsIntegral() == Y.getAsIntegral(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2042 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2043 | case TemplateArgument::Expression: { |
| 2044 | llvm::FoldingSetNodeID XID, YID; |
| 2045 | X.getAsExpr()->Profile(XID, Context, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2046 | Y.getAsExpr()->Profile(YID, Context, true); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2047 | return XID == YID; |
| 2048 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2049 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2050 | case TemplateArgument::Pack: |
| 2051 | if (X.pack_size() != Y.pack_size()) |
| 2052 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2053 | |
| 2054 | for (TemplateArgument::pack_iterator XP = X.pack_begin(), |
| 2055 | XPEnd = X.pack_end(), |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2056 | YP = Y.pack_begin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2057 | XP != XPEnd; ++XP, ++YP) |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2058 | if (!isSameTemplateArg(Context, *XP, *YP)) |
| 2059 | return false; |
| 2060 | |
| 2061 | return true; |
| 2062 | } |
| 2063 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2064 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2067 | /// \brief Allocate a TemplateArgumentLoc where all locations have |
| 2068 | /// been initialized to the given location. |
| 2069 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2070 | /// \param Arg The template argument we are producing template argument |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2071 | /// location information for. |
| 2072 | /// |
| 2073 | /// \param NTTPType For a declaration template argument, the type of |
| 2074 | /// the non-type template parameter that corresponds to this template |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame^] | 2075 | /// argument. Can be null if no type sugar is available to add to the |
| 2076 | /// type from the template argument. |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2077 | /// |
| 2078 | /// \param Loc The source location to use for the resulting template |
| 2079 | /// argument. |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2080 | TemplateArgumentLoc |
| 2081 | Sema::getTrivialTemplateArgumentLoc(const TemplateArgument &Arg, |
| 2082 | QualType NTTPType, SourceLocation Loc) { |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2083 | switch (Arg.getKind()) { |
| 2084 | case TemplateArgument::Null: |
| 2085 | llvm_unreachable("Can't get a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2086 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2087 | case TemplateArgument::Type: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2088 | return TemplateArgumentLoc( |
| 2089 | Arg, Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2090 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2091 | case TemplateArgument::Declaration: { |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame^] | 2092 | if (NTTPType.isNull()) |
| 2093 | NTTPType = Arg.getParamTypeForDecl(); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2094 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
| 2095 | .getAs<Expr>(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2096 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2097 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2098 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2099 | case TemplateArgument::NullPtr: { |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame^] | 2100 | if (NTTPType.isNull()) |
| 2101 | NTTPType = Arg.getNullPtrType(); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2102 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
| 2103 | .getAs<Expr>(); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2104 | return TemplateArgumentLoc(TemplateArgument(NTTPType, /*isNullPtr*/true), |
| 2105 | E); |
| 2106 | } |
| 2107 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2108 | case TemplateArgument::Integral: { |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2109 | Expr *E = |
| 2110 | BuildExpressionFromIntegralTemplateArgument(Arg, Loc).getAs<Expr>(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2111 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2112 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2113 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2114 | case TemplateArgument::Template: |
| 2115 | case TemplateArgument::TemplateExpansion: { |
| 2116 | NestedNameSpecifierLocBuilder Builder; |
| 2117 | TemplateName Template = Arg.getAsTemplate(); |
| 2118 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2119 | Builder.MakeTrivial(Context, DTN->getQualifier(), Loc); |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 2120 | else if (QualifiedTemplateName *QTN = |
| 2121 | Template.getAsQualifiedTemplateName()) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2122 | Builder.MakeTrivial(Context, QTN->getQualifier(), Loc); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2123 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2124 | if (Arg.getKind() == TemplateArgument::Template) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2125 | return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(Context), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2126 | Loc); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2127 | |
| 2128 | return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(Context), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2129 | Loc, Loc); |
| 2130 | } |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2131 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2132 | case TemplateArgument::Expression: |
| 2133 | return TemplateArgumentLoc(Arg, Arg.getAsExpr()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2134 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2135 | case TemplateArgument::Pack: |
| 2136 | return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo()); |
| 2137 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2138 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2139 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
| 2142 | |
| 2143 | /// \brief Convert the given deduced template argument and add it to the set of |
| 2144 | /// fully-converted template arguments. |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2145 | static bool |
| 2146 | ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param, |
| 2147 | DeducedTemplateArgument Arg, |
| 2148 | NamedDecl *Template, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2149 | TemplateDeductionInfo &Info, |
| 2150 | bool InFunctionTemplate, |
| 2151 | SmallVectorImpl<TemplateArgument> &Output) { |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2152 | auto ConvertArg = [&](DeducedTemplateArgument Arg, |
| 2153 | unsigned ArgumentPackIndex) { |
| 2154 | // Convert the deduced template argument into a template |
| 2155 | // argument that we can check, almost as if the user had written |
| 2156 | // the template argument explicitly. |
| 2157 | TemplateArgumentLoc ArgLoc = |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame^] | 2158 | S.getTrivialTemplateArgumentLoc(Arg, QualType(), Info.getLocation()); |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2159 | |
| 2160 | // Check the template argument, converting it as necessary. |
| 2161 | return S.CheckTemplateArgument( |
| 2162 | Param, ArgLoc, Template, Template->getLocation(), |
| 2163 | Template->getSourceRange().getEnd(), ArgumentPackIndex, Output, |
| 2164 | InFunctionTemplate |
| 2165 | ? (Arg.wasDeducedFromArrayBound() ? Sema::CTAK_DeducedFromArrayBound |
| 2166 | : Sema::CTAK_Deduced) |
| 2167 | : Sema::CTAK_Specified); |
| 2168 | }; |
| 2169 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2170 | if (Arg.getKind() == TemplateArgument::Pack) { |
| 2171 | // This is a template argument pack, so check each of its arguments against |
| 2172 | // the template parameter. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2173 | SmallVector<TemplateArgument, 2> PackedArgsBuilder; |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 2174 | for (const auto &P : Arg.pack_elements()) { |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2175 | // When converting the deduced template argument, append it to the |
| 2176 | // general output list. We need to do this so that the template argument |
| 2177 | // checking logic has all of the prior template arguments available. |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 2178 | DeducedTemplateArgument InnerArg(P); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2179 | InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound()); |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2180 | assert(InnerArg.getKind() != TemplateArgument::Pack && |
| 2181 | "deduced nested pack"); |
| 2182 | if (ConvertArg(InnerArg, PackedArgsBuilder.size())) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2183 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2184 | |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2185 | // Move the converted template argument into our argument pack. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 2186 | PackedArgsBuilder.push_back(Output.pop_back_val()); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2187 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2188 | |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2189 | // 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^] | 2190 | // itself, in case that substitution fails. |
| 2191 | if (PackedArgsBuilder.empty()) { |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2192 | LocalInstantiationScope Scope(S); |
Richard Smith | e824775 | 2016-12-22 07:24:39 +0000 | [diff] [blame] | 2193 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Output); |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame^] | 2194 | MultiLevelTemplateArgumentList Args(TemplateArgs); |
| 2195 | |
| 2196 | if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2197 | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
| 2198 | NTTP, Output, |
| 2199 | Template->getSourceRange()); |
| 2200 | if (Inst.isInvalid() || |
| 2201 | S.SubstType(NTTP->getType(), Args, NTTP->getLocation(), |
| 2202 | NTTP->getDeclName()).isNull()) |
| 2203 | return true; |
| 2204 | } else if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 2205 | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
| 2206 | TTP, Output, |
| 2207 | Template->getSourceRange()); |
| 2208 | if (Inst.isInvalid() || !S.SubstDecl(TTP, S.CurContext, Args)) |
| 2209 | return true; |
| 2210 | } |
| 2211 | // For type parameters, no substitution is ever required. |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2212 | } |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2213 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2214 | // Create the resulting argument pack. |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 2215 | Output.push_back( |
| 2216 | TemplateArgument::CreatePackCopy(S.Context, PackedArgsBuilder)); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2217 | return false; |
| 2218 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2219 | |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2220 | return ConvertArg(Arg, 0); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2223 | // FIXME: This should not be a template, but |
| 2224 | // ClassTemplatePartialSpecializationDecl sadly does not derive from |
| 2225 | // TemplateDecl. |
| 2226 | template<typename TemplateDeclT> |
| 2227 | static Sema::TemplateDeductionResult ConvertDeducedTemplateArguments( |
| 2228 | Sema &S, TemplateDeclT *Template, |
| 2229 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2230 | TemplateDeductionInfo &Info, SmallVectorImpl<TemplateArgument> &Builder, |
| 2231 | LocalInstantiationScope *CurrentInstantiationScope = nullptr, |
| 2232 | unsigned NumAlreadyConverted = 0, bool PartialOverloading = false) { |
| 2233 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| 2234 | |
| 2235 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 2236 | NamedDecl *Param = TemplateParams->getParam(I); |
| 2237 | |
| 2238 | if (!Deduced[I].isNull()) { |
| 2239 | if (I < NumAlreadyConverted) { |
| 2240 | // We have already fully type-checked and converted this |
| 2241 | // argument, because it was explicitly-specified. Just record the |
| 2242 | // presence of this argument. |
| 2243 | Builder.push_back(Deduced[I]); |
| 2244 | // We may have had explicitly-specified template arguments for a |
| 2245 | // template parameter pack (that may or may not have been extended |
| 2246 | // via additional deduced arguments). |
| 2247 | if (Param->isParameterPack() && CurrentInstantiationScope) { |
| 2248 | if (CurrentInstantiationScope->getPartiallySubstitutedPack() == |
| 2249 | Param) { |
| 2250 | // Forget the partially-substituted pack; its substitution is now |
| 2251 | // complete. |
| 2252 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2253 | } |
| 2254 | } |
| 2255 | continue; |
| 2256 | } |
| 2257 | |
| 2258 | // We have deduced this argument, so it still needs to be |
| 2259 | // checked and converted. |
| 2260 | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Template, Info, |
| 2261 | isa<FunctionTemplateDecl>(Template), |
| 2262 | Builder)) { |
| 2263 | Info.Param = makeTemplateParameter(Param); |
| 2264 | // FIXME: These template arguments are temporary. Free them! |
| 2265 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2266 | return Sema::TDK_SubstitutionFailure; |
| 2267 | } |
| 2268 | |
| 2269 | continue; |
| 2270 | } |
| 2271 | |
| 2272 | // C++0x [temp.arg.explicit]p3: |
| 2273 | // A trailing template parameter pack (14.5.3) not otherwise deduced will |
| 2274 | // be deduced to an empty sequence of template arguments. |
| 2275 | // FIXME: Where did the word "trailing" come from? |
| 2276 | if (Param->isTemplateParameterPack()) { |
| 2277 | // We may have had explicitly-specified template arguments for this |
| 2278 | // template parameter pack. If so, our empty deduction extends the |
| 2279 | // explicitly-specified set (C++0x [temp.arg.explicit]p9). |
| 2280 | const TemplateArgument *ExplicitArgs; |
| 2281 | unsigned NumExplicitArgs; |
| 2282 | if (CurrentInstantiationScope && |
| 2283 | CurrentInstantiationScope->getPartiallySubstitutedPack( |
| 2284 | &ExplicitArgs, &NumExplicitArgs) == Param) { |
| 2285 | Builder.push_back(TemplateArgument( |
| 2286 | llvm::makeArrayRef(ExplicitArgs, NumExplicitArgs))); |
| 2287 | |
| 2288 | // Forget the partially-substituted pack; its substitution is now |
| 2289 | // complete. |
| 2290 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2291 | } else { |
| 2292 | // Go through the motions of checking the empty argument pack against |
| 2293 | // the parameter pack. |
| 2294 | DeducedTemplateArgument DeducedPack(TemplateArgument::getEmptyPack()); |
| 2295 | if (ConvertDeducedTemplateArgument( |
| 2296 | S, Param, DeducedPack, Template, Info, |
| 2297 | isa<FunctionTemplateDecl>(Template), Builder)) { |
| 2298 | Info.Param = makeTemplateParameter(Param); |
| 2299 | // FIXME: These template arguments are temporary. Free them! |
| 2300 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2301 | return Sema::TDK_SubstitutionFailure; |
| 2302 | } |
| 2303 | } |
| 2304 | continue; |
| 2305 | } |
| 2306 | |
| 2307 | // Substitute into the default template argument, if available. |
| 2308 | bool HasDefaultArg = false; |
| 2309 | TemplateDecl *TD = dyn_cast<TemplateDecl>(Template); |
| 2310 | if (!TD) { |
| 2311 | assert(isa<ClassTemplatePartialSpecializationDecl>(Template)); |
| 2312 | return Sema::TDK_Incomplete; |
| 2313 | } |
| 2314 | |
| 2315 | TemplateArgumentLoc DefArg = S.SubstDefaultTemplateArgumentIfAvailable( |
| 2316 | TD, TD->getLocation(), TD->getSourceRange().getEnd(), Param, Builder, |
| 2317 | HasDefaultArg); |
| 2318 | |
| 2319 | // If there was no default argument, deduction is incomplete. |
| 2320 | if (DefArg.getArgument().isNull()) { |
| 2321 | Info.Param = makeTemplateParameter( |
| 2322 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
| 2323 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2324 | if (PartialOverloading) break; |
| 2325 | |
| 2326 | return HasDefaultArg ? Sema::TDK_SubstitutionFailure |
| 2327 | : Sema::TDK_Incomplete; |
| 2328 | } |
| 2329 | |
| 2330 | // Check whether we can actually use the default argument. |
| 2331 | if (S.CheckTemplateArgument(Param, DefArg, TD, TD->getLocation(), |
| 2332 | TD->getSourceRange().getEnd(), 0, Builder, |
| 2333 | Sema::CTAK_Specified)) { |
| 2334 | Info.Param = makeTemplateParameter( |
| 2335 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
| 2336 | // FIXME: These template arguments are temporary. Free them! |
| 2337 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2338 | return Sema::TDK_SubstitutionFailure; |
| 2339 | } |
| 2340 | |
| 2341 | // If we get here, we successfully used the default template argument. |
| 2342 | } |
| 2343 | |
| 2344 | return Sema::TDK_Success; |
| 2345 | } |
| 2346 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2347 | /// Complete template argument deduction for a class template partial |
| 2348 | /// specialization. |
| 2349 | static Sema::TemplateDeductionResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2350 | FinishTemplateArgumentDeduction(Sema &S, |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2351 | ClassTemplatePartialSpecializationDecl *Partial, |
| 2352 | const TemplateArgumentList &TemplateArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2353 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2354 | TemplateDeductionInfo &Info) { |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2355 | // Unevaluated SFINAE context. |
| 2356 | EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2357 | Sema::SFINAETrap Trap(S); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2358 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2359 | Sema::ContextRAII SavedContext(S, Partial); |
| 2360 | |
| 2361 | // C++ [temp.deduct.type]p2: |
| 2362 | // [...] or if any template argument remains neither deduced nor |
| 2363 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2364 | SmallVector<TemplateArgument, 4> Builder; |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2365 | if (auto Result = ConvertDeducedTemplateArguments(S, Partial, Deduced, |
| 2366 | Info, Builder)) |
| 2367 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2368 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2369 | // Form the template argument list from the deduced template arguments. |
| 2370 | TemplateArgumentList *DeducedArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2371 | = TemplateArgumentList::CreateCopy(S.Context, Builder); |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2372 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2373 | Info.reset(DeducedArgumentList); |
| 2374 | |
| 2375 | // Substitute the deduced template arguments into the template |
| 2376 | // arguments of the class template partial specialization, and |
| 2377 | // verify that the instantiated template arguments are both valid |
| 2378 | // and are equivalent to the template arguments originally provided |
| 2379 | // to the class template. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2380 | LocalInstantiationScope InstScope(S); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2381 | ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate(); |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2382 | const ASTTemplateArgumentListInfo *PartialTemplArgInfo |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2383 | = Partial->getTemplateArgsAsWritten(); |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2384 | const TemplateArgumentLoc *PartialTemplateArgs |
| 2385 | = PartialTemplArgInfo->getTemplateArgs(); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2386 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2387 | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, |
| 2388 | PartialTemplArgInfo->RAngleLoc); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2389 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2390 | if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs, |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2391 | InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { |
| 2392 | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; |
| 2393 | if (ParamIdx >= Partial->getTemplateParameters()->size()) |
| 2394 | ParamIdx = Partial->getTemplateParameters()->size() - 1; |
| 2395 | |
| 2396 | Decl *Param |
| 2397 | = const_cast<NamedDecl *>( |
| 2398 | Partial->getTemplateParameters()->getParam(ParamIdx)); |
| 2399 | Info.Param = makeTemplateParameter(Param); |
| 2400 | Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); |
| 2401 | return Sema::TDK_SubstitutionFailure; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2402 | } |
| 2403 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2404 | SmallVector<TemplateArgument, 4> ConvertedInstArgs; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2405 | if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(), |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2406 | InstArgs, false, ConvertedInstArgs)) |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2407 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2408 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2409 | TemplateParameterList *TemplateParams |
| 2410 | = ClassTemplate->getTemplateParameters(); |
| 2411 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2412 | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2413 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 2414 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2415 | Info.FirstArg = TemplateArgs[I]; |
| 2416 | Info.SecondArg = InstArg; |
| 2417 | return Sema::TDK_NonDeducedMismatch; |
| 2418 | } |
| 2419 | } |
| 2420 | |
| 2421 | if (Trap.hasErrorOccurred()) |
| 2422 | return Sema::TDK_SubstitutionFailure; |
| 2423 | |
| 2424 | return Sema::TDK_Success; |
| 2425 | } |
| 2426 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2427 | /// \brief Perform template argument deduction to determine whether |
Larisse Voufo | 833b05a | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 2428 | /// the given template arguments match the given class template |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2429 | /// partial specialization per C++ [temp.class.spec.match]. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2430 | Sema::TemplateDeductionResult |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2431 | Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2432 | const TemplateArgumentList &TemplateArgs, |
| 2433 | TemplateDeductionInfo &Info) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 2434 | if (Partial->isInvalidDecl()) |
| 2435 | return TDK_Invalid; |
| 2436 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2437 | // C++ [temp.class.spec.match]p2: |
| 2438 | // A partial specialization matches a given actual template |
| 2439 | // argument list if the template arguments of the partial |
| 2440 | // specialization can be deduced from the actual template argument |
| 2441 | // list (14.8.2). |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2442 | |
| 2443 | // Unevaluated SFINAE context. |
| 2444 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2445 | SFINAETrap Trap(*this); |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2446 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2447 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2448 | Deduced.resize(Partial->getTemplateParameters()->size()); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2449 | if (TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2450 | = ::DeduceTemplateArguments(*this, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2451 | Partial->getTemplateParameters(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2452 | Partial->getTemplateArgs(), |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2453 | TemplateArgs, Info, Deduced)) |
| 2454 | return Result; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 2455 | |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2456 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2457 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2458 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2459 | if (Inst.isInvalid()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2460 | return TDK_InstantiationDepth; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2461 | |
Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2462 | if (Trap.hasErrorOccurred()) |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2463 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2464 | |
| 2465 | return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs, |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2466 | Deduced, Info); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2467 | } |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2468 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2469 | /// Complete template argument deduction for a variable template partial |
| 2470 | /// specialization. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2471 | /// TODO: Unify with ClassTemplatePartialSpecializationDecl version? |
| 2472 | /// May require unifying ClassTemplate(Partial)SpecializationDecl and |
| 2473 | /// VarTemplate(Partial)SpecializationDecl with a new data |
| 2474 | /// structure Template(Partial)SpecializationDecl, and |
| 2475 | /// using Template(Partial)SpecializationDecl as input type. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2476 | static Sema::TemplateDeductionResult FinishTemplateArgumentDeduction( |
| 2477 | Sema &S, VarTemplatePartialSpecializationDecl *Partial, |
| 2478 | const TemplateArgumentList &TemplateArgs, |
| 2479 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2480 | TemplateDeductionInfo &Info) { |
| 2481 | // Unevaluated SFINAE context. |
| 2482 | EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated); |
| 2483 | Sema::SFINAETrap Trap(S); |
| 2484 | |
| 2485 | // C++ [temp.deduct.type]p2: |
| 2486 | // [...] or if any template argument remains neither deduced nor |
| 2487 | // explicitly specified, template argument deduction fails. |
| 2488 | SmallVector<TemplateArgument, 4> Builder; |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2489 | if (auto Result = ConvertDeducedTemplateArguments(S, Partial, Deduced, |
| 2490 | Info, Builder)) |
| 2491 | return Result; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2492 | |
| 2493 | // Form the template argument list from the deduced template arguments. |
| 2494 | TemplateArgumentList *DeducedArgumentList = TemplateArgumentList::CreateCopy( |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2495 | S.Context, Builder); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2496 | |
| 2497 | Info.reset(DeducedArgumentList); |
| 2498 | |
| 2499 | // Substitute the deduced template arguments into the template |
| 2500 | // arguments of the class template partial specialization, and |
| 2501 | // verify that the instantiated template arguments are both valid |
| 2502 | // and are equivalent to the template arguments originally provided |
| 2503 | // to the class template. |
| 2504 | LocalInstantiationScope InstScope(S); |
| 2505 | VarTemplateDecl *VarTemplate = Partial->getSpecializedTemplate(); |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2506 | const ASTTemplateArgumentListInfo *PartialTemplArgInfo |
| 2507 | = Partial->getTemplateArgsAsWritten(); |
| 2508 | const TemplateArgumentLoc *PartialTemplateArgs |
| 2509 | = PartialTemplArgInfo->getTemplateArgs(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2510 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2511 | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, |
| 2512 | PartialTemplArgInfo->RAngleLoc); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2513 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2514 | if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2515 | InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { |
| 2516 | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; |
| 2517 | if (ParamIdx >= Partial->getTemplateParameters()->size()) |
| 2518 | ParamIdx = Partial->getTemplateParameters()->size() - 1; |
| 2519 | |
| 2520 | Decl *Param = const_cast<NamedDecl *>( |
| 2521 | Partial->getTemplateParameters()->getParam(ParamIdx)); |
| 2522 | Info.Param = makeTemplateParameter(Param); |
| 2523 | Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); |
| 2524 | return Sema::TDK_SubstitutionFailure; |
| 2525 | } |
| 2526 | SmallVector<TemplateArgument, 4> ConvertedInstArgs; |
| 2527 | if (S.CheckTemplateArgumentList(VarTemplate, Partial->getLocation(), InstArgs, |
| 2528 | false, ConvertedInstArgs)) |
| 2529 | return Sema::TDK_SubstitutionFailure; |
| 2530 | |
| 2531 | TemplateParameterList *TemplateParams = VarTemplate->getTemplateParameters(); |
| 2532 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
| 2533 | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; |
| 2534 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { |
| 2535 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
| 2536 | Info.FirstArg = TemplateArgs[I]; |
| 2537 | Info.SecondArg = InstArg; |
| 2538 | return Sema::TDK_NonDeducedMismatch; |
| 2539 | } |
| 2540 | } |
| 2541 | |
| 2542 | if (Trap.hasErrorOccurred()) |
| 2543 | return Sema::TDK_SubstitutionFailure; |
| 2544 | |
| 2545 | return Sema::TDK_Success; |
| 2546 | } |
| 2547 | |
| 2548 | /// \brief Perform template argument deduction to determine whether |
| 2549 | /// the given template arguments match the given variable template |
| 2550 | /// partial specialization per C++ [temp.class.spec.match]. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2551 | /// TODO: Unify with ClassTemplatePartialSpecializationDecl version? |
| 2552 | /// May require unifying ClassTemplate(Partial)SpecializationDecl and |
| 2553 | /// VarTemplate(Partial)SpecializationDecl with a new data |
| 2554 | /// structure Template(Partial)SpecializationDecl, and |
| 2555 | /// using Template(Partial)SpecializationDecl as input type. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2556 | Sema::TemplateDeductionResult |
| 2557 | Sema::DeduceTemplateArguments(VarTemplatePartialSpecializationDecl *Partial, |
| 2558 | const TemplateArgumentList &TemplateArgs, |
| 2559 | TemplateDeductionInfo &Info) { |
| 2560 | if (Partial->isInvalidDecl()) |
| 2561 | return TDK_Invalid; |
| 2562 | |
| 2563 | // C++ [temp.class.spec.match]p2: |
| 2564 | // A partial specialization matches a given actual template |
| 2565 | // argument list if the template arguments of the partial |
| 2566 | // specialization can be deduced from the actual template argument |
| 2567 | // list (14.8.2). |
| 2568 | |
| 2569 | // Unevaluated SFINAE context. |
| 2570 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
| 2571 | SFINAETrap Trap(*this); |
| 2572 | |
| 2573 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 2574 | Deduced.resize(Partial->getTemplateParameters()->size()); |
| 2575 | if (TemplateDeductionResult Result = ::DeduceTemplateArguments( |
| 2576 | *this, Partial->getTemplateParameters(), Partial->getTemplateArgs(), |
| 2577 | TemplateArgs, Info, Deduced)) |
| 2578 | return Result; |
| 2579 | |
| 2580 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2581 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2582 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2583 | if (Inst.isInvalid()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2584 | return TDK_InstantiationDepth; |
| 2585 | |
| 2586 | if (Trap.hasErrorOccurred()) |
| 2587 | return Sema::TDK_SubstitutionFailure; |
| 2588 | |
| 2589 | return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs, |
| 2590 | Deduced, Info); |
| 2591 | } |
| 2592 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2593 | /// \brief Determine whether the given type T is a simple-template-id type. |
| 2594 | static bool isSimpleTemplateIdType(QualType T) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2595 | if (const TemplateSpecializationType *Spec |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2596 | = T->getAs<TemplateSpecializationType>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2597 | return Spec->getTemplateName().getAsTemplateDecl() != nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2598 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2599 | return false; |
| 2600 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2601 | |
| 2602 | /// \brief Substitute the explicitly-provided template arguments into the |
| 2603 | /// given function template according to C++ [temp.arg.explicit]. |
| 2604 | /// |
| 2605 | /// \param FunctionTemplate the function template into which the explicit |
| 2606 | /// template arguments will be substituted. |
| 2607 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2608 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2609 | /// arguments. |
| 2610 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2611 | /// \param Deduced the deduced template arguments, which will be populated |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2612 | /// with the converted and checked explicit template arguments. |
| 2613 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2614 | /// \param ParamTypes will be populated with the instantiated function |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2615 | /// parameters. |
| 2616 | /// |
| 2617 | /// \param FunctionType if non-NULL, the result type of the function template |
| 2618 | /// will also be instantiated and the pointed-to value will be updated with |
| 2619 | /// the instantiated function type. |
| 2620 | /// |
| 2621 | /// \param Info if substitution fails for any reason, this object will be |
| 2622 | /// populated with more information about the failure. |
| 2623 | /// |
| 2624 | /// \returns TDK_Success if substitution was successful, or some failure |
| 2625 | /// condition. |
| 2626 | Sema::TemplateDeductionResult |
| 2627 | Sema::SubstituteExplicitTemplateArguments( |
| 2628 | FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2629 | TemplateArgumentListInfo &ExplicitTemplateArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2630 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2631 | SmallVectorImpl<QualType> &ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2632 | QualType *FunctionType, |
| 2633 | TemplateDeductionInfo &Info) { |
| 2634 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 2635 | TemplateParameterList *TemplateParams |
| 2636 | = FunctionTemplate->getTemplateParameters(); |
| 2637 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2638 | if (ExplicitTemplateArgs.size() == 0) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2639 | // No arguments to substitute; just copy over the parameter types and |
| 2640 | // fill in the function type. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2641 | for (auto P : Function->parameters()) |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 2642 | ParamTypes.push_back(P->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2643 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2644 | if (FunctionType) |
| 2645 | *FunctionType = Function->getType(); |
| 2646 | return TDK_Success; |
| 2647 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2648 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2649 | // Unevaluated SFINAE context. |
| 2650 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2651 | SFINAETrap Trap(*this); |
| 2652 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2653 | // C++ [temp.arg.explicit]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2654 | // Template arguments that are present shall be specified in the |
| 2655 | // declaration order of their corresponding template-parameters. The |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2656 | // template argument list shall not specify more template-arguments than |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2657 | // there are corresponding template-parameters. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2658 | SmallVector<TemplateArgument, 4> Builder; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2659 | |
| 2660 | // Enter a new template instantiation context where we check the |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2661 | // explicitly-specified template arguments against this function template, |
| 2662 | // and then substitute them into the function parameter types. |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2663 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2664 | InstantiatingTemplate Inst(*this, Info.getLocation(), FunctionTemplate, |
| 2665 | DeducedArgs, |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2666 | ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution, |
| 2667 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2668 | if (Inst.isInvalid()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2669 | return TDK_InstantiationDepth; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2670 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2671 | if (CheckTemplateArgumentList(FunctionTemplate, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2672 | SourceLocation(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2673 | ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2674 | true, |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 2675 | Builder) || Trap.hasErrorOccurred()) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2676 | unsigned Index = Builder.size(); |
Douglas Gregor | 62c281a | 2010-05-09 01:26:06 +0000 | [diff] [blame] | 2677 | if (Index >= TemplateParams->size()) |
| 2678 | Index = TemplateParams->size() - 1; |
| 2679 | Info.Param = makeTemplateParameter(TemplateParams->getParam(Index)); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2680 | return TDK_InvalidExplicitArguments; |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 2681 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2682 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2683 | // Form the template argument list from the explicitly-specified |
| 2684 | // template arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2685 | TemplateArgumentList *ExplicitArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2686 | = TemplateArgumentList::CreateCopy(Context, Builder); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2687 | Info.reset(ExplicitArgumentList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2688 | |
John McCall | 036855a | 2010-10-12 19:40:14 +0000 | [diff] [blame] | 2689 | // Template argument deduction and the final substitution should be |
| 2690 | // done in the context of the templated declaration. Explicit |
| 2691 | // argument substitution, on the other hand, needs to happen in the |
| 2692 | // calling context. |
| 2693 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
| 2694 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2695 | // If we deduced template arguments for a template parameter pack, |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2696 | // note that the template argument pack is partially substituted and record |
| 2697 | // the explicit template arguments. They'll be used as part of deduction |
| 2698 | // for this template parameter pack. |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2699 | for (unsigned I = 0, N = Builder.size(); I != N; ++I) { |
| 2700 | const TemplateArgument &Arg = Builder[I]; |
| 2701 | if (Arg.getKind() == TemplateArgument::Pack) { |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2702 | CurrentInstantiationScope->SetPartiallySubstitutedPack( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2703 | TemplateParams->getParam(I), |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2704 | Arg.pack_begin(), |
| 2705 | Arg.pack_size()); |
| 2706 | break; |
| 2707 | } |
| 2708 | } |
| 2709 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2710 | const FunctionProtoType *Proto |
| 2711 | = Function->getType()->getAs<FunctionProtoType>(); |
| 2712 | assert(Proto && "Function template does not have a prototype?"); |
| 2713 | |
Richard Smith | 70b1304 | 2015-01-09 01:19:56 +0000 | [diff] [blame] | 2714 | // Isolate our substituted parameters from our caller. |
| 2715 | LocalInstantiationScope InstScope(*this, /*MergeWithOuterScope*/true); |
| 2716 | |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2717 | ExtParameterInfoBuilder ExtParamInfos; |
| 2718 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2719 | // Instantiate the types of each of the function parameters given the |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2720 | // explicitly-specified template arguments. If the function has a trailing |
| 2721 | // return type, substitute it after the arguments to ensure we substitute |
| 2722 | // in lexical order. |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2723 | if (Proto->hasTrailingReturn()) { |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2724 | if (SubstParmTypes(Function->getLocation(), Function->parameters(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2725 | Proto->getExtParameterInfosOrNull(), |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2726 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2727 | ParamTypes, /*params*/ nullptr, ExtParamInfos)) |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2728 | return TDK_SubstitutionFailure; |
| 2729 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2730 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2731 | // Instantiate the return type. |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2732 | QualType ResultType; |
| 2733 | { |
| 2734 | // C++11 [expr.prim.general]p3: |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2735 | // If a declaration declares a member function or member function |
| 2736 | // 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] | 2737 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2738 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2739 | // declarator. |
| 2740 | unsigned ThisTypeQuals = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2741 | CXXRecordDecl *ThisContext = nullptr; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2742 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
| 2743 | ThisContext = Method->getParent(); |
| 2744 | ThisTypeQuals = Method->getTypeQualifiers(); |
| 2745 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2746 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2747 | CXXThisScopeRAII ThisScope(*this, ThisContext, ThisTypeQuals, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2748 | getLangOpts().CPlusPlus11); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2749 | |
| 2750 | ResultType = |
| 2751 | SubstType(Proto->getReturnType(), |
| 2752 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 2753 | Function->getTypeSpecStartLoc(), Function->getDeclName()); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2754 | if (ResultType.isNull() || Trap.hasErrorOccurred()) |
| 2755 | return TDK_SubstitutionFailure; |
| 2756 | } |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2757 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2758 | // Instantiate the types of each of the function parameters given the |
| 2759 | // explicitly-specified template arguments if we didn't do so earlier. |
| 2760 | if (!Proto->hasTrailingReturn() && |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2761 | SubstParmTypes(Function->getLocation(), Function->parameters(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2762 | Proto->getExtParameterInfosOrNull(), |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2763 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2764 | ParamTypes, /*params*/ nullptr, ExtParamInfos)) |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2765 | return TDK_SubstitutionFailure; |
| 2766 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2767 | if (FunctionType) { |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2768 | auto EPI = Proto->getExtProtoInfo(); |
| 2769 | EPI.ExtParameterInfos = ExtParamInfos.getPointerOrNull(ParamTypes.size()); |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2770 | *FunctionType = BuildFunctionType(ResultType, ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2771 | Function->getLocation(), |
Eli Friedman | d8725a9 | 2010-08-05 02:54:05 +0000 | [diff] [blame] | 2772 | Function->getDeclName(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2773 | EPI); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2774 | if (FunctionType->isNull() || Trap.hasErrorOccurred()) |
| 2775 | return TDK_SubstitutionFailure; |
| 2776 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2777 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2778 | // C++ [temp.arg.explicit]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2779 | // Trailing template arguments that can be deduced (14.8.2) may be |
| 2780 | // omitted from the list of explicit template-arguments. If all of the |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2781 | // template arguments can be deduced, they may all be omitted; in this |
| 2782 | // case, the empty template argument list <> itself may also be omitted. |
| 2783 | // |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2784 | // Take all of the explicitly-specified arguments and put them into |
| 2785 | // the set of deduced template arguments. Explicitly-specified |
| 2786 | // parameter packs, however, will be set to NULL since the deduction |
| 2787 | // mechanisms handle explicitly-specified argument packs directly. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2788 | Deduced.reserve(TemplateParams->size()); |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2789 | for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) { |
| 2790 | const TemplateArgument &Arg = ExplicitArgumentList->get(I); |
| 2791 | if (Arg.getKind() == TemplateArgument::Pack) |
| 2792 | Deduced.push_back(DeducedTemplateArgument()); |
| 2793 | else |
| 2794 | Deduced.push_back(Arg); |
| 2795 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2796 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2797 | return TDK_Success; |
| 2798 | } |
| 2799 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2800 | /// \brief Check whether the deduced argument type for a call to a function |
| 2801 | /// template matches the actual argument type per C++ [temp.deduct.call]p4. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2802 | static bool |
| 2803 | CheckOriginalCallArgDeduction(Sema &S, Sema::OriginalCallArg OriginalArg, |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2804 | QualType DeducedA) { |
| 2805 | ASTContext &Context = S.Context; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2806 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2807 | QualType A = OriginalArg.OriginalArgType; |
| 2808 | QualType OriginalParamType = OriginalArg.OriginalParamType; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2809 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2810 | // Check for type equality (top-level cv-qualifiers are ignored). |
| 2811 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| 2812 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2813 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2814 | // Strip off references on the argument types; they aren't needed for |
| 2815 | // the following checks. |
| 2816 | if (const ReferenceType *DeducedARef = DeducedA->getAs<ReferenceType>()) |
| 2817 | DeducedA = DeducedARef->getPointeeType(); |
| 2818 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
| 2819 | A = ARef->getPointeeType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2820 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2821 | // C++ [temp.deduct.call]p4: |
| 2822 | // [...] However, there are three cases that allow a difference: |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2823 | // - If the original P is a reference type, the deduced A (i.e., the |
| 2824 | // type referred to by the reference) can be more cv-qualified than |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2825 | // the transformed A. |
| 2826 | if (const ReferenceType *OriginalParamRef |
| 2827 | = OriginalParamType->getAs<ReferenceType>()) { |
| 2828 | // We don't want to keep the reference around any more. |
| 2829 | OriginalParamType = OriginalParamRef->getPointeeType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2830 | |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 2831 | // FIXME: Resolve core issue (no number yet): if the original P is a |
| 2832 | // reference type and the transformed A is function type "noexcept F", |
| 2833 | // the deduced A can be F. |
| 2834 | QualType Tmp; |
| 2835 | if (A->isFunctionType() && S.IsFunctionConversion(A, DeducedA, Tmp)) |
| 2836 | return false; |
| 2837 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2838 | Qualifiers AQuals = A.getQualifiers(); |
| 2839 | Qualifiers DeducedAQuals = DeducedA.getQualifiers(); |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2840 | |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2841 | // Under Objective-C++ ARC, the deduced type may have implicitly |
| 2842 | // been given strong or (when dealing with a const reference) |
| 2843 | // unsafe_unretained lifetime. If so, update the original |
| 2844 | // qualifiers to include this lifetime. |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2845 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2846 | ((DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_Strong && |
| 2847 | AQuals.getObjCLifetime() == Qualifiers::OCL_None) || |
| 2848 | (DeducedAQuals.hasConst() && |
| 2849 | DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone))) { |
| 2850 | AQuals.setObjCLifetime(DeducedAQuals.getObjCLifetime()); |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2853 | if (AQuals == DeducedAQuals) { |
| 2854 | // Qualifiers match; there's nothing to do. |
| 2855 | } else if (!DeducedAQuals.compatiblyIncludes(AQuals)) { |
Douglas Gregor | ddaae52 | 2011-06-17 14:36:00 +0000 | [diff] [blame] | 2856 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2857 | } else { |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2858 | // Qualifiers are compatible, so have the argument type adopt the |
| 2859 | // deduced argument type's qualifiers as if we had performed the |
| 2860 | // qualification conversion. |
| 2861 | A = Context.getQualifiedType(A.getUnqualifiedType(), DeducedAQuals); |
| 2862 | } |
| 2863 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2864 | |
| 2865 | // - The transformed A can be another pointer or pointer to member |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 2866 | // type that can be converted to the deduced A via a function pointer |
| 2867 | // conversion and/or a qualification conversion. |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2868 | // |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 2869 | // Also allow conversions which merely strip __attribute__((noreturn)) from |
| 2870 | // function types (recursively). |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2871 | bool ObjCLifetimeConversion = false; |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2872 | QualType ResultTy; |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2873 | if ((A->isAnyPointerType() || A->isMemberPointerType()) && |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2874 | (S.IsQualificationConversion(A, DeducedA, false, |
| 2875 | ObjCLifetimeConversion) || |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 2876 | S.IsFunctionConversion(A, DeducedA, ResultTy))) |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2877 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2878 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2879 | // - 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] | 2880 | // transformed A can be a derived class of the deduced A. [...] |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2881 | // [...] Likewise, if P is a pointer to a class of the form |
| 2882 | // simple-template-id, the transformed A can be a pointer to a |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2883 | // derived class pointed to by the deduced A. |
| 2884 | if (const PointerType *OriginalParamPtr |
| 2885 | = OriginalParamType->getAs<PointerType>()) { |
| 2886 | if (const PointerType *DeducedAPtr = DeducedA->getAs<PointerType>()) { |
| 2887 | if (const PointerType *APtr = A->getAs<PointerType>()) { |
| 2888 | if (A->getPointeeType()->isRecordType()) { |
| 2889 | OriginalParamType = OriginalParamPtr->getPointeeType(); |
| 2890 | DeducedA = DeducedAPtr->getPointeeType(); |
| 2891 | A = APtr->getPointeeType(); |
| 2892 | } |
| 2893 | } |
| 2894 | } |
| 2895 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2896 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2897 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| 2898 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2899 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2900 | if (A->isRecordType() && isSimpleTemplateIdType(OriginalParamType) && |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 2901 | S.IsDerivedFrom(SourceLocation(), A, DeducedA)) |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2902 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2903 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2904 | return true; |
| 2905 | } |
| 2906 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2907 | /// \brief Finish template argument deduction for a function template, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2908 | /// checking the deduced template arguments for completeness and forming |
| 2909 | /// the function template specialization. |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2910 | /// |
| 2911 | /// \param OriginalCallArgs If non-NULL, the original call arguments against |
| 2912 | /// which the deduced argument types should be compared. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2913 | Sema::TemplateDeductionResult |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2914 | Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2915 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2916 | unsigned NumExplicitlySpecified, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2917 | FunctionDecl *&Specialization, |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2918 | TemplateDeductionInfo &Info, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 2919 | SmallVectorImpl<OriginalCallArg> const *OriginalCallArgs, |
| 2920 | bool PartialOverloading) { |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2921 | // Unevaluated SFINAE context. |
| 2922 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2923 | SFINAETrap Trap(*this); |
| 2924 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2925 | // Enter a new template instantiation context while we instantiate the |
| 2926 | // actual function declaration. |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2927 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2928 | InstantiatingTemplate Inst(*this, Info.getLocation(), FunctionTemplate, |
| 2929 | DeducedArgs, |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2930 | ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution, |
| 2931 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2932 | if (Inst.isInvalid()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2933 | return TDK_InstantiationDepth; |
| 2934 | |
John McCall | e23b871 | 2010-04-29 01:18:58 +0000 | [diff] [blame] | 2935 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
John McCall | 80e58cd | 2010-04-29 00:35:03 +0000 | [diff] [blame] | 2936 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2937 | // C++ [temp.deduct.type]p2: |
| 2938 | // [...] or if any template argument remains neither deduced nor |
| 2939 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2940 | SmallVector<TemplateArgument, 4> Builder; |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2941 | if (auto Result = ConvertDeducedTemplateArguments( |
| 2942 | *this, FunctionTemplate, Deduced, Info, Builder, |
| 2943 | CurrentInstantiationScope, NumExplicitlySpecified, |
| 2944 | PartialOverloading)) |
| 2945 | return Result; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2946 | |
| 2947 | // Form the template argument list from the deduced template arguments. |
| 2948 | TemplateArgumentList *DeducedArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2949 | = TemplateArgumentList::CreateCopy(Context, Builder); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2950 | Info.reset(DeducedArgumentList); |
| 2951 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2952 | // Substitute the deduced template arguments into the function template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2953 | // declaration to produce the function template specialization. |
Douglas Gregor | 1614237 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 2954 | DeclContext *Owner = FunctionTemplate->getDeclContext(); |
| 2955 | if (FunctionTemplate->getFriendObjectKind()) |
| 2956 | Owner = FunctionTemplate->getLexicalDeclContext(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2957 | Specialization = cast_or_null<FunctionDecl>( |
Douglas Gregor | 1614237 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 2958 | SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner, |
Douglas Gregor | 39cacdb | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 2959 | MultiLevelTemplateArgumentList(*DeducedArgumentList))); |
Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 2960 | if (!Specialization || Specialization->isInvalidDecl()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2961 | return TDK_SubstitutionFailure; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2962 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2963 | assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == |
Douglas Gregor | 31fae89 | 2009-09-15 18:26:13 +0000 | [diff] [blame] | 2964 | FunctionTemplate->getCanonicalDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2965 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2966 | // If the template argument list is owned by the function template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2967 | // specialization, release it. |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 2968 | if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList && |
| 2969 | !Trap.hasErrorOccurred()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2970 | Info.take(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2971 | |
Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 2972 | // There may have been an error that did not prevent us from constructing a |
| 2973 | // declaration. Mark the declaration invalid and return with a substitution |
| 2974 | // failure. |
| 2975 | if (Trap.hasErrorOccurred()) { |
| 2976 | Specialization->setInvalidDecl(true); |
| 2977 | return TDK_SubstitutionFailure; |
| 2978 | } |
| 2979 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2980 | if (OriginalCallArgs) { |
| 2981 | // C++ [temp.deduct.call]p4: |
| 2982 | // In general, the deduction process attempts to find template argument |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2983 | // 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] | 2984 | // is transformed as described above). [...] |
| 2985 | for (unsigned I = 0, N = OriginalCallArgs->size(); I != N; ++I) { |
| 2986 | OriginalCallArg OriginalArg = (*OriginalCallArgs)[I]; |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2987 | unsigned ParamIdx = OriginalArg.ArgIdx; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2988 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2989 | if (ParamIdx >= Specialization->getNumParams()) |
| 2990 | continue; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2991 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2992 | QualType DeducedA = Specialization->getParamDecl(ParamIdx)->getType(); |
Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 2993 | if (CheckOriginalCallArgDeduction(*this, OriginalArg, DeducedA)) { |
| 2994 | Info.FirstArg = TemplateArgument(DeducedA); |
| 2995 | Info.SecondArg = TemplateArgument(OriginalArg.OriginalArgType); |
| 2996 | Info.CallArgIndex = OriginalArg.ArgIdx; |
| 2997 | return TDK_DeducedMismatch; |
| 2998 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2999 | } |
| 3000 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3001 | |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3002 | // If we suppressed any diagnostics while performing template argument |
| 3003 | // deduction, and if we haven't already instantiated this declaration, |
| 3004 | // keep track of these diagnostics. They'll be emitted if this specialization |
| 3005 | // is actually used. |
| 3006 | if (Info.diag_begin() != Info.diag_end()) { |
Craig Topper | 79be4cd | 2013-07-05 04:33:53 +0000 | [diff] [blame] | 3007 | SuppressedDiagnosticsMap::iterator |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3008 | Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl()); |
| 3009 | if (Pos == SuppressedDiagnostics.end()) |
| 3010 | SuppressedDiagnostics[Specialization->getCanonicalDecl()] |
| 3011 | .append(Info.diag_begin(), Info.diag_end()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3012 | } |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3013 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3014 | return TDK_Success; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3015 | } |
| 3016 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3017 | /// Gets the type of a function for template-argument-deducton |
| 3018 | /// purposes when it's considered as part of an overload set. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3019 | static QualType GetTypeOfFunction(Sema &S, const OverloadExpr::FindResult &R, |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3020 | FunctionDecl *Fn) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3021 | // We may need to deduce the return type of the function now. |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3022 | if (S.getLangOpts().CPlusPlus14 && Fn->getReturnType()->isUndeducedType() && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3023 | S.DeduceReturnType(Fn, R.Expression->getExprLoc(), /*Diagnose*/ false)) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3024 | return QualType(); |
| 3025 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3026 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3027 | if (Method->isInstance()) { |
| 3028 | // An instance method that's referenced in a form that doesn't |
| 3029 | // look like a member pointer is just invalid. |
| 3030 | if (!R.HasFormOfMemberPointer) return QualType(); |
| 3031 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3032 | return S.Context.getMemberPointerType(Fn->getType(), |
| 3033 | S.Context.getTypeDeclType(Method->getParent()).getTypePtr()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | if (!R.IsAddressOfOperand) return Fn->getType(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3037 | return S.Context.getPointerType(Fn->getType()); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3038 | } |
| 3039 | |
| 3040 | /// Apply the deduction rules for overload sets. |
| 3041 | /// |
| 3042 | /// \return the null type if this argument should be treated as an |
| 3043 | /// undeduced context |
| 3044 | static QualType |
| 3045 | ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams, |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3046 | Expr *Arg, QualType ParamType, |
| 3047 | bool ParamWasReference) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3048 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3049 | OverloadExpr::FindResult R = OverloadExpr::find(Arg); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3050 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3051 | OverloadExpr *Ovl = R.Expression; |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3052 | |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3053 | // C++0x [temp.deduct.call]p4 |
| 3054 | unsigned TDF = 0; |
| 3055 | if (ParamWasReference) |
| 3056 | TDF |= TDF_ParamWithReferenceType; |
| 3057 | if (R.IsAddressOfOperand) |
| 3058 | TDF |= TDF_IgnoreQualifiers; |
| 3059 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3060 | // C++0x [temp.deduct.call]p6: |
| 3061 | // When P is a function type, pointer to function type, or pointer |
| 3062 | // to member function type: |
| 3063 | |
| 3064 | if (!ParamType->isFunctionType() && |
| 3065 | !ParamType->isFunctionPointerType() && |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3066 | !ParamType->isMemberFunctionPointerType()) { |
| 3067 | if (Ovl->hasExplicitTemplateArgs()) { |
| 3068 | // But we can still look for an explicit specialization. |
| 3069 | if (FunctionDecl *ExplicitSpec |
| 3070 | = S.ResolveSingleFunctionTemplateSpecialization(Ovl)) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3071 | return GetTypeOfFunction(S, R, ExplicitSpec); |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3072 | } |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3073 | |
George Burgess IV | cc2f355 | 2016-03-19 21:51:45 +0000 | [diff] [blame] | 3074 | DeclAccessPair DAP; |
| 3075 | if (FunctionDecl *Viable = |
| 3076 | S.resolveAddressOfOnlyViableOverloadCandidate(Arg, DAP)) |
| 3077 | return GetTypeOfFunction(S, R, Viable); |
| 3078 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3079 | return QualType(); |
| 3080 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3081 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3082 | // Gather the explicit template arguments, if any. |
| 3083 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 3084 | if (Ovl->hasExplicitTemplateArgs()) |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 3085 | Ovl->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3086 | QualType Match; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 3087 | for (UnresolvedSetIterator I = Ovl->decls_begin(), |
| 3088 | E = Ovl->decls_end(); I != E; ++I) { |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3089 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 3090 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3091 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) { |
| 3092 | // - If the argument is an overload set containing one or more |
| 3093 | // function templates, the parameter is treated as a |
| 3094 | // non-deduced context. |
| 3095 | if (!Ovl->hasExplicitTemplateArgs()) |
| 3096 | return QualType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3097 | |
| 3098 | // Otherwise, see if we can resolve a function type |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3099 | FunctionDecl *Specialization = nullptr; |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3100 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3101 | if (S.DeduceTemplateArguments(FunTmpl, &ExplicitTemplateArgs, |
| 3102 | Specialization, Info)) |
| 3103 | continue; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3104 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3105 | D = Specialization; |
| 3106 | } |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3107 | |
| 3108 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3109 | QualType ArgType = GetTypeOfFunction(S, R, Fn); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3110 | if (ArgType.isNull()) continue; |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3111 | |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3112 | // Function-to-pointer conversion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3113 | if (!ParamWasReference && ParamType->isPointerType() && |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3114 | ArgType->isFunctionType()) |
| 3115 | ArgType = S.Context.getPointerType(ArgType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3116 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3117 | // - If the argument is an overload set (not containing function |
| 3118 | // templates), trial argument deduction is attempted using each |
| 3119 | // of the members of the set. If deduction succeeds for only one |
| 3120 | // of the overload set members, that member is used as the |
| 3121 | // argument value for the deduction. If deduction succeeds for |
| 3122 | // more than one member of the overload set the parameter is |
| 3123 | // treated as a non-deduced context. |
| 3124 | |
| 3125 | // We do all of this in a fresh context per C++0x [temp.deduct.type]p2: |
| 3126 | // Type deduction is done independently for each P/A pair, and |
| 3127 | // the deduced template argument values are then combined. |
| 3128 | // So we do not reject deductions which were made elsewhere. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3129 | SmallVector<DeducedTemplateArgument, 8> |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3130 | Deduced(TemplateParams->size()); |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3131 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3132 | Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3133 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
| 3134 | ArgType, Info, Deduced, TDF); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3135 | if (Result) continue; |
| 3136 | if (!Match.isNull()) return QualType(); |
| 3137 | Match = ArgType; |
| 3138 | } |
| 3139 | |
| 3140 | return Match; |
| 3141 | } |
| 3142 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3143 | /// \brief Perform the adjustments to the parameter and argument types |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3144 | /// described in C++ [temp.deduct.call]. |
| 3145 | /// |
| 3146 | /// \returns true if the caller should not attempt to perform any template |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3147 | /// argument deduction based on this P/A pair because the argument is an |
| 3148 | /// overloaded function set that could not be resolved. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3149 | static bool AdjustFunctionParmAndArgTypesForDeduction(Sema &S, |
| 3150 | TemplateParameterList *TemplateParams, |
| 3151 | QualType &ParamType, |
| 3152 | QualType &ArgType, |
| 3153 | Expr *Arg, |
| 3154 | unsigned &TDF) { |
| 3155 | // C++0x [temp.deduct.call]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3156 | // 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] | 3157 | // are ignored for type deduction. |
Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3158 | if (ParamType.hasQualifiers()) |
| 3159 | ParamType = ParamType.getUnqualifiedType(); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3160 | |
| 3161 | // [...] If P is a reference type, the type referred to by P is |
| 3162 | // used for type deduction. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3163 | const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>(); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3164 | if (ParamRefType) |
| 3165 | ParamType = ParamRefType->getPointeeType(); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3166 | |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3167 | // Overload sets usually make this parameter an undeduced context, |
| 3168 | // but there are sometimes special circumstances. Typically |
| 3169 | // involving a template-id-expr. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3170 | if (ArgType == S.Context.OverloadTy) { |
| 3171 | ArgType = ResolveOverloadForDeduction(S, TemplateParams, |
| 3172 | Arg, ParamType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3173 | ParamRefType != nullptr); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3174 | if (ArgType.isNull()) |
| 3175 | return true; |
| 3176 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3177 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3178 | if (ParamRefType) { |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3179 | // If the argument has incomplete array type, try to complete its type. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3180 | if (ArgType->isIncompleteArrayType()) { |
| 3181 | S.completeExprArrayBound(Arg); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3182 | ArgType = Arg->getType(); |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3183 | } |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3184 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3185 | // C++0x [temp.deduct.call]p3: |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3186 | // If P is an rvalue reference to a cv-unqualified template |
| 3187 | // parameter and the argument is an lvalue, the type "lvalue |
| 3188 | // reference to A" is used in place of A for type deduction. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3189 | if (ParamRefType->isRValueReferenceType() && |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3190 | !ParamType.getQualifiers() && |
| 3191 | isa<TemplateTypeParmType>(ParamType) && |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3192 | Arg->isLValue()) |
| 3193 | ArgType = S.Context.getLValueReferenceType(ArgType); |
| 3194 | } else { |
| 3195 | // C++ [temp.deduct.call]p2: |
| 3196 | // If P is not a reference type: |
| 3197 | // - If A is an array type, the pointer type produced by the |
| 3198 | // array-to-pointer standard conversion (4.2) is used in place of |
| 3199 | // A for type deduction; otherwise, |
| 3200 | if (ArgType->isArrayType()) |
| 3201 | ArgType = S.Context.getArrayDecayedType(ArgType); |
| 3202 | // - If A is a function type, the pointer type produced by the |
| 3203 | // function-to-pointer standard conversion (4.3) is used in place |
| 3204 | // of A for type deduction; otherwise, |
| 3205 | else if (ArgType->isFunctionType()) |
| 3206 | ArgType = S.Context.getPointerType(ArgType); |
| 3207 | else { |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3208 | // - 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] | 3209 | // type are ignored for type deduction. |
Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3210 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3211 | } |
| 3212 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3213 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3214 | // C++0x [temp.deduct.call]p4: |
| 3215 | // In general, the deduction process attempts to find template argument |
| 3216 | // values that will make the deduced A identical to A (after the type A |
| 3217 | // is transformed as described above). [...] |
| 3218 | TDF = TDF_SkipNonDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3219 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3220 | // - If the original P is a reference type, the deduced A (i.e., the |
| 3221 | // type referred to by the reference) can be more cv-qualified than |
| 3222 | // the transformed A. |
| 3223 | if (ParamRefType) |
| 3224 | TDF |= TDF_ParamWithReferenceType; |
| 3225 | // - The transformed A can be another pointer or pointer to member |
| 3226 | // type that can be converted to the deduced A via a qualification |
| 3227 | // conversion (4.4). |
| 3228 | if (ArgType->isPointerType() || ArgType->isMemberPointerType() || |
| 3229 | ArgType->isObjCObjectPointerType()) |
| 3230 | TDF |= TDF_IgnoreQualifiers; |
| 3231 | // - If P is a class and P has the form simple-template-id, then the |
| 3232 | // transformed A can be a derived class of the deduced A. Likewise, |
| 3233 | // if P is a pointer to a class of the form simple-template-id, the |
| 3234 | // transformed A can be a pointer to a derived class pointed to by |
| 3235 | // the deduced A. |
| 3236 | if (isSimpleTemplateIdType(ParamType) || |
| 3237 | (isa<PointerType>(ParamType) && |
| 3238 | isSimpleTemplateIdType( |
| 3239 | ParamType->getAs<PointerType>()->getPointeeType()))) |
| 3240 | TDF |= TDF_DerivedClass; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3241 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3242 | return false; |
| 3243 | } |
| 3244 | |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3245 | static bool |
| 3246 | hasDeducibleTemplateParameters(Sema &S, FunctionTemplateDecl *FunctionTemplate, |
| 3247 | QualType T); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3248 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3249 | static Sema::TemplateDeductionResult DeduceTemplateArgumentByListElement( |
| 3250 | Sema &S, TemplateParameterList *TemplateParams, QualType ParamType, |
| 3251 | Expr *Arg, TemplateDeductionInfo &Info, |
| 3252 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, unsigned TDF); |
| 3253 | |
| 3254 | /// \brief Attempt template argument deduction from an initializer list |
| 3255 | /// deemed to be an argument in a function call. |
| 3256 | static bool |
| 3257 | DeduceFromInitializerList(Sema &S, TemplateParameterList *TemplateParams, |
| 3258 | QualType AdjustedParamType, InitListExpr *ILE, |
| 3259 | TemplateDeductionInfo &Info, |
| 3260 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3261 | unsigned TDF, Sema::TemplateDeductionResult &Result) { |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3262 | |
| 3263 | // [temp.deduct.call] p1 (post CWG-1591) |
| 3264 | // If removing references and cv-qualifiers from P gives |
| 3265 | // std::initializer_list<P0> or P0[N] for some P0 and N and the argument is a |
| 3266 | // non-empty initializer list (8.5.4), then deduction is performed instead for |
| 3267 | // each element of the initializer list, taking P0 as a function template |
| 3268 | // parameter type and the initializer element as its argument, and in the |
| 3269 | // P0[N] case, if N is a non-type template parameter, N is deduced from the |
| 3270 | // length of the initializer list. Otherwise, an initializer list argument |
| 3271 | // causes the parameter to be considered a non-deduced context |
| 3272 | |
| 3273 | const bool IsConstSizedArray = AdjustedParamType->isConstantArrayType(); |
| 3274 | |
| 3275 | const bool IsDependentSizedArray = |
| 3276 | !IsConstSizedArray && AdjustedParamType->isDependentSizedArrayType(); |
| 3277 | |
Faisal Vali | dd76cc1 | 2015-12-10 12:29:11 +0000 | [diff] [blame] | 3278 | 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] | 3279 | |
| 3280 | const bool IsSTDList = !IsConstSizedArray && !IsDependentSizedArray && |
| 3281 | S.isStdInitializerList(AdjustedParamType, &ElTy); |
| 3282 | |
| 3283 | if (!IsConstSizedArray && !IsDependentSizedArray && !IsSTDList) |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3284 | return false; |
| 3285 | |
| 3286 | Result = Sema::TDK_Success; |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3287 | // If we are not deducing against the 'T' in a std::initializer_list<T> then |
| 3288 | // deduce against the 'T' in T[N]. |
| 3289 | if (ElTy.isNull()) { |
| 3290 | assert(!IsSTDList); |
| 3291 | ElTy = S.Context.getAsArrayType(AdjustedParamType)->getElementType(); |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3292 | } |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3293 | // Deduction only needs to be done for dependent types. |
| 3294 | if (ElTy->isDependentType()) { |
| 3295 | for (Expr *E : ILE->inits()) { |
Craig Topper | 0852953 | 2015-12-10 08:49:55 +0000 | [diff] [blame] | 3296 | if ((Result = DeduceTemplateArgumentByListElement(S, TemplateParams, ElTy, |
| 3297 | E, Info, Deduced, TDF))) |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3298 | return true; |
| 3299 | } |
| 3300 | } |
| 3301 | if (IsDependentSizedArray) { |
| 3302 | const DependentSizedArrayType *ArrTy = |
| 3303 | S.Context.getAsDependentSizedArrayType(AdjustedParamType); |
| 3304 | // Determine the array bound is something we can deduce. |
| 3305 | if (NonTypeTemplateParmDecl *NTTP = |
| 3306 | getDeducedParameterFromExpr(ArrTy->getSizeExpr())) { |
| 3307 | // We can perform template argument deduction for the given non-type |
| 3308 | // template parameter. |
| 3309 | assert(NTTP->getDepth() == 0 && |
| 3310 | "Cannot deduce non-type template argument at depth > 0"); |
| 3311 | llvm::APInt Size(S.Context.getIntWidth(NTTP->getType()), |
| 3312 | ILE->getNumInits()); |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3313 | |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3314 | Result = DeduceNonTypeTemplateArgument( |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 3315 | S, TemplateParams, NTTP, llvm::APSInt(Size), NTTP->getType(), |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3316 | /*ArrayBound=*/true, Info, Deduced); |
| 3317 | } |
| 3318 | } |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3319 | return true; |
| 3320 | } |
| 3321 | |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3322 | /// \brief Perform template argument deduction by matching a parameter type |
| 3323 | /// against a single expression, where the expression is an element of |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3324 | /// an initializer list that was originally matched against a parameter |
| 3325 | /// of type \c initializer_list\<ParamType\>. |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3326 | static Sema::TemplateDeductionResult |
| 3327 | DeduceTemplateArgumentByListElement(Sema &S, |
| 3328 | TemplateParameterList *TemplateParams, |
| 3329 | QualType ParamType, Expr *Arg, |
| 3330 | TemplateDeductionInfo &Info, |
| 3331 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3332 | unsigned TDF) { |
| 3333 | // Handle the case where an init list contains another init list as the |
| 3334 | // element. |
| 3335 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3336 | Sema::TemplateDeductionResult Result; |
| 3337 | if (!DeduceFromInitializerList(S, TemplateParams, |
| 3338 | ParamType.getNonReferenceType(), ILE, Info, |
| 3339 | Deduced, TDF, Result)) |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3340 | return Sema::TDK_Success; // Just ignore this expression. |
| 3341 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3342 | return Result; |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3343 | } |
| 3344 | |
| 3345 | // For all other cases, just match by type. |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3346 | QualType ArgType = Arg->getType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3347 | if (AdjustFunctionParmAndArgTypesForDeduction(S, TemplateParams, ParamType, |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3348 | ArgType, Arg, TDF)) { |
| 3349 | Info.Expression = Arg; |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3350 | return Sema::TDK_FailedOverloadResolution; |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3351 | } |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3352 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3353 | ArgType, Info, Deduced, TDF); |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3354 | } |
| 3355 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3356 | /// \brief Perform template argument deduction from a function call |
| 3357 | /// (C++ [temp.deduct.call]). |
| 3358 | /// |
| 3359 | /// \param FunctionTemplate the function template for which we are performing |
| 3360 | /// template argument deduction. |
| 3361 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3362 | /// \param ExplicitTemplateArgs the explicit template arguments provided |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 3363 | /// for this call. |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3364 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3365 | /// \param Args the function call arguments |
| 3366 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3367 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3368 | /// this will be set to the function template specialization produced by |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3369 | /// template argument deduction. |
| 3370 | /// |
| 3371 | /// \param Info the argument will be updated to provide additional information |
| 3372 | /// about template argument deduction. |
| 3373 | /// |
| 3374 | /// \returns the result of template argument deduction. |
Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 3375 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3376 | FunctionTemplateDecl *FunctionTemplate, |
| 3377 | TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3378 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 3379 | bool PartialOverloading) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3380 | if (FunctionTemplate->isInvalidDecl()) |
| 3381 | return TDK_Invalid; |
| 3382 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3383 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3384 | unsigned NumParams = Function->getNumParams(); |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3385 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3386 | // C++ [temp.deduct.call]p1: |
| 3387 | // Template argument deduction is done by comparing each function template |
| 3388 | // parameter type (call it P) with the type of the corresponding argument |
| 3389 | // of the call (call it A) as described below. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3390 | unsigned CheckArgs = Args.size(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3391 | if (Args.size() < Function->getMinRequiredArguments() && !PartialOverloading) |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3392 | return TDK_TooFewArguments; |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3393 | else if (TooManyArguments(NumParams, Args.size(), PartialOverloading)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3394 | const FunctionProtoType *Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3395 | = Function->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3396 | if (Proto->isTemplateVariadic()) |
| 3397 | /* Do nothing */; |
| 3398 | else if (Proto->isVariadic()) |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3399 | CheckArgs = NumParams; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3400 | else |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3401 | return TDK_TooManyArguments; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3402 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3403 | |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3404 | // The types of the parameters from which we will perform template argument |
| 3405 | // deduction. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3406 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3407 | TemplateParameterList *TemplateParams |
| 3408 | = FunctionTemplate->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3409 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 3410 | SmallVector<QualType, 4> ParamTypes; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3411 | unsigned NumExplicitlySpecified = 0; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3412 | if (ExplicitTemplateArgs) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3413 | TemplateDeductionResult Result = |
| 3414 | SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3415 | *ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3416 | Deduced, |
| 3417 | ParamTypes, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3418 | nullptr, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3419 | Info); |
| 3420 | if (Result) |
| 3421 | return Result; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3422 | |
| 3423 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3424 | } else { |
| 3425 | // Just fill in the parameter types from the function declaration. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3426 | for (unsigned I = 0; I != NumParams; ++I) |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3427 | ParamTypes.push_back(Function->getParamDecl(I)->getType()); |
| 3428 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3429 | |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3430 | // Deduce template arguments from the function parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3431 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3432 | unsigned ArgIdx = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3433 | SmallVector<OriginalCallArg, 4> OriginalCallArgs; |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3434 | for (unsigned ParamIdx = 0, NumParamTypes = ParamTypes.size(); |
| 3435 | ParamIdx != NumParamTypes; ++ParamIdx) { |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3436 | QualType OrigParamType = ParamTypes[ParamIdx]; |
| 3437 | QualType ParamType = OrigParamType; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3438 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3439 | const PackExpansionType *ParamExpansion |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3440 | = dyn_cast<PackExpansionType>(ParamType); |
| 3441 | if (!ParamExpansion) { |
| 3442 | // Simple case: matching a function parameter to a function argument. |
| 3443 | if (ArgIdx >= CheckArgs) |
| 3444 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3445 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3446 | Expr *Arg = Args[ArgIdx++]; |
| 3447 | QualType ArgType = Arg->getType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3448 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3449 | unsigned TDF = 0; |
| 3450 | if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams, |
| 3451 | ParamType, ArgType, Arg, |
| 3452 | TDF)) |
| 3453 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3454 | |
Douglas Gregor | 0c83c81 | 2011-10-09 22:06:46 +0000 | [diff] [blame] | 3455 | // If we have nothing to deduce, we're done. |
| 3456 | if (!hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
| 3457 | continue; |
| 3458 | |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3459 | // If the argument is an initializer list ... |
| 3460 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3461 | TemplateDeductionResult Result; |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3462 | // Removing references was already done. |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3463 | if (!DeduceFromInitializerList(*this, TemplateParams, ParamType, ILE, |
| 3464 | Info, Deduced, TDF, Result)) |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3465 | continue; |
| 3466 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3467 | if (Result) |
| 3468 | return Result; |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3469 | // Don't track the argument type, since an initializer list has none. |
| 3470 | continue; |
| 3471 | } |
| 3472 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3473 | // Keep track of the argument type and corresponding parameter index, |
| 3474 | // so we can check for compatibility between the deduced A and A. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3475 | OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx-1, |
Douglas Gregor | 0c83c81 | 2011-10-09 22:06:46 +0000 | [diff] [blame] | 3476 | ArgType)); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3477 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3478 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3479 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3480 | ParamType, ArgType, |
| 3481 | Info, Deduced, TDF)) |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3482 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3483 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3484 | continue; |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3485 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3486 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3487 | // C++0x [temp.deduct.call]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3488 | // For a function parameter pack that occurs at the end of the |
| 3489 | // parameter-declaration-list, the type A of each remaining argument of |
| 3490 | // the call is compared with the type P of the declarator-id of the |
| 3491 | // function parameter pack. Each comparison deduces template arguments |
| 3492 | // for subsequent positions in the template parameter packs expanded by |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3493 | // the function parameter pack. For a function parameter pack that does |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3494 | // 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] | 3495 | // the parameter pack is a non-deduced context. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3496 | if (ParamIdx + 1 < NumParamTypes) |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3497 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3498 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3499 | QualType ParamPattern = ParamExpansion->getPattern(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3500 | PackDeductionScope PackScope(*this, TemplateParams, Deduced, Info, |
| 3501 | ParamPattern); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3502 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3503 | bool HasAnyArguments = false; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3504 | for (; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3505 | HasAnyArguments = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3506 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3507 | QualType OrigParamType = ParamPattern; |
| 3508 | ParamType = OrigParamType; |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3509 | Expr *Arg = Args[ArgIdx]; |
| 3510 | QualType ArgType = Arg->getType(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3511 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3512 | unsigned TDF = 0; |
| 3513 | if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams, |
| 3514 | ParamType, ArgType, Arg, |
| 3515 | TDF)) { |
| 3516 | // We can't actually perform any deduction for this argument, so stop |
| 3517 | // deduction at this point. |
| 3518 | ++ArgIdx; |
| 3519 | break; |
| 3520 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3521 | |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3522 | // As above, initializer lists need special handling. |
| 3523 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3524 | TemplateDeductionResult Result; |
| 3525 | if (!DeduceFromInitializerList(*this, TemplateParams, ParamType, ILE, |
| 3526 | Info, Deduced, TDF, Result)) { |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3527 | ++ArgIdx; |
| 3528 | break; |
| 3529 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3530 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3531 | if (Result) |
| 3532 | return Result; |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3533 | } else { |
| 3534 | |
| 3535 | // Keep track of the argument type and corresponding argument index, |
| 3536 | // so we can check for compatibility between the deduced A and A. |
| 3537 | if (hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3538 | OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx, |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3539 | ArgType)); |
| 3540 | |
| 3541 | if (TemplateDeductionResult Result |
| 3542 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3543 | ParamType, ArgType, Info, |
| 3544 | Deduced, TDF)) |
| 3545 | return Result; |
| 3546 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3547 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3548 | PackScope.nextPackElement(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3549 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3550 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3551 | // Build argument packs for each of the parameter packs expanded by this |
| 3552 | // pack expansion. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3553 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3554 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3555 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3556 | // After we've matching against a parameter pack, we're done. |
| 3557 | break; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3558 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3559 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3560 | return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3561 | NumExplicitlySpecified, Specialization, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3562 | Info, &OriginalCallArgs, |
| 3563 | PartialOverloading); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3564 | } |
| 3565 | |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3566 | QualType Sema::adjustCCAndNoReturn(QualType ArgFunctionType, |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3567 | QualType FunctionType, |
| 3568 | bool AdjustExceptionSpec) { |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3569 | if (ArgFunctionType.isNull()) |
| 3570 | return ArgFunctionType; |
| 3571 | |
| 3572 | const FunctionProtoType *FunctionTypeP = |
| 3573 | FunctionType->castAs<FunctionProtoType>(); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3574 | const FunctionProtoType *ArgFunctionTypeP = |
| 3575 | ArgFunctionType->getAs<FunctionProtoType>(); |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3576 | |
| 3577 | FunctionProtoType::ExtProtoInfo EPI = ArgFunctionTypeP->getExtProtoInfo(); |
| 3578 | bool Rebuild = false; |
| 3579 | |
| 3580 | CallingConv CC = FunctionTypeP->getCallConv(); |
| 3581 | if (EPI.ExtInfo.getCC() != CC) { |
| 3582 | EPI.ExtInfo = EPI.ExtInfo.withCallingConv(CC); |
| 3583 | Rebuild = true; |
| 3584 | } |
| 3585 | |
| 3586 | bool NoReturn = FunctionTypeP->getNoReturnAttr(); |
| 3587 | if (EPI.ExtInfo.getNoReturn() != NoReturn) { |
| 3588 | EPI.ExtInfo = EPI.ExtInfo.withNoReturn(NoReturn); |
| 3589 | Rebuild = true; |
| 3590 | } |
| 3591 | |
| 3592 | if (AdjustExceptionSpec && (FunctionTypeP->hasExceptionSpec() || |
| 3593 | ArgFunctionTypeP->hasExceptionSpec())) { |
| 3594 | EPI.ExceptionSpec = FunctionTypeP->getExtProtoInfo().ExceptionSpec; |
| 3595 | Rebuild = true; |
| 3596 | } |
| 3597 | |
| 3598 | if (!Rebuild) |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3599 | return ArgFunctionType; |
| 3600 | |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3601 | return Context.getFunctionType(ArgFunctionTypeP->getReturnType(), |
| 3602 | ArgFunctionTypeP->getParamTypes(), EPI); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3603 | } |
| 3604 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3605 | /// \brief Deduce template arguments when taking the address of a function |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3606 | /// template (C++ [temp.deduct.funcaddr]) or matching a specialization to |
| 3607 | /// a template. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3608 | /// |
| 3609 | /// \param FunctionTemplate the function template for which we are performing |
| 3610 | /// template argument deduction. |
| 3611 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3612 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3613 | /// arguments. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3614 | /// |
| 3615 | /// \param ArgFunctionType the function type that will be used as the |
| 3616 | /// "argument" type (A) when performing template argument deduction from the |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3617 | /// function template's function type. This type may be NULL, if there is no |
| 3618 | /// argument type to compare against, in C++0x [temp.arg.explicit]p3. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3619 | /// |
| 3620 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3621 | /// this will be set to the function template specialization produced by |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3622 | /// template argument deduction. |
| 3623 | /// |
| 3624 | /// \param Info the argument will be updated to provide additional information |
| 3625 | /// about template argument deduction. |
| 3626 | /// |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3627 | /// \param IsAddressOfFunction If \c true, we are deducing as part of taking |
| 3628 | /// the address of a function template per [temp.deduct.funcaddr] and |
| 3629 | /// [over.over]. If \c false, we are looking up a function template |
| 3630 | /// specialization based on its signature, per [temp.deduct.decl]. |
| 3631 | /// |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3632 | /// \returns the result of template argument deduction. |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3633 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3634 | FunctionTemplateDecl *FunctionTemplate, |
| 3635 | TemplateArgumentListInfo *ExplicitTemplateArgs, QualType ArgFunctionType, |
| 3636 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 3637 | bool IsAddressOfFunction) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3638 | if (FunctionTemplate->isInvalidDecl()) |
| 3639 | return TDK_Invalid; |
| 3640 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3641 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 3642 | TemplateParameterList *TemplateParams |
| 3643 | = FunctionTemplate->getTemplateParameters(); |
| 3644 | QualType FunctionType = Function->getType(); |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3645 | |
| 3646 | // When taking the address of a function, we require convertibility of |
| 3647 | // the resulting function type. Otherwise, we allow arbitrary mismatches |
| 3648 | // of calling convention, noreturn, and noexcept. |
| 3649 | if (!IsAddressOfFunction) |
| 3650 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, FunctionType, |
| 3651 | /*AdjustExceptionSpec*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3652 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3653 | // Substitute any explicit template arguments. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3654 | LocalInstantiationScope InstScope(*this); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3655 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3656 | unsigned NumExplicitlySpecified = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3657 | SmallVector<QualType, 4> ParamTypes; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3658 | if (ExplicitTemplateArgs) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3659 | if (TemplateDeductionResult Result |
| 3660 | = SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3661 | *ExplicitTemplateArgs, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3662 | Deduced, ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3663 | &FunctionType, Info)) |
| 3664 | return Result; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3665 | |
| 3666 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3667 | } |
| 3668 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3669 | // Unevaluated SFINAE context. |
| 3670 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3671 | SFINAETrap Trap(*this); |
| 3672 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3673 | Deduced.resize(TemplateParams->size()); |
| 3674 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3675 | // 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] | 3676 | // type so that we treat it as a non-deduced context in what follows. If we |
| 3677 | // are looking up by signature, the signature type should also have a deduced |
| 3678 | // return type, which we instead expect to exactly match. |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3679 | bool HasDeducedReturnType = false; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3680 | if (getLangOpts().CPlusPlus14 && IsAddressOfFunction && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3681 | Function->getReturnType()->getContainedAutoType()) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3682 | FunctionType = SubstAutoType(FunctionType, Context.DependentTy); |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3683 | HasDeducedReturnType = true; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3684 | } |
| 3685 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3686 | if (!ArgFunctionType.isNull()) { |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3687 | unsigned TDF = TDF_TopLevelParameterTypeList; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3688 | if (IsAddressOfFunction) |
| 3689 | TDF |= TDF_InOverloadResolution; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3690 | // Deduce template arguments from the function type. |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3691 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3692 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3693 | FunctionType, ArgFunctionType, |
| 3694 | Info, Deduced, TDF)) |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3695 | return Result; |
| 3696 | } |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3697 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3698 | if (TemplateDeductionResult Result |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3699 | = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
| 3700 | NumExplicitlySpecified, |
| 3701 | Specialization, Info)) |
| 3702 | return Result; |
| 3703 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3704 | // If the function has a deduced return type, deduce it now, so we can check |
| 3705 | // that the deduced function type matches the requested type. |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3706 | if (HasDeducedReturnType && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3707 | Specialization->getReturnType()->isUndeducedType() && |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3708 | DeduceReturnType(Specialization, Info.getLocation(), false)) |
| 3709 | return TDK_MiscellaneousDeductionFailure; |
| 3710 | |
Richard Smith | 9095e5b | 2016-11-01 01:31:23 +0000 | [diff] [blame] | 3711 | // If the function has a dependent exception specification, resolve it now, |
| 3712 | // so we can check that the exception specification matches. |
| 3713 | auto *SpecializationFPT = |
| 3714 | Specialization->getType()->castAs<FunctionProtoType>(); |
| 3715 | if (getLangOpts().CPlusPlus1z && |
| 3716 | isUnresolvedExceptionSpec(SpecializationFPT->getExceptionSpecType()) && |
| 3717 | !ResolveExceptionSpec(Info.getLocation(), SpecializationFPT)) |
| 3718 | return TDK_MiscellaneousDeductionFailure; |
| 3719 | |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3720 | // Adjust the exception specification of the argument again to match the |
| 3721 | // substituted and resolved type we just formed. (Calling convention and |
| 3722 | // noreturn can't be dependent, so we don't actually need this for them |
| 3723 | // right now.) |
| 3724 | QualType SpecializationType = Specialization->getType(); |
| 3725 | if (!IsAddressOfFunction) |
| 3726 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, SpecializationType, |
| 3727 | /*AdjustExceptionSpec*/true); |
| 3728 | |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3729 | // 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] | 3730 | // specialization with respect to arguments of compatible pointer to function |
| 3731 | // types, template argument deduction fails. |
| 3732 | if (!ArgFunctionType.isNull()) { |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3733 | if (IsAddressOfFunction && |
| 3734 | !isSameOrCompatibleFunctionType( |
| 3735 | Context.getCanonicalType(SpecializationType), |
| 3736 | Context.getCanonicalType(ArgFunctionType))) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3737 | return TDK_MiscellaneousDeductionFailure; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3738 | |
| 3739 | if (!IsAddressOfFunction && |
| 3740 | !Context.hasSameType(SpecializationType, ArgFunctionType)) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3741 | return TDK_MiscellaneousDeductionFailure; |
| 3742 | } |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3743 | |
| 3744 | return TDK_Success; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3745 | } |
| 3746 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3747 | /// \brief Given a function declaration (e.g. a generic lambda conversion |
| 3748 | /// function) that contains an 'auto' in its result type, substitute it |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3749 | /// with TypeToReplaceAutoWith. Be careful to pass in the type you want |
| 3750 | /// to replace 'auto' with and not the actual result type you want |
| 3751 | /// to set the function to. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3752 | static inline void |
| 3753 | SubstAutoWithinFunctionReturnType(FunctionDecl *F, |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3754 | QualType TypeToReplaceAutoWith, Sema &S) { |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3755 | assert(!TypeToReplaceAutoWith->getContainedAutoType()); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3756 | QualType AutoResultType = F->getReturnType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3757 | assert(AutoResultType->getContainedAutoType()); |
| 3758 | QualType DeducedResultType = S.SubstAutoType(AutoResultType, |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3759 | TypeToReplaceAutoWith); |
| 3760 | S.Context.adjustDeducedFunctionResultType(F, DeducedResultType); |
| 3761 | } |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3762 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3763 | /// \brief Given a specialized conversion operator of a generic lambda |
| 3764 | /// create the corresponding specializations of the call operator and |
| 3765 | /// the static-invoker. If the return type of the call operator is auto, |
| 3766 | /// deduce its return type and check if that matches the |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3767 | /// return type of the destination function ptr. |
| 3768 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3769 | static inline Sema::TemplateDeductionResult |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3770 | SpecializeCorrespondingLambdaCallOperatorAndInvoker( |
| 3771 | CXXConversionDecl *ConversionSpecialized, |
| 3772 | SmallVectorImpl<DeducedTemplateArgument> &DeducedArguments, |
| 3773 | QualType ReturnTypeOfDestFunctionPtr, |
| 3774 | TemplateDeductionInfo &TDInfo, |
| 3775 | Sema &S) { |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3776 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3777 | CXXRecordDecl *LambdaClass = ConversionSpecialized->getParent(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3778 | assert(LambdaClass && LambdaClass->isGenericLambda()); |
| 3779 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3780 | CXXMethodDecl *CallOpGeneric = LambdaClass->getLambdaCallOperator(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3781 | QualType CallOpResultType = CallOpGeneric->getReturnType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3782 | const bool GenericLambdaCallOperatorHasDeducedReturnType = |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3783 | CallOpResultType->getContainedAutoType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3784 | |
| 3785 | FunctionTemplateDecl *CallOpTemplate = |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3786 | CallOpGeneric->getDescribedFunctionTemplate(); |
| 3787 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3788 | FunctionDecl *CallOpSpecialized = nullptr; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3789 | // Use the deduced arguments of the conversion function, to specialize our |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3790 | // generic lambda's call operator. |
| 3791 | if (Sema::TemplateDeductionResult Result |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3792 | = S.FinishTemplateArgumentDeduction(CallOpTemplate, |
| 3793 | DeducedArguments, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3794 | 0, CallOpSpecialized, TDInfo)) |
| 3795 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3796 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3797 | // 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] | 3798 | if (GenericLambdaCallOperatorHasDeducedReturnType && |
| 3799 | CallOpSpecialized->getReturnType()->isUndeducedType()) |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3800 | S.DeduceReturnType(CallOpSpecialized, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3801 | CallOpSpecialized->getPointOfInstantiation(), |
| 3802 | /*Diagnose*/ true); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3803 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3804 | // Check to see if the return type of the destination ptr-to-function |
| 3805 | // matches the return type of the call operator. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3806 | if (!S.Context.hasSameType(CallOpSpecialized->getReturnType(), |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3807 | ReturnTypeOfDestFunctionPtr)) |
| 3808 | return Sema::TDK_NonDeducedMismatch; |
| 3809 | // Since we have succeeded in matching the source and destination |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3810 | // ptr-to-functions (now including return type), and have successfully |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3811 | // specialized our corresponding call operator, we are ready to |
| 3812 | // specialize the static invoker with the deduced arguments of our |
| 3813 | // ptr-to-function. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3814 | FunctionDecl *InvokerSpecialized = nullptr; |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3815 | FunctionTemplateDecl *InvokerTemplate = LambdaClass-> |
| 3816 | getLambdaStaticInvoker()->getDescribedFunctionTemplate(); |
| 3817 | |
Yaron Keren | f428fcf | 2015-05-13 17:56:46 +0000 | [diff] [blame] | 3818 | #ifndef NDEBUG |
| 3819 | Sema::TemplateDeductionResult LLVM_ATTRIBUTE_UNUSED Result = |
| 3820 | #endif |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3821 | S.FinishTemplateArgumentDeduction(InvokerTemplate, DeducedArguments, 0, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3822 | InvokerSpecialized, TDInfo); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3823 | assert(Result == Sema::TDK_Success && |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3824 | "If the call operator succeeded so should the invoker!"); |
| 3825 | // Set the result type to match the corresponding call operator |
| 3826 | // specialization's result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3827 | if (GenericLambdaCallOperatorHasDeducedReturnType && |
| 3828 | InvokerSpecialized->getReturnType()->isUndeducedType()) { |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3829 | // Be sure to get the type to replace 'auto' with and not |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3830 | // the full result type of the call op specialization |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3831 | // to substitute into the 'auto' of the invoker and conversion |
| 3832 | // function. |
| 3833 | // For e.g. |
| 3834 | // int* (*fp)(int*) = [](auto* a) -> auto* { return a; }; |
| 3835 | // We don't want to subst 'int*' into 'auto' to get int**. |
| 3836 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3837 | QualType TypeToReplaceAutoWith = CallOpSpecialized->getReturnType() |
| 3838 | ->getContainedAutoType() |
| 3839 | ->getDeducedType(); |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3840 | SubstAutoWithinFunctionReturnType(InvokerSpecialized, |
| 3841 | TypeToReplaceAutoWith, S); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3842 | SubstAutoWithinFunctionReturnType(ConversionSpecialized, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3843 | TypeToReplaceAutoWith, S); |
| 3844 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3845 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3846 | // Ensure that static invoker doesn't have a const qualifier. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3847 | // FIXME: When creating the InvokerTemplate in SemaLambda.cpp |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3848 | // do not use the CallOperator's TypeSourceInfo which allows |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3849 | // the const qualifier to leak through. |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3850 | const FunctionProtoType *InvokerFPT = InvokerSpecialized-> |
| 3851 | getType().getTypePtr()->castAs<FunctionProtoType>(); |
| 3852 | FunctionProtoType::ExtProtoInfo EPI = InvokerFPT->getExtProtoInfo(); |
| 3853 | EPI.TypeQuals = 0; |
| 3854 | InvokerSpecialized->setType(S.Context.getFunctionType( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3855 | InvokerFPT->getReturnType(), InvokerFPT->getParamTypes(), EPI)); |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3856 | return Sema::TDK_Success; |
| 3857 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3858 | /// \brief Deduce template arguments for a templated conversion |
| 3859 | /// function (C++ [temp.deduct.conv]) and, if successful, produce a |
| 3860 | /// conversion function template specialization. |
| 3861 | Sema::TemplateDeductionResult |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3862 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *ConversionTemplate, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3863 | QualType ToType, |
| 3864 | CXXConversionDecl *&Specialization, |
| 3865 | TemplateDeductionInfo &Info) { |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3866 | if (ConversionTemplate->isInvalidDecl()) |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3867 | return TDK_Invalid; |
| 3868 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3869 | CXXConversionDecl *ConversionGeneric |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3870 | = cast<CXXConversionDecl>(ConversionTemplate->getTemplatedDecl()); |
| 3871 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3872 | QualType FromType = ConversionGeneric->getConversionType(); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3873 | |
| 3874 | // Canonicalize the types for deduction. |
| 3875 | QualType P = Context.getCanonicalType(FromType); |
| 3876 | QualType A = Context.getCanonicalType(ToType); |
| 3877 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3878 | // C++0x [temp.deduct.conv]p2: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3879 | // If P is a reference type, the type referred to by P is used for |
| 3880 | // type deduction. |
| 3881 | if (const ReferenceType *PRef = P->getAs<ReferenceType>()) |
| 3882 | P = PRef->getPointeeType(); |
| 3883 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3884 | // C++0x [temp.deduct.conv]p4: |
| 3885 | // [...] 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] | 3886 | // for type deduction. |
| 3887 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3888 | A = ARef->getPointeeType().getUnqualifiedType(); |
| 3889 | // C++ [temp.deduct.conv]p3: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3890 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3891 | // If A is not a reference type: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3892 | else { |
| 3893 | assert(!A->isReferenceType() && "Reference types were handled above"); |
| 3894 | |
| 3895 | // - If P is an array type, the pointer type produced by the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3896 | // array-to-pointer standard conversion (4.2) is used in place |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3897 | // of P for type deduction; otherwise, |
| 3898 | if (P->isArrayType()) |
| 3899 | P = Context.getArrayDecayedType(P); |
| 3900 | // - If P is a function type, the pointer type produced by the |
| 3901 | // function-to-pointer standard conversion (4.3) is used in |
| 3902 | // place of P for type deduction; otherwise, |
| 3903 | else if (P->isFunctionType()) |
| 3904 | P = Context.getPointerType(P); |
| 3905 | // - 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] | 3906 | // P's type are ignored for type deduction. |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3907 | else |
| 3908 | P = P.getUnqualifiedType(); |
| 3909 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3910 | // C++0x [temp.deduct.conv]p4: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3911 | // 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] | 3912 | // 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] | 3913 | // referred to by A is used for type deduction. |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3914 | A = A.getUnqualifiedType(); |
| 3915 | } |
| 3916 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3917 | // Unevaluated SFINAE context. |
| 3918 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3919 | SFINAETrap Trap(*this); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3920 | |
| 3921 | // C++ [temp.deduct.conv]p1: |
| 3922 | // Template argument deduction is done by comparing the return |
| 3923 | // type of the template conversion function (call it P) with the |
| 3924 | // type that is required as the result of the conversion (call it |
| 3925 | // A) as described in 14.8.2.4. |
| 3926 | TemplateParameterList *TemplateParams |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3927 | = ConversionTemplate->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3928 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3929 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3930 | |
| 3931 | // C++0x [temp.deduct.conv]p4: |
| 3932 | // In general, the deduction process attempts to find template |
| 3933 | // argument values that will make the deduced A identical to |
| 3934 | // A. However, there are two cases that allow a difference: |
| 3935 | unsigned TDF = 0; |
| 3936 | // - If the original A is a reference type, A can be more |
| 3937 | // cv-qualified than the deduced A (i.e., the type referred to |
| 3938 | // by the reference) |
| 3939 | if (ToType->isReferenceType()) |
| 3940 | TDF |= TDF_ParamWithReferenceType; |
| 3941 | // - The deduced A can be another pointer or pointer to member |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3942 | // type that can be converted to A via a qualification |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3943 | // conversion. |
| 3944 | // |
| 3945 | // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when |
| 3946 | // both P and A are pointers or member pointers. In this case, we |
| 3947 | // just ignore cv-qualifiers completely). |
| 3948 | if ((P->isPointerType() && A->isPointerType()) || |
Douglas Gregor | 9f05ed5 | 2011-08-30 00:37:54 +0000 | [diff] [blame] | 3949 | (P->isMemberPointerType() && A->isMemberPointerType())) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3950 | TDF |= TDF_IgnoreQualifiers; |
| 3951 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3952 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3953 | P, A, Info, Deduced, TDF)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3954 | return Result; |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3955 | |
| 3956 | // Create an Instantiation Scope for finalizing the operator. |
| 3957 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3958 | // Finish template argument deduction. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3959 | FunctionDecl *ConversionSpecialized = nullptr; |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3960 | TemplateDeductionResult Result |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3961 | = FinishTemplateArgumentDeduction(ConversionTemplate, Deduced, 0, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3962 | ConversionSpecialized, Info); |
| 3963 | Specialization = cast_or_null<CXXConversionDecl>(ConversionSpecialized); |
| 3964 | |
| 3965 | // 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] | 3966 | // to a ptr-to-function, use the deduced arguments from the conversion |
| 3967 | // function to specialize the corresponding call operator. |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3968 | // e.g., int (*fp)(int) = [](auto a) { return a; }; |
| 3969 | if (Result == TDK_Success && isLambdaConversionOperator(ConversionGeneric)) { |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3970 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3971 | // 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] | 3972 | // to. This is necessary for matching the lambda call operator's return |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3973 | // type to that of the destination ptr-to-function's return type. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3974 | assert(A->isPointerType() && |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3975 | "Can only convert from lambda to ptr-to-function"); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3976 | const FunctionType *ToFunType = |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3977 | A->getPointeeType().getTypePtr()->getAs<FunctionType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3978 | const QualType DestFunctionPtrReturnType = ToFunType->getReturnType(); |
| 3979 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3980 | // Create the corresponding specializations of the call operator and |
| 3981 | // the static-invoker; and if the return type is auto, |
| 3982 | // deduce the return type and check if it matches the |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3983 | // DestFunctionPtrReturnType. |
| 3984 | // For instance: |
| 3985 | // auto L = [](auto a) { return f(a); }; |
| 3986 | // int (*fp)(int) = L; |
| 3987 | // char (*fp2)(int) = L; <-- Not OK. |
| 3988 | |
| 3989 | Result = SpecializeCorrespondingLambdaCallOperatorAndInvoker( |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3990 | Specialization, Deduced, DestFunctionPtrReturnType, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3991 | Info, *this); |
| 3992 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3993 | return Result; |
| 3994 | } |
| 3995 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3996 | /// \brief Deduce template arguments for a function template when there is |
| 3997 | /// nothing to deduce against (C++0x [temp.arg.explicit]p3). |
| 3998 | /// |
| 3999 | /// \param FunctionTemplate the function template for which we are performing |
| 4000 | /// template argument deduction. |
| 4001 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 4002 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4003 | /// arguments. |
| 4004 | /// |
| 4005 | /// \param Specialization if template argument deduction was successful, |
| 4006 | /// this will be set to the function template specialization produced by |
| 4007 | /// template argument deduction. |
| 4008 | /// |
| 4009 | /// \param Info the argument will be updated to provide additional information |
| 4010 | /// about template argument deduction. |
| 4011 | /// |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4012 | /// \param IsAddressOfFunction If \c true, we are deducing as part of taking |
| 4013 | /// the address of a function template in a context where we do not have a |
| 4014 | /// target type, per [over.over]. If \c false, we are looking up a function |
| 4015 | /// template specialization based on its signature, which only happens when |
| 4016 | /// deducing a function parameter type from an argument that is a template-id |
| 4017 | /// naming a function template specialization. |
| 4018 | /// |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4019 | /// \returns the result of template argument deduction. |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4020 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 4021 | FunctionTemplateDecl *FunctionTemplate, |
| 4022 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 4023 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 4024 | bool IsAddressOfFunction) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4025 | return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 4026 | QualType(), Specialization, Info, |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4027 | IsAddressOfFunction); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4028 | } |
| 4029 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4030 | namespace { |
| 4031 | /// Substitute the 'auto' type specifier within a type for a given replacement |
| 4032 | /// type. |
| 4033 | class SubstituteAutoTransform : |
| 4034 | public TreeTransform<SubstituteAutoTransform> { |
| 4035 | QualType Replacement; |
| 4036 | public: |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 4037 | SubstituteAutoTransform(Sema &SemaRef, QualType Replacement) |
| 4038 | : TreeTransform<SubstituteAutoTransform>(SemaRef), |
| 4039 | Replacement(Replacement) {} |
| 4040 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4041 | QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) { |
| 4042 | // If we're building the type pattern to deduce against, don't wrap the |
| 4043 | // substituted type in an AutoType. Certain template deduction rules |
| 4044 | // apply only when a template type parameter appears directly (and not if |
| 4045 | // the parameter is found through desugaring). For instance: |
| 4046 | // auto &&lref = lvalue; |
| 4047 | // must transform into "rvalue reference to T" not "rvalue reference to |
| 4048 | // auto type deduced as T" in order for [temp.deduct.call]p3 to apply. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4049 | if (!Replacement.isNull() && isa<TemplateTypeParmType>(Replacement)) { |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4050 | QualType Result = Replacement; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4051 | TemplateTypeParmTypeLoc NewTL = |
| 4052 | TLB.push<TemplateTypeParmTypeLoc>(Result); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4053 | NewTL.setNameLoc(TL.getNameLoc()); |
| 4054 | return Result; |
| 4055 | } else { |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 4056 | bool Dependent = |
| 4057 | !Replacement.isNull() && Replacement->isDependentType(); |
| 4058 | QualType Result = |
| 4059 | SemaRef.Context.getAutoType(Dependent ? QualType() : Replacement, |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4060 | TL.getTypePtr()->getKeyword(), |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 4061 | Dependent); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4062 | AutoTypeLoc NewTL = TLB.push<AutoTypeLoc>(Result); |
| 4063 | NewTL.setNameLoc(TL.getNameLoc()); |
| 4064 | return Result; |
| 4065 | } |
| 4066 | } |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 4067 | |
| 4068 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 4069 | // Lambdas never need to be transformed. |
| 4070 | return E; |
| 4071 | } |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4072 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4073 | QualType Apply(TypeLoc TL) { |
| 4074 | // Create some scratch storage for the transformed type locations. |
| 4075 | // FIXME: We're just going to throw this information away. Don't build it. |
| 4076 | TypeLocBuilder TLB; |
| 4077 | TLB.reserve(TL.getFullDataSize()); |
| 4078 | return TransformType(TLB, TL); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4079 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4080 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4081 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4082 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4083 | Sema::DeduceAutoResult |
| 4084 | Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *&Init, QualType &Result) { |
| 4085 | return DeduceAutoType(Type->getTypeLoc(), Init, Result); |
| 4086 | } |
| 4087 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4088 | /// \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] | 4089 | /// |
| 4090 | /// \param Type the type pattern using the auto type-specifier. |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4091 | /// \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] | 4092 | /// \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] | 4093 | /// deduced type. |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4094 | Sema::DeduceAutoResult |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4095 | Sema::DeduceAutoType(TypeLoc Type, Expr *&Init, QualType &Result) { |
John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4096 | if (Init->getType()->isNonOverloadPlaceholderType()) { |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4097 | ExprResult NonPlaceholder = CheckPlaceholderExpr(Init); |
| 4098 | if (NonPlaceholder.isInvalid()) |
| 4099 | return DAR_FailedAlreadyDiagnosed; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4100 | Init = NonPlaceholder.get(); |
John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4101 | } |
| 4102 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4103 | if (Init->isTypeDependent() || Type.getType()->isDependentType()) { |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4104 | Result = SubstituteAutoTransform(*this, Context.DependentTy).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4105 | assert(!Result.isNull() && "substituting DependentTy can't fail"); |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4106 | return DAR_Succeeded; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4109 | // If this is a 'decltype(auto)' specifier, do the decltype dance. |
| 4110 | // Since 'decltype(auto)' can only occur at the top of the type, we |
| 4111 | // don't need to go digging for it. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4112 | if (const AutoType *AT = Type.getType()->getAs<AutoType>()) { |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4113 | if (AT->isDecltypeAuto()) { |
| 4114 | if (isa<InitListExpr>(Init)) { |
| 4115 | Diag(Init->getLocStart(), diag::err_decltype_auto_initializer_list); |
| 4116 | return DAR_FailedAlreadyDiagnosed; |
| 4117 | } |
| 4118 | |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 4119 | QualType Deduced = BuildDecltypeType(Init, Init->getLocStart(), false); |
David Majnemer | 3c20ab2 | 2015-07-01 00:29:28 +0000 | [diff] [blame] | 4120 | if (Deduced.isNull()) |
| 4121 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4122 | // FIXME: Support a non-canonical deduced type for 'auto'. |
| 4123 | Deduced = Context.getCanonicalType(Deduced); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4124 | Result = SubstituteAutoTransform(*this, Deduced).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4125 | if (Result.isNull()) |
| 4126 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4127 | return DAR_Succeeded; |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4128 | } else if (!getLangOpts().CPlusPlus) { |
| 4129 | if (isa<InitListExpr>(Init)) { |
| 4130 | Diag(Init->getLocStart(), diag::err_auto_init_list_from_c); |
| 4131 | return DAR_FailedAlreadyDiagnosed; |
| 4132 | } |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4133 | } |
| 4134 | } |
| 4135 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4136 | SourceLocation Loc = Init->getExprLoc(); |
| 4137 | |
| 4138 | LocalInstantiationScope InstScope(*this); |
| 4139 | |
| 4140 | // Build template<class TemplParam> void Func(FuncParam); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4141 | TemplateTypeParmDecl *TemplParam = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4142 | TemplateTypeParmDecl::Create(Context, nullptr, SourceLocation(), Loc, 0, 0, |
| 4143 | nullptr, false, false); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4144 | QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0); |
| 4145 | NamedDecl *TemplParamPtr = TemplParam; |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 4146 | FixedSizeTemplateParameterListStorage<1, false> TemplateParamsSt( |
| 4147 | Loc, Loc, TemplParamPtr, Loc, nullptr); |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 4148 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4149 | QualType FuncParam = SubstituteAutoTransform(*this, TemplArg).Apply(Type); |
| 4150 | assert(!FuncParam.isNull() && |
| 4151 | "substituting template parameter for 'auto' failed"); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4152 | |
| 4153 | // Deduce type of TemplParam in Func(Init) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4154 | SmallVector<DeducedTemplateArgument, 1> Deduced; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4155 | Deduced.resize(1); |
| 4156 | QualType InitType = Init->getType(); |
| 4157 | unsigned TDF = 0; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4158 | |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4159 | TemplateDeductionInfo Info(Loc); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4160 | |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 4161 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4162 | if (InitList) { |
| 4163 | for (unsigned i = 0, e = InitList->getNumInits(); i < e; ++i) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4164 | if (DeduceTemplateArgumentByListElement(*this, TemplateParamsSt.get(), |
| 4165 | TemplArg, InitList->getInit(i), |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 4166 | Info, Deduced, TDF)) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4167 | return DAR_Failed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4168 | } |
| 4169 | } else { |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4170 | if (!getLangOpts().CPlusPlus && Init->refersToBitField()) { |
| 4171 | Diag(Loc, diag::err_auto_bitfield); |
| 4172 | return DAR_FailedAlreadyDiagnosed; |
| 4173 | } |
| 4174 | |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4175 | if (AdjustFunctionParmAndArgTypesForDeduction( |
| 4176 | *this, TemplateParamsSt.get(), FuncParam, InitType, Init, TDF)) |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 4177 | return DAR_Failed; |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 4178 | |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4179 | if (DeduceTemplateArgumentsByTypeMatch(*this, TemplateParamsSt.get(), |
| 4180 | FuncParam, InitType, Info, Deduced, |
| 4181 | TDF)) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4182 | return DAR_Failed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4183 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4184 | |
Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4185 | if (Deduced[0].getKind() != TemplateArgument::Type) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4186 | return DAR_Failed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4187 | |
Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4188 | QualType DeducedType = Deduced[0].getAsType(); |
| 4189 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4190 | if (InitList) { |
| 4191 | DeducedType = BuildStdInitializerList(DeducedType, Loc); |
| 4192 | if (DeducedType.isNull()) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4193 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4194 | } |
| 4195 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4196 | Result = SubstituteAutoTransform(*this, DeducedType).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4197 | if (Result.isNull()) |
| 4198 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4199 | |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4200 | // Check that the deduced argument type is compatible with the original |
| 4201 | // argument type per C++ [temp.deduct.call]p4. |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4202 | if (!InitList && !Result.isNull() && |
| 4203 | CheckOriginalCallArgDeduction(*this, |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4204 | Sema::OriginalCallArg(FuncParam,0,InitType), |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4205 | Result)) { |
| 4206 | Result = QualType(); |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4207 | return DAR_Failed; |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4208 | } |
| 4209 | |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4210 | return DAR_Succeeded; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4211 | } |
| 4212 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 4213 | QualType Sema::SubstAutoType(QualType TypeWithAuto, |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4214 | QualType TypeToReplaceAuto) { |
| 4215 | return SubstituteAutoTransform(*this, TypeToReplaceAuto). |
| 4216 | TransformType(TypeWithAuto); |
| 4217 | } |
| 4218 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 4219 | TypeSourceInfo* Sema::SubstAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto, |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4220 | QualType TypeToReplaceAuto) { |
| 4221 | return SubstituteAutoTransform(*this, TypeToReplaceAuto). |
| 4222 | TransformType(TypeWithAuto); |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 4223 | } |
| 4224 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4225 | void Sema::DiagnoseAutoDeductionFailure(VarDecl *VDecl, Expr *Init) { |
| 4226 | if (isa<InitListExpr>(Init)) |
| 4227 | Diag(VDecl->getLocation(), |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4228 | VDecl->isInitCapture() |
| 4229 | ? diag::err_init_capture_deduction_failure_from_init_list |
| 4230 | : diag::err_auto_var_deduction_failure_from_init_list) |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4231 | << VDecl->getDeclName() << VDecl->getType() << Init->getSourceRange(); |
| 4232 | else |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4233 | Diag(VDecl->getLocation(), |
| 4234 | VDecl->isInitCapture() ? diag::err_init_capture_deduction_failure |
| 4235 | : diag::err_auto_var_deduction_failure) |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4236 | << VDecl->getDeclName() << VDecl->getType() << Init->getType() |
| 4237 | << Init->getSourceRange(); |
| 4238 | } |
| 4239 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4240 | bool Sema::DeduceReturnType(FunctionDecl *FD, SourceLocation Loc, |
| 4241 | bool Diagnose) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4242 | assert(FD->getReturnType()->isUndeducedType()); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4243 | |
| 4244 | if (FD->getTemplateInstantiationPattern()) |
| 4245 | InstantiateFunctionDefinition(Loc, FD); |
| 4246 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4247 | bool StillUndeduced = FD->getReturnType()->isUndeducedType(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4248 | if (StillUndeduced && Diagnose && !FD->isInvalidDecl()) { |
| 4249 | Diag(Loc, diag::err_auto_fn_used_before_defined) << FD; |
| 4250 | Diag(FD->getLocation(), diag::note_callee_decl) << FD; |
| 4251 | } |
| 4252 | |
| 4253 | return StillUndeduced; |
| 4254 | } |
| 4255 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4256 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4257 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4258 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4259 | unsigned Level, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4260 | llvm::SmallBitVector &Deduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4261 | |
| 4262 | /// \brief If this is a non-static member function, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 4263 | static void |
| 4264 | AddImplicitObjectParameterType(ASTContext &Context, |
| 4265 | CXXMethodDecl *Method, |
| 4266 | SmallVectorImpl<QualType> &ArgTypes) { |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4267 | // C++11 [temp.func.order]p3: |
| 4268 | // [...] The new parameter is of type "reference to cv A," where cv are |
| 4269 | // the cv-qualifiers of the function template (if any) and A is |
| 4270 | // the class of which the function template is a member. |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4271 | // |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4272 | // The standard doesn't say explicitly, but we pick the appropriate kind of |
| 4273 | // reference type based on [over.match.funcs]p4. |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4274 | QualType ArgTy = Context.getTypeDeclType(Method->getParent()); |
| 4275 | ArgTy = Context.getQualifiedType(ArgTy, |
| 4276 | Qualifiers::fromCVRMask(Method->getTypeQualifiers())); |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4277 | if (Method->getRefQualifier() == RQ_RValue) |
| 4278 | ArgTy = Context.getRValueReferenceType(ArgTy); |
| 4279 | else |
| 4280 | ArgTy = Context.getLValueReferenceType(ArgTy); |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4281 | ArgTypes.push_back(ArgTy); |
| 4282 | } |
| 4283 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4284 | /// \brief Determine whether the function template \p FT1 is at least as |
| 4285 | /// specialized as \p FT2. |
| 4286 | static bool isAtLeastAsSpecializedAs(Sema &S, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4287 | SourceLocation Loc, |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4288 | FunctionTemplateDecl *FT1, |
| 4289 | FunctionTemplateDecl *FT2, |
| 4290 | TemplatePartialOrderingContext TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4291 | unsigned NumCallArguments1) { |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4292 | FunctionDecl *FD1 = FT1->getTemplatedDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4293 | FunctionDecl *FD2 = FT2->getTemplatedDecl(); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4294 | const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); |
| 4295 | const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4296 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4297 | assert(Proto1 && Proto2 && "Function templates must have prototypes"); |
| 4298 | TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4299 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4300 | Deduced.resize(TemplateParams->size()); |
| 4301 | |
| 4302 | // C++0x [temp.deduct.partial]p3: |
| 4303 | // The types used to determine the ordering depend on the context in which |
| 4304 | // the partial ordering is done: |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4305 | TemplateDeductionInfo Info(Loc); |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4306 | SmallVector<QualType, 4> Args2; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4307 | switch (TPOC) { |
| 4308 | case TPOC_Call: { |
| 4309 | // - In the context of a function call, the function parameter types are |
| 4310 | // used. |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4311 | CXXMethodDecl *Method1 = dyn_cast<CXXMethodDecl>(FD1); |
| 4312 | CXXMethodDecl *Method2 = dyn_cast<CXXMethodDecl>(FD2); |
Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4313 | |
Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4314 | // C++11 [temp.func.order]p3: |
Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4315 | // [...] If only one of the function templates is a non-static |
| 4316 | // member, that function template is considered to have a new |
| 4317 | // first parameter inserted in its function parameter list. The |
| 4318 | // new parameter is of type "reference to cv A," where cv are |
| 4319 | // the cv-qualifiers of the function template (if any) and A is |
| 4320 | // the class of which the function template is a member. |
| 4321 | // |
Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4322 | // Note that we interpret this to mean "if one of the function |
| 4323 | // templates is a non-static member and the other is a non-member"; |
| 4324 | // otherwise, the ordering rules for static functions against non-static |
| 4325 | // functions don't make any sense. |
| 4326 | // |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4327 | // C++98/03 doesn't have this provision but we've extended DR532 to cover |
| 4328 | // it as wording was broken prior to it. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4329 | SmallVector<QualType, 4> Args1; |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4330 | |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4331 | unsigned NumComparedArguments = NumCallArguments1; |
| 4332 | |
| 4333 | if (!Method2 && Method1 && !Method1->isStatic()) { |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4334 | // Compare 'this' from Method1 against first parameter from Method2. |
| 4335 | AddImplicitObjectParameterType(S.Context, Method1, Args1); |
| 4336 | ++NumComparedArguments; |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4337 | } else if (!Method1 && Method2 && !Method2->isStatic()) { |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4338 | // Compare 'this' from Method2 against first parameter from Method1. |
| 4339 | AddImplicitObjectParameterType(S.Context, Method2, Args2); |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4340 | } |
| 4341 | |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4342 | Args1.insert(Args1.end(), Proto1->param_type_begin(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4343 | Proto1->param_type_end()); |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4344 | Args2.insert(Args2.end(), Proto2->param_type_begin(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4345 | Proto2->param_type_end()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4346 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4347 | // C++ [temp.func.order]p5: |
| 4348 | // The presence of unused ellipsis and default arguments has no effect on |
| 4349 | // the partial ordering of function templates. |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4350 | if (Args1.size() > NumComparedArguments) |
| 4351 | Args1.resize(NumComparedArguments); |
| 4352 | if (Args2.size() > NumComparedArguments) |
| 4353 | Args2.resize(NumComparedArguments); |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4354 | if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(), |
| 4355 | Args1.data(), Args1.size(), Info, Deduced, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4356 | TDF_None, /*PartialOrdering=*/true)) |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 4357 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4358 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4359 | break; |
| 4360 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4361 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4362 | case TPOC_Conversion: |
| 4363 | // - In the context of a call to a conversion operator, the return types |
| 4364 | // of the conversion function templates are used. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4365 | if (DeduceTemplateArgumentsByTypeMatch( |
| 4366 | S, TemplateParams, Proto2->getReturnType(), Proto1->getReturnType(), |
| 4367 | Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4368 | /*PartialOrdering=*/true)) |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4369 | return false; |
| 4370 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4371 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4372 | case TPOC_Other: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4373 | // - 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] | 4374 | // is used. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4375 | if (DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 4376 | FD2->getType(), FD1->getType(), |
| 4377 | Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4378 | /*PartialOrdering=*/true)) |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4379 | return false; |
| 4380 | break; |
| 4381 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4382 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4383 | // C++0x [temp.deduct.partial]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4384 | // In most cases, all template parameters must have values in order for |
| 4385 | // deduction to succeed, but for partial ordering purposes a template |
| 4386 | // 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] | 4387 | // types being used for partial ordering. [ Note: a template parameter used |
| 4388 | // in a non-deduced context is considered used. -end note] |
| 4389 | unsigned ArgIdx = 0, NumArgs = Deduced.size(); |
| 4390 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4391 | if (Deduced[ArgIdx].isNull()) |
| 4392 | break; |
| 4393 | |
| 4394 | if (ArgIdx == NumArgs) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4395 | // All template arguments were deduced. FT1 is at least as specialized |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4396 | // as FT2. |
| 4397 | return true; |
| 4398 | } |
| 4399 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4400 | // Figure out which template parameters were used. |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4401 | llvm::SmallBitVector UsedParameters(TemplateParams->size()); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4402 | switch (TPOC) { |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4403 | case TPOC_Call: |
| 4404 | for (unsigned I = 0, N = Args2.size(); I != N; ++I) |
| 4405 | ::MarkUsedTemplateParameters(S.Context, Args2[I], false, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4406 | TemplateParams->getDepth(), |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4407 | UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4408 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4409 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4410 | case TPOC_Conversion: |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4411 | ::MarkUsedTemplateParameters(S.Context, Proto2->getReturnType(), false, |
| 4412 | TemplateParams->getDepth(), UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4413 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4414 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4415 | case TPOC_Other: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4416 | ::MarkUsedTemplateParameters(S.Context, FD2->getType(), false, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4417 | TemplateParams->getDepth(), |
| 4418 | UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4419 | break; |
| 4420 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4421 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4422 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4423 | // If this argument had no value deduced but was used in one of the types |
| 4424 | // used for partial ordering, then deduction fails. |
| 4425 | if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) |
| 4426 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4427 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4428 | return true; |
| 4429 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4430 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4431 | /// \brief Determine whether this a function template whose parameter-type-list |
| 4432 | /// ends with a function parameter pack. |
| 4433 | static bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) { |
| 4434 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
| 4435 | unsigned NumParams = Function->getNumParams(); |
| 4436 | if (NumParams == 0) |
| 4437 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4438 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4439 | ParmVarDecl *Last = Function->getParamDecl(NumParams - 1); |
| 4440 | if (!Last->isParameterPack()) |
| 4441 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4442 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4443 | // Make sure that no previous parameter is a parameter pack. |
| 4444 | while (--NumParams > 0) { |
| 4445 | if (Function->getParamDecl(NumParams - 1)->isParameterPack()) |
| 4446 | return false; |
| 4447 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4448 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4449 | return true; |
| 4450 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4451 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4452 | /// \brief Returns the more specialized function template according |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4453 | /// to the rules of function template partial ordering (C++ [temp.func.order]). |
| 4454 | /// |
| 4455 | /// \param FT1 the first function template |
| 4456 | /// |
| 4457 | /// \param FT2 the second function template |
| 4458 | /// |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4459 | /// \param TPOC the context in which we are performing partial ordering of |
| 4460 | /// function templates. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4461 | /// |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4462 | /// \param NumCallArguments1 The number of arguments in the call to FT1, used |
| 4463 | /// only when \c TPOC is \c TPOC_Call. |
| 4464 | /// |
| 4465 | /// \param NumCallArguments2 The number of arguments in the call to FT2, used |
| 4466 | /// only when \c TPOC is \c TPOC_Call. |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4467 | /// |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4468 | /// \returns the more specialized function template. If neither |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4469 | /// template is more specialized, returns NULL. |
| 4470 | FunctionTemplateDecl * |
| 4471 | Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, |
| 4472 | FunctionTemplateDecl *FT2, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4473 | SourceLocation Loc, |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4474 | TemplatePartialOrderingContext TPOC, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4475 | unsigned NumCallArguments1, |
| 4476 | unsigned NumCallArguments2) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4477 | bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4478 | NumCallArguments1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4479 | bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4480 | NumCallArguments2); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4481 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4482 | if (Better1 != Better2) // We have a clear winner |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4483 | return Better1 ? FT1 : FT2; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4484 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4485 | if (!Better1 && !Better2) // Neither is better than the other |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4486 | return nullptr; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4487 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4488 | // FIXME: This mimics what GCC implements, but doesn't match up with the |
| 4489 | // proposed resolution for core issue 692. This area needs to be sorted out, |
| 4490 | // but for now we attempt to maintain compatibility. |
| 4491 | bool Variadic1 = isVariadicFunctionTemplate(FT1); |
| 4492 | bool Variadic2 = isVariadicFunctionTemplate(FT2); |
| 4493 | if (Variadic1 != Variadic2) |
| 4494 | return Variadic1? FT2 : FT1; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4495 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4496 | return nullptr; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4497 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4498 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4499 | /// \brief Determine if the two templates are equivalent. |
| 4500 | static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) { |
| 4501 | if (T1 == T2) |
| 4502 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4503 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4504 | if (!T1 || !T2) |
| 4505 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4506 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4507 | return T1->getCanonicalDecl() == T2->getCanonicalDecl(); |
| 4508 | } |
| 4509 | |
| 4510 | /// \brief Retrieve the most specialized of the given function template |
| 4511 | /// specializations. |
| 4512 | /// |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4513 | /// \param SpecBegin the start iterator of the function template |
| 4514 | /// specializations that we will be comparing. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4515 | /// |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4516 | /// \param SpecEnd the end iterator of the function template |
| 4517 | /// specializations, paired with \p SpecBegin. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4518 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4519 | /// \param Loc the location where the ambiguity or no-specializations |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4520 | /// diagnostic should occur. |
| 4521 | /// |
| 4522 | /// \param NoneDiag partial diagnostic used to diagnose cases where there are |
| 4523 | /// no matching candidates. |
| 4524 | /// |
| 4525 | /// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one |
| 4526 | /// occurs. |
| 4527 | /// |
| 4528 | /// \param CandidateDiag partial diagnostic used for each function template |
| 4529 | /// specialization that is a candidate in the ambiguous ordering. One parameter |
| 4530 | /// in this diagnostic should be unbound, which will correspond to the string |
| 4531 | /// describing the template arguments for the function template specialization. |
| 4532 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4533 | /// \returns the most specialized function template specialization, if |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4534 | /// found. Otherwise, returns SpecEnd. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4535 | UnresolvedSetIterator Sema::getMostSpecialized( |
| 4536 | UnresolvedSetIterator SpecBegin, UnresolvedSetIterator SpecEnd, |
| 4537 | TemplateSpecCandidateSet &FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4538 | SourceLocation Loc, const PartialDiagnostic &NoneDiag, |
| 4539 | const PartialDiagnostic &AmbigDiag, const PartialDiagnostic &CandidateDiag, |
| 4540 | bool Complain, QualType TargetType) { |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4541 | if (SpecBegin == SpecEnd) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4542 | if (Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4543 | Diag(Loc, NoneDiag); |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4544 | FailedCandidates.NoteCandidates(*this, Loc); |
| 4545 | } |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4546 | return SpecEnd; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4547 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4548 | |
| 4549 | if (SpecBegin + 1 == SpecEnd) |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4550 | return SpecBegin; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4551 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4552 | // Find the function template that is better than all of the templates it |
| 4553 | // has been compared to. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4554 | UnresolvedSetIterator Best = SpecBegin; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4555 | FunctionTemplateDecl *BestTemplate |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4556 | = cast<FunctionDecl>(*Best)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4557 | assert(BestTemplate && "Not a function template specialization?"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4558 | for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) { |
| 4559 | FunctionTemplateDecl *Challenger |
| 4560 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4561 | assert(Challenger && "Not a function template specialization?"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4562 | if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4563 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4564 | Challenger)) { |
| 4565 | Best = I; |
| 4566 | BestTemplate = Challenger; |
| 4567 | } |
| 4568 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4569 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4570 | // Make sure that the "best" function template is more specialized than all |
| 4571 | // of the others. |
| 4572 | bool Ambiguous = false; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4573 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4574 | FunctionTemplateDecl *Challenger |
| 4575 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4576 | if (I != Best && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4577 | !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4578 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4579 | BestTemplate)) { |
| 4580 | Ambiguous = true; |
| 4581 | break; |
| 4582 | } |
| 4583 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4584 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4585 | if (!Ambiguous) { |
| 4586 | // We found an answer. Return it. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4587 | return Best; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4588 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4589 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4590 | // Diagnose the ambiguity. |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4591 | if (Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4592 | Diag(Loc, AmbigDiag); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4593 | |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4594 | // FIXME: Can we order the candidates in some sane way? |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4595 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4596 | PartialDiagnostic PD = CandidateDiag; |
Saleem Abdulrasool | 78704fb | 2016-12-22 04:26:57 +0000 | [diff] [blame] | 4597 | const auto *FD = cast<FunctionDecl>(*I); |
| 4598 | PD << FD << getTemplateArgumentBindingsText( |
| 4599 | FD->getPrimaryTemplate()->getTemplateParameters(), |
| 4600 | *FD->getTemplateSpecializationArgs()); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4601 | if (!TargetType.isNull()) |
Saleem Abdulrasool | 78704fb | 2016-12-22 04:26:57 +0000 | [diff] [blame] | 4602 | HandleFunctionTypeMismatch(PD, FD->getType(), TargetType); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4603 | Diag((*I)->getLocation(), PD); |
| 4604 | } |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4605 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4606 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4607 | return SpecEnd; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4608 | } |
| 4609 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4610 | /// \brief Returns the more specialized class template partial specialization |
| 4611 | /// according to the rules of partial ordering of class template partial |
| 4612 | /// specializations (C++ [temp.class.order]). |
| 4613 | /// |
| 4614 | /// \param PS1 the first class template partial specialization |
| 4615 | /// |
| 4616 | /// \param PS2 the second class template partial specialization |
| 4617 | /// |
| 4618 | /// \returns the more specialized class template partial specialization. If |
| 4619 | /// neither partial specialization is more specialized, returns NULL. |
| 4620 | ClassTemplatePartialSpecializationDecl * |
| 4621 | Sema::getMoreSpecializedPartialSpecialization( |
| 4622 | ClassTemplatePartialSpecializationDecl *PS1, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4623 | ClassTemplatePartialSpecializationDecl *PS2, |
| 4624 | SourceLocation Loc) { |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4625 | // C++ [temp.class.order]p1: |
| 4626 | // For two class template partial specializations, the first is at least as |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4627 | // specialized as the second if, given the following rewrite to two |
| 4628 | // function templates, the first function template is at least as |
| 4629 | // specialized as the second according to the ordering rules for function |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4630 | // templates (14.6.6.2): |
| 4631 | // - the first function template has the same template parameters as the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4632 | // first partial specialization and has a single function parameter |
| 4633 | // whose type is a class template specialization with the template |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4634 | // arguments of the first partial specialization, and |
| 4635 | // - the second function template has the same template parameters as the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4636 | // second partial specialization and has a single function parameter |
| 4637 | // whose type is a class template specialization with the template |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4638 | // arguments of the second partial specialization. |
| 4639 | // |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 4640 | // Rather than synthesize function templates, we merely perform the |
| 4641 | // equivalent partial ordering by performing deduction directly on |
| 4642 | // the template arguments of the class template partial |
| 4643 | // specializations. This computation is slightly simpler than the |
| 4644 | // general problem of function template partial ordering, because |
| 4645 | // class template partial specializations are more constrained. We |
| 4646 | // know that every template parameter is deducible from the class |
| 4647 | // template partial specialization's template arguments, for |
| 4648 | // example. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4649 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4650 | TemplateDeductionInfo Info(Loc); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4651 | |
| 4652 | QualType PT1 = PS1->getInjectedSpecializationType(); |
| 4653 | QualType PT2 = PS2->getInjectedSpecializationType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4654 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4655 | // Determine whether PS1 is at least as specialized as PS2 |
| 4656 | Deduced.resize(PS2->getTemplateParameters()->size()); |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4657 | bool Better1 = !DeduceTemplateArgumentsByTypeMatch(*this, |
| 4658 | PS2->getTemplateParameters(), |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4659 | PT2, PT1, Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4660 | /*PartialOrdering=*/true); |
Argyrios Kyrtzidis | 7d39155 | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4661 | if (Better1) { |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 4662 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(),Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 4663 | InstantiatingTemplate Inst(*this, Loc, PS2, DeducedArgs, Info); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4664 | Better1 = !::FinishTemplateArgumentDeduction( |
| 4665 | *this, PS2, PS1->getTemplateArgs(), Deduced, Info); |
| 4666 | } |
| 4667 | |
| 4668 | // Determine whether PS2 is at least as specialized as PS1 |
| 4669 | Deduced.clear(); |
| 4670 | Deduced.resize(PS1->getTemplateParameters()->size()); |
| 4671 | bool Better2 = !DeduceTemplateArgumentsByTypeMatch( |
| 4672 | *this, PS1->getTemplateParameters(), PT1, PT2, Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4673 | /*PartialOrdering=*/true); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4674 | if (Better2) { |
| 4675 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), |
| 4676 | Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 4677 | InstantiatingTemplate Inst(*this, Loc, PS1, DeducedArgs, Info); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4678 | Better2 = !::FinishTemplateArgumentDeduction( |
| 4679 | *this, PS1, PS2->getTemplateArgs(), Deduced, Info); |
| 4680 | } |
| 4681 | |
| 4682 | if (Better1 == Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4683 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4684 | |
| 4685 | return Better1 ? PS1 : PS2; |
| 4686 | } |
| 4687 | |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 4688 | /// TODO: Unify with ClassTemplatePartialSpecializationDecl version? |
| 4689 | /// May require unifying ClassTemplate(Partial)SpecializationDecl and |
| 4690 | /// VarTemplate(Partial)SpecializationDecl with a new data |
| 4691 | /// structure Template(Partial)SpecializationDecl, and |
| 4692 | /// using Template(Partial)SpecializationDecl as input type. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4693 | VarTemplatePartialSpecializationDecl * |
| 4694 | Sema::getMoreSpecializedPartialSpecialization( |
| 4695 | VarTemplatePartialSpecializationDecl *PS1, |
| 4696 | VarTemplatePartialSpecializationDecl *PS2, SourceLocation Loc) { |
| 4697 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 4698 | TemplateDeductionInfo Info(Loc); |
| 4699 | |
Richard Smith | f04fd0b | 2013-12-12 23:14:16 +0000 | [diff] [blame] | 4700 | assert(PS1->getSpecializedTemplate() == PS2->getSpecializedTemplate() && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4701 | "the partial specializations being compared should specialize" |
| 4702 | " the same template."); |
| 4703 | TemplateName Name(PS1->getSpecializedTemplate()); |
| 4704 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4705 | QualType PT1 = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4706 | CanonTemplate, PS1->getTemplateArgs().asArray()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4707 | QualType PT2 = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4708 | CanonTemplate, PS2->getTemplateArgs().asArray()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4709 | |
| 4710 | // Determine whether PS1 is at least as specialized as PS2 |
| 4711 | Deduced.resize(PS2->getTemplateParameters()->size()); |
| 4712 | bool Better1 = !DeduceTemplateArgumentsByTypeMatch( |
| 4713 | *this, PS2->getTemplateParameters(), PT2, PT1, Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4714 | /*PartialOrdering=*/true); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4715 | if (Better1) { |
| 4716 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), |
| 4717 | Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 4718 | InstantiatingTemplate Inst(*this, Loc, PS2, DeducedArgs, Info); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4719 | Better1 = !::FinishTemplateArgumentDeduction(*this, PS2, |
| 4720 | PS1->getTemplateArgs(), |
Douglas Gregor | 9225b02 | 2010-04-29 06:31:36 +0000 | [diff] [blame] | 4721 | Deduced, Info); |
Argyrios Kyrtzidis | 7d39155 | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4722 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4723 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4724 | // Determine whether PS2 is at least as specialized as PS1 |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 4725 | Deduced.clear(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4726 | Deduced.resize(PS1->getTemplateParameters()->size()); |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4727 | bool Better2 = !DeduceTemplateArgumentsByTypeMatch(*this, |
| 4728 | PS1->getTemplateParameters(), |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4729 | PT1, PT2, Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4730 | /*PartialOrdering=*/true); |
Argyrios Kyrtzidis | 7d39155 | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4731 | if (Better2) { |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 4732 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(),Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 4733 | InstantiatingTemplate Inst(*this, Loc, PS1, DeducedArgs, Info); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4734 | Better2 = !::FinishTemplateArgumentDeduction(*this, PS1, |
| 4735 | PS2->getTemplateArgs(), |
Douglas Gregor | 9225b02 | 2010-04-29 06:31:36 +0000 | [diff] [blame] | 4736 | Deduced, Info); |
Argyrios Kyrtzidis | 7d39155 | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4737 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4738 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4739 | if (Better1 == Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4740 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4741 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4742 | return Better1? PS1 : PS2; |
| 4743 | } |
| 4744 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4745 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4746 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4747 | const TemplateArgument &TemplateArg, |
| 4748 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4749 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4750 | llvm::SmallBitVector &Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4751 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4752 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4753 | /// expression. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4754 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4755 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4756 | const Expr *E, |
| 4757 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4758 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4759 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 4760 | // We can deduce from a pack expansion. |
| 4761 | if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E)) |
| 4762 | E = Expansion->getPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4763 | |
Richard Smith | 3434900 | 2012-07-09 03:07:20 +0000 | [diff] [blame] | 4764 | // Skip through any implicit casts we added while type-checking, and any |
| 4765 | // substitutions performed by template alias expansion. |
| 4766 | while (1) { |
| 4767 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 4768 | E = ICE->getSubExpr(); |
| 4769 | else if (const SubstNonTypeTemplateParmExpr *Subst = |
| 4770 | dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 4771 | E = Subst->getReplacement(); |
| 4772 | else |
| 4773 | break; |
| 4774 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4775 | |
| 4776 | // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4777 | // find other occurrences of template parameters. |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4778 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
Douglas Gregor | 04b1152 | 2010-01-14 18:13:22 +0000 | [diff] [blame] | 4779 | if (!DRE) |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4780 | return; |
| 4781 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4782 | const NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4783 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 4784 | if (!NTTP) |
| 4785 | return; |
| 4786 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4787 | if (NTTP->getDepth() == Depth) |
| 4788 | Used[NTTP->getIndex()] = true; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 4789 | |
| 4790 | // In C++1z mode, additional arguments may be deduced from the type of a |
| 4791 | // non-type argument. |
| 4792 | if (Ctx.getLangOpts().CPlusPlus1z) |
| 4793 | MarkUsedTemplateParameters(Ctx, NTTP->getType(), OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4794 | } |
| 4795 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4796 | /// \brief Mark the template parameters that are used by the given |
| 4797 | /// nested name specifier. |
| 4798 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4799 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4800 | NestedNameSpecifier *NNS, |
| 4801 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4802 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4803 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4804 | if (!NNS) |
| 4805 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4806 | |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4807 | MarkUsedTemplateParameters(Ctx, NNS->getPrefix(), OnlyDeduced, Depth, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4808 | Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4809 | MarkUsedTemplateParameters(Ctx, QualType(NNS->getAsType(), 0), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4810 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4811 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4812 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4813 | /// \brief Mark the template parameters that are used by the given |
| 4814 | /// template name. |
| 4815 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4816 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4817 | TemplateName Name, |
| 4818 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4819 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4820 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4821 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 4822 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4823 | = dyn_cast<TemplateTemplateParmDecl>(Template)) { |
| 4824 | if (TTP->getDepth() == Depth) |
| 4825 | Used[TTP->getIndex()] = true; |
| 4826 | } |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4827 | return; |
| 4828 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4829 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4830 | if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4831 | MarkUsedTemplateParameters(Ctx, QTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4832 | Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4833 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4834 | MarkUsedTemplateParameters(Ctx, DTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4835 | Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4836 | } |
| 4837 | |
| 4838 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4839 | /// type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4840 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4841 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4842 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4843 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4844 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4845 | if (T.isNull()) |
| 4846 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4847 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4848 | // Non-dependent types have nothing deducible |
| 4849 | if (!T->isDependentType()) |
| 4850 | return; |
| 4851 | |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4852 | T = Ctx.getCanonicalType(T); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4853 | switch (T->getTypeClass()) { |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4854 | case Type::Pointer: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4855 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4856 | cast<PointerType>(T)->getPointeeType(), |
| 4857 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4858 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4859 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4860 | break; |
| 4861 | |
| 4862 | case Type::BlockPointer: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4863 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4864 | cast<BlockPointerType>(T)->getPointeeType(), |
| 4865 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4866 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4867 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4868 | break; |
| 4869 | |
| 4870 | case Type::LValueReference: |
| 4871 | case Type::RValueReference: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4872 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4873 | cast<ReferenceType>(T)->getPointeeType(), |
| 4874 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4875 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4876 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4877 | break; |
| 4878 | |
| 4879 | case Type::MemberPointer: { |
| 4880 | const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4881 | MarkUsedTemplateParameters(Ctx, MemPtr->getPointeeType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4882 | Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4883 | MarkUsedTemplateParameters(Ctx, QualType(MemPtr->getClass(), 0), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4884 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4885 | break; |
| 4886 | } |
| 4887 | |
| 4888 | case Type::DependentSizedArray: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4889 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4890 | cast<DependentSizedArrayType>(T)->getSizeExpr(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4891 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4892 | // Fall through to check the element type |
| 4893 | |
| 4894 | case Type::ConstantArray: |
| 4895 | case Type::IncompleteArray: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4896 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4897 | cast<ArrayType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4898 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4899 | break; |
| 4900 | |
| 4901 | case Type::Vector: |
| 4902 | case Type::ExtVector: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4903 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4904 | cast<VectorType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4905 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4906 | break; |
| 4907 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4908 | case Type::DependentSizedExtVector: { |
| 4909 | const DependentSizedExtVectorType *VecType |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4910 | = cast<DependentSizedExtVectorType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4911 | MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4912 | Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4913 | MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4914 | Depth, Used); |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4915 | break; |
| 4916 | } |
| 4917 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4918 | case Type::FunctionProto: { |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4919 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4920 | MarkUsedTemplateParameters(Ctx, Proto->getReturnType(), OnlyDeduced, Depth, |
| 4921 | Used); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4922 | for (unsigned I = 0, N = Proto->getNumParams(); I != N; ++I) |
| 4923 | MarkUsedTemplateParameters(Ctx, Proto->getParamType(I), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4924 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4925 | break; |
| 4926 | } |
| 4927 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4928 | case Type::TemplateTypeParm: { |
| 4929 | const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T); |
| 4930 | if (TTP->getDepth() == Depth) |
| 4931 | Used[TTP->getIndex()] = true; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4932 | break; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4933 | } |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4934 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4935 | case Type::SubstTemplateTypeParmPack: { |
| 4936 | const SubstTemplateTypeParmPackType *Subst |
| 4937 | = cast<SubstTemplateTypeParmPackType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4938 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4939 | QualType(Subst->getReplacedParameter(), 0), |
| 4940 | OnlyDeduced, Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4941 | MarkUsedTemplateParameters(Ctx, Subst->getArgumentPack(), |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4942 | OnlyDeduced, Depth, Used); |
| 4943 | break; |
| 4944 | } |
| 4945 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4946 | case Type::InjectedClassName: |
| 4947 | T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); |
| 4948 | // fall through |
| 4949 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4950 | case Type::TemplateSpecialization: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4951 | const TemplateSpecializationType *Spec |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4952 | = cast<TemplateSpecializationType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4953 | MarkUsedTemplateParameters(Ctx, Spec->getTemplateName(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4954 | Depth, Used); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4955 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4956 | // C++0x [temp.deduct.type]p9: |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 4957 | // If the template argument list of P contains a pack expansion that is |
| 4958 | // not the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4959 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4960 | if (OnlyDeduced && |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4961 | hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs())) |
| 4962 | break; |
| 4963 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4964 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4965 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4966 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4967 | break; |
| 4968 | } |
| 4969 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4970 | case Type::Complex: |
| 4971 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4972 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4973 | cast<ComplexType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4974 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4975 | break; |
| 4976 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4977 | case Type::Atomic: |
| 4978 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4979 | MarkUsedTemplateParameters(Ctx, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4980 | cast<AtomicType>(T)->getValueType(), |
| 4981 | OnlyDeduced, Depth, Used); |
| 4982 | break; |
| 4983 | |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4984 | case Type::DependentName: |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4985 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4986 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4987 | cast<DependentNameType>(T)->getQualifier(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4988 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4989 | break; |
| 4990 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4991 | case Type::DependentTemplateSpecialization: { |
Richard Smith | 50d5b97 | 2015-12-30 20:56:05 +0000 | [diff] [blame] | 4992 | // C++14 [temp.deduct.type]p5: |
| 4993 | // The non-deduced contexts are: |
| 4994 | // -- The nested-name-specifier of a type that was specified using a |
| 4995 | // qualified-id |
| 4996 | // |
| 4997 | // C++14 [temp.deduct.type]p6: |
| 4998 | // When a type name is specified in a way that includes a non-deduced |
| 4999 | // context, all of the types that comprise that type name are also |
| 5000 | // non-deduced. |
| 5001 | if (OnlyDeduced) |
| 5002 | break; |
| 5003 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5004 | const DependentTemplateSpecializationType *Spec |
| 5005 | = cast<DependentTemplateSpecializationType>(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5006 | |
Richard Smith | 50d5b97 | 2015-12-30 20:56:05 +0000 | [diff] [blame] | 5007 | MarkUsedTemplateParameters(Ctx, Spec->getQualifier(), |
| 5008 | OnlyDeduced, Depth, Used); |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5009 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5010 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5011 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5012 | Used); |
| 5013 | break; |
| 5014 | } |
| 5015 | |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5016 | case Type::TypeOf: |
| 5017 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5018 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5019 | cast<TypeOfType>(T)->getUnderlyingType(), |
| 5020 | OnlyDeduced, Depth, Used); |
| 5021 | break; |
| 5022 | |
| 5023 | case Type::TypeOfExpr: |
| 5024 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5025 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5026 | cast<TypeOfExprType>(T)->getUnderlyingExpr(), |
| 5027 | OnlyDeduced, Depth, Used); |
| 5028 | break; |
| 5029 | |
| 5030 | case Type::Decltype: |
| 5031 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5032 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5033 | cast<DecltypeType>(T)->getUnderlyingExpr(), |
| 5034 | OnlyDeduced, Depth, Used); |
| 5035 | break; |
| 5036 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 5037 | case Type::UnaryTransform: |
| 5038 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5039 | MarkUsedTemplateParameters(Ctx, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5040 | cast<UnaryTransformType>(T)->getUnderlyingType(), |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 5041 | OnlyDeduced, Depth, Used); |
| 5042 | break; |
| 5043 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 5044 | case Type::PackExpansion: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5045 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 5046 | cast<PackExpansionType>(T)->getPattern(), |
| 5047 | OnlyDeduced, Depth, Used); |
| 5048 | break; |
| 5049 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5050 | case Type::Auto: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5051 | MarkUsedTemplateParameters(Ctx, |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5052 | cast<AutoType>(T)->getDeducedType(), |
| 5053 | OnlyDeduced, Depth, Used); |
| 5054 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5055 | // None of these types have any template parameters in them. |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5056 | case Type::Builtin: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5057 | case Type::VariableArray: |
| 5058 | case Type::FunctionNoProto: |
| 5059 | case Type::Record: |
| 5060 | case Type::Enum: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5061 | case Type::ObjCInterface: |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5062 | case Type::ObjCObject: |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 5063 | case Type::ObjCObjectPointer: |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5064 | case Type::UnresolvedUsing: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5065 | case Type::Pipe: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5066 | #define TYPE(Class, Base) |
| 5067 | #define ABSTRACT_TYPE(Class, Base) |
| 5068 | #define DEPENDENT_TYPE(Class, Base) |
| 5069 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 5070 | #include "clang/AST/TypeNodes.def" |
| 5071 | break; |
| 5072 | } |
| 5073 | } |
| 5074 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5075 | /// \brief Mark the template parameters that are used by this |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5076 | /// template argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5077 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5078 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5079 | const TemplateArgument &TemplateArg, |
| 5080 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5081 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5082 | llvm::SmallBitVector &Used) { |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5083 | switch (TemplateArg.getKind()) { |
| 5084 | case TemplateArgument::Null: |
| 5085 | case TemplateArgument::Integral: |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5086 | case TemplateArgument::Declaration: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5087 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5088 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5089 | case TemplateArgument::NullPtr: |
| 5090 | MarkUsedTemplateParameters(Ctx, TemplateArg.getNullPtrType(), OnlyDeduced, |
| 5091 | Depth, Used); |
| 5092 | break; |
| 5093 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5094 | case TemplateArgument::Type: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5095 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5096 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5097 | break; |
| 5098 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5099 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 5100 | case TemplateArgument::TemplateExpansion: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5101 | MarkUsedTemplateParameters(Ctx, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5102 | TemplateArg.getAsTemplateOrTemplatePattern(), |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5103 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5104 | break; |
| 5105 | |
| 5106 | case TemplateArgument::Expression: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5107 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsExpr(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5108 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5109 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5110 | |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5111 | case TemplateArgument::Pack: |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 5112 | for (const auto &P : TemplateArg.pack_elements()) |
| 5113 | MarkUsedTemplateParameters(Ctx, P, OnlyDeduced, Depth, Used); |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5114 | break; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5115 | } |
| 5116 | } |
| 5117 | |
James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 5118 | /// \brief Mark which template parameters can be deduced from a given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5119 | /// template argument list. |
| 5120 | /// |
| 5121 | /// \param TemplateArgs the template argument list from which template |
| 5122 | /// parameters will be deduced. |
| 5123 | /// |
James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 5124 | /// \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] | 5125 | /// to indicate when the corresponding template parameter will be |
| 5126 | /// deduced. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5127 | void |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5128 | Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5129 | bool OnlyDeduced, unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5130 | llvm::SmallBitVector &Used) { |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5131 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5132 | // If the template argument list of P contains a pack expansion that is not |
| 5133 | // the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5134 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5135 | if (OnlyDeduced && |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5136 | hasPackExpansionBeforeEnd(TemplateArgs.data(), TemplateArgs.size())) |
| 5137 | return; |
| 5138 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5139 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5140 | ::MarkUsedTemplateParameters(Context, TemplateArgs[I], OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5141 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5142 | } |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5143 | |
| 5144 | /// \brief Marks all of the template parameters that will be deduced by a |
| 5145 | /// call to the given function template. |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 5146 | void Sema::MarkDeducedTemplateParameters( |
| 5147 | ASTContext &Ctx, const FunctionTemplateDecl *FunctionTemplate, |
| 5148 | llvm::SmallBitVector &Deduced) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5149 | TemplateParameterList *TemplateParams |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5150 | = FunctionTemplate->getTemplateParameters(); |
| 5151 | Deduced.clear(); |
| 5152 | Deduced.resize(TemplateParams->size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5153 | |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5154 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 5155 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5156 | ::MarkUsedTemplateParameters(Ctx, Function->getParamDecl(I)->getType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5157 | true, TemplateParams->getDepth(), Deduced); |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5158 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5159 | |
| 5160 | bool hasDeducibleTemplateParameters(Sema &S, |
| 5161 | FunctionTemplateDecl *FunctionTemplate, |
| 5162 | QualType T) { |
| 5163 | if (!T->isDependentType()) |
| 5164 | return false; |
| 5165 | |
| 5166 | TemplateParameterList *TemplateParams |
| 5167 | = FunctionTemplate->getTemplateParameters(); |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5168 | llvm::SmallBitVector Deduced(TemplateParams->size()); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 5169 | ::MarkUsedTemplateParameters(S.Context, T, true, TemplateParams->getDepth(), |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5170 | Deduced); |
| 5171 | |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5172 | return Deduced.any(); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5173 | } |