Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1 | //===------- SemaTemplateDeduction.cpp - Template Argument Deduction ------===/ |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | //===----------------------------------------------------------------------===/ |
| 8 | // |
| 9 | // This file implements C++ template argument deduction. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===/ |
| 12 | |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 13 | #include "clang/Sema/TemplateDeduction.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "TreeTransform.h" |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTLambda.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
| 20 | #include "clang/AST/ExprCXX.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/AST/StmtVisitor.h" |
| 22 | #include "clang/Sema/DeclSpec.h" |
| 23 | #include "clang/Sema/Sema.h" |
| 24 | #include "clang/Sema/Template.h" |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallBitVector.h" |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 27 | |
| 28 | namespace clang { |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 29 | using namespace sema; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 30 | /// \brief Various flags that control template argument deduction. |
| 31 | /// |
| 32 | /// These flags can be bitwise-OR'd together. |
| 33 | enum TemplateDeductionFlags { |
| 34 | /// \brief No template argument deduction flags, which indicates the |
| 35 | /// strictest results for template argument deduction (as used for, e.g., |
| 36 | /// matching class template partial specializations). |
| 37 | TDF_None = 0, |
| 38 | /// \brief Within template argument deduction from a function call, we are |
| 39 | /// matching with a parameter type for which the original parameter was |
| 40 | /// a reference. |
| 41 | TDF_ParamWithReferenceType = 0x1, |
| 42 | /// \brief Within template argument deduction from a function call, we |
| 43 | /// are matching in a case where we ignore cv-qualifiers. |
| 44 | TDF_IgnoreQualifiers = 0x02, |
| 45 | /// \brief Within template argument deduction from a function call, |
| 46 | /// we are matching in a case where we can perform template argument |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 47 | /// deduction from a template-id of a derived class of the argument type. |
Douglas Gregor | 406f634 | 2009-09-14 20:00:47 +0000 | [diff] [blame] | 48 | TDF_DerivedClass = 0x04, |
| 49 | /// \brief Allow non-dependent types to differ, e.g., when performing |
| 50 | /// template argument deduction from a function call where conversions |
| 51 | /// may apply. |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 52 | TDF_SkipNonDependent = 0x08, |
| 53 | /// \brief Whether we are performing template argument deduction for |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 54 | /// parameters and arguments in a top-level template argument |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 55 | TDF_TopLevelParameterTypeList = 0x10, |
| 56 | /// \brief Within template argument deduction from overload resolution per |
| 57 | /// C++ [over.over] allow matching function types that are compatible in |
| 58 | /// terms of noreturn and default calling convention adjustments. |
| 59 | TDF_InOverloadResolution = 0x20 |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 60 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 61 | } |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 62 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 63 | using namespace clang; |
| 64 | |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 65 | /// \brief Compare two APSInts, extending and switching the sign as |
| 66 | /// necessary to compare their values regardless of underlying type. |
| 67 | static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) { |
| 68 | if (Y.getBitWidth() > X.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 69 | X = X.extend(Y.getBitWidth()); |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 70 | else if (Y.getBitWidth() < X.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 71 | Y = Y.extend(X.getBitWidth()); |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 72 | |
| 73 | // If there is a signedness mismatch, correct it. |
| 74 | if (X.isSigned() != Y.isSigned()) { |
| 75 | // If the signed value is negative, then the values cannot be the same. |
| 76 | if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative())) |
| 77 | return false; |
| 78 | |
| 79 | Y.setIsSigned(true); |
| 80 | X.setIsSigned(true); |
| 81 | } |
| 82 | |
| 83 | return X == Y; |
| 84 | } |
| 85 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 86 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 87 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 88 | TemplateParameterList *TemplateParams, |
| 89 | const TemplateArgument &Param, |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 90 | TemplateArgument Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 91 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 92 | SmallVectorImpl<DeducedTemplateArgument> &Deduced); |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 93 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 94 | static Sema::TemplateDeductionResult |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 95 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 96 | TemplateParameterList *TemplateParams, |
| 97 | QualType Param, |
| 98 | QualType Arg, |
| 99 | TemplateDeductionInfo &Info, |
| 100 | SmallVectorImpl<DeducedTemplateArgument> & |
| 101 | Deduced, |
| 102 | unsigned TDF, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 103 | bool PartialOrdering = false, |
| 104 | bool DeducedFromArrayBound = false); |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 105 | |
| 106 | static Sema::TemplateDeductionResult |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 107 | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 108 | ArrayRef<TemplateArgument> Params, |
| 109 | ArrayRef<TemplateArgument> Args, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 110 | TemplateDeductionInfo &Info, |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 111 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 112 | bool NumberOfArgumentsMustMatch); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 113 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 114 | /// \brief If the given expression is of a form that permits the deduction |
| 115 | /// of a non-type template parameter, return the declaration of that |
| 116 | /// non-type template parameter. |
| 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. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 523 | return DeduceTemplateArguments(S, TemplateParams, |
| 524 | Param->template_arguments(), |
| 525 | SpecArg->template_arguments(), Info, Deduced, |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 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. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 557 | return DeduceTemplateArguments(S, TemplateParams, Param->template_arguments(), |
| 558 | SpecArg->getTemplateArgs().asArray(), Info, |
| 559 | Deduced, /*NumberOfArgumentsMustMatch=*/true); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 560 | } |
| 561 | |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 562 | /// \brief Determines whether the given type is an opaque type that |
| 563 | /// might be more qualified when instantiated. |
| 564 | static bool IsPossiblyOpaquelyQualifiedType(QualType T) { |
| 565 | switch (T->getTypeClass()) { |
| 566 | case Type::TypeOfExpr: |
| 567 | case Type::TypeOf: |
| 568 | case Type::DependentName: |
| 569 | case Type::Decltype: |
| 570 | case Type::UnresolvedUsing: |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 571 | case Type::TemplateTypeParm: |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 572 | return true; |
| 573 | |
| 574 | case Type::ConstantArray: |
| 575 | case Type::IncompleteArray: |
| 576 | case Type::VariableArray: |
| 577 | case Type::DependentSizedArray: |
| 578 | return IsPossiblyOpaquelyQualifiedType( |
| 579 | cast<ArrayType>(T)->getElementType()); |
| 580 | |
| 581 | default: |
| 582 | return false; |
| 583 | } |
| 584 | } |
| 585 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 586 | /// \brief Retrieve the depth and index of a template parameter. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 587 | static std::pair<unsigned, unsigned> |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 588 | getDepthAndIndex(NamedDecl *ND) { |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 589 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND)) |
| 590 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 591 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 592 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND)) |
| 593 | return std::make_pair(NTTP->getDepth(), NTTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 594 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 595 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND); |
| 596 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| 597 | } |
| 598 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 599 | /// \brief Retrieve the depth and index of an unexpanded parameter pack. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 600 | static std::pair<unsigned, unsigned> |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 601 | getDepthAndIndex(UnexpandedParameterPack UPP) { |
| 602 | if (const TemplateTypeParmType *TTP |
| 603 | = UPP.first.dyn_cast<const TemplateTypeParmType *>()) |
| 604 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 605 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 606 | return getDepthAndIndex(UPP.first.get<NamedDecl *>()); |
| 607 | } |
| 608 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 609 | /// \brief Helper function to build a TemplateParameter when we don't |
| 610 | /// know its type statically. |
| 611 | static TemplateParameter makeTemplateParameter(Decl *D) { |
| 612 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) |
| 613 | return TemplateParameter(TTP); |
Craig Topper | 4b482ee | 2013-07-08 04:24:47 +0000 | [diff] [blame] | 614 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 615 | return TemplateParameter(NTTP); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 616 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 617 | return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); |
| 618 | } |
| 619 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 620 | /// A pack that we're currently deducing. |
| 621 | struct clang::DeducedPack { |
| 622 | DeducedPack(unsigned Index) : Index(Index), Outer(nullptr) {} |
Craig Topper | 0a4e1f5 | 2013-07-08 04:44:01 +0000 | [diff] [blame] | 623 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 624 | // The index of the pack. |
| 625 | unsigned Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 626 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 627 | // The old value of the pack before we started deducing it. |
| 628 | DeducedTemplateArgument Saved; |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 629 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 630 | // A deferred value of this pack from an inner deduction, that couldn't be |
| 631 | // deduced because this deduction hadn't happened yet. |
| 632 | DeducedTemplateArgument DeferredDeduction; |
| 633 | |
| 634 | // The new value of the pack. |
| 635 | SmallVector<DeducedTemplateArgument, 4> New; |
| 636 | |
| 637 | // The outer deduction for this pack, if any. |
| 638 | DeducedPack *Outer; |
| 639 | }; |
| 640 | |
Benjamin Kramer | d5748c7 | 2015-03-23 12:31:05 +0000 | [diff] [blame] | 641 | namespace { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 642 | /// A scope in which we're performing pack deduction. |
| 643 | class PackDeductionScope { |
| 644 | public: |
| 645 | PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, |
| 646 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 647 | TemplateDeductionInfo &Info, TemplateArgument Pattern) |
| 648 | : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { |
| 649 | // Compute the set of template parameter indices that correspond to |
| 650 | // parameter packs expanded by the pack expansion. |
| 651 | { |
| 652 | llvm::SmallBitVector SawIndices(TemplateParams->size()); |
| 653 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 654 | S.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
| 655 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 656 | unsigned Depth, Index; |
| 657 | std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); |
| 658 | if (Depth == 0 && !SawIndices[Index]) { |
| 659 | SawIndices[Index] = true; |
| 660 | |
| 661 | // Save the deduced template argument for the parameter pack expanded |
| 662 | // by this pack expansion, then clear out the deduction. |
| 663 | DeducedPack Pack(Index); |
| 664 | Pack.Saved = Deduced[Index]; |
| 665 | Deduced[Index] = TemplateArgument(); |
| 666 | |
| 667 | Packs.push_back(Pack); |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | assert(!Packs.empty() && "Pack expansion without unexpanded packs?"); |
| 672 | |
| 673 | for (auto &Pack : Packs) { |
| 674 | if (Info.PendingDeducedPacks.size() > Pack.Index) |
| 675 | Pack.Outer = Info.PendingDeducedPacks[Pack.Index]; |
| 676 | else |
| 677 | Info.PendingDeducedPacks.resize(Pack.Index + 1); |
| 678 | Info.PendingDeducedPacks[Pack.Index] = &Pack; |
| 679 | |
| 680 | if (S.CurrentInstantiationScope) { |
| 681 | // If the template argument pack was explicitly specified, add that to |
| 682 | // the set of deduced arguments. |
| 683 | const TemplateArgument *ExplicitArgs; |
| 684 | unsigned NumExplicitArgs; |
| 685 | NamedDecl *PartiallySubstitutedPack = |
| 686 | S.CurrentInstantiationScope->getPartiallySubstitutedPack( |
| 687 | &ExplicitArgs, &NumExplicitArgs); |
| 688 | if (PartiallySubstitutedPack && |
| 689 | getDepthAndIndex(PartiallySubstitutedPack).second == Pack.Index) |
| 690 | Pack.New.append(ExplicitArgs, ExplicitArgs + NumExplicitArgs); |
| 691 | } |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 692 | } |
| 693 | } |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 694 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 695 | ~PackDeductionScope() { |
| 696 | for (auto &Pack : Packs) |
| 697 | Info.PendingDeducedPacks[Pack.Index] = Pack.Outer; |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 698 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 699 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 700 | /// Move to deducing the next element in each pack that is being deduced. |
| 701 | void nextPackElement() { |
| 702 | // Capture the deduced template arguments for each parameter pack expanded |
| 703 | // by this pack expansion, add them to the list of arguments we've deduced |
| 704 | // for that pack, then clear out the deduced argument. |
| 705 | for (auto &Pack : Packs) { |
| 706 | DeducedTemplateArgument &DeducedArg = Deduced[Pack.Index]; |
| 707 | if (!DeducedArg.isNull()) { |
| 708 | Pack.New.push_back(DeducedArg); |
| 709 | DeducedArg = DeducedTemplateArgument(); |
| 710 | } |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | /// \brief Finish template argument deduction for a set of argument packs, |
| 715 | /// producing the argument packs and checking for consistency with prior |
| 716 | /// deductions. |
| 717 | Sema::TemplateDeductionResult finish(bool HasAnyArguments) { |
| 718 | // Build argument packs for each of the parameter packs expanded by this |
| 719 | // pack expansion. |
| 720 | for (auto &Pack : Packs) { |
| 721 | // Put back the old value for this pack. |
| 722 | Deduced[Pack.Index] = Pack.Saved; |
| 723 | |
| 724 | // Build or find a new value for this pack. |
| 725 | DeducedTemplateArgument NewPack; |
| 726 | if (HasAnyArguments && Pack.New.empty()) { |
| 727 | if (Pack.DeferredDeduction.isNull()) { |
| 728 | // We were not able to deduce anything for this parameter pack |
| 729 | // (because it only appeared in non-deduced contexts), so just |
| 730 | // restore the saved argument pack. |
| 731 | continue; |
| 732 | } |
| 733 | |
| 734 | NewPack = Pack.DeferredDeduction; |
| 735 | Pack.DeferredDeduction = TemplateArgument(); |
| 736 | } else if (Pack.New.empty()) { |
| 737 | // If we deduced an empty argument pack, create it now. |
| 738 | NewPack = DeducedTemplateArgument(TemplateArgument::getEmptyPack()); |
| 739 | } else { |
| 740 | TemplateArgument *ArgumentPack = |
| 741 | new (S.Context) TemplateArgument[Pack.New.size()]; |
| 742 | std::copy(Pack.New.begin(), Pack.New.end(), ArgumentPack); |
| 743 | NewPack = DeducedTemplateArgument( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 744 | TemplateArgument(llvm::makeArrayRef(ArgumentPack, Pack.New.size())), |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 745 | Pack.New[0].wasDeducedFromArrayBound()); |
| 746 | } |
| 747 | |
| 748 | // Pick where we're going to put the merged pack. |
| 749 | DeducedTemplateArgument *Loc; |
| 750 | if (Pack.Outer) { |
| 751 | if (Pack.Outer->DeferredDeduction.isNull()) { |
| 752 | // Defer checking this pack until we have a complete pack to compare |
| 753 | // it against. |
| 754 | Pack.Outer->DeferredDeduction = NewPack; |
| 755 | continue; |
| 756 | } |
| 757 | Loc = &Pack.Outer->DeferredDeduction; |
| 758 | } else { |
| 759 | Loc = &Deduced[Pack.Index]; |
| 760 | } |
| 761 | |
| 762 | // Check the new pack matches any previous value. |
| 763 | DeducedTemplateArgument OldPack = *Loc; |
| 764 | DeducedTemplateArgument Result = |
| 765 | checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 766 | |
| 767 | // If we deferred a deduction of this pack, check that one now too. |
| 768 | if (!Result.isNull() && !Pack.DeferredDeduction.isNull()) { |
| 769 | OldPack = Result; |
| 770 | NewPack = Pack.DeferredDeduction; |
| 771 | Result = checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 772 | } |
| 773 | |
| 774 | if (Result.isNull()) { |
| 775 | Info.Param = |
| 776 | makeTemplateParameter(TemplateParams->getParam(Pack.Index)); |
| 777 | Info.FirstArg = OldPack; |
| 778 | Info.SecondArg = NewPack; |
| 779 | return Sema::TDK_Inconsistent; |
| 780 | } |
| 781 | |
| 782 | *Loc = Result; |
| 783 | } |
| 784 | |
| 785 | return Sema::TDK_Success; |
| 786 | } |
| 787 | |
| 788 | private: |
| 789 | Sema &S; |
| 790 | TemplateParameterList *TemplateParams; |
| 791 | SmallVectorImpl<DeducedTemplateArgument> &Deduced; |
| 792 | TemplateDeductionInfo &Info; |
| 793 | |
| 794 | SmallVector<DeducedPack, 2> Packs; |
| 795 | }; |
Benjamin Kramer | d5748c7 | 2015-03-23 12:31:05 +0000 | [diff] [blame] | 796 | } // namespace |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 797 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 798 | /// \brief Deduce the template arguments by comparing the list of parameter |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 799 | /// types to the list of argument types, as in the parameter-type-lists of |
| 800 | /// function types (C++ [temp.deduct.type]p10). |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 801 | /// |
| 802 | /// \param S The semantic analysis object within which we are deducing |
| 803 | /// |
| 804 | /// \param TemplateParams The template parameters that we are deducing |
| 805 | /// |
| 806 | /// \param Params The list of parameter types |
| 807 | /// |
| 808 | /// \param NumParams The number of types in \c Params |
| 809 | /// |
| 810 | /// \param Args The list of argument types |
| 811 | /// |
| 812 | /// \param NumArgs The number of types in \c Args |
| 813 | /// |
| 814 | /// \param Info information about the template argument deduction itself |
| 815 | /// |
| 816 | /// \param Deduced the deduced template arguments |
| 817 | /// |
| 818 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
| 819 | /// how template argument deduction is performed. |
| 820 | /// |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 821 | /// \param PartialOrdering If true, we are performing template argument |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 822 | /// deduction for during partial ordering for a call |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 823 | /// (C++0x [temp.deduct.partial]). |
| 824 | /// |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 825 | /// \returns the result of template argument deduction so far. Note that a |
| 826 | /// "success" result means that template argument deduction has not yet failed, |
| 827 | /// but it may still fail, later, for other reasons. |
| 828 | static Sema::TemplateDeductionResult |
| 829 | DeduceTemplateArguments(Sema &S, |
| 830 | TemplateParameterList *TemplateParams, |
| 831 | const QualType *Params, unsigned NumParams, |
| 832 | const QualType *Args, unsigned NumArgs, |
| 833 | TemplateDeductionInfo &Info, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 834 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 835 | unsigned TDF, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 836 | bool PartialOrdering = false) { |
Douglas Gregor | 86bea35 | 2011-01-05 23:23:17 +0000 | [diff] [blame] | 837 | // Fast-path check to see if we have too many/too few arguments. |
| 838 | if (NumParams != NumArgs && |
| 839 | !(NumParams && isa<PackExpansionType>(Params[NumParams - 1])) && |
| 840 | !(NumArgs && isa<PackExpansionType>(Args[NumArgs - 1]))) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 841 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 842 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 843 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 844 | // Similarly, if P has a form that contains (T), then each parameter type |
| 845 | // Pi of the respective parameter-type- list of P is compared with the |
| 846 | // corresponding parameter type Ai of the corresponding parameter-type-list |
| 847 | // of A. [...] |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 848 | unsigned ArgIdx = 0, ParamIdx = 0; |
| 849 | for (; ParamIdx != NumParams; ++ParamIdx) { |
| 850 | // Check argument types. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 851 | const PackExpansionType *Expansion |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 852 | = dyn_cast<PackExpansionType>(Params[ParamIdx]); |
| 853 | if (!Expansion) { |
| 854 | // Simple case: compare the parameter and argument types at this point. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 855 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 856 | // Make sure we have an argument. |
| 857 | if (ArgIdx >= NumArgs) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 858 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 859 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 860 | if (isa<PackExpansionType>(Args[ArgIdx])) { |
| 861 | // C++0x [temp.deduct.type]p22: |
| 862 | // If the original function parameter associated with A is a function |
| 863 | // parameter pack and the function parameter associated with P is not |
| 864 | // a function parameter pack, then template argument deduction fails. |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 865 | return Sema::TDK_MiscellaneousDeductionFailure; |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 866 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 867 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 868 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 869 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 870 | Params[ParamIdx], Args[ArgIdx], |
| 871 | Info, Deduced, TDF, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 872 | PartialOrdering)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 873 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 874 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 875 | ++ArgIdx; |
| 876 | continue; |
| 877 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 878 | |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 879 | // C++0x [temp.deduct.type]p5: |
| 880 | // The non-deduced contexts are: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 881 | // - 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] | 882 | // parameter-declaration-clause. |
| 883 | if (ParamIdx + 1 < NumParams) |
| 884 | return Sema::TDK_Success; |
| 885 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 886 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 887 | // If the parameter-declaration corresponding to Pi is a function |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 888 | // parameter pack, then the type of its declarator- id is compared with |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 889 | // each remaining parameter type in the parameter-type-list of A. Each |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 890 | // comparison deduces template arguments for subsequent positions in the |
| 891 | // template parameter packs expanded by the function parameter pack. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 892 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 893 | QualType Pattern = Expansion->getPattern(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 894 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 895 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 896 | bool HasAnyArguments = false; |
| 897 | for (; ArgIdx < NumArgs; ++ArgIdx) { |
| 898 | HasAnyArguments = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 899 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 900 | // Deduce template arguments from the pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 901 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 902 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, Pattern, |
| 903 | Args[ArgIdx], Info, Deduced, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 904 | TDF, PartialOrdering)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 905 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 906 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 907 | PackScope.nextPackElement(); |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 908 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 909 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 910 | // Build argument packs for each of the parameter packs expanded by this |
| 911 | // pack expansion. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 912 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 913 | return Result; |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 914 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 915 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 916 | // Make sure we don't have any extra arguments. |
| 917 | if (ArgIdx < NumArgs) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 918 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 919 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 920 | return Sema::TDK_Success; |
| 921 | } |
| 922 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 923 | /// \brief Determine whether the parameter has qualifiers that are either |
| 924 | /// inconsistent with or a superset of the argument's qualifiers. |
| 925 | static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType, |
| 926 | QualType ArgType) { |
| 927 | Qualifiers ParamQs = ParamType.getQualifiers(); |
| 928 | Qualifiers ArgQs = ArgType.getQualifiers(); |
| 929 | |
| 930 | if (ParamQs == ArgQs) |
| 931 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 932 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 933 | // Mismatched (but not missing) Objective-C GC attributes. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 934 | if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() && |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 935 | ParamQs.hasObjCGCAttr()) |
| 936 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 937 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 938 | // Mismatched (but not missing) address spaces. |
| 939 | if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() && |
| 940 | ParamQs.hasAddressSpace()) |
| 941 | return true; |
| 942 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 943 | // Mismatched (but not missing) Objective-C lifetime qualifiers. |
| 944 | if (ParamQs.getObjCLifetime() != ArgQs.getObjCLifetime() && |
| 945 | ParamQs.hasObjCLifetime()) |
| 946 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 947 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 948 | // CVR qualifier superset. |
| 949 | return (ParamQs.getCVRQualifiers() != ArgQs.getCVRQualifiers()) && |
| 950 | ((ParamQs.getCVRQualifiers() | ArgQs.getCVRQualifiers()) |
| 951 | == ParamQs.getCVRQualifiers()); |
| 952 | } |
| 953 | |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 954 | /// \brief Compare types for equality with respect to possibly compatible |
| 955 | /// function types (noreturn adjustment, implicit calling conventions). If any |
| 956 | /// of parameter and argument is not a function, just perform type comparison. |
| 957 | /// |
| 958 | /// \param Param the template parameter type. |
| 959 | /// |
| 960 | /// \param Arg the argument type. |
| 961 | bool Sema::isSameOrCompatibleFunctionType(CanQualType Param, |
| 962 | CanQualType Arg) { |
| 963 | const FunctionType *ParamFunction = Param->getAs<FunctionType>(), |
| 964 | *ArgFunction = Arg->getAs<FunctionType>(); |
| 965 | |
| 966 | // Just compare if not functions. |
| 967 | if (!ParamFunction || !ArgFunction) |
| 968 | return Param == Arg; |
| 969 | |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 970 | // Noreturn and noexcept adjustment. |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 971 | QualType AdjustedParam; |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 972 | if (IsFunctionConversion(Param, Arg, AdjustedParam)) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 973 | return Arg == Context.getCanonicalType(AdjustedParam); |
| 974 | |
| 975 | // FIXME: Compatible calling conventions. |
| 976 | |
| 977 | return Param == Arg; |
| 978 | } |
| 979 | |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 980 | /// \brief Deduce the template arguments by comparing the parameter type and |
| 981 | /// the argument type (C++ [temp.deduct.type]). |
| 982 | /// |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 983 | /// \param S the semantic analysis object within which we are deducing |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 984 | /// |
| 985 | /// \param TemplateParams the template parameters that we are deducing |
| 986 | /// |
| 987 | /// \param ParamIn the parameter type |
| 988 | /// |
| 989 | /// \param ArgIn the argument type |
| 990 | /// |
| 991 | /// \param Info information about the template argument deduction itself |
| 992 | /// |
| 993 | /// \param Deduced the deduced template arguments |
| 994 | /// |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 995 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 996 | /// how template argument deduction is performed. |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 997 | /// |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 998 | /// \param PartialOrdering Whether we're performing template argument deduction |
| 999 | /// in the context of partial ordering (C++0x [temp.deduct.partial]). |
| 1000 | /// |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1001 | /// \returns the result of template argument deduction so far. Note that a |
| 1002 | /// "success" result means that template argument deduction has not yet failed, |
| 1003 | /// but it may still fail, later, for other reasons. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1004 | static Sema::TemplateDeductionResult |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1005 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 1006 | TemplateParameterList *TemplateParams, |
| 1007 | QualType ParamIn, QualType ArgIn, |
| 1008 | TemplateDeductionInfo &Info, |
| 1009 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 1010 | unsigned TDF, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1011 | bool PartialOrdering, |
| 1012 | bool DeducedFromArrayBound) { |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1013 | // We only want to look at the canonical types, since typedefs and |
| 1014 | // sugar are not part of template argument deduction. |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1015 | QualType Param = S.Context.getCanonicalType(ParamIn); |
| 1016 | QualType Arg = S.Context.getCanonicalType(ArgIn); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1017 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1018 | // If the argument type is a pack expansion, look at its pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1019 | // This isn't explicitly called out |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1020 | if (const PackExpansionType *ArgExpansion |
| 1021 | = dyn_cast<PackExpansionType>(Arg)) |
| 1022 | Arg = ArgExpansion->getPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1023 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1024 | if (PartialOrdering) { |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1025 | // C++11 [temp.deduct.partial]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1026 | // Before the partial ordering is done, certain transformations are |
| 1027 | // performed on the types used for partial ordering: |
| 1028 | // - 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] | 1029 | const ReferenceType *ParamRef = Param->getAs<ReferenceType>(); |
| 1030 | if (ParamRef) |
| 1031 | Param = ParamRef->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1032 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1033 | // - If A is a reference type, A is replaced by the type referred to. |
| 1034 | const ReferenceType *ArgRef = Arg->getAs<ReferenceType>(); |
| 1035 | if (ArgRef) |
| 1036 | Arg = ArgRef->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1037 | |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1038 | if (ParamRef && ArgRef && S.Context.hasSameUnqualifiedType(Param, Arg)) { |
| 1039 | // C++11 [temp.deduct.partial]p9: |
| 1040 | // If, for a given type, deduction succeeds in both directions (i.e., |
| 1041 | // the types are identical after the transformations above) and both |
| 1042 | // P and A were reference types [...]: |
| 1043 | // - if [one type] was an lvalue reference and [the other type] was |
| 1044 | // not, [the other type] is not considered to be at least as |
| 1045 | // specialized as [the first type] |
| 1046 | // - if [one type] is more cv-qualified than [the other type], |
| 1047 | // [the other type] is not considered to be at least as specialized |
| 1048 | // as [the first type] |
| 1049 | // Objective-C ARC adds: |
| 1050 | // - [one type] has non-trivial lifetime, [the other type] has |
| 1051 | // __unsafe_unretained lifetime, and the types are otherwise |
| 1052 | // identical |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1053 | // |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1054 | // A is "considered to be at least as specialized" as P iff deduction |
| 1055 | // succeeds, so we model this as a deduction failure. Note that |
| 1056 | // [the first type] is P and [the other type] is A here; the standard |
| 1057 | // gets this backwards. |
Douglas Gregor | 85894a8 | 2011-04-30 17:07:52 +0000 | [diff] [blame] | 1058 | Qualifiers ParamQuals = Param.getQualifiers(); |
| 1059 | Qualifiers ArgQuals = Arg.getQualifiers(); |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1060 | if ((ParamRef->isLValueReferenceType() && |
| 1061 | !ArgRef->isLValueReferenceType()) || |
| 1062 | ParamQuals.isStrictSupersetOf(ArgQuals) || |
| 1063 | (ParamQuals.hasNonTrivialObjCLifetime() && |
| 1064 | ArgQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone && |
| 1065 | ParamQuals.withoutObjCLifetime() == |
| 1066 | ArgQuals.withoutObjCLifetime())) { |
| 1067 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1068 | Info.SecondArg = TemplateArgument(ArgIn); |
| 1069 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 6beabee | 2014-01-02 19:42:02 +0000 | [diff] [blame] | 1070 | } |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1071 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1072 | |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1073 | // C++11 [temp.deduct.partial]p7: |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1074 | // Remove any top-level cv-qualifiers: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1075 | // - 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] | 1076 | // version of P. |
| 1077 | Param = Param.getUnqualifiedType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1078 | // - 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] | 1079 | // version of A. |
| 1080 | Arg = Arg.getUnqualifiedType(); |
| 1081 | } else { |
| 1082 | // C++0x [temp.deduct.call]p4 bullet 1: |
| 1083 | // - If the original P is a reference type, the deduced A (i.e., the type |
| 1084 | // referred to by the reference) can be more cv-qualified than the |
| 1085 | // transformed A. |
| 1086 | if (TDF & TDF_ParamWithReferenceType) { |
| 1087 | Qualifiers Quals; |
| 1088 | QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals); |
| 1089 | Quals.setCVRQualifiers(Quals.getCVRQualifiers() & |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 1090 | Arg.getCVRQualifiers()); |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1091 | Param = S.Context.getQualifiedType(UnqualParam, Quals); |
| 1092 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1093 | |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1094 | if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) { |
| 1095 | // C++0x [temp.deduct.type]p10: |
| 1096 | // If P and A are function types that originated from deduction when |
| 1097 | // taking the address of a function template (14.8.2.2) or when deducing |
| 1098 | // 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] | 1099 | // Ai are parameters of the top-level parameter-type-list of P and A, |
| 1100 | // respectively, Pi is adjusted if it is an rvalue reference to a |
| 1101 | // cv-unqualified template parameter and Ai is an lvalue reference, in |
| 1102 | // 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] | 1103 | // type (i.e., T&& is changed to simply T). [ Note: As a result, when |
| 1104 | // 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] | 1105 | // deduced as X&. - end note ] |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1106 | TDF &= ~TDF_TopLevelParameterTypeList; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1107 | |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1108 | if (const RValueReferenceType *ParamRef |
| 1109 | = Param->getAs<RValueReferenceType>()) { |
| 1110 | if (isa<TemplateTypeParmType>(ParamRef->getPointeeType()) && |
| 1111 | !ParamRef->getPointeeType().getQualifiers()) |
| 1112 | if (Arg->isLValueReferenceType()) |
| 1113 | Param = ParamRef->getPointeeType(); |
| 1114 | } |
| 1115 | } |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1116 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1117 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1118 | // C++ [temp.deduct.type]p9: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1119 | // A template type argument T, a template template argument TT or a |
| 1120 | // 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] | 1121 | // the following forms: |
| 1122 | // |
| 1123 | // T |
| 1124 | // cv-list T |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1125 | if (const TemplateTypeParmType *TemplateTypeParm |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1126 | = Param->getAs<TemplateTypeParmType>()) { |
Douglas Gregor | 4ea5dec | 2011-09-22 15:57:07 +0000 | [diff] [blame] | 1127 | // Just skip any attempts to deduce from a placeholder type. |
| 1128 | if (Arg->isPlaceholderType()) |
| 1129 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1130 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1131 | unsigned Index = TemplateTypeParm->getIndex(); |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1132 | bool RecanonicalizeArg = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1133 | |
Douglas Gregor | 6045482 | 2009-07-22 20:02:25 +0000 | [diff] [blame] | 1134 | // If the argument type is an array type, move the qualifiers up to the |
| 1135 | // 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] | 1136 | if (isa<ArrayType>(Arg)) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1137 | Qualifiers Quals; |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1138 | Arg = S.Context.getUnqualifiedArrayType(Arg, Quals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1139 | if (Quals) { |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1140 | Arg = S.Context.getQualifiedType(Arg, Quals); |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1141 | RecanonicalizeArg = true; |
| 1142 | } |
| 1143 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1144 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1145 | // The argument type can not be less qualified than the parameter |
| 1146 | // type. |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1147 | if (!(TDF & TDF_IgnoreQualifiers) && |
| 1148 | hasInconsistentOrSupersetQualifiersOf(Param, Arg)) { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1149 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1150 | Info.FirstArg = TemplateArgument(Param); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1151 | Info.SecondArg = TemplateArgument(Arg); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1152 | return Sema::TDK_Underqualified; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1153 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1154 | |
| 1155 | assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0"); |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1156 | assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function"); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1157 | QualType DeducedType = Arg; |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1158 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1159 | // Remove any qualifiers on the parameter from the deduced type. |
| 1160 | // We checked the qualifiers for consistency above. |
| 1161 | Qualifiers DeducedQs = DeducedType.getQualifiers(); |
| 1162 | Qualifiers ParamQs = Param.getQualifiers(); |
| 1163 | DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers()); |
| 1164 | if (ParamQs.hasObjCGCAttr()) |
| 1165 | DeducedQs.removeObjCGCAttr(); |
| 1166 | if (ParamQs.hasAddressSpace()) |
| 1167 | DeducedQs.removeAddressSpace(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1168 | if (ParamQs.hasObjCLifetime()) |
| 1169 | DeducedQs.removeObjCLifetime(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1170 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1171 | // Objective-C ARC: |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1172 | // If template deduction would produce a lifetime qualifier on a type |
| 1173 | // that is not a lifetime type, template argument deduction fails. |
| 1174 | if (ParamQs.hasObjCLifetime() && !DeducedType->isObjCLifetimeType() && |
| 1175 | !DeducedType->isDependentType()) { |
| 1176 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1177 | Info.FirstArg = TemplateArgument(Param); |
| 1178 | Info.SecondArg = TemplateArgument(Arg); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1179 | return Sema::TDK_Underqualified; |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1180 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1181 | |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1182 | // Objective-C ARC: |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1183 | // If template deduction would produce an argument type with lifetime type |
| 1184 | // but no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1185 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1186 | DeducedType->isObjCLifetimeType() && |
| 1187 | !DeducedQs.hasObjCLifetime()) |
| 1188 | DeducedQs.setObjCLifetime(Qualifiers::OCL_Strong); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1189 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1190 | DeducedType = S.Context.getQualifiedType(DeducedType.getUnqualifiedType(), |
| 1191 | DeducedQs); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1192 | |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1193 | if (RecanonicalizeArg) |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1194 | DeducedType = S.Context.getCanonicalType(DeducedType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1195 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1196 | DeducedTemplateArgument NewDeduced(DeducedType, DeducedFromArrayBound); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1197 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1198 | Deduced[Index], |
| 1199 | NewDeduced); |
| 1200 | if (Result.isNull()) { |
| 1201 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1202 | Info.FirstArg = Deduced[Index]; |
| 1203 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1204 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1205 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1206 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1207 | Deduced[Index] = Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1208 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1211 | // Set up the template argument deduction information for a failure. |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1212 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1213 | Info.SecondArg = TemplateArgument(ArgIn); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1214 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 1215 | // If the parameter is an already-substituted template parameter |
| 1216 | // pack, do nothing: we don't know which of its arguments to look |
| 1217 | // at, so we have to wait until all of the parameter packs in this |
| 1218 | // expansion have arguments. |
| 1219 | if (isa<SubstTemplateTypeParmPackType>(Param)) |
| 1220 | return Sema::TDK_Success; |
| 1221 | |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1222 | // Check the cv-qualifiers on the parameter and argument types. |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1223 | CanQualType CanParam = S.Context.getCanonicalType(Param); |
| 1224 | CanQualType CanArg = S.Context.getCanonicalType(Arg); |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1225 | if (!(TDF & TDF_IgnoreQualifiers)) { |
| 1226 | if (TDF & TDF_ParamWithReferenceType) { |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1227 | if (hasInconsistentOrSupersetQualifiersOf(Param, Arg)) |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1228 | return Sema::TDK_NonDeducedMismatch; |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 1229 | } else if (!IsPossiblyOpaquelyQualifiedType(Param)) { |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1230 | if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1231 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1232 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1233 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1234 | // If the parameter type is not dependent, there is nothing to deduce. |
| 1235 | if (!Param->isDependentType()) { |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1236 | if (!(TDF & TDF_SkipNonDependent)) { |
| 1237 | bool NonDeduced = (TDF & TDF_InOverloadResolution)? |
| 1238 | !S.isSameOrCompatibleFunctionType(CanParam, CanArg) : |
| 1239 | Param != Arg; |
| 1240 | if (NonDeduced) { |
| 1241 | return Sema::TDK_NonDeducedMismatch; |
| 1242 | } |
| 1243 | } |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1244 | return Sema::TDK_Success; |
| 1245 | } |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1246 | } else if (!Param->isDependentType()) { |
| 1247 | CanQualType ParamUnqualType = CanParam.getUnqualifiedType(), |
| 1248 | ArgUnqualType = CanArg.getUnqualifiedType(); |
| 1249 | bool Success = (TDF & TDF_InOverloadResolution)? |
| 1250 | S.isSameOrCompatibleFunctionType(ParamUnqualType, |
| 1251 | ArgUnqualType) : |
| 1252 | ParamUnqualType == ArgUnqualType; |
| 1253 | if (Success) |
| 1254 | return Sema::TDK_Success; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1255 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1256 | |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1257 | switch (Param->getTypeClass()) { |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1258 | // Non-canonical types cannot appear here. |
| 1259 | #define NON_CANONICAL_TYPE(Class, Base) \ |
| 1260 | case Type::Class: llvm_unreachable("deducing non-canonical type: " #Class); |
| 1261 | #define TYPE(Class, Base) |
| 1262 | #include "clang/AST/TypeNodes.def" |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1263 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1264 | case Type::TemplateTypeParm: |
| 1265 | case Type::SubstTemplateTypeParmPack: |
| 1266 | llvm_unreachable("Type nodes handled above"); |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1267 | |
| 1268 | // These types cannot be dependent, so simply check whether the types are |
| 1269 | // the same. |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1270 | case Type::Builtin: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1271 | case Type::VariableArray: |
| 1272 | case Type::Vector: |
| 1273 | case Type::FunctionNoProto: |
| 1274 | case Type::Record: |
| 1275 | case Type::Enum: |
| 1276 | case Type::ObjCObject: |
| 1277 | case Type::ObjCInterface: |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1278 | case Type::ObjCObjectPointer: { |
| 1279 | if (TDF & TDF_SkipNonDependent) |
| 1280 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1281 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1282 | if (TDF & TDF_IgnoreQualifiers) { |
| 1283 | Param = Param.getUnqualifiedType(); |
| 1284 | Arg = Arg.getUnqualifiedType(); |
| 1285 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1286 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1287 | return Param == Arg? Sema::TDK_Success : Sema::TDK_NonDeducedMismatch; |
| 1288 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1289 | |
| 1290 | // _Complex T [placeholder extension] |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1291 | case Type::Complex: |
| 1292 | if (const ComplexType *ComplexArg = Arg->getAs<ComplexType>()) |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1293 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1294 | cast<ComplexType>(Param)->getElementType(), |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1295 | ComplexArg->getElementType(), |
| 1296 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1297 | |
| 1298 | return Sema::TDK_NonDeducedMismatch; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1299 | |
| 1300 | // _Atomic T [extension] |
| 1301 | case Type::Atomic: |
| 1302 | if (const AtomicType *AtomicArg = Arg->getAs<AtomicType>()) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1303 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1304 | cast<AtomicType>(Param)->getValueType(), |
| 1305 | AtomicArg->getValueType(), |
| 1306 | Info, Deduced, TDF); |
| 1307 | |
| 1308 | return Sema::TDK_NonDeducedMismatch; |
| 1309 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1310 | // T * |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1311 | case Type::Pointer: { |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1312 | QualType PointeeType; |
| 1313 | if (const PointerType *PointerArg = Arg->getAs<PointerType>()) { |
| 1314 | PointeeType = PointerArg->getPointeeType(); |
| 1315 | } else if (const ObjCObjectPointerType *PointerArg |
| 1316 | = Arg->getAs<ObjCObjectPointerType>()) { |
| 1317 | PointeeType = PointerArg->getPointeeType(); |
| 1318 | } else { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1319 | return Sema::TDK_NonDeducedMismatch; |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1320 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1321 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1322 | unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1323 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1324 | cast<PointerType>(Param)->getPointeeType(), |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1325 | PointeeType, |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1326 | Info, Deduced, SubTDF); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1327 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1328 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1329 | // T & |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1330 | case Type::LValueReference: { |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 1331 | const LValueReferenceType *ReferenceArg = |
| 1332 | Arg->getAs<LValueReferenceType>(); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1333 | if (!ReferenceArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1334 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1335 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1336 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1337 | cast<LValueReferenceType>(Param)->getPointeeType(), |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1338 | ReferenceArg->getPointeeType(), Info, Deduced, 0); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1339 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1340 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1341 | // T && [C++0x] |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1342 | case Type::RValueReference: { |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 1343 | const RValueReferenceType *ReferenceArg = |
| 1344 | Arg->getAs<RValueReferenceType>(); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1345 | if (!ReferenceArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1346 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1347 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1348 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1349 | cast<RValueReferenceType>(Param)->getPointeeType(), |
| 1350 | ReferenceArg->getPointeeType(), |
| 1351 | Info, Deduced, 0); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1352 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1353 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1354 | // T [] (implied, but not stated explicitly) |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1355 | case Type::IncompleteArray: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1356 | const IncompleteArrayType *IncompleteArrayArg = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1357 | S.Context.getAsIncompleteArrayType(Arg); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1358 | if (!IncompleteArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1359 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1360 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1361 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1362 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1363 | S.Context.getAsIncompleteArrayType(Param)->getElementType(), |
| 1364 | IncompleteArrayArg->getElementType(), |
| 1365 | Info, Deduced, SubTDF); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1366 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1367 | |
| 1368 | // T [integer-constant] |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1369 | case Type::ConstantArray: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1370 | const ConstantArrayType *ConstantArrayArg = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1371 | S.Context.getAsConstantArrayType(Arg); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1372 | if (!ConstantArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1373 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1374 | |
| 1375 | const ConstantArrayType *ConstantArrayParm = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1376 | S.Context.getAsConstantArrayType(Param); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1377 | if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1378 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1379 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1380 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1381 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1382 | ConstantArrayParm->getElementType(), |
| 1383 | ConstantArrayArg->getElementType(), |
| 1384 | Info, Deduced, SubTDF); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1387 | // type [i] |
| 1388 | case Type::DependentSizedArray: { |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1389 | const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1390 | if (!ArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1391 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1392 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1393 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
| 1394 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1395 | // Check the element type of the arrays |
| 1396 | const DependentSizedArrayType *DependentArrayParm |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1397 | = S.Context.getAsDependentSizedArrayType(Param); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1398 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1399 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1400 | DependentArrayParm->getElementType(), |
| 1401 | ArrayArg->getElementType(), |
| 1402 | Info, Deduced, SubTDF)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1403 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1405 | // Determine the array bound is something we can deduce. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1406 | NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1407 | = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr()); |
| 1408 | if (!NTTP) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1409 | return Sema::TDK_Success; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1410 | |
| 1411 | // We can perform template argument deduction for the given non-type |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1412 | // template parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1413 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1414 | "Cannot deduce non-type template argument at depth > 0"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1415 | if (const ConstantArrayType *ConstantArrayArg |
Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1416 | = dyn_cast<ConstantArrayType>(ArrayArg)) { |
| 1417 | llvm::APSInt Size(ConstantArrayArg->getSize()); |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1418 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, Size, |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1419 | S.Context.getSizeType(), |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1420 | /*ArrayBound=*/true, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1421 | Info, Deduced); |
Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1422 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1423 | if (const DependentSizedArrayType *DependentArrayArg |
| 1424 | = dyn_cast<DependentSizedArrayType>(ArrayArg)) |
Douglas Gregor | 7a49ead | 2010-12-22 23:15:38 +0000 | [diff] [blame] | 1425 | if (DependentArrayArg->getSizeExpr()) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1426 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
Douglas Gregor | 7a49ead | 2010-12-22 23:15:38 +0000 | [diff] [blame] | 1427 | DependentArrayArg->getSizeExpr(), |
| 1428 | Info, Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1430 | // Incomplete type does not match a dependently-sized array type |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1431 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1432 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1433 | |
| 1434 | // type(*)(T) |
| 1435 | // T(*)() |
| 1436 | // T(*)(T) |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1437 | case Type::FunctionProto: { |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1438 | unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1439 | const FunctionProtoType *FunctionProtoArg = |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1440 | dyn_cast<FunctionProtoType>(Arg); |
| 1441 | if (!FunctionProtoArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1442 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1443 | |
| 1444 | const FunctionProtoType *FunctionProtoParam = |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1445 | cast<FunctionProtoType>(Param); |
Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1446 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1447 | if (FunctionProtoParam->getTypeQuals() |
Douglas Gregor | 54e462a | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1448 | != FunctionProtoArg->getTypeQuals() || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1449 | FunctionProtoParam->getRefQualifier() |
Douglas Gregor | 54e462a | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1450 | != FunctionProtoArg->getRefQualifier() || |
| 1451 | FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1452 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1453 | |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1454 | // Check return types. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1455 | if (Sema::TemplateDeductionResult Result = |
| 1456 | DeduceTemplateArgumentsByTypeMatch( |
| 1457 | S, TemplateParams, FunctionProtoParam->getReturnType(), |
| 1458 | FunctionProtoArg->getReturnType(), Info, Deduced, 0)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1459 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1460 | |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1461 | return DeduceTemplateArguments( |
| 1462 | S, TemplateParams, FunctionProtoParam->param_type_begin(), |
| 1463 | FunctionProtoParam->getNumParams(), |
| 1464 | FunctionProtoArg->param_type_begin(), |
| 1465 | FunctionProtoArg->getNumParams(), Info, Deduced, SubTDF); |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1466 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1467 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1468 | case Type::InjectedClassName: { |
| 1469 | // Treat a template's injected-class-name as if the template |
| 1470 | // specialization type had been used. |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1471 | Param = cast<InjectedClassNameType>(Param) |
| 1472 | ->getInjectedSpecializationType(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1473 | assert(isa<TemplateSpecializationType>(Param) && |
| 1474 | "injected class name is not a template specialization type"); |
| 1475 | // fall through |
| 1476 | } |
| 1477 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1478 | // template-name<T> (where template-name refers to a class template) |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1479 | // template-name<i> |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1480 | // TT<T> |
| 1481 | // TT<i> |
| 1482 | // TT<> |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1483 | case Type::TemplateSpecialization: { |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1484 | const TemplateSpecializationType *SpecParam = |
| 1485 | cast<TemplateSpecializationType>(Param); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1486 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1487 | // When Arg cannot be a derived class, we can just try to deduce template |
| 1488 | // arguments from the template-id. |
| 1489 | const RecordType *RecordT = Arg->getAs<RecordType>(); |
| 1490 | if (!(TDF & TDF_DerivedClass) || !RecordT) |
| 1491 | return DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg, Info, |
| 1492 | Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1493 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1494 | SmallVector<DeducedTemplateArgument, 8> DeducedOrig(Deduced.begin(), |
| 1495 | Deduced.end()); |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1496 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1497 | Sema::TemplateDeductionResult Result = DeduceTemplateArguments( |
| 1498 | S, TemplateParams, SpecParam, Arg, Info, Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1499 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1500 | if (Result == Sema::TDK_Success) |
| 1501 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1502 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1503 | // We cannot inspect base classes as part of deduction when the type |
| 1504 | // is incomplete, so either instantiate any templates necessary to |
| 1505 | // complete the type, or skip over it if it cannot be completed. |
| 1506 | if (!S.isCompleteType(Info.getLocation(), Arg)) |
| 1507 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1508 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1509 | // C++14 [temp.deduct.call] p4b3: |
| 1510 | // If P is a class and P has the form simple-template-id, then the |
| 1511 | // transformed A can be a derived class of the deduced A. Likewise if |
| 1512 | // P is a pointer to a class of the form simple-template-id, the |
| 1513 | // transformed A can be a pointer to a derived class pointed to by the |
| 1514 | // deduced A. |
| 1515 | // |
| 1516 | // These alternatives are considered only if type deduction would |
| 1517 | // otherwise fail. If they yield more than one possible deduced A, the |
| 1518 | // type deduction fails. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1519 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1520 | // Reset the incorrectly deduced argument from above. |
| 1521 | Deduced = DeducedOrig; |
| 1522 | |
| 1523 | // Use data recursion to crawl through the list of base classes. |
| 1524 | // Visited contains the set of nodes we have already visited, while |
| 1525 | // ToVisit is our stack of records that we still need to visit. |
| 1526 | llvm::SmallPtrSet<const RecordType *, 8> Visited; |
| 1527 | SmallVector<const RecordType *, 8> ToVisit; |
| 1528 | ToVisit.push_back(RecordT); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1529 | bool Successful = false; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1530 | SmallVector<DeducedTemplateArgument, 8> SuccessfulDeduced; |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1531 | while (!ToVisit.empty()) { |
| 1532 | // Retrieve the next class in the inheritance hierarchy. |
| 1533 | const RecordType *NextT = ToVisit.pop_back_val(); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1534 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1535 | // If we have already seen this type, skip it. |
| 1536 | if (!Visited.insert(NextT).second) |
| 1537 | continue; |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1538 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1539 | // If this is a base class, try to perform template argument |
| 1540 | // deduction from it. |
| 1541 | if (NextT != RecordT) { |
| 1542 | TemplateDeductionInfo BaseInfo(Info.getLocation()); |
| 1543 | Sema::TemplateDeductionResult BaseResult = |
| 1544 | DeduceTemplateArguments(S, TemplateParams, SpecParam, |
| 1545 | QualType(NextT, 0), BaseInfo, Deduced); |
| 1546 | |
| 1547 | // If template argument deduction for this base was successful, |
| 1548 | // note that we had some success. Otherwise, ignore any deductions |
| 1549 | // from this base class. |
| 1550 | if (BaseResult == Sema::TDK_Success) { |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1551 | // If we've already seen some success, then deduction fails due to |
| 1552 | // an ambiguity (temp.deduct.call p5). |
| 1553 | if (Successful) |
| 1554 | return Sema::TDK_MiscellaneousDeductionFailure; |
| 1555 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1556 | Successful = true; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1557 | std::swap(SuccessfulDeduced, Deduced); |
| 1558 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1559 | Info.Param = BaseInfo.Param; |
| 1560 | Info.FirstArg = BaseInfo.FirstArg; |
| 1561 | Info.SecondArg = BaseInfo.SecondArg; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | Deduced = DeducedOrig; |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1565 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1566 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1567 | // Visit base classes |
| 1568 | CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl()); |
| 1569 | for (const auto &Base : Next->bases()) { |
| 1570 | assert(Base.getType()->isRecordType() && |
| 1571 | "Base class that isn't a record?"); |
| 1572 | ToVisit.push_back(Base.getType()->getAs<RecordType>()); |
| 1573 | } |
| 1574 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1575 | |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1576 | if (Successful) { |
| 1577 | std::swap(SuccessfulDeduced, Deduced); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1578 | return Sema::TDK_Success; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1579 | } |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1580 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1581 | return Result; |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1584 | // T type::* |
| 1585 | // T T::* |
| 1586 | // T (type::*)() |
| 1587 | // type (T::*)() |
| 1588 | // type (type::*)(T) |
| 1589 | // type (T::*)(T) |
| 1590 | // T (type::*)(T) |
| 1591 | // T (T::*)() |
| 1592 | // T (T::*)(T) |
| 1593 | case Type::MemberPointer: { |
| 1594 | const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); |
| 1595 | const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); |
| 1596 | if (!MemPtrArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1597 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1598 | |
David Majnemer | a381cda | 2015-11-30 20:34:28 +0000 | [diff] [blame] | 1599 | QualType ParamPointeeType = MemPtrParam->getPointeeType(); |
| 1600 | if (ParamPointeeType->isFunctionType()) |
| 1601 | S.adjustMemberFunctionCC(ParamPointeeType, /*IsStatic=*/true, |
| 1602 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
| 1603 | QualType ArgPointeeType = MemPtrArg->getPointeeType(); |
| 1604 | if (ArgPointeeType->isFunctionType()) |
| 1605 | S.adjustMemberFunctionCC(ArgPointeeType, /*IsStatic=*/true, |
| 1606 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
| 1607 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1608 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1609 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
David Majnemer | a381cda | 2015-11-30 20:34:28 +0000 | [diff] [blame] | 1610 | ParamPointeeType, |
| 1611 | ArgPointeeType, |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1612 | Info, Deduced, |
| 1613 | TDF & TDF_IgnoreQualifiers)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1614 | return Result; |
| 1615 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1616 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1617 | QualType(MemPtrParam->getClass(), 0), |
| 1618 | QualType(MemPtrArg->getClass(), 0), |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1619 | Info, Deduced, |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1620 | TDF & TDF_IgnoreQualifiers); |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1621 | } |
| 1622 | |
Anders Carlsson | 15f1dd1 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1623 | // (clang extension) |
| 1624 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1625 | // type(^)(T) |
| 1626 | // T(^)() |
| 1627 | // T(^)(T) |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1628 | case Type::BlockPointer: { |
| 1629 | const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); |
| 1630 | const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1631 | |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1632 | if (!BlockPtrArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1633 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1634 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1635 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1636 | BlockPtrParam->getPointeeType(), |
| 1637 | BlockPtrArg->getPointeeType(), |
| 1638 | Info, Deduced, 0); |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1641 | // (clang extension) |
| 1642 | // |
| 1643 | // T __attribute__(((ext_vector_type(<integral constant>)))) |
| 1644 | case Type::ExtVector: { |
| 1645 | const ExtVectorType *VectorParam = cast<ExtVectorType>(Param); |
| 1646 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1647 | // Make sure that the vectors have the same number of elements. |
| 1648 | if (VectorParam->getNumElements() != VectorArg->getNumElements()) |
| 1649 | return Sema::TDK_NonDeducedMismatch; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1650 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1651 | // Perform deduction on the element types. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1652 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1653 | VectorParam->getElementType(), |
| 1654 | VectorArg->getElementType(), |
| 1655 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1656 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1657 | |
| 1658 | if (const DependentSizedExtVectorType *VectorArg |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1659 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1660 | // We can't check the number of elements, since the argument has a |
| 1661 | // dependent number of elements. This can only occur during partial |
| 1662 | // ordering. |
| 1663 | |
| 1664 | // Perform deduction on the element types. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1665 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1666 | VectorParam->getElementType(), |
| 1667 | VectorArg->getElementType(), |
| 1668 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1669 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1670 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1671 | return Sema::TDK_NonDeducedMismatch; |
| 1672 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1673 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1674 | // (clang extension) |
| 1675 | // |
| 1676 | // T __attribute__(((ext_vector_type(N)))) |
| 1677 | case Type::DependentSizedExtVector: { |
| 1678 | const DependentSizedExtVectorType *VectorParam |
| 1679 | = cast<DependentSizedExtVectorType>(Param); |
| 1680 | |
| 1681 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1682 | // Perform deduction on the element types. |
| 1683 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1684 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1685 | VectorParam->getElementType(), |
| 1686 | VectorArg->getElementType(), |
| 1687 | Info, Deduced, TDF)) |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1688 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1689 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1690 | // Perform deduction on the vector size, if we can. |
| 1691 | NonTypeTemplateParmDecl *NTTP |
| 1692 | = getDeducedParameterFromExpr(VectorParam->getSizeExpr()); |
| 1693 | if (!NTTP) |
| 1694 | return Sema::TDK_Success; |
| 1695 | |
| 1696 | llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); |
| 1697 | ArgSize = VectorArg->getNumElements(); |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1698 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, |
Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 1699 | S.Context.IntTy, false, Info, |
| 1700 | Deduced); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1701 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1702 | |
| 1703 | if (const DependentSizedExtVectorType *VectorArg |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1704 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1705 | // Perform deduction on the element types. |
| 1706 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1707 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1708 | VectorParam->getElementType(), |
| 1709 | VectorArg->getElementType(), |
| 1710 | Info, Deduced, TDF)) |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1711 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1712 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1713 | // Perform deduction on the vector size, if we can. |
| 1714 | NonTypeTemplateParmDecl *NTTP |
| 1715 | = getDeducedParameterFromExpr(VectorParam->getSizeExpr()); |
| 1716 | if (!NTTP) |
| 1717 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1718 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1719 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 1720 | VectorArg->getSizeExpr(), |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1721 | Info, Deduced); |
| 1722 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1723 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1724 | return Sema::TDK_NonDeducedMismatch; |
| 1725 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1726 | |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1727 | case Type::TypeOfExpr: |
| 1728 | case Type::TypeOf: |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 1729 | case Type::DependentName: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1730 | case Type::UnresolvedUsing: |
| 1731 | case Type::Decltype: |
| 1732 | case Type::UnaryTransform: |
| 1733 | case Type::Auto: |
| 1734 | case Type::DependentTemplateSpecialization: |
| 1735 | case Type::PackExpansion: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 1736 | case Type::Pipe: |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1737 | // No template argument deduction for these types |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1738 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1741 | llvm_unreachable("Invalid Type Class!"); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1744 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1745 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1746 | TemplateParameterList *TemplateParams, |
| 1747 | const TemplateArgument &Param, |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1748 | TemplateArgument Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1749 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 1750 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1751 | // If the template argument is a pack expansion, perform template argument |
| 1752 | // deduction against the pattern of that expansion. This only occurs during |
| 1753 | // partial ordering. |
| 1754 | if (Arg.isPackExpansion()) |
| 1755 | Arg = Arg.getPackExpansionPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1756 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1757 | switch (Param.getKind()) { |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1758 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1759 | llvm_unreachable("Null template argument in parameter list"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1760 | |
| 1761 | case TemplateArgument::Type: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1762 | if (Arg.getKind() == TemplateArgument::Type) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1763 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1764 | Param.getAsType(), |
| 1765 | Arg.getAsType(), |
| 1766 | Info, Deduced, 0); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1767 | Info.FirstArg = Param; |
| 1768 | Info.SecondArg = Arg; |
| 1769 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1770 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1771 | case TemplateArgument::Template: |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1772 | if (Arg.getKind() == TemplateArgument::Template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1773 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1774 | Param.getAsTemplate(), |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1775 | Arg.getAsTemplate(), Info, Deduced); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1776 | Info.FirstArg = Param; |
| 1777 | Info.SecondArg = Arg; |
| 1778 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1779 | |
| 1780 | case TemplateArgument::TemplateExpansion: |
| 1781 | llvm_unreachable("caller should handle pack expansions"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1782 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1783 | case TemplateArgument::Declaration: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1784 | if (Arg.getKind() == TemplateArgument::Declaration && |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 1785 | isSameDeclaration(Param.getAsDecl(), Arg.getAsDecl())) |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 1786 | return Sema::TDK_Success; |
| 1787 | |
| 1788 | Info.FirstArg = Param; |
| 1789 | Info.SecondArg = Arg; |
| 1790 | return Sema::TDK_NonDeducedMismatch; |
| 1791 | |
| 1792 | case TemplateArgument::NullPtr: |
| 1793 | if (Arg.getKind() == TemplateArgument::NullPtr && |
| 1794 | S.Context.hasSameType(Param.getNullPtrType(), Arg.getNullPtrType())) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1795 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1796 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1797 | Info.FirstArg = Param; |
| 1798 | Info.SecondArg = Arg; |
| 1799 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1800 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1801 | case TemplateArgument::Integral: |
| 1802 | if (Arg.getKind() == TemplateArgument::Integral) { |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1803 | if (hasSameExtendedValue(Param.getAsIntegral(), Arg.getAsIntegral())) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1804 | return Sema::TDK_Success; |
| 1805 | |
| 1806 | Info.FirstArg = Param; |
| 1807 | Info.SecondArg = Arg; |
| 1808 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1809 | } |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1810 | |
| 1811 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 1812 | Info.FirstArg = Param; |
| 1813 | Info.SecondArg = Arg; |
| 1814 | return Sema::TDK_NonDeducedMismatch; |
| 1815 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1816 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1817 | Info.FirstArg = Param; |
| 1818 | Info.SecondArg = Arg; |
| 1819 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1820 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1821 | case TemplateArgument::Expression: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1822 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1823 | = getDeducedParameterFromExpr(Param.getAsExpr())) { |
| 1824 | if (Arg.getKind() == TemplateArgument::Integral) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1825 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1826 | Arg.getAsIntegral(), |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1827 | Arg.getIntegralType(), |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1828 | /*ArrayBound=*/false, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1829 | Info, Deduced); |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 1830 | if (Arg.getKind() == TemplateArgument::NullPtr) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1831 | return DeduceNullPtrTemplateArgument(S, TemplateParams, NTTP, |
| 1832 | Arg.getNullPtrType(), |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 1833 | Info, Deduced); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1834 | if (Arg.getKind() == TemplateArgument::Expression) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1835 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 1836 | Arg.getAsExpr(), Info, Deduced); |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 1837 | if (Arg.getKind() == TemplateArgument::Declaration) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1838 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 1839 | Arg.getAsDecl(), |
| 1840 | Arg.getParamTypeForDecl(), |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 1841 | Info, Deduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1842 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1843 | Info.FirstArg = Param; |
| 1844 | Info.SecondArg = Arg; |
| 1845 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1846 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1847 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1848 | // Can't deduce anything, but that's okay. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1849 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1850 | } |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 1851 | case TemplateArgument::Pack: |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1852 | llvm_unreachable("Argument packs should be expanded by the caller!"); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1853 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1854 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1855 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1858 | /// \brief Determine whether there is a template argument to be used for |
| 1859 | /// deduction. |
| 1860 | /// |
| 1861 | /// This routine "expands" argument packs in-place, overriding its input |
| 1862 | /// parameters so that \c Args[ArgIdx] will be the available template argument. |
| 1863 | /// |
| 1864 | /// \returns true if there is another template argument (which will be at |
| 1865 | /// \c Args[ArgIdx]), false otherwise. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1866 | static bool hasTemplateArgumentForDeduction(ArrayRef<TemplateArgument> &Args, |
| 1867 | unsigned &ArgIdx) { |
| 1868 | if (ArgIdx == Args.size()) |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1869 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1870 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1871 | const TemplateArgument &Arg = Args[ArgIdx]; |
| 1872 | if (Arg.getKind() != TemplateArgument::Pack) |
| 1873 | return true; |
| 1874 | |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1875 | assert(ArgIdx == Args.size() - 1 && "Pack not at the end of argument list?"); |
| 1876 | Args = Arg.pack_elements(); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1877 | ArgIdx = 0; |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1878 | return ArgIdx < Args.size(); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1881 | /// \brief Determine whether the given set of template arguments has a pack |
| 1882 | /// expansion that is not the last template argument. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1883 | static bool hasPackExpansionBeforeEnd(ArrayRef<TemplateArgument> Args) { |
| 1884 | bool FoundPackExpansion = false; |
| 1885 | for (const auto &A : Args) { |
| 1886 | if (FoundPackExpansion) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1887 | return true; |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1888 | |
| 1889 | if (A.getKind() == TemplateArgument::Pack) |
| 1890 | return hasPackExpansionBeforeEnd(A.pack_elements()); |
| 1891 | |
| 1892 | if (A.isPackExpansion()) |
| 1893 | FoundPackExpansion = true; |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1894 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1895 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1896 | return false; |
| 1897 | } |
| 1898 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1899 | static Sema::TemplateDeductionResult |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1900 | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1901 | ArrayRef<TemplateArgument> Params, |
| 1902 | ArrayRef<TemplateArgument> Args, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1903 | TemplateDeductionInfo &Info, |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1904 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 1905 | bool NumberOfArgumentsMustMatch) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1906 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1907 | // If the template argument list of P contains a pack expansion that is not |
| 1908 | // the last template argument, the entire template argument list is a |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1909 | // non-deduced context. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1910 | if (hasPackExpansionBeforeEnd(Params)) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1911 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1912 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1913 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1914 | // If P has a form that contains <T> or <i>, then each argument Pi of the |
| 1915 | // respective template argument list P is compared with the corresponding |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1916 | // argument Ai of the corresponding template argument list of A. |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1917 | unsigned ArgIdx = 0, ParamIdx = 0; |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1918 | for (; hasTemplateArgumentForDeduction(Params, ParamIdx); ++ParamIdx) { |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1919 | if (!Params[ParamIdx].isPackExpansion()) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1920 | // The simple case: deduce template arguments by matching Pi and Ai. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1921 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1922 | // Check whether we have enough arguments. |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1923 | if (!hasTemplateArgumentForDeduction(Args, ArgIdx)) |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1924 | return NumberOfArgumentsMustMatch ? Sema::TDK_TooFewArguments |
| 1925 | : Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1926 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1927 | if (Args[ArgIdx].isPackExpansion()) { |
| 1928 | // FIXME: We follow the logic of C++0x [temp.deduct.type]p22 here, |
| 1929 | // but applied to pack expansions that are template arguments. |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 1930 | return Sema::TDK_MiscellaneousDeductionFailure; |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1931 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1932 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1933 | // Perform deduction for this Pi/Ai pair. |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1934 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1935 | = DeduceTemplateArguments(S, TemplateParams, |
| 1936 | Params[ParamIdx], Args[ArgIdx], |
| 1937 | Info, Deduced)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1938 | return Result; |
| 1939 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1940 | // Move to the next argument. |
| 1941 | ++ArgIdx; |
| 1942 | continue; |
| 1943 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1944 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1945 | // The parameter is a pack expansion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1946 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1947 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1948 | // If Pi is a pack expansion, then the pattern of Pi is compared with |
| 1949 | // each remaining argument in the template argument list of A. Each |
| 1950 | // comparison deduces template arguments for subsequent positions in the |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1951 | // template parameter packs expanded by Pi. |
| 1952 | TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1953 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1954 | // FIXME: If there are no remaining arguments, we can bail out early |
| 1955 | // and set any deduced parameter packs to an empty argument pack. |
| 1956 | // The latter part of this is a (minor) correctness issue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1957 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1958 | // Prepare to deduce the packs within the pattern. |
| 1959 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1960 | |
| 1961 | // Keep track of the deduced template arguments for each parameter pack |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1962 | // expanded by this pack expansion (the outer index) and for each |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1963 | // template argument (the inner SmallVectors). |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1964 | bool HasAnyArguments = false; |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1965 | for (; hasTemplateArgumentForDeduction(Args, ArgIdx); ++ArgIdx) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1966 | HasAnyArguments = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1967 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1968 | // Deduce template arguments from the pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1969 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1970 | = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx], |
| 1971 | Info, Deduced)) |
| 1972 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1973 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1974 | PackScope.nextPackElement(); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1975 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1976 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1977 | // Build argument packs for each of the parameter packs expanded by this |
| 1978 | // pack expansion. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1979 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1980 | return Result; |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1981 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1982 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1983 | return Sema::TDK_Success; |
| 1984 | } |
| 1985 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1986 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1987 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1988 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1989 | const TemplateArgumentList &ParamList, |
| 1990 | const TemplateArgumentList &ArgList, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1991 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 1992 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 1993 | return DeduceTemplateArguments(S, TemplateParams, ParamList.asArray(), |
| 1994 | ArgList.asArray(), Info, Deduced, false); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1997 | /// \brief Determine whether two template arguments are the same. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1998 | static bool isSameTemplateArg(ASTContext &Context, |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1999 | const TemplateArgument &X, |
| 2000 | const TemplateArgument &Y) { |
| 2001 | if (X.getKind() != Y.getKind()) |
| 2002 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2003 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2004 | switch (X.getKind()) { |
| 2005 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2006 | llvm_unreachable("Comparing NULL template argument"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2007 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2008 | case TemplateArgument::Type: |
| 2009 | return Context.getCanonicalType(X.getAsType()) == |
| 2010 | Context.getCanonicalType(Y.getAsType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2011 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2012 | case TemplateArgument::Declaration: |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 2013 | return isSameDeclaration(X.getAsDecl(), Y.getAsDecl()); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2014 | |
| 2015 | case TemplateArgument::NullPtr: |
| 2016 | return Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2017 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2018 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2019 | case TemplateArgument::TemplateExpansion: |
| 2020 | return Context.getCanonicalTemplateName( |
| 2021 | X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() == |
| 2022 | Context.getCanonicalTemplateName( |
| 2023 | Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2024 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2025 | case TemplateArgument::Integral: |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 2026 | return X.getAsIntegral() == Y.getAsIntegral(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2027 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2028 | case TemplateArgument::Expression: { |
| 2029 | llvm::FoldingSetNodeID XID, YID; |
| 2030 | X.getAsExpr()->Profile(XID, Context, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2031 | Y.getAsExpr()->Profile(YID, Context, true); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2032 | return XID == YID; |
| 2033 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2034 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2035 | case TemplateArgument::Pack: |
| 2036 | if (X.pack_size() != Y.pack_size()) |
| 2037 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2038 | |
| 2039 | for (TemplateArgument::pack_iterator XP = X.pack_begin(), |
| 2040 | XPEnd = X.pack_end(), |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2041 | YP = Y.pack_begin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2042 | XP != XPEnd; ++XP, ++YP) |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2043 | if (!isSameTemplateArg(Context, *XP, *YP)) |
| 2044 | return false; |
| 2045 | |
| 2046 | return true; |
| 2047 | } |
| 2048 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2049 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2052 | /// \brief Allocate a TemplateArgumentLoc where all locations have |
| 2053 | /// been initialized to the given location. |
| 2054 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2055 | /// \param Arg The template argument we are producing template argument |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2056 | /// location information for. |
| 2057 | /// |
| 2058 | /// \param NTTPType For a declaration template argument, the type of |
| 2059 | /// the non-type template parameter that corresponds to this template |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2060 | /// argument. Can be null if no type sugar is available to add to the |
| 2061 | /// type from the template argument. |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2062 | /// |
| 2063 | /// \param Loc The source location to use for the resulting template |
| 2064 | /// argument. |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2065 | TemplateArgumentLoc |
| 2066 | Sema::getTrivialTemplateArgumentLoc(const TemplateArgument &Arg, |
| 2067 | QualType NTTPType, SourceLocation Loc) { |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2068 | switch (Arg.getKind()) { |
| 2069 | case TemplateArgument::Null: |
| 2070 | llvm_unreachable("Can't get a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2071 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2072 | case TemplateArgument::Type: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2073 | return TemplateArgumentLoc( |
| 2074 | Arg, Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2075 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2076 | case TemplateArgument::Declaration: { |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2077 | if (NTTPType.isNull()) |
| 2078 | NTTPType = Arg.getParamTypeForDecl(); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2079 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
| 2080 | .getAs<Expr>(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2081 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2082 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2083 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2084 | case TemplateArgument::NullPtr: { |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2085 | if (NTTPType.isNull()) |
| 2086 | NTTPType = Arg.getNullPtrType(); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2087 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
| 2088 | .getAs<Expr>(); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2089 | return TemplateArgumentLoc(TemplateArgument(NTTPType, /*isNullPtr*/true), |
| 2090 | E); |
| 2091 | } |
| 2092 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2093 | case TemplateArgument::Integral: { |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2094 | Expr *E = |
| 2095 | BuildExpressionFromIntegralTemplateArgument(Arg, Loc).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 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2099 | case TemplateArgument::Template: |
| 2100 | case TemplateArgument::TemplateExpansion: { |
| 2101 | NestedNameSpecifierLocBuilder Builder; |
| 2102 | TemplateName Template = Arg.getAsTemplate(); |
| 2103 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2104 | Builder.MakeTrivial(Context, DTN->getQualifier(), Loc); |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 2105 | else if (QualifiedTemplateName *QTN = |
| 2106 | Template.getAsQualifiedTemplateName()) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2107 | Builder.MakeTrivial(Context, QTN->getQualifier(), Loc); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2108 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2109 | if (Arg.getKind() == TemplateArgument::Template) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2110 | return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(Context), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2111 | Loc); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2112 | |
| 2113 | return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(Context), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2114 | Loc, Loc); |
| 2115 | } |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2116 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2117 | case TemplateArgument::Expression: |
| 2118 | return TemplateArgumentLoc(Arg, Arg.getAsExpr()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2119 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2120 | case TemplateArgument::Pack: |
| 2121 | return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo()); |
| 2122 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2123 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2124 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
| 2127 | |
| 2128 | /// \brief Convert the given deduced template argument and add it to the set of |
| 2129 | /// fully-converted template arguments. |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2130 | static bool |
| 2131 | ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param, |
| 2132 | DeducedTemplateArgument Arg, |
| 2133 | NamedDecl *Template, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2134 | TemplateDeductionInfo &Info, |
| 2135 | bool InFunctionTemplate, |
| 2136 | SmallVectorImpl<TemplateArgument> &Output) { |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2137 | auto ConvertArg = [&](DeducedTemplateArgument Arg, |
| 2138 | unsigned ArgumentPackIndex) { |
| 2139 | // Convert the deduced template argument into a template |
| 2140 | // argument that we can check, almost as if the user had written |
| 2141 | // the template argument explicitly. |
| 2142 | TemplateArgumentLoc ArgLoc = |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2143 | S.getTrivialTemplateArgumentLoc(Arg, QualType(), Info.getLocation()); |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2144 | |
| 2145 | // Check the template argument, converting it as necessary. |
| 2146 | return S.CheckTemplateArgument( |
| 2147 | Param, ArgLoc, Template, Template->getLocation(), |
| 2148 | Template->getSourceRange().getEnd(), ArgumentPackIndex, Output, |
| 2149 | InFunctionTemplate |
| 2150 | ? (Arg.wasDeducedFromArrayBound() ? Sema::CTAK_DeducedFromArrayBound |
| 2151 | : Sema::CTAK_Deduced) |
| 2152 | : Sema::CTAK_Specified); |
| 2153 | }; |
| 2154 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2155 | if (Arg.getKind() == TemplateArgument::Pack) { |
| 2156 | // This is a template argument pack, so check each of its arguments against |
| 2157 | // the template parameter. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2158 | SmallVector<TemplateArgument, 2> PackedArgsBuilder; |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 2159 | for (const auto &P : Arg.pack_elements()) { |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2160 | // When converting the deduced template argument, append it to the |
| 2161 | // general output list. We need to do this so that the template argument |
| 2162 | // checking logic has all of the prior template arguments available. |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 2163 | DeducedTemplateArgument InnerArg(P); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2164 | InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound()); |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2165 | assert(InnerArg.getKind() != TemplateArgument::Pack && |
| 2166 | "deduced nested pack"); |
| 2167 | if (ConvertArg(InnerArg, PackedArgsBuilder.size())) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2168 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2169 | |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2170 | // Move the converted template argument into our argument pack. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 2171 | PackedArgsBuilder.push_back(Output.pop_back_val()); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2172 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2173 | |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2174 | // 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] | 2175 | // itself, in case that substitution fails. |
| 2176 | if (PackedArgsBuilder.empty()) { |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2177 | LocalInstantiationScope Scope(S); |
Richard Smith | e824775 | 2016-12-22 07:24:39 +0000 | [diff] [blame] | 2178 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Output); |
Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2179 | MultiLevelTemplateArgumentList Args(TemplateArgs); |
| 2180 | |
| 2181 | if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2182 | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
| 2183 | NTTP, Output, |
| 2184 | Template->getSourceRange()); |
| 2185 | if (Inst.isInvalid() || |
| 2186 | S.SubstType(NTTP->getType(), Args, NTTP->getLocation(), |
| 2187 | NTTP->getDeclName()).isNull()) |
| 2188 | return true; |
| 2189 | } else if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 2190 | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
| 2191 | TTP, Output, |
| 2192 | Template->getSourceRange()); |
| 2193 | if (Inst.isInvalid() || !S.SubstDecl(TTP, S.CurContext, Args)) |
| 2194 | return true; |
| 2195 | } |
| 2196 | // For type parameters, no substitution is ever required. |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2197 | } |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2198 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2199 | // Create the resulting argument pack. |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 2200 | Output.push_back( |
| 2201 | TemplateArgument::CreatePackCopy(S.Context, PackedArgsBuilder)); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2202 | return false; |
| 2203 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2204 | |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2205 | return ConvertArg(Arg, 0); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2208 | // FIXME: This should not be a template, but |
| 2209 | // ClassTemplatePartialSpecializationDecl sadly does not derive from |
| 2210 | // TemplateDecl. |
| 2211 | template<typename TemplateDeclT> |
| 2212 | static Sema::TemplateDeductionResult ConvertDeducedTemplateArguments( |
| 2213 | Sema &S, TemplateDeclT *Template, |
| 2214 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2215 | TemplateDeductionInfo &Info, SmallVectorImpl<TemplateArgument> &Builder, |
| 2216 | LocalInstantiationScope *CurrentInstantiationScope = nullptr, |
| 2217 | unsigned NumAlreadyConverted = 0, bool PartialOverloading = false) { |
| 2218 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| 2219 | |
| 2220 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 2221 | NamedDecl *Param = TemplateParams->getParam(I); |
| 2222 | |
| 2223 | if (!Deduced[I].isNull()) { |
| 2224 | if (I < NumAlreadyConverted) { |
| 2225 | // We have already fully type-checked and converted this |
| 2226 | // argument, because it was explicitly-specified. Just record the |
| 2227 | // presence of this argument. |
| 2228 | Builder.push_back(Deduced[I]); |
| 2229 | // We may have had explicitly-specified template arguments for a |
| 2230 | // template parameter pack (that may or may not have been extended |
| 2231 | // via additional deduced arguments). |
| 2232 | if (Param->isParameterPack() && CurrentInstantiationScope) { |
| 2233 | if (CurrentInstantiationScope->getPartiallySubstitutedPack() == |
| 2234 | Param) { |
| 2235 | // Forget the partially-substituted pack; its substitution is now |
| 2236 | // complete. |
| 2237 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2238 | } |
| 2239 | } |
| 2240 | continue; |
| 2241 | } |
| 2242 | |
| 2243 | // We have deduced this argument, so it still needs to be |
| 2244 | // checked and converted. |
| 2245 | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Template, Info, |
| 2246 | isa<FunctionTemplateDecl>(Template), |
| 2247 | Builder)) { |
| 2248 | Info.Param = makeTemplateParameter(Param); |
| 2249 | // FIXME: These template arguments are temporary. Free them! |
| 2250 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2251 | return Sema::TDK_SubstitutionFailure; |
| 2252 | } |
| 2253 | |
| 2254 | continue; |
| 2255 | } |
| 2256 | |
| 2257 | // C++0x [temp.arg.explicit]p3: |
| 2258 | // A trailing template parameter pack (14.5.3) not otherwise deduced will |
| 2259 | // be deduced to an empty sequence of template arguments. |
| 2260 | // FIXME: Where did the word "trailing" come from? |
| 2261 | if (Param->isTemplateParameterPack()) { |
| 2262 | // We may have had explicitly-specified template arguments for this |
| 2263 | // template parameter pack. If so, our empty deduction extends the |
| 2264 | // explicitly-specified set (C++0x [temp.arg.explicit]p9). |
| 2265 | const TemplateArgument *ExplicitArgs; |
| 2266 | unsigned NumExplicitArgs; |
| 2267 | if (CurrentInstantiationScope && |
| 2268 | CurrentInstantiationScope->getPartiallySubstitutedPack( |
| 2269 | &ExplicitArgs, &NumExplicitArgs) == Param) { |
| 2270 | Builder.push_back(TemplateArgument( |
| 2271 | llvm::makeArrayRef(ExplicitArgs, NumExplicitArgs))); |
| 2272 | |
| 2273 | // Forget the partially-substituted pack; its substitution is now |
| 2274 | // complete. |
| 2275 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2276 | } else { |
| 2277 | // Go through the motions of checking the empty argument pack against |
| 2278 | // the parameter pack. |
| 2279 | DeducedTemplateArgument DeducedPack(TemplateArgument::getEmptyPack()); |
| 2280 | if (ConvertDeducedTemplateArgument( |
| 2281 | S, Param, DeducedPack, Template, Info, |
| 2282 | isa<FunctionTemplateDecl>(Template), Builder)) { |
| 2283 | Info.Param = makeTemplateParameter(Param); |
| 2284 | // FIXME: These template arguments are temporary. Free them! |
| 2285 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2286 | return Sema::TDK_SubstitutionFailure; |
| 2287 | } |
| 2288 | } |
| 2289 | continue; |
| 2290 | } |
| 2291 | |
| 2292 | // Substitute into the default template argument, if available. |
| 2293 | bool HasDefaultArg = false; |
| 2294 | TemplateDecl *TD = dyn_cast<TemplateDecl>(Template); |
| 2295 | if (!TD) { |
| 2296 | assert(isa<ClassTemplatePartialSpecializationDecl>(Template)); |
| 2297 | return Sema::TDK_Incomplete; |
| 2298 | } |
| 2299 | |
| 2300 | TemplateArgumentLoc DefArg = S.SubstDefaultTemplateArgumentIfAvailable( |
| 2301 | TD, TD->getLocation(), TD->getSourceRange().getEnd(), Param, Builder, |
| 2302 | HasDefaultArg); |
| 2303 | |
| 2304 | // If there was no default argument, deduction is incomplete. |
| 2305 | if (DefArg.getArgument().isNull()) { |
| 2306 | Info.Param = makeTemplateParameter( |
| 2307 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
| 2308 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2309 | if (PartialOverloading) break; |
| 2310 | |
| 2311 | return HasDefaultArg ? Sema::TDK_SubstitutionFailure |
| 2312 | : Sema::TDK_Incomplete; |
| 2313 | } |
| 2314 | |
| 2315 | // Check whether we can actually use the default argument. |
| 2316 | if (S.CheckTemplateArgument(Param, DefArg, TD, TD->getLocation(), |
| 2317 | TD->getSourceRange().getEnd(), 0, Builder, |
| 2318 | Sema::CTAK_Specified)) { |
| 2319 | Info.Param = makeTemplateParameter( |
| 2320 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
| 2321 | // FIXME: These template arguments are temporary. Free them! |
| 2322 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2323 | return Sema::TDK_SubstitutionFailure; |
| 2324 | } |
| 2325 | |
| 2326 | // If we get here, we successfully used the default template argument. |
| 2327 | } |
| 2328 | |
| 2329 | return Sema::TDK_Success; |
| 2330 | } |
| 2331 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2332 | DeclContext *getAsDeclContextOrEnclosing(Decl *D) { |
| 2333 | if (auto *DC = dyn_cast<DeclContext>(D)) |
| 2334 | return DC; |
| 2335 | return D->getDeclContext(); |
| 2336 | } |
| 2337 | |
| 2338 | template<typename T> struct IsPartialSpecialization { |
| 2339 | static constexpr bool value = false; |
| 2340 | }; |
| 2341 | template<> |
| 2342 | struct IsPartialSpecialization<ClassTemplatePartialSpecializationDecl> { |
| 2343 | static constexpr bool value = true; |
| 2344 | }; |
| 2345 | template<> |
| 2346 | struct IsPartialSpecialization<VarTemplatePartialSpecializationDecl> { |
| 2347 | static constexpr bool value = true; |
| 2348 | }; |
| 2349 | |
| 2350 | /// Complete template argument deduction for a partial specialization. |
| 2351 | template <typename T> |
| 2352 | static typename std::enable_if<IsPartialSpecialization<T>::value, |
| 2353 | Sema::TemplateDeductionResult>::type |
| 2354 | FinishTemplateArgumentDeduction( |
| 2355 | Sema &S, T *Partial, const TemplateArgumentList &TemplateArgs, |
| 2356 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2357 | TemplateDeductionInfo &Info) { |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2358 | // Unevaluated SFINAE context. |
| 2359 | EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2360 | Sema::SFINAETrap Trap(S); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2361 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2362 | Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Partial)); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2363 | |
| 2364 | // C++ [temp.deduct.type]p2: |
| 2365 | // [...] or if any template argument remains neither deduced nor |
| 2366 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2367 | SmallVector<TemplateArgument, 4> Builder; |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2368 | if (auto Result = ConvertDeducedTemplateArguments(S, Partial, Deduced, |
| 2369 | Info, Builder)) |
| 2370 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2371 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2372 | // Form the template argument list from the deduced template arguments. |
| 2373 | TemplateArgumentList *DeducedArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2374 | = TemplateArgumentList::CreateCopy(S.Context, Builder); |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2375 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2376 | Info.reset(DeducedArgumentList); |
| 2377 | |
| 2378 | // Substitute the deduced template arguments into the template |
| 2379 | // arguments of the class template partial specialization, and |
| 2380 | // verify that the instantiated template arguments are both valid |
| 2381 | // and are equivalent to the template arguments originally provided |
| 2382 | // to the class template. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2383 | LocalInstantiationScope InstScope(S); |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2384 | auto *Template = Partial->getSpecializedTemplate(); |
| 2385 | const ASTTemplateArgumentListInfo *PartialTemplArgInfo = |
| 2386 | Partial->getTemplateArgsAsWritten(); |
| 2387 | const TemplateArgumentLoc *PartialTemplateArgs = |
| 2388 | PartialTemplArgInfo->getTemplateArgs(); |
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 | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, |
| 2391 | PartialTemplArgInfo->RAngleLoc); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2392 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2393 | if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs, |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2394 | InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { |
| 2395 | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; |
| 2396 | if (ParamIdx >= Partial->getTemplateParameters()->size()) |
| 2397 | ParamIdx = Partial->getTemplateParameters()->size() - 1; |
| 2398 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2399 | Decl *Param = const_cast<NamedDecl *>( |
| 2400 | Partial->getTemplateParameters()->getParam(ParamIdx)); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2401 | Info.Param = makeTemplateParameter(Param); |
| 2402 | Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); |
| 2403 | return Sema::TDK_SubstitutionFailure; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2404 | } |
| 2405 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2406 | SmallVector<TemplateArgument, 4> ConvertedInstArgs; |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2407 | if (S.CheckTemplateArgumentList(Template, Partial->getLocation(), InstArgs, |
| 2408 | false, ConvertedInstArgs)) |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2409 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2410 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2411 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2412 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2413 | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2414 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 2415 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2416 | Info.FirstArg = TemplateArgs[I]; |
| 2417 | Info.SecondArg = InstArg; |
| 2418 | return Sema::TDK_NonDeducedMismatch; |
| 2419 | } |
| 2420 | } |
| 2421 | |
| 2422 | if (Trap.hasErrorOccurred()) |
| 2423 | return Sema::TDK_SubstitutionFailure; |
| 2424 | |
| 2425 | return Sema::TDK_Success; |
| 2426 | } |
| 2427 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2428 | /// \brief Perform template argument deduction to determine whether |
Larisse Voufo | 833b05a | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 2429 | /// the given template arguments match the given class template |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2430 | /// partial specialization per C++ [temp.class.spec.match]. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2431 | Sema::TemplateDeductionResult |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2432 | Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2433 | const TemplateArgumentList &TemplateArgs, |
| 2434 | TemplateDeductionInfo &Info) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 2435 | if (Partial->isInvalidDecl()) |
| 2436 | return TDK_Invalid; |
| 2437 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2438 | // C++ [temp.class.spec.match]p2: |
| 2439 | // A partial specialization matches a given actual template |
| 2440 | // argument list if the template arguments of the partial |
| 2441 | // specialization can be deduced from the actual template argument |
| 2442 | // list (14.8.2). |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2443 | |
| 2444 | // Unevaluated SFINAE context. |
| 2445 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2446 | SFINAETrap Trap(*this); |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2447 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2448 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2449 | Deduced.resize(Partial->getTemplateParameters()->size()); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2450 | if (TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2451 | = ::DeduceTemplateArguments(*this, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2452 | Partial->getTemplateParameters(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2453 | Partial->getTemplateArgs(), |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2454 | TemplateArgs, Info, Deduced)) |
| 2455 | return Result; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 2456 | |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2457 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2458 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2459 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2460 | if (Inst.isInvalid()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2461 | return TDK_InstantiationDepth; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2462 | |
Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2463 | if (Trap.hasErrorOccurred()) |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2464 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2465 | |
| 2466 | return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs, |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2467 | Deduced, Info); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2468 | } |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2469 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2470 | /// \brief Perform template argument deduction to determine whether |
| 2471 | /// the given template arguments match the given variable template |
| 2472 | /// partial specialization per C++ [temp.class.spec.match]. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2473 | Sema::TemplateDeductionResult |
| 2474 | Sema::DeduceTemplateArguments(VarTemplatePartialSpecializationDecl *Partial, |
| 2475 | const TemplateArgumentList &TemplateArgs, |
| 2476 | TemplateDeductionInfo &Info) { |
| 2477 | if (Partial->isInvalidDecl()) |
| 2478 | return TDK_Invalid; |
| 2479 | |
| 2480 | // C++ [temp.class.spec.match]p2: |
| 2481 | // A partial specialization matches a given actual template |
| 2482 | // argument list if the template arguments of the partial |
| 2483 | // specialization can be deduced from the actual template argument |
| 2484 | // list (14.8.2). |
| 2485 | |
| 2486 | // Unevaluated SFINAE context. |
| 2487 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
| 2488 | SFINAETrap Trap(*this); |
| 2489 | |
| 2490 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 2491 | Deduced.resize(Partial->getTemplateParameters()->size()); |
| 2492 | if (TemplateDeductionResult Result = ::DeduceTemplateArguments( |
| 2493 | *this, Partial->getTemplateParameters(), Partial->getTemplateArgs(), |
| 2494 | TemplateArgs, Info, Deduced)) |
| 2495 | return Result; |
| 2496 | |
| 2497 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2498 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2499 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2500 | if (Inst.isInvalid()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2501 | return TDK_InstantiationDepth; |
| 2502 | |
| 2503 | if (Trap.hasErrorOccurred()) |
| 2504 | return Sema::TDK_SubstitutionFailure; |
| 2505 | |
| 2506 | return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs, |
| 2507 | Deduced, Info); |
| 2508 | } |
| 2509 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2510 | /// \brief Determine whether the given type T is a simple-template-id type. |
| 2511 | static bool isSimpleTemplateIdType(QualType T) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2512 | if (const TemplateSpecializationType *Spec |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2513 | = T->getAs<TemplateSpecializationType>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2514 | return Spec->getTemplateName().getAsTemplateDecl() != nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2515 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2516 | return false; |
| 2517 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2518 | |
| 2519 | /// \brief Substitute the explicitly-provided template arguments into the |
| 2520 | /// given function template according to C++ [temp.arg.explicit]. |
| 2521 | /// |
| 2522 | /// \param FunctionTemplate the function template into which the explicit |
| 2523 | /// template arguments will be substituted. |
| 2524 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2525 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2526 | /// arguments. |
| 2527 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2528 | /// \param Deduced the deduced template arguments, which will be populated |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2529 | /// with the converted and checked explicit template arguments. |
| 2530 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2531 | /// \param ParamTypes will be populated with the instantiated function |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2532 | /// parameters. |
| 2533 | /// |
| 2534 | /// \param FunctionType if non-NULL, the result type of the function template |
| 2535 | /// will also be instantiated and the pointed-to value will be updated with |
| 2536 | /// the instantiated function type. |
| 2537 | /// |
| 2538 | /// \param Info if substitution fails for any reason, this object will be |
| 2539 | /// populated with more information about the failure. |
| 2540 | /// |
| 2541 | /// \returns TDK_Success if substitution was successful, or some failure |
| 2542 | /// condition. |
| 2543 | Sema::TemplateDeductionResult |
| 2544 | Sema::SubstituteExplicitTemplateArguments( |
| 2545 | FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2546 | TemplateArgumentListInfo &ExplicitTemplateArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2547 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2548 | SmallVectorImpl<QualType> &ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2549 | QualType *FunctionType, |
| 2550 | TemplateDeductionInfo &Info) { |
| 2551 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 2552 | TemplateParameterList *TemplateParams |
| 2553 | = FunctionTemplate->getTemplateParameters(); |
| 2554 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2555 | if (ExplicitTemplateArgs.size() == 0) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2556 | // No arguments to substitute; just copy over the parameter types and |
| 2557 | // fill in the function type. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2558 | for (auto P : Function->parameters()) |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 2559 | ParamTypes.push_back(P->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2560 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2561 | if (FunctionType) |
| 2562 | *FunctionType = Function->getType(); |
| 2563 | return TDK_Success; |
| 2564 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2565 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2566 | // Unevaluated SFINAE context. |
| 2567 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2568 | SFINAETrap Trap(*this); |
| 2569 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2570 | // C++ [temp.arg.explicit]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2571 | // Template arguments that are present shall be specified in the |
| 2572 | // declaration order of their corresponding template-parameters. The |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2573 | // template argument list shall not specify more template-arguments than |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2574 | // there are corresponding template-parameters. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2575 | SmallVector<TemplateArgument, 4> Builder; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2576 | |
| 2577 | // Enter a new template instantiation context where we check the |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2578 | // explicitly-specified template arguments against this function template, |
| 2579 | // and then substitute them into the function parameter types. |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 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(), FunctionTemplate, |
| 2582 | DeducedArgs, |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2583 | ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution, |
| 2584 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2585 | if (Inst.isInvalid()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2586 | return TDK_InstantiationDepth; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2587 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2588 | if (CheckTemplateArgumentList(FunctionTemplate, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2589 | SourceLocation(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2590 | ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2591 | true, |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 2592 | Builder) || Trap.hasErrorOccurred()) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2593 | unsigned Index = Builder.size(); |
Douglas Gregor | 62c281a | 2010-05-09 01:26:06 +0000 | [diff] [blame] | 2594 | if (Index >= TemplateParams->size()) |
| 2595 | Index = TemplateParams->size() - 1; |
| 2596 | Info.Param = makeTemplateParameter(TemplateParams->getParam(Index)); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2597 | return TDK_InvalidExplicitArguments; |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 2598 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2599 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2600 | // Form the template argument list from the explicitly-specified |
| 2601 | // template arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2602 | TemplateArgumentList *ExplicitArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2603 | = TemplateArgumentList::CreateCopy(Context, Builder); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2604 | Info.reset(ExplicitArgumentList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2605 | |
John McCall | 036855a | 2010-10-12 19:40:14 +0000 | [diff] [blame] | 2606 | // Template argument deduction and the final substitution should be |
| 2607 | // done in the context of the templated declaration. Explicit |
| 2608 | // argument substitution, on the other hand, needs to happen in the |
| 2609 | // calling context. |
| 2610 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
| 2611 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2612 | // If we deduced template arguments for a template parameter pack, |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2613 | // note that the template argument pack is partially substituted and record |
| 2614 | // the explicit template arguments. They'll be used as part of deduction |
| 2615 | // for this template parameter pack. |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2616 | for (unsigned I = 0, N = Builder.size(); I != N; ++I) { |
| 2617 | const TemplateArgument &Arg = Builder[I]; |
| 2618 | if (Arg.getKind() == TemplateArgument::Pack) { |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2619 | CurrentInstantiationScope->SetPartiallySubstitutedPack( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2620 | TemplateParams->getParam(I), |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2621 | Arg.pack_begin(), |
| 2622 | Arg.pack_size()); |
| 2623 | break; |
| 2624 | } |
| 2625 | } |
| 2626 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2627 | const FunctionProtoType *Proto |
| 2628 | = Function->getType()->getAs<FunctionProtoType>(); |
| 2629 | assert(Proto && "Function template does not have a prototype?"); |
| 2630 | |
Richard Smith | 70b1304 | 2015-01-09 01:19:56 +0000 | [diff] [blame] | 2631 | // Isolate our substituted parameters from our caller. |
| 2632 | LocalInstantiationScope InstScope(*this, /*MergeWithOuterScope*/true); |
| 2633 | |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2634 | ExtParameterInfoBuilder ExtParamInfos; |
| 2635 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2636 | // Instantiate the types of each of the function parameters given the |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2637 | // explicitly-specified template arguments. If the function has a trailing |
| 2638 | // return type, substitute it after the arguments to ensure we substitute |
| 2639 | // in lexical order. |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2640 | if (Proto->hasTrailingReturn()) { |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2641 | if (SubstParmTypes(Function->getLocation(), Function->parameters(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2642 | Proto->getExtParameterInfosOrNull(), |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2643 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2644 | ParamTypes, /*params*/ nullptr, ExtParamInfos)) |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2645 | return TDK_SubstitutionFailure; |
| 2646 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2647 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2648 | // Instantiate the return type. |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2649 | QualType ResultType; |
| 2650 | { |
| 2651 | // C++11 [expr.prim.general]p3: |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2652 | // If a declaration declares a member function or member function |
| 2653 | // 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] | 2654 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2655 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2656 | // declarator. |
| 2657 | unsigned ThisTypeQuals = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2658 | CXXRecordDecl *ThisContext = nullptr; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2659 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
| 2660 | ThisContext = Method->getParent(); |
| 2661 | ThisTypeQuals = Method->getTypeQualifiers(); |
| 2662 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2663 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2664 | CXXThisScopeRAII ThisScope(*this, ThisContext, ThisTypeQuals, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2665 | getLangOpts().CPlusPlus11); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2666 | |
| 2667 | ResultType = |
| 2668 | SubstType(Proto->getReturnType(), |
| 2669 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 2670 | Function->getTypeSpecStartLoc(), Function->getDeclName()); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2671 | if (ResultType.isNull() || Trap.hasErrorOccurred()) |
| 2672 | return TDK_SubstitutionFailure; |
| 2673 | } |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2674 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2675 | // Instantiate the types of each of the function parameters given the |
| 2676 | // explicitly-specified template arguments if we didn't do so earlier. |
| 2677 | if (!Proto->hasTrailingReturn() && |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2678 | SubstParmTypes(Function->getLocation(), Function->parameters(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2679 | Proto->getExtParameterInfosOrNull(), |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2680 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2681 | ParamTypes, /*params*/ nullptr, ExtParamInfos)) |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2682 | return TDK_SubstitutionFailure; |
| 2683 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2684 | if (FunctionType) { |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2685 | auto EPI = Proto->getExtProtoInfo(); |
| 2686 | EPI.ExtParameterInfos = ExtParamInfos.getPointerOrNull(ParamTypes.size()); |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2687 | *FunctionType = BuildFunctionType(ResultType, ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2688 | Function->getLocation(), |
Eli Friedman | d8725a9 | 2010-08-05 02:54:05 +0000 | [diff] [blame] | 2689 | Function->getDeclName(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2690 | EPI); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2691 | if (FunctionType->isNull() || Trap.hasErrorOccurred()) |
| 2692 | return TDK_SubstitutionFailure; |
| 2693 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2694 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2695 | // C++ [temp.arg.explicit]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2696 | // Trailing template arguments that can be deduced (14.8.2) may be |
| 2697 | // omitted from the list of explicit template-arguments. If all of the |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2698 | // template arguments can be deduced, they may all be omitted; in this |
| 2699 | // case, the empty template argument list <> itself may also be omitted. |
| 2700 | // |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2701 | // Take all of the explicitly-specified arguments and put them into |
| 2702 | // the set of deduced template arguments. Explicitly-specified |
| 2703 | // parameter packs, however, will be set to NULL since the deduction |
| 2704 | // mechanisms handle explicitly-specified argument packs directly. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2705 | Deduced.reserve(TemplateParams->size()); |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2706 | for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) { |
| 2707 | const TemplateArgument &Arg = ExplicitArgumentList->get(I); |
| 2708 | if (Arg.getKind() == TemplateArgument::Pack) |
| 2709 | Deduced.push_back(DeducedTemplateArgument()); |
| 2710 | else |
| 2711 | Deduced.push_back(Arg); |
| 2712 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2713 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2714 | return TDK_Success; |
| 2715 | } |
| 2716 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2717 | /// \brief Check whether the deduced argument type for a call to a function |
| 2718 | /// template matches the actual argument type per C++ [temp.deduct.call]p4. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2719 | static bool |
| 2720 | CheckOriginalCallArgDeduction(Sema &S, Sema::OriginalCallArg OriginalArg, |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2721 | QualType DeducedA) { |
| 2722 | ASTContext &Context = S.Context; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2723 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2724 | QualType A = OriginalArg.OriginalArgType; |
| 2725 | QualType OriginalParamType = OriginalArg.OriginalParamType; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2726 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2727 | // Check for type equality (top-level cv-qualifiers are ignored). |
| 2728 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| 2729 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2730 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2731 | // Strip off references on the argument types; they aren't needed for |
| 2732 | // the following checks. |
| 2733 | if (const ReferenceType *DeducedARef = DeducedA->getAs<ReferenceType>()) |
| 2734 | DeducedA = DeducedARef->getPointeeType(); |
| 2735 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
| 2736 | A = ARef->getPointeeType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2737 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2738 | // C++ [temp.deduct.call]p4: |
| 2739 | // [...] However, there are three cases that allow a difference: |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2740 | // - If the original P is a reference type, the deduced A (i.e., the |
| 2741 | // type referred to by the reference) can be more cv-qualified than |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2742 | // the transformed A. |
| 2743 | if (const ReferenceType *OriginalParamRef |
| 2744 | = OriginalParamType->getAs<ReferenceType>()) { |
| 2745 | // We don't want to keep the reference around any more. |
| 2746 | OriginalParamType = OriginalParamRef->getPointeeType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2747 | |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 2748 | // FIXME: Resolve core issue (no number yet): if the original P is a |
| 2749 | // reference type and the transformed A is function type "noexcept F", |
| 2750 | // the deduced A can be F. |
| 2751 | QualType Tmp; |
| 2752 | if (A->isFunctionType() && S.IsFunctionConversion(A, DeducedA, Tmp)) |
| 2753 | return false; |
| 2754 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2755 | Qualifiers AQuals = A.getQualifiers(); |
| 2756 | Qualifiers DeducedAQuals = DeducedA.getQualifiers(); |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2757 | |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2758 | // Under Objective-C++ ARC, the deduced type may have implicitly |
| 2759 | // been given strong or (when dealing with a const reference) |
| 2760 | // unsafe_unretained lifetime. If so, update the original |
| 2761 | // qualifiers to include this lifetime. |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2762 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2763 | ((DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_Strong && |
| 2764 | AQuals.getObjCLifetime() == Qualifiers::OCL_None) || |
| 2765 | (DeducedAQuals.hasConst() && |
| 2766 | DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone))) { |
| 2767 | AQuals.setObjCLifetime(DeducedAQuals.getObjCLifetime()); |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2770 | if (AQuals == DeducedAQuals) { |
| 2771 | // Qualifiers match; there's nothing to do. |
| 2772 | } else if (!DeducedAQuals.compatiblyIncludes(AQuals)) { |
Douglas Gregor | ddaae52 | 2011-06-17 14:36:00 +0000 | [diff] [blame] | 2773 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2774 | } else { |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2775 | // Qualifiers are compatible, so have the argument type adopt the |
| 2776 | // deduced argument type's qualifiers as if we had performed the |
| 2777 | // qualification conversion. |
| 2778 | A = Context.getQualifiedType(A.getUnqualifiedType(), DeducedAQuals); |
| 2779 | } |
| 2780 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2781 | |
| 2782 | // - The transformed A can be another pointer or pointer to member |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 2783 | // type that can be converted to the deduced A via a function pointer |
| 2784 | // conversion and/or a qualification conversion. |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2785 | // |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 2786 | // Also allow conversions which merely strip __attribute__((noreturn)) from |
| 2787 | // function types (recursively). |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2788 | bool ObjCLifetimeConversion = false; |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2789 | QualType ResultTy; |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2790 | if ((A->isAnyPointerType() || A->isMemberPointerType()) && |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2791 | (S.IsQualificationConversion(A, DeducedA, false, |
| 2792 | ObjCLifetimeConversion) || |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 2793 | S.IsFunctionConversion(A, DeducedA, ResultTy))) |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2794 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2795 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2796 | // - 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] | 2797 | // transformed A can be a derived class of the deduced A. [...] |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2798 | // [...] Likewise, if P is a pointer to a class of the form |
| 2799 | // simple-template-id, the transformed A can be a pointer to a |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2800 | // derived class pointed to by the deduced A. |
| 2801 | if (const PointerType *OriginalParamPtr |
| 2802 | = OriginalParamType->getAs<PointerType>()) { |
| 2803 | if (const PointerType *DeducedAPtr = DeducedA->getAs<PointerType>()) { |
| 2804 | if (const PointerType *APtr = A->getAs<PointerType>()) { |
| 2805 | if (A->getPointeeType()->isRecordType()) { |
| 2806 | OriginalParamType = OriginalParamPtr->getPointeeType(); |
| 2807 | DeducedA = DeducedAPtr->getPointeeType(); |
| 2808 | A = APtr->getPointeeType(); |
| 2809 | } |
| 2810 | } |
| 2811 | } |
| 2812 | } |
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 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| 2815 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2816 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2817 | if (A->isRecordType() && isSimpleTemplateIdType(OriginalParamType) && |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 2818 | S.IsDerivedFrom(SourceLocation(), A, DeducedA)) |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2819 | return false; |
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 | return true; |
| 2822 | } |
| 2823 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2824 | /// \brief Finish template argument deduction for a function template, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2825 | /// checking the deduced template arguments for completeness and forming |
| 2826 | /// the function template specialization. |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2827 | /// |
| 2828 | /// \param OriginalCallArgs If non-NULL, the original call arguments against |
| 2829 | /// which the deduced argument types should be compared. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2830 | Sema::TemplateDeductionResult |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2831 | Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2832 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2833 | unsigned NumExplicitlySpecified, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2834 | FunctionDecl *&Specialization, |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2835 | TemplateDeductionInfo &Info, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 2836 | SmallVectorImpl<OriginalCallArg> const *OriginalCallArgs, |
| 2837 | bool PartialOverloading) { |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2838 | // Unevaluated SFINAE context. |
| 2839 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2840 | SFINAETrap Trap(*this); |
| 2841 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2842 | // Enter a new template instantiation context while we instantiate the |
| 2843 | // actual function declaration. |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2844 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2845 | InstantiatingTemplate Inst(*this, Info.getLocation(), FunctionTemplate, |
| 2846 | DeducedArgs, |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2847 | ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution, |
| 2848 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2849 | if (Inst.isInvalid()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2850 | return TDK_InstantiationDepth; |
| 2851 | |
John McCall | e23b871 | 2010-04-29 01:18:58 +0000 | [diff] [blame] | 2852 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
John McCall | 80e58cd | 2010-04-29 00:35:03 +0000 | [diff] [blame] | 2853 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2854 | // C++ [temp.deduct.type]p2: |
| 2855 | // [...] or if any template argument remains neither deduced nor |
| 2856 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2857 | SmallVector<TemplateArgument, 4> Builder; |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2858 | if (auto Result = ConvertDeducedTemplateArguments( |
| 2859 | *this, FunctionTemplate, Deduced, Info, Builder, |
| 2860 | CurrentInstantiationScope, NumExplicitlySpecified, |
| 2861 | PartialOverloading)) |
| 2862 | return Result; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2863 | |
| 2864 | // Form the template argument list from the deduced template arguments. |
| 2865 | TemplateArgumentList *DeducedArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2866 | = TemplateArgumentList::CreateCopy(Context, Builder); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2867 | Info.reset(DeducedArgumentList); |
| 2868 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2869 | // Substitute the deduced template arguments into the function template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2870 | // declaration to produce the function template specialization. |
Douglas Gregor | 1614237 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 2871 | DeclContext *Owner = FunctionTemplate->getDeclContext(); |
| 2872 | if (FunctionTemplate->getFriendObjectKind()) |
| 2873 | Owner = FunctionTemplate->getLexicalDeclContext(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2874 | Specialization = cast_or_null<FunctionDecl>( |
Douglas Gregor | 1614237 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 2875 | SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner, |
Douglas Gregor | 39cacdb | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 2876 | MultiLevelTemplateArgumentList(*DeducedArgumentList))); |
Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 2877 | if (!Specialization || Specialization->isInvalidDecl()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2878 | return TDK_SubstitutionFailure; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2879 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2880 | assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == |
Douglas Gregor | 31fae89 | 2009-09-15 18:26:13 +0000 | [diff] [blame] | 2881 | FunctionTemplate->getCanonicalDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2882 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2883 | // If the template argument list is owned by the function template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2884 | // specialization, release it. |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 2885 | if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList && |
| 2886 | !Trap.hasErrorOccurred()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2887 | Info.take(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2888 | |
Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 2889 | // There may have been an error that did not prevent us from constructing a |
| 2890 | // declaration. Mark the declaration invalid and return with a substitution |
| 2891 | // failure. |
| 2892 | if (Trap.hasErrorOccurred()) { |
| 2893 | Specialization->setInvalidDecl(true); |
| 2894 | return TDK_SubstitutionFailure; |
| 2895 | } |
| 2896 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2897 | if (OriginalCallArgs) { |
| 2898 | // C++ [temp.deduct.call]p4: |
| 2899 | // In general, the deduction process attempts to find template argument |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2900 | // 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] | 2901 | // is transformed as described above). [...] |
| 2902 | for (unsigned I = 0, N = OriginalCallArgs->size(); I != N; ++I) { |
| 2903 | OriginalCallArg OriginalArg = (*OriginalCallArgs)[I]; |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2904 | unsigned ParamIdx = OriginalArg.ArgIdx; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2905 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2906 | if (ParamIdx >= Specialization->getNumParams()) |
| 2907 | continue; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2908 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2909 | QualType DeducedA = Specialization->getParamDecl(ParamIdx)->getType(); |
Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 2910 | if (CheckOriginalCallArgDeduction(*this, OriginalArg, DeducedA)) { |
| 2911 | Info.FirstArg = TemplateArgument(DeducedA); |
| 2912 | Info.SecondArg = TemplateArgument(OriginalArg.OriginalArgType); |
| 2913 | Info.CallArgIndex = OriginalArg.ArgIdx; |
| 2914 | return TDK_DeducedMismatch; |
| 2915 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2916 | } |
| 2917 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2918 | |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2919 | // If we suppressed any diagnostics while performing template argument |
| 2920 | // deduction, and if we haven't already instantiated this declaration, |
| 2921 | // keep track of these diagnostics. They'll be emitted if this specialization |
| 2922 | // is actually used. |
| 2923 | if (Info.diag_begin() != Info.diag_end()) { |
Craig Topper | 79be4cd | 2013-07-05 04:33:53 +0000 | [diff] [blame] | 2924 | SuppressedDiagnosticsMap::iterator |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2925 | Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl()); |
| 2926 | if (Pos == SuppressedDiagnostics.end()) |
| 2927 | SuppressedDiagnostics[Specialization->getCanonicalDecl()] |
| 2928 | .append(Info.diag_begin(), Info.diag_end()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2929 | } |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2930 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2931 | return TDK_Success; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2932 | } |
| 2933 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2934 | /// Gets the type of a function for template-argument-deducton |
| 2935 | /// purposes when it's considered as part of an overload set. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2936 | static QualType GetTypeOfFunction(Sema &S, const OverloadExpr::FindResult &R, |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2937 | FunctionDecl *Fn) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2938 | // We may need to deduce the return type of the function now. |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 2939 | if (S.getLangOpts().CPlusPlus14 && Fn->getReturnType()->isUndeducedType() && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2940 | S.DeduceReturnType(Fn, R.Expression->getExprLoc(), /*Diagnose*/ false)) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2941 | return QualType(); |
| 2942 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2943 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2944 | if (Method->isInstance()) { |
| 2945 | // An instance method that's referenced in a form that doesn't |
| 2946 | // look like a member pointer is just invalid. |
| 2947 | if (!R.HasFormOfMemberPointer) return QualType(); |
| 2948 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2949 | return S.Context.getMemberPointerType(Fn->getType(), |
| 2950 | S.Context.getTypeDeclType(Method->getParent()).getTypePtr()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2951 | } |
| 2952 | |
| 2953 | if (!R.IsAddressOfOperand) return Fn->getType(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2954 | return S.Context.getPointerType(Fn->getType()); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2955 | } |
| 2956 | |
| 2957 | /// Apply the deduction rules for overload sets. |
| 2958 | /// |
| 2959 | /// \return the null type if this argument should be treated as an |
| 2960 | /// undeduced context |
| 2961 | static QualType |
| 2962 | ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams, |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 2963 | Expr *Arg, QualType ParamType, |
| 2964 | bool ParamWasReference) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2965 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2966 | OverloadExpr::FindResult R = OverloadExpr::find(Arg); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2967 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2968 | OverloadExpr *Ovl = R.Expression; |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2969 | |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 2970 | // C++0x [temp.deduct.call]p4 |
| 2971 | unsigned TDF = 0; |
| 2972 | if (ParamWasReference) |
| 2973 | TDF |= TDF_ParamWithReferenceType; |
| 2974 | if (R.IsAddressOfOperand) |
| 2975 | TDF |= TDF_IgnoreQualifiers; |
| 2976 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2977 | // C++0x [temp.deduct.call]p6: |
| 2978 | // When P is a function type, pointer to function type, or pointer |
| 2979 | // to member function type: |
| 2980 | |
| 2981 | if (!ParamType->isFunctionType() && |
| 2982 | !ParamType->isFunctionPointerType() && |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 2983 | !ParamType->isMemberFunctionPointerType()) { |
| 2984 | if (Ovl->hasExplicitTemplateArgs()) { |
| 2985 | // But we can still look for an explicit specialization. |
| 2986 | if (FunctionDecl *ExplicitSpec |
| 2987 | = S.ResolveSingleFunctionTemplateSpecialization(Ovl)) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2988 | return GetTypeOfFunction(S, R, ExplicitSpec); |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 2989 | } |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2990 | |
George Burgess IV | cc2f355 | 2016-03-19 21:51:45 +0000 | [diff] [blame] | 2991 | DeclAccessPair DAP; |
| 2992 | if (FunctionDecl *Viable = |
| 2993 | S.resolveAddressOfOnlyViableOverloadCandidate(Arg, DAP)) |
| 2994 | return GetTypeOfFunction(S, R, Viable); |
| 2995 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 2996 | return QualType(); |
| 2997 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2998 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 2999 | // Gather the explicit template arguments, if any. |
| 3000 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 3001 | if (Ovl->hasExplicitTemplateArgs()) |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 3002 | Ovl->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3003 | QualType Match; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 3004 | for (UnresolvedSetIterator I = Ovl->decls_begin(), |
| 3005 | E = Ovl->decls_end(); I != E; ++I) { |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3006 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 3007 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3008 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) { |
| 3009 | // - If the argument is an overload set containing one or more |
| 3010 | // function templates, the parameter is treated as a |
| 3011 | // non-deduced context. |
| 3012 | if (!Ovl->hasExplicitTemplateArgs()) |
| 3013 | return QualType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3014 | |
| 3015 | // Otherwise, see if we can resolve a function type |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3016 | FunctionDecl *Specialization = nullptr; |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3017 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3018 | if (S.DeduceTemplateArguments(FunTmpl, &ExplicitTemplateArgs, |
| 3019 | Specialization, Info)) |
| 3020 | continue; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3021 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3022 | D = Specialization; |
| 3023 | } |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3024 | |
| 3025 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3026 | QualType ArgType = GetTypeOfFunction(S, R, Fn); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3027 | if (ArgType.isNull()) continue; |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3028 | |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3029 | // Function-to-pointer conversion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3030 | if (!ParamWasReference && ParamType->isPointerType() && |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3031 | ArgType->isFunctionType()) |
| 3032 | ArgType = S.Context.getPointerType(ArgType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3033 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3034 | // - If the argument is an overload set (not containing function |
| 3035 | // templates), trial argument deduction is attempted using each |
| 3036 | // of the members of the set. If deduction succeeds for only one |
| 3037 | // of the overload set members, that member is used as the |
| 3038 | // argument value for the deduction. If deduction succeeds for |
| 3039 | // more than one member of the overload set the parameter is |
| 3040 | // treated as a non-deduced context. |
| 3041 | |
| 3042 | // We do all of this in a fresh context per C++0x [temp.deduct.type]p2: |
| 3043 | // Type deduction is done independently for each P/A pair, and |
| 3044 | // the deduced template argument values are then combined. |
| 3045 | // So we do not reject deductions which were made elsewhere. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3046 | SmallVector<DeducedTemplateArgument, 8> |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3047 | Deduced(TemplateParams->size()); |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3048 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3049 | Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3050 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
| 3051 | ArgType, Info, Deduced, TDF); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3052 | if (Result) continue; |
| 3053 | if (!Match.isNull()) return QualType(); |
| 3054 | Match = ArgType; |
| 3055 | } |
| 3056 | |
| 3057 | return Match; |
| 3058 | } |
| 3059 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3060 | /// \brief Perform the adjustments to the parameter and argument types |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3061 | /// described in C++ [temp.deduct.call]. |
| 3062 | /// |
| 3063 | /// \returns true if the caller should not attempt to perform any template |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3064 | /// argument deduction based on this P/A pair because the argument is an |
| 3065 | /// overloaded function set that could not be resolved. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3066 | static bool AdjustFunctionParmAndArgTypesForDeduction(Sema &S, |
| 3067 | TemplateParameterList *TemplateParams, |
| 3068 | QualType &ParamType, |
| 3069 | QualType &ArgType, |
| 3070 | Expr *Arg, |
| 3071 | unsigned &TDF) { |
| 3072 | // C++0x [temp.deduct.call]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3073 | // 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] | 3074 | // are ignored for type deduction. |
Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3075 | if (ParamType.hasQualifiers()) |
| 3076 | ParamType = ParamType.getUnqualifiedType(); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3077 | |
| 3078 | // [...] If P is a reference type, the type referred to by P is |
| 3079 | // used for type deduction. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3080 | const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>(); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3081 | if (ParamRefType) |
| 3082 | ParamType = ParamRefType->getPointeeType(); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3083 | |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3084 | // Overload sets usually make this parameter an undeduced context, |
| 3085 | // but there are sometimes special circumstances. Typically |
| 3086 | // involving a template-id-expr. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3087 | if (ArgType == S.Context.OverloadTy) { |
| 3088 | ArgType = ResolveOverloadForDeduction(S, TemplateParams, |
| 3089 | Arg, ParamType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3090 | ParamRefType != nullptr); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3091 | if (ArgType.isNull()) |
| 3092 | return true; |
| 3093 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3094 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3095 | if (ParamRefType) { |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3096 | // If the argument has incomplete array type, try to complete its type. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3097 | if (ArgType->isIncompleteArrayType()) { |
| 3098 | S.completeExprArrayBound(Arg); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3099 | ArgType = Arg->getType(); |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3100 | } |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3101 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3102 | // C++0x [temp.deduct.call]p3: |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3103 | // If P is an rvalue reference to a cv-unqualified template |
| 3104 | // parameter and the argument is an lvalue, the type "lvalue |
| 3105 | // reference to A" is used in place of A for type deduction. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3106 | if (ParamRefType->isRValueReferenceType() && |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3107 | !ParamType.getQualifiers() && |
| 3108 | isa<TemplateTypeParmType>(ParamType) && |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3109 | Arg->isLValue()) |
| 3110 | ArgType = S.Context.getLValueReferenceType(ArgType); |
| 3111 | } else { |
| 3112 | // C++ [temp.deduct.call]p2: |
| 3113 | // If P is not a reference type: |
| 3114 | // - If A is an array type, the pointer type produced by the |
| 3115 | // array-to-pointer standard conversion (4.2) is used in place of |
| 3116 | // A for type deduction; otherwise, |
| 3117 | if (ArgType->isArrayType()) |
| 3118 | ArgType = S.Context.getArrayDecayedType(ArgType); |
| 3119 | // - If A is a function type, the pointer type produced by the |
| 3120 | // function-to-pointer standard conversion (4.3) is used in place |
| 3121 | // of A for type deduction; otherwise, |
| 3122 | else if (ArgType->isFunctionType()) |
| 3123 | ArgType = S.Context.getPointerType(ArgType); |
| 3124 | else { |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3125 | // - 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] | 3126 | // type are ignored for type deduction. |
Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3127 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3128 | } |
| 3129 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3130 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3131 | // C++0x [temp.deduct.call]p4: |
| 3132 | // In general, the deduction process attempts to find template argument |
| 3133 | // values that will make the deduced A identical to A (after the type A |
| 3134 | // is transformed as described above). [...] |
| 3135 | TDF = TDF_SkipNonDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3136 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3137 | // - If the original P is a reference type, the deduced A (i.e., the |
| 3138 | // type referred to by the reference) can be more cv-qualified than |
| 3139 | // the transformed A. |
| 3140 | if (ParamRefType) |
| 3141 | TDF |= TDF_ParamWithReferenceType; |
| 3142 | // - The transformed A can be another pointer or pointer to member |
| 3143 | // type that can be converted to the deduced A via a qualification |
| 3144 | // conversion (4.4). |
| 3145 | if (ArgType->isPointerType() || ArgType->isMemberPointerType() || |
| 3146 | ArgType->isObjCObjectPointerType()) |
| 3147 | TDF |= TDF_IgnoreQualifiers; |
| 3148 | // - If P is a class and P has the form simple-template-id, then the |
| 3149 | // transformed A can be a derived class of the deduced A. Likewise, |
| 3150 | // if P is a pointer to a class of the form simple-template-id, the |
| 3151 | // transformed A can be a pointer to a derived class pointed to by |
| 3152 | // the deduced A. |
| 3153 | if (isSimpleTemplateIdType(ParamType) || |
| 3154 | (isa<PointerType>(ParamType) && |
| 3155 | isSimpleTemplateIdType( |
| 3156 | ParamType->getAs<PointerType>()->getPointeeType()))) |
| 3157 | TDF |= TDF_DerivedClass; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3158 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3159 | return false; |
| 3160 | } |
| 3161 | |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3162 | static bool |
| 3163 | hasDeducibleTemplateParameters(Sema &S, FunctionTemplateDecl *FunctionTemplate, |
| 3164 | QualType T); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3165 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3166 | static Sema::TemplateDeductionResult DeduceTemplateArgumentByListElement( |
| 3167 | Sema &S, TemplateParameterList *TemplateParams, QualType ParamType, |
| 3168 | Expr *Arg, TemplateDeductionInfo &Info, |
| 3169 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, unsigned TDF); |
| 3170 | |
| 3171 | /// \brief Attempt template argument deduction from an initializer list |
| 3172 | /// deemed to be an argument in a function call. |
| 3173 | static bool |
| 3174 | DeduceFromInitializerList(Sema &S, TemplateParameterList *TemplateParams, |
| 3175 | QualType AdjustedParamType, InitListExpr *ILE, |
| 3176 | TemplateDeductionInfo &Info, |
| 3177 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3178 | unsigned TDF, Sema::TemplateDeductionResult &Result) { |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3179 | |
| 3180 | // [temp.deduct.call] p1 (post CWG-1591) |
| 3181 | // If removing references and cv-qualifiers from P gives |
| 3182 | // std::initializer_list<P0> or P0[N] for some P0 and N and the argument is a |
| 3183 | // non-empty initializer list (8.5.4), then deduction is performed instead for |
| 3184 | // each element of the initializer list, taking P0 as a function template |
| 3185 | // parameter type and the initializer element as its argument, and in the |
| 3186 | // P0[N] case, if N is a non-type template parameter, N is deduced from the |
| 3187 | // length of the initializer list. Otherwise, an initializer list argument |
| 3188 | // causes the parameter to be considered a non-deduced context |
| 3189 | |
| 3190 | const bool IsConstSizedArray = AdjustedParamType->isConstantArrayType(); |
| 3191 | |
| 3192 | const bool IsDependentSizedArray = |
| 3193 | !IsConstSizedArray && AdjustedParamType->isDependentSizedArrayType(); |
| 3194 | |
Faisal Vali | dd76cc1 | 2015-12-10 12:29:11 +0000 | [diff] [blame] | 3195 | 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] | 3196 | |
| 3197 | const bool IsSTDList = !IsConstSizedArray && !IsDependentSizedArray && |
| 3198 | S.isStdInitializerList(AdjustedParamType, &ElTy); |
| 3199 | |
| 3200 | if (!IsConstSizedArray && !IsDependentSizedArray && !IsSTDList) |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3201 | return false; |
| 3202 | |
| 3203 | Result = Sema::TDK_Success; |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3204 | // If we are not deducing against the 'T' in a std::initializer_list<T> then |
| 3205 | // deduce against the 'T' in T[N]. |
| 3206 | if (ElTy.isNull()) { |
| 3207 | assert(!IsSTDList); |
| 3208 | ElTy = S.Context.getAsArrayType(AdjustedParamType)->getElementType(); |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3209 | } |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3210 | // Deduction only needs to be done for dependent types. |
| 3211 | if (ElTy->isDependentType()) { |
| 3212 | for (Expr *E : ILE->inits()) { |
Craig Topper | 0852953 | 2015-12-10 08:49:55 +0000 | [diff] [blame] | 3213 | if ((Result = DeduceTemplateArgumentByListElement(S, TemplateParams, ElTy, |
| 3214 | E, Info, Deduced, TDF))) |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3215 | return true; |
| 3216 | } |
| 3217 | } |
| 3218 | if (IsDependentSizedArray) { |
| 3219 | const DependentSizedArrayType *ArrTy = |
| 3220 | S.Context.getAsDependentSizedArrayType(AdjustedParamType); |
| 3221 | // Determine the array bound is something we can deduce. |
| 3222 | if (NonTypeTemplateParmDecl *NTTP = |
| 3223 | getDeducedParameterFromExpr(ArrTy->getSizeExpr())) { |
| 3224 | // We can perform template argument deduction for the given non-type |
| 3225 | // template parameter. |
| 3226 | assert(NTTP->getDepth() == 0 && |
| 3227 | "Cannot deduce non-type template argument at depth > 0"); |
| 3228 | llvm::APInt Size(S.Context.getIntWidth(NTTP->getType()), |
| 3229 | ILE->getNumInits()); |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3230 | |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3231 | Result = DeduceNonTypeTemplateArgument( |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 3232 | S, TemplateParams, NTTP, llvm::APSInt(Size), NTTP->getType(), |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3233 | /*ArrayBound=*/true, Info, Deduced); |
| 3234 | } |
| 3235 | } |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3236 | return true; |
| 3237 | } |
| 3238 | |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3239 | /// \brief Perform template argument deduction by matching a parameter type |
| 3240 | /// against a single expression, where the expression is an element of |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3241 | /// an initializer list that was originally matched against a parameter |
| 3242 | /// of type \c initializer_list\<ParamType\>. |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3243 | static Sema::TemplateDeductionResult |
| 3244 | DeduceTemplateArgumentByListElement(Sema &S, |
| 3245 | TemplateParameterList *TemplateParams, |
| 3246 | QualType ParamType, Expr *Arg, |
| 3247 | TemplateDeductionInfo &Info, |
| 3248 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3249 | unsigned TDF) { |
| 3250 | // Handle the case where an init list contains another init list as the |
| 3251 | // element. |
| 3252 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3253 | Sema::TemplateDeductionResult Result; |
| 3254 | if (!DeduceFromInitializerList(S, TemplateParams, |
| 3255 | ParamType.getNonReferenceType(), ILE, Info, |
| 3256 | Deduced, TDF, Result)) |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3257 | return Sema::TDK_Success; // Just ignore this expression. |
| 3258 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3259 | return Result; |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3260 | } |
| 3261 | |
| 3262 | // For all other cases, just match by type. |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3263 | QualType ArgType = Arg->getType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3264 | if (AdjustFunctionParmAndArgTypesForDeduction(S, TemplateParams, ParamType, |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3265 | ArgType, Arg, TDF)) { |
| 3266 | Info.Expression = Arg; |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3267 | return Sema::TDK_FailedOverloadResolution; |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3268 | } |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3269 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3270 | ArgType, Info, Deduced, TDF); |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3271 | } |
| 3272 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3273 | /// \brief Perform template argument deduction from a function call |
| 3274 | /// (C++ [temp.deduct.call]). |
| 3275 | /// |
| 3276 | /// \param FunctionTemplate the function template for which we are performing |
| 3277 | /// template argument deduction. |
| 3278 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3279 | /// \param ExplicitTemplateArgs the explicit template arguments provided |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 3280 | /// for this call. |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3281 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3282 | /// \param Args the function call arguments |
| 3283 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3284 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3285 | /// this will be set to the function template specialization produced by |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3286 | /// template argument deduction. |
| 3287 | /// |
| 3288 | /// \param Info the argument will be updated to provide additional information |
| 3289 | /// about template argument deduction. |
| 3290 | /// |
| 3291 | /// \returns the result of template argument deduction. |
Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 3292 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3293 | FunctionTemplateDecl *FunctionTemplate, |
| 3294 | TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3295 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 3296 | bool PartialOverloading) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3297 | if (FunctionTemplate->isInvalidDecl()) |
| 3298 | return TDK_Invalid; |
| 3299 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3300 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3301 | unsigned NumParams = Function->getNumParams(); |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3302 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3303 | // C++ [temp.deduct.call]p1: |
| 3304 | // Template argument deduction is done by comparing each function template |
| 3305 | // parameter type (call it P) with the type of the corresponding argument |
| 3306 | // of the call (call it A) as described below. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3307 | unsigned CheckArgs = Args.size(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3308 | if (Args.size() < Function->getMinRequiredArguments() && !PartialOverloading) |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3309 | return TDK_TooFewArguments; |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3310 | else if (TooManyArguments(NumParams, Args.size(), PartialOverloading)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3311 | const FunctionProtoType *Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3312 | = Function->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3313 | if (Proto->isTemplateVariadic()) |
| 3314 | /* Do nothing */; |
| 3315 | else if (Proto->isVariadic()) |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3316 | CheckArgs = NumParams; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3317 | else |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3318 | return TDK_TooManyArguments; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3319 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3320 | |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3321 | // The types of the parameters from which we will perform template argument |
| 3322 | // deduction. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3323 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3324 | TemplateParameterList *TemplateParams |
| 3325 | = FunctionTemplate->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3326 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 3327 | SmallVector<QualType, 4> ParamTypes; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3328 | unsigned NumExplicitlySpecified = 0; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3329 | if (ExplicitTemplateArgs) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3330 | TemplateDeductionResult Result = |
| 3331 | SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3332 | *ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3333 | Deduced, |
| 3334 | ParamTypes, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3335 | nullptr, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3336 | Info); |
| 3337 | if (Result) |
| 3338 | return Result; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3339 | |
| 3340 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3341 | } else { |
| 3342 | // Just fill in the parameter types from the function declaration. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3343 | for (unsigned I = 0; I != NumParams; ++I) |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3344 | ParamTypes.push_back(Function->getParamDecl(I)->getType()); |
| 3345 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3346 | |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3347 | // Deduce template arguments from the function parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3348 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3349 | unsigned ArgIdx = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3350 | SmallVector<OriginalCallArg, 4> OriginalCallArgs; |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3351 | for (unsigned ParamIdx = 0, NumParamTypes = ParamTypes.size(); |
| 3352 | ParamIdx != NumParamTypes; ++ParamIdx) { |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3353 | QualType OrigParamType = ParamTypes[ParamIdx]; |
| 3354 | QualType ParamType = OrigParamType; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3355 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3356 | const PackExpansionType *ParamExpansion |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3357 | = dyn_cast<PackExpansionType>(ParamType); |
| 3358 | if (!ParamExpansion) { |
| 3359 | // Simple case: matching a function parameter to a function argument. |
| 3360 | if (ArgIdx >= CheckArgs) |
| 3361 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3362 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3363 | Expr *Arg = Args[ArgIdx++]; |
| 3364 | QualType ArgType = Arg->getType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3365 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3366 | unsigned TDF = 0; |
| 3367 | if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams, |
| 3368 | ParamType, ArgType, Arg, |
| 3369 | TDF)) |
| 3370 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3371 | |
Douglas Gregor | 0c83c81 | 2011-10-09 22:06:46 +0000 | [diff] [blame] | 3372 | // If we have nothing to deduce, we're done. |
| 3373 | if (!hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
| 3374 | continue; |
| 3375 | |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3376 | // If the argument is an initializer list ... |
| 3377 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3378 | TemplateDeductionResult Result; |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3379 | // Removing references was already done. |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3380 | if (!DeduceFromInitializerList(*this, TemplateParams, ParamType, ILE, |
| 3381 | Info, Deduced, TDF, Result)) |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3382 | continue; |
| 3383 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3384 | if (Result) |
| 3385 | return Result; |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3386 | // Don't track the argument type, since an initializer list has none. |
| 3387 | continue; |
| 3388 | } |
| 3389 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3390 | // Keep track of the argument type and corresponding parameter index, |
| 3391 | // so we can check for compatibility between the deduced A and A. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3392 | OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx-1, |
Douglas Gregor | 0c83c81 | 2011-10-09 22:06:46 +0000 | [diff] [blame] | 3393 | ArgType)); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3394 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3395 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3396 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3397 | ParamType, ArgType, |
| 3398 | Info, Deduced, TDF)) |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3399 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3400 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3401 | continue; |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3402 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3403 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3404 | // C++0x [temp.deduct.call]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3405 | // For a function parameter pack that occurs at the end of the |
| 3406 | // parameter-declaration-list, the type A of each remaining argument of |
| 3407 | // the call is compared with the type P of the declarator-id of the |
| 3408 | // function parameter pack. Each comparison deduces template arguments |
| 3409 | // for subsequent positions in the template parameter packs expanded by |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3410 | // the function parameter pack. For a function parameter pack that does |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3411 | // 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] | 3412 | // the parameter pack is a non-deduced context. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3413 | if (ParamIdx + 1 < NumParamTypes) |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3414 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3415 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3416 | QualType ParamPattern = ParamExpansion->getPattern(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3417 | PackDeductionScope PackScope(*this, TemplateParams, Deduced, Info, |
| 3418 | ParamPattern); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3419 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3420 | bool HasAnyArguments = false; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3421 | for (; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3422 | HasAnyArguments = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3423 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3424 | QualType OrigParamType = ParamPattern; |
| 3425 | ParamType = OrigParamType; |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3426 | Expr *Arg = Args[ArgIdx]; |
| 3427 | QualType ArgType = Arg->getType(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3428 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3429 | unsigned TDF = 0; |
| 3430 | if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams, |
| 3431 | ParamType, ArgType, Arg, |
| 3432 | TDF)) { |
| 3433 | // We can't actually perform any deduction for this argument, so stop |
| 3434 | // deduction at this point. |
| 3435 | ++ArgIdx; |
| 3436 | break; |
| 3437 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3438 | |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3439 | // As above, initializer lists need special handling. |
| 3440 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3441 | TemplateDeductionResult Result; |
| 3442 | if (!DeduceFromInitializerList(*this, TemplateParams, ParamType, ILE, |
| 3443 | Info, Deduced, TDF, Result)) { |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3444 | ++ArgIdx; |
| 3445 | break; |
| 3446 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3447 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3448 | if (Result) |
| 3449 | return Result; |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3450 | } else { |
| 3451 | |
| 3452 | // Keep track of the argument type and corresponding argument index, |
| 3453 | // so we can check for compatibility between the deduced A and A. |
| 3454 | if (hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3455 | OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx, |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3456 | ArgType)); |
| 3457 | |
| 3458 | if (TemplateDeductionResult Result |
| 3459 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3460 | ParamType, ArgType, Info, |
| 3461 | Deduced, TDF)) |
| 3462 | return Result; |
| 3463 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3464 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3465 | PackScope.nextPackElement(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3466 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3467 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3468 | // Build argument packs for each of the parameter packs expanded by this |
| 3469 | // pack expansion. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3470 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3471 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3472 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3473 | // After we've matching against a parameter pack, we're done. |
| 3474 | break; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3475 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3476 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3477 | return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3478 | NumExplicitlySpecified, Specialization, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3479 | Info, &OriginalCallArgs, |
| 3480 | PartialOverloading); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3481 | } |
| 3482 | |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3483 | QualType Sema::adjustCCAndNoReturn(QualType ArgFunctionType, |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3484 | QualType FunctionType, |
| 3485 | bool AdjustExceptionSpec) { |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3486 | if (ArgFunctionType.isNull()) |
| 3487 | return ArgFunctionType; |
| 3488 | |
| 3489 | const FunctionProtoType *FunctionTypeP = |
| 3490 | FunctionType->castAs<FunctionProtoType>(); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3491 | const FunctionProtoType *ArgFunctionTypeP = |
| 3492 | ArgFunctionType->getAs<FunctionProtoType>(); |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3493 | |
| 3494 | FunctionProtoType::ExtProtoInfo EPI = ArgFunctionTypeP->getExtProtoInfo(); |
| 3495 | bool Rebuild = false; |
| 3496 | |
| 3497 | CallingConv CC = FunctionTypeP->getCallConv(); |
| 3498 | if (EPI.ExtInfo.getCC() != CC) { |
| 3499 | EPI.ExtInfo = EPI.ExtInfo.withCallingConv(CC); |
| 3500 | Rebuild = true; |
| 3501 | } |
| 3502 | |
| 3503 | bool NoReturn = FunctionTypeP->getNoReturnAttr(); |
| 3504 | if (EPI.ExtInfo.getNoReturn() != NoReturn) { |
| 3505 | EPI.ExtInfo = EPI.ExtInfo.withNoReturn(NoReturn); |
| 3506 | Rebuild = true; |
| 3507 | } |
| 3508 | |
| 3509 | if (AdjustExceptionSpec && (FunctionTypeP->hasExceptionSpec() || |
| 3510 | ArgFunctionTypeP->hasExceptionSpec())) { |
| 3511 | EPI.ExceptionSpec = FunctionTypeP->getExtProtoInfo().ExceptionSpec; |
| 3512 | Rebuild = true; |
| 3513 | } |
| 3514 | |
| 3515 | if (!Rebuild) |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3516 | return ArgFunctionType; |
| 3517 | |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3518 | return Context.getFunctionType(ArgFunctionTypeP->getReturnType(), |
| 3519 | ArgFunctionTypeP->getParamTypes(), EPI); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3520 | } |
| 3521 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3522 | /// \brief Deduce template arguments when taking the address of a function |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3523 | /// template (C++ [temp.deduct.funcaddr]) or matching a specialization to |
| 3524 | /// a template. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3525 | /// |
| 3526 | /// \param FunctionTemplate the function template for which we are performing |
| 3527 | /// template argument deduction. |
| 3528 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3529 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3530 | /// arguments. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3531 | /// |
| 3532 | /// \param ArgFunctionType the function type that will be used as the |
| 3533 | /// "argument" type (A) when performing template argument deduction from the |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3534 | /// function template's function type. This type may be NULL, if there is no |
| 3535 | /// argument type to compare against, in C++0x [temp.arg.explicit]p3. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3536 | /// |
| 3537 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3538 | /// this will be set to the function template specialization produced by |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3539 | /// template argument deduction. |
| 3540 | /// |
| 3541 | /// \param Info the argument will be updated to provide additional information |
| 3542 | /// about template argument deduction. |
| 3543 | /// |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3544 | /// \param IsAddressOfFunction If \c true, we are deducing as part of taking |
| 3545 | /// the address of a function template per [temp.deduct.funcaddr] and |
| 3546 | /// [over.over]. If \c false, we are looking up a function template |
| 3547 | /// specialization based on its signature, per [temp.deduct.decl]. |
| 3548 | /// |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3549 | /// \returns the result of template argument deduction. |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3550 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3551 | FunctionTemplateDecl *FunctionTemplate, |
| 3552 | TemplateArgumentListInfo *ExplicitTemplateArgs, QualType ArgFunctionType, |
| 3553 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 3554 | bool IsAddressOfFunction) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3555 | if (FunctionTemplate->isInvalidDecl()) |
| 3556 | return TDK_Invalid; |
| 3557 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3558 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 3559 | TemplateParameterList *TemplateParams |
| 3560 | = FunctionTemplate->getTemplateParameters(); |
| 3561 | QualType FunctionType = Function->getType(); |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3562 | |
| 3563 | // When taking the address of a function, we require convertibility of |
| 3564 | // the resulting function type. Otherwise, we allow arbitrary mismatches |
| 3565 | // of calling convention, noreturn, and noexcept. |
| 3566 | if (!IsAddressOfFunction) |
| 3567 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, FunctionType, |
| 3568 | /*AdjustExceptionSpec*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3569 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3570 | // Substitute any explicit template arguments. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3571 | LocalInstantiationScope InstScope(*this); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3572 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3573 | unsigned NumExplicitlySpecified = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3574 | SmallVector<QualType, 4> ParamTypes; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3575 | if (ExplicitTemplateArgs) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3576 | if (TemplateDeductionResult Result |
| 3577 | = SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3578 | *ExplicitTemplateArgs, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3579 | Deduced, ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3580 | &FunctionType, Info)) |
| 3581 | return Result; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3582 | |
| 3583 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3586 | // Unevaluated SFINAE context. |
| 3587 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3588 | SFINAETrap Trap(*this); |
| 3589 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3590 | Deduced.resize(TemplateParams->size()); |
| 3591 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3592 | // 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] | 3593 | // type so that we treat it as a non-deduced context in what follows. If we |
| 3594 | // are looking up by signature, the signature type should also have a deduced |
| 3595 | // return type, which we instead expect to exactly match. |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3596 | bool HasDeducedReturnType = false; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3597 | if (getLangOpts().CPlusPlus14 && IsAddressOfFunction && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3598 | Function->getReturnType()->getContainedAutoType()) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3599 | FunctionType = SubstAutoType(FunctionType, Context.DependentTy); |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3600 | HasDeducedReturnType = true; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3601 | } |
| 3602 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3603 | if (!ArgFunctionType.isNull()) { |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3604 | unsigned TDF = TDF_TopLevelParameterTypeList; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3605 | if (IsAddressOfFunction) |
| 3606 | TDF |= TDF_InOverloadResolution; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3607 | // Deduce template arguments from the function type. |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3608 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3609 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3610 | FunctionType, ArgFunctionType, |
| 3611 | Info, Deduced, TDF)) |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3612 | return Result; |
| 3613 | } |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3614 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3615 | if (TemplateDeductionResult Result |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3616 | = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
| 3617 | NumExplicitlySpecified, |
| 3618 | Specialization, Info)) |
| 3619 | return Result; |
| 3620 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3621 | // If the function has a deduced return type, deduce it now, so we can check |
| 3622 | // that the deduced function type matches the requested type. |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3623 | if (HasDeducedReturnType && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3624 | Specialization->getReturnType()->isUndeducedType() && |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3625 | DeduceReturnType(Specialization, Info.getLocation(), false)) |
| 3626 | return TDK_MiscellaneousDeductionFailure; |
| 3627 | |
Richard Smith | 9095e5b | 2016-11-01 01:31:23 +0000 | [diff] [blame] | 3628 | // If the function has a dependent exception specification, resolve it now, |
| 3629 | // so we can check that the exception specification matches. |
| 3630 | auto *SpecializationFPT = |
| 3631 | Specialization->getType()->castAs<FunctionProtoType>(); |
| 3632 | if (getLangOpts().CPlusPlus1z && |
| 3633 | isUnresolvedExceptionSpec(SpecializationFPT->getExceptionSpecType()) && |
| 3634 | !ResolveExceptionSpec(Info.getLocation(), SpecializationFPT)) |
| 3635 | return TDK_MiscellaneousDeductionFailure; |
| 3636 | |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3637 | // Adjust the exception specification of the argument again to match the |
| 3638 | // substituted and resolved type we just formed. (Calling convention and |
| 3639 | // noreturn can't be dependent, so we don't actually need this for them |
| 3640 | // right now.) |
| 3641 | QualType SpecializationType = Specialization->getType(); |
| 3642 | if (!IsAddressOfFunction) |
| 3643 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, SpecializationType, |
| 3644 | /*AdjustExceptionSpec*/true); |
| 3645 | |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3646 | // 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] | 3647 | // specialization with respect to arguments of compatible pointer to function |
| 3648 | // types, template argument deduction fails. |
| 3649 | if (!ArgFunctionType.isNull()) { |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3650 | if (IsAddressOfFunction && |
| 3651 | !isSameOrCompatibleFunctionType( |
| 3652 | Context.getCanonicalType(SpecializationType), |
| 3653 | Context.getCanonicalType(ArgFunctionType))) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3654 | return TDK_MiscellaneousDeductionFailure; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3655 | |
| 3656 | if (!IsAddressOfFunction && |
| 3657 | !Context.hasSameType(SpecializationType, ArgFunctionType)) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3658 | return TDK_MiscellaneousDeductionFailure; |
| 3659 | } |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3660 | |
| 3661 | return TDK_Success; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3662 | } |
| 3663 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3664 | /// \brief Given a function declaration (e.g. a generic lambda conversion |
| 3665 | /// function) that contains an 'auto' in its result type, substitute it |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3666 | /// with TypeToReplaceAutoWith. Be careful to pass in the type you want |
| 3667 | /// to replace 'auto' with and not the actual result type you want |
| 3668 | /// to set the function to. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3669 | static inline void |
| 3670 | SubstAutoWithinFunctionReturnType(FunctionDecl *F, |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3671 | QualType TypeToReplaceAutoWith, Sema &S) { |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3672 | assert(!TypeToReplaceAutoWith->getContainedAutoType()); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3673 | QualType AutoResultType = F->getReturnType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3674 | assert(AutoResultType->getContainedAutoType()); |
| 3675 | QualType DeducedResultType = S.SubstAutoType(AutoResultType, |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3676 | TypeToReplaceAutoWith); |
| 3677 | S.Context.adjustDeducedFunctionResultType(F, DeducedResultType); |
| 3678 | } |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3679 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3680 | /// \brief Given a specialized conversion operator of a generic lambda |
| 3681 | /// create the corresponding specializations of the call operator and |
| 3682 | /// the static-invoker. If the return type of the call operator is auto, |
| 3683 | /// deduce its return type and check if that matches the |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3684 | /// return type of the destination function ptr. |
| 3685 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3686 | static inline Sema::TemplateDeductionResult |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3687 | SpecializeCorrespondingLambdaCallOperatorAndInvoker( |
| 3688 | CXXConversionDecl *ConversionSpecialized, |
| 3689 | SmallVectorImpl<DeducedTemplateArgument> &DeducedArguments, |
| 3690 | QualType ReturnTypeOfDestFunctionPtr, |
| 3691 | TemplateDeductionInfo &TDInfo, |
| 3692 | Sema &S) { |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3693 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3694 | CXXRecordDecl *LambdaClass = ConversionSpecialized->getParent(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3695 | assert(LambdaClass && LambdaClass->isGenericLambda()); |
| 3696 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3697 | CXXMethodDecl *CallOpGeneric = LambdaClass->getLambdaCallOperator(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3698 | QualType CallOpResultType = CallOpGeneric->getReturnType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3699 | const bool GenericLambdaCallOperatorHasDeducedReturnType = |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3700 | CallOpResultType->getContainedAutoType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3701 | |
| 3702 | FunctionTemplateDecl *CallOpTemplate = |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3703 | CallOpGeneric->getDescribedFunctionTemplate(); |
| 3704 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3705 | FunctionDecl *CallOpSpecialized = nullptr; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3706 | // Use the deduced arguments of the conversion function, to specialize our |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3707 | // generic lambda's call operator. |
| 3708 | if (Sema::TemplateDeductionResult Result |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3709 | = S.FinishTemplateArgumentDeduction(CallOpTemplate, |
| 3710 | DeducedArguments, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3711 | 0, CallOpSpecialized, TDInfo)) |
| 3712 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3713 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3714 | // 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] | 3715 | if (GenericLambdaCallOperatorHasDeducedReturnType && |
| 3716 | CallOpSpecialized->getReturnType()->isUndeducedType()) |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3717 | S.DeduceReturnType(CallOpSpecialized, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3718 | CallOpSpecialized->getPointOfInstantiation(), |
| 3719 | /*Diagnose*/ true); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3720 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3721 | // Check to see if the return type of the destination ptr-to-function |
| 3722 | // matches the return type of the call operator. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3723 | if (!S.Context.hasSameType(CallOpSpecialized->getReturnType(), |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3724 | ReturnTypeOfDestFunctionPtr)) |
| 3725 | return Sema::TDK_NonDeducedMismatch; |
| 3726 | // Since we have succeeded in matching the source and destination |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3727 | // ptr-to-functions (now including return type), and have successfully |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3728 | // specialized our corresponding call operator, we are ready to |
| 3729 | // specialize the static invoker with the deduced arguments of our |
| 3730 | // ptr-to-function. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3731 | FunctionDecl *InvokerSpecialized = nullptr; |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3732 | FunctionTemplateDecl *InvokerTemplate = LambdaClass-> |
| 3733 | getLambdaStaticInvoker()->getDescribedFunctionTemplate(); |
| 3734 | |
Yaron Keren | f428fcf | 2015-05-13 17:56:46 +0000 | [diff] [blame] | 3735 | #ifndef NDEBUG |
| 3736 | Sema::TemplateDeductionResult LLVM_ATTRIBUTE_UNUSED Result = |
| 3737 | #endif |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3738 | S.FinishTemplateArgumentDeduction(InvokerTemplate, DeducedArguments, 0, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3739 | InvokerSpecialized, TDInfo); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3740 | assert(Result == Sema::TDK_Success && |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3741 | "If the call operator succeeded so should the invoker!"); |
| 3742 | // Set the result type to match the corresponding call operator |
| 3743 | // specialization's result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3744 | if (GenericLambdaCallOperatorHasDeducedReturnType && |
| 3745 | InvokerSpecialized->getReturnType()->isUndeducedType()) { |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3746 | // Be sure to get the type to replace 'auto' with and not |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3747 | // the full result type of the call op specialization |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3748 | // to substitute into the 'auto' of the invoker and conversion |
| 3749 | // function. |
| 3750 | // For e.g. |
| 3751 | // int* (*fp)(int*) = [](auto* a) -> auto* { return a; }; |
| 3752 | // We don't want to subst 'int*' into 'auto' to get int**. |
| 3753 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3754 | QualType TypeToReplaceAutoWith = CallOpSpecialized->getReturnType() |
| 3755 | ->getContainedAutoType() |
| 3756 | ->getDeducedType(); |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3757 | SubstAutoWithinFunctionReturnType(InvokerSpecialized, |
| 3758 | TypeToReplaceAutoWith, S); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3759 | SubstAutoWithinFunctionReturnType(ConversionSpecialized, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3760 | TypeToReplaceAutoWith, S); |
| 3761 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3762 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3763 | // Ensure that static invoker doesn't have a const qualifier. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3764 | // FIXME: When creating the InvokerTemplate in SemaLambda.cpp |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3765 | // do not use the CallOperator's TypeSourceInfo which allows |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3766 | // the const qualifier to leak through. |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3767 | const FunctionProtoType *InvokerFPT = InvokerSpecialized-> |
| 3768 | getType().getTypePtr()->castAs<FunctionProtoType>(); |
| 3769 | FunctionProtoType::ExtProtoInfo EPI = InvokerFPT->getExtProtoInfo(); |
| 3770 | EPI.TypeQuals = 0; |
| 3771 | InvokerSpecialized->setType(S.Context.getFunctionType( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3772 | InvokerFPT->getReturnType(), InvokerFPT->getParamTypes(), EPI)); |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3773 | return Sema::TDK_Success; |
| 3774 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3775 | /// \brief Deduce template arguments for a templated conversion |
| 3776 | /// function (C++ [temp.deduct.conv]) and, if successful, produce a |
| 3777 | /// conversion function template specialization. |
| 3778 | Sema::TemplateDeductionResult |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3779 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *ConversionTemplate, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3780 | QualType ToType, |
| 3781 | CXXConversionDecl *&Specialization, |
| 3782 | TemplateDeductionInfo &Info) { |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3783 | if (ConversionTemplate->isInvalidDecl()) |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3784 | return TDK_Invalid; |
| 3785 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3786 | CXXConversionDecl *ConversionGeneric |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3787 | = cast<CXXConversionDecl>(ConversionTemplate->getTemplatedDecl()); |
| 3788 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3789 | QualType FromType = ConversionGeneric->getConversionType(); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3790 | |
| 3791 | // Canonicalize the types for deduction. |
| 3792 | QualType P = Context.getCanonicalType(FromType); |
| 3793 | QualType A = Context.getCanonicalType(ToType); |
| 3794 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3795 | // C++0x [temp.deduct.conv]p2: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3796 | // If P is a reference type, the type referred to by P is used for |
| 3797 | // type deduction. |
| 3798 | if (const ReferenceType *PRef = P->getAs<ReferenceType>()) |
| 3799 | P = PRef->getPointeeType(); |
| 3800 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3801 | // C++0x [temp.deduct.conv]p4: |
| 3802 | // [...] 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] | 3803 | // for type deduction. |
| 3804 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3805 | A = ARef->getPointeeType().getUnqualifiedType(); |
| 3806 | // C++ [temp.deduct.conv]p3: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3807 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3808 | // If A is not a reference type: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3809 | else { |
| 3810 | assert(!A->isReferenceType() && "Reference types were handled above"); |
| 3811 | |
| 3812 | // - If P is an array type, the pointer type produced by the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3813 | // array-to-pointer standard conversion (4.2) is used in place |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3814 | // of P for type deduction; otherwise, |
| 3815 | if (P->isArrayType()) |
| 3816 | P = Context.getArrayDecayedType(P); |
| 3817 | // - If P is a function type, the pointer type produced by the |
| 3818 | // function-to-pointer standard conversion (4.3) is used in |
| 3819 | // place of P for type deduction; otherwise, |
| 3820 | else if (P->isFunctionType()) |
| 3821 | P = Context.getPointerType(P); |
| 3822 | // - 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] | 3823 | // P's type are ignored for type deduction. |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3824 | else |
| 3825 | P = P.getUnqualifiedType(); |
| 3826 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3827 | // C++0x [temp.deduct.conv]p4: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3828 | // 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] | 3829 | // 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] | 3830 | // referred to by A is used for type deduction. |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3831 | A = A.getUnqualifiedType(); |
| 3832 | } |
| 3833 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3834 | // Unevaluated SFINAE context. |
| 3835 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3836 | SFINAETrap Trap(*this); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3837 | |
| 3838 | // C++ [temp.deduct.conv]p1: |
| 3839 | // Template argument deduction is done by comparing the return |
| 3840 | // type of the template conversion function (call it P) with the |
| 3841 | // type that is required as the result of the conversion (call it |
| 3842 | // A) as described in 14.8.2.4. |
| 3843 | TemplateParameterList *TemplateParams |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3844 | = ConversionTemplate->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3845 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3846 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3847 | |
| 3848 | // C++0x [temp.deduct.conv]p4: |
| 3849 | // In general, the deduction process attempts to find template |
| 3850 | // argument values that will make the deduced A identical to |
| 3851 | // A. However, there are two cases that allow a difference: |
| 3852 | unsigned TDF = 0; |
| 3853 | // - If the original A is a reference type, A can be more |
| 3854 | // cv-qualified than the deduced A (i.e., the type referred to |
| 3855 | // by the reference) |
| 3856 | if (ToType->isReferenceType()) |
| 3857 | TDF |= TDF_ParamWithReferenceType; |
| 3858 | // - The deduced A can be another pointer or pointer to member |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3859 | // type that can be converted to A via a qualification |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3860 | // conversion. |
| 3861 | // |
| 3862 | // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when |
| 3863 | // both P and A are pointers or member pointers. In this case, we |
| 3864 | // just ignore cv-qualifiers completely). |
| 3865 | if ((P->isPointerType() && A->isPointerType()) || |
Douglas Gregor | 9f05ed5 | 2011-08-30 00:37:54 +0000 | [diff] [blame] | 3866 | (P->isMemberPointerType() && A->isMemberPointerType())) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3867 | TDF |= TDF_IgnoreQualifiers; |
| 3868 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3869 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3870 | P, A, Info, Deduced, TDF)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3871 | return Result; |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3872 | |
| 3873 | // Create an Instantiation Scope for finalizing the operator. |
| 3874 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3875 | // Finish template argument deduction. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3876 | FunctionDecl *ConversionSpecialized = nullptr; |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3877 | TemplateDeductionResult Result |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3878 | = FinishTemplateArgumentDeduction(ConversionTemplate, Deduced, 0, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3879 | ConversionSpecialized, Info); |
| 3880 | Specialization = cast_or_null<CXXConversionDecl>(ConversionSpecialized); |
| 3881 | |
| 3882 | // 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] | 3883 | // to a ptr-to-function, use the deduced arguments from the conversion |
| 3884 | // function to specialize the corresponding call operator. |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3885 | // e.g., int (*fp)(int) = [](auto a) { return a; }; |
| 3886 | if (Result == TDK_Success && isLambdaConversionOperator(ConversionGeneric)) { |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3887 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3888 | // 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] | 3889 | // to. This is necessary for matching the lambda call operator's return |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3890 | // type to that of the destination ptr-to-function's return type. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3891 | assert(A->isPointerType() && |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3892 | "Can only convert from lambda to ptr-to-function"); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3893 | const FunctionType *ToFunType = |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3894 | A->getPointeeType().getTypePtr()->getAs<FunctionType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3895 | const QualType DestFunctionPtrReturnType = ToFunType->getReturnType(); |
| 3896 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3897 | // Create the corresponding specializations of the call operator and |
| 3898 | // the static-invoker; and if the return type is auto, |
| 3899 | // deduce the return type and check if it matches the |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3900 | // DestFunctionPtrReturnType. |
| 3901 | // For instance: |
| 3902 | // auto L = [](auto a) { return f(a); }; |
| 3903 | // int (*fp)(int) = L; |
| 3904 | // char (*fp2)(int) = L; <-- Not OK. |
| 3905 | |
| 3906 | Result = SpecializeCorrespondingLambdaCallOperatorAndInvoker( |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3907 | Specialization, Deduced, DestFunctionPtrReturnType, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3908 | Info, *this); |
| 3909 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3910 | return Result; |
| 3911 | } |
| 3912 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3913 | /// \brief Deduce template arguments for a function template when there is |
| 3914 | /// nothing to deduce against (C++0x [temp.arg.explicit]p3). |
| 3915 | /// |
| 3916 | /// \param FunctionTemplate the function template for which we are performing |
| 3917 | /// template argument deduction. |
| 3918 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3919 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3920 | /// arguments. |
| 3921 | /// |
| 3922 | /// \param Specialization if template argument deduction was successful, |
| 3923 | /// this will be set to the function template specialization produced by |
| 3924 | /// template argument deduction. |
| 3925 | /// |
| 3926 | /// \param Info the argument will be updated to provide additional information |
| 3927 | /// about template argument deduction. |
| 3928 | /// |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3929 | /// \param IsAddressOfFunction If \c true, we are deducing as part of taking |
| 3930 | /// the address of a function template in a context where we do not have a |
| 3931 | /// target type, per [over.over]. If \c false, we are looking up a function |
| 3932 | /// template specialization based on its signature, which only happens when |
| 3933 | /// deducing a function parameter type from an argument that is a template-id |
| 3934 | /// naming a function template specialization. |
| 3935 | /// |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3936 | /// \returns the result of template argument deduction. |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3937 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3938 | FunctionTemplateDecl *FunctionTemplate, |
| 3939 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 3940 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 3941 | bool IsAddressOfFunction) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3942 | return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3943 | QualType(), Specialization, Info, |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3944 | IsAddressOfFunction); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3945 | } |
| 3946 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3947 | namespace { |
| 3948 | /// Substitute the 'auto' type specifier within a type for a given replacement |
| 3949 | /// type. |
| 3950 | class SubstituteAutoTransform : |
| 3951 | public TreeTransform<SubstituteAutoTransform> { |
| 3952 | QualType Replacement; |
| 3953 | public: |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3954 | SubstituteAutoTransform(Sema &SemaRef, QualType Replacement) |
| 3955 | : TreeTransform<SubstituteAutoTransform>(SemaRef), |
| 3956 | Replacement(Replacement) {} |
| 3957 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3958 | QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) { |
| 3959 | // If we're building the type pattern to deduce against, don't wrap the |
| 3960 | // substituted type in an AutoType. Certain template deduction rules |
| 3961 | // apply only when a template type parameter appears directly (and not if |
| 3962 | // the parameter is found through desugaring). For instance: |
| 3963 | // auto &&lref = lvalue; |
| 3964 | // must transform into "rvalue reference to T" not "rvalue reference to |
| 3965 | // 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] | 3966 | if (!Replacement.isNull() && isa<TemplateTypeParmType>(Replacement)) { |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3967 | QualType Result = Replacement; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 3968 | TemplateTypeParmTypeLoc NewTL = |
| 3969 | TLB.push<TemplateTypeParmTypeLoc>(Result); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3970 | NewTL.setNameLoc(TL.getNameLoc()); |
| 3971 | return Result; |
| 3972 | } else { |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 3973 | bool Dependent = |
| 3974 | !Replacement.isNull() && Replacement->isDependentType(); |
| 3975 | QualType Result = |
| 3976 | SemaRef.Context.getAutoType(Dependent ? QualType() : Replacement, |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 3977 | TL.getTypePtr()->getKeyword(), |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3978 | Dependent); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3979 | AutoTypeLoc NewTL = TLB.push<AutoTypeLoc>(Result); |
| 3980 | NewTL.setNameLoc(TL.getNameLoc()); |
| 3981 | return Result; |
| 3982 | } |
| 3983 | } |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 3984 | |
| 3985 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 3986 | // Lambdas never need to be transformed. |
| 3987 | return E; |
| 3988 | } |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3989 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3990 | QualType Apply(TypeLoc TL) { |
| 3991 | // Create some scratch storage for the transformed type locations. |
| 3992 | // FIXME: We're just going to throw this information away. Don't build it. |
| 3993 | TypeLocBuilder TLB; |
| 3994 | TLB.reserve(TL.getFullDataSize()); |
| 3995 | return TransformType(TLB, TL); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3996 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3997 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3998 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3999 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4000 | Sema::DeduceAutoResult |
| 4001 | Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *&Init, QualType &Result) { |
| 4002 | return DeduceAutoType(Type->getTypeLoc(), Init, Result); |
| 4003 | } |
| 4004 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4005 | /// \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] | 4006 | /// |
| 4007 | /// \param Type the type pattern using the auto type-specifier. |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4008 | /// \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] | 4009 | /// \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] | 4010 | /// deduced type. |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4011 | Sema::DeduceAutoResult |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4012 | Sema::DeduceAutoType(TypeLoc Type, Expr *&Init, QualType &Result) { |
John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4013 | if (Init->getType()->isNonOverloadPlaceholderType()) { |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4014 | ExprResult NonPlaceholder = CheckPlaceholderExpr(Init); |
| 4015 | if (NonPlaceholder.isInvalid()) |
| 4016 | return DAR_FailedAlreadyDiagnosed; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4017 | Init = NonPlaceholder.get(); |
John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4018 | } |
| 4019 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4020 | if (Init->isTypeDependent() || Type.getType()->isDependentType()) { |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4021 | Result = SubstituteAutoTransform(*this, Context.DependentTy).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4022 | assert(!Result.isNull() && "substituting DependentTy can't fail"); |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4023 | return DAR_Succeeded; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4024 | } |
| 4025 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4026 | // If this is a 'decltype(auto)' specifier, do the decltype dance. |
| 4027 | // Since 'decltype(auto)' can only occur at the top of the type, we |
| 4028 | // don't need to go digging for it. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4029 | if (const AutoType *AT = Type.getType()->getAs<AutoType>()) { |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4030 | if (AT->isDecltypeAuto()) { |
| 4031 | if (isa<InitListExpr>(Init)) { |
| 4032 | Diag(Init->getLocStart(), diag::err_decltype_auto_initializer_list); |
| 4033 | return DAR_FailedAlreadyDiagnosed; |
| 4034 | } |
| 4035 | |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 4036 | QualType Deduced = BuildDecltypeType(Init, Init->getLocStart(), false); |
David Majnemer | 3c20ab2 | 2015-07-01 00:29:28 +0000 | [diff] [blame] | 4037 | if (Deduced.isNull()) |
| 4038 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4039 | // FIXME: Support a non-canonical deduced type for 'auto'. |
| 4040 | Deduced = Context.getCanonicalType(Deduced); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4041 | Result = SubstituteAutoTransform(*this, Deduced).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4042 | if (Result.isNull()) |
| 4043 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4044 | return DAR_Succeeded; |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4045 | } else if (!getLangOpts().CPlusPlus) { |
| 4046 | if (isa<InitListExpr>(Init)) { |
| 4047 | Diag(Init->getLocStart(), diag::err_auto_init_list_from_c); |
| 4048 | return DAR_FailedAlreadyDiagnosed; |
| 4049 | } |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4050 | } |
| 4051 | } |
| 4052 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4053 | SourceLocation Loc = Init->getExprLoc(); |
| 4054 | |
| 4055 | LocalInstantiationScope InstScope(*this); |
| 4056 | |
| 4057 | // Build template<class TemplParam> void Func(FuncParam); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4058 | TemplateTypeParmDecl *TemplParam = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4059 | TemplateTypeParmDecl::Create(Context, nullptr, SourceLocation(), Loc, 0, 0, |
| 4060 | nullptr, false, false); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4061 | QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0); |
| 4062 | NamedDecl *TemplParamPtr = TemplParam; |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 4063 | FixedSizeTemplateParameterListStorage<1, false> TemplateParamsSt( |
| 4064 | Loc, Loc, TemplParamPtr, Loc, nullptr); |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 4065 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4066 | QualType FuncParam = SubstituteAutoTransform(*this, TemplArg).Apply(Type); |
| 4067 | assert(!FuncParam.isNull() && |
| 4068 | "substituting template parameter for 'auto' failed"); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4069 | |
| 4070 | // Deduce type of TemplParam in Func(Init) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4071 | SmallVector<DeducedTemplateArgument, 1> Deduced; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4072 | Deduced.resize(1); |
| 4073 | QualType InitType = Init->getType(); |
| 4074 | unsigned TDF = 0; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4075 | |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4076 | TemplateDeductionInfo Info(Loc); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4077 | |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 4078 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4079 | if (InitList) { |
| 4080 | for (unsigned i = 0, e = InitList->getNumInits(); i < e; ++i) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4081 | if (DeduceTemplateArgumentByListElement(*this, TemplateParamsSt.get(), |
| 4082 | TemplArg, InitList->getInit(i), |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 4083 | Info, Deduced, TDF)) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4084 | return DAR_Failed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4085 | } |
| 4086 | } else { |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4087 | if (!getLangOpts().CPlusPlus && Init->refersToBitField()) { |
| 4088 | Diag(Loc, diag::err_auto_bitfield); |
| 4089 | return DAR_FailedAlreadyDiagnosed; |
| 4090 | } |
| 4091 | |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4092 | if (AdjustFunctionParmAndArgTypesForDeduction( |
| 4093 | *this, TemplateParamsSt.get(), FuncParam, InitType, Init, TDF)) |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 4094 | return DAR_Failed; |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 4095 | |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4096 | if (DeduceTemplateArgumentsByTypeMatch(*this, TemplateParamsSt.get(), |
| 4097 | FuncParam, InitType, Info, Deduced, |
| 4098 | TDF)) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4099 | return DAR_Failed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4100 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4101 | |
Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4102 | if (Deduced[0].getKind() != TemplateArgument::Type) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4103 | return DAR_Failed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4104 | |
Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4105 | QualType DeducedType = Deduced[0].getAsType(); |
| 4106 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4107 | if (InitList) { |
| 4108 | DeducedType = BuildStdInitializerList(DeducedType, Loc); |
| 4109 | if (DeducedType.isNull()) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4110 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4111 | } |
| 4112 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4113 | Result = SubstituteAutoTransform(*this, DeducedType).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4114 | if (Result.isNull()) |
| 4115 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4116 | |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4117 | // Check that the deduced argument type is compatible with the original |
| 4118 | // argument type per C++ [temp.deduct.call]p4. |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4119 | if (!InitList && !Result.isNull() && |
| 4120 | CheckOriginalCallArgDeduction(*this, |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4121 | Sema::OriginalCallArg(FuncParam,0,InitType), |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4122 | Result)) { |
| 4123 | Result = QualType(); |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4124 | return DAR_Failed; |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4125 | } |
| 4126 | |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4127 | return DAR_Succeeded; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4128 | } |
| 4129 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 4130 | QualType Sema::SubstAutoType(QualType TypeWithAuto, |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4131 | QualType TypeToReplaceAuto) { |
| 4132 | return SubstituteAutoTransform(*this, TypeToReplaceAuto). |
| 4133 | TransformType(TypeWithAuto); |
| 4134 | } |
| 4135 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 4136 | TypeSourceInfo* Sema::SubstAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto, |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4137 | QualType TypeToReplaceAuto) { |
| 4138 | return SubstituteAutoTransform(*this, TypeToReplaceAuto). |
| 4139 | TransformType(TypeWithAuto); |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 4140 | } |
| 4141 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4142 | void Sema::DiagnoseAutoDeductionFailure(VarDecl *VDecl, Expr *Init) { |
| 4143 | if (isa<InitListExpr>(Init)) |
| 4144 | Diag(VDecl->getLocation(), |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4145 | VDecl->isInitCapture() |
| 4146 | ? diag::err_init_capture_deduction_failure_from_init_list |
| 4147 | : diag::err_auto_var_deduction_failure_from_init_list) |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4148 | << VDecl->getDeclName() << VDecl->getType() << Init->getSourceRange(); |
| 4149 | else |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4150 | Diag(VDecl->getLocation(), |
| 4151 | VDecl->isInitCapture() ? diag::err_init_capture_deduction_failure |
| 4152 | : diag::err_auto_var_deduction_failure) |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4153 | << VDecl->getDeclName() << VDecl->getType() << Init->getType() |
| 4154 | << Init->getSourceRange(); |
| 4155 | } |
| 4156 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4157 | bool Sema::DeduceReturnType(FunctionDecl *FD, SourceLocation Loc, |
| 4158 | bool Diagnose) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4159 | assert(FD->getReturnType()->isUndeducedType()); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4160 | |
| 4161 | if (FD->getTemplateInstantiationPattern()) |
| 4162 | InstantiateFunctionDefinition(Loc, FD); |
| 4163 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4164 | bool StillUndeduced = FD->getReturnType()->isUndeducedType(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4165 | if (StillUndeduced && Diagnose && !FD->isInvalidDecl()) { |
| 4166 | Diag(Loc, diag::err_auto_fn_used_before_defined) << FD; |
| 4167 | Diag(FD->getLocation(), diag::note_callee_decl) << FD; |
| 4168 | } |
| 4169 | |
| 4170 | return StillUndeduced; |
| 4171 | } |
| 4172 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4173 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4174 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4175 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4176 | unsigned Level, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4177 | llvm::SmallBitVector &Deduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4178 | |
| 4179 | /// \brief If this is a non-static member function, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 4180 | static void |
| 4181 | AddImplicitObjectParameterType(ASTContext &Context, |
| 4182 | CXXMethodDecl *Method, |
| 4183 | SmallVectorImpl<QualType> &ArgTypes) { |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4184 | // C++11 [temp.func.order]p3: |
| 4185 | // [...] The new parameter is of type "reference to cv A," where cv are |
| 4186 | // the cv-qualifiers of the function template (if any) and A is |
| 4187 | // the class of which the function template is a member. |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4188 | // |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4189 | // The standard doesn't say explicitly, but we pick the appropriate kind of |
| 4190 | // reference type based on [over.match.funcs]p4. |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4191 | QualType ArgTy = Context.getTypeDeclType(Method->getParent()); |
| 4192 | ArgTy = Context.getQualifiedType(ArgTy, |
| 4193 | Qualifiers::fromCVRMask(Method->getTypeQualifiers())); |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4194 | if (Method->getRefQualifier() == RQ_RValue) |
| 4195 | ArgTy = Context.getRValueReferenceType(ArgTy); |
| 4196 | else |
| 4197 | ArgTy = Context.getLValueReferenceType(ArgTy); |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4198 | ArgTypes.push_back(ArgTy); |
| 4199 | } |
| 4200 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4201 | /// \brief Determine whether the function template \p FT1 is at least as |
| 4202 | /// specialized as \p FT2. |
| 4203 | static bool isAtLeastAsSpecializedAs(Sema &S, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4204 | SourceLocation Loc, |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4205 | FunctionTemplateDecl *FT1, |
| 4206 | FunctionTemplateDecl *FT2, |
| 4207 | TemplatePartialOrderingContext TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4208 | unsigned NumCallArguments1) { |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4209 | FunctionDecl *FD1 = FT1->getTemplatedDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4210 | FunctionDecl *FD2 = FT2->getTemplatedDecl(); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4211 | const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); |
| 4212 | const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4213 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4214 | assert(Proto1 && Proto2 && "Function templates must have prototypes"); |
| 4215 | TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4216 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4217 | Deduced.resize(TemplateParams->size()); |
| 4218 | |
| 4219 | // C++0x [temp.deduct.partial]p3: |
| 4220 | // The types used to determine the ordering depend on the context in which |
| 4221 | // the partial ordering is done: |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4222 | TemplateDeductionInfo Info(Loc); |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4223 | SmallVector<QualType, 4> Args2; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4224 | switch (TPOC) { |
| 4225 | case TPOC_Call: { |
| 4226 | // - In the context of a function call, the function parameter types are |
| 4227 | // used. |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4228 | CXXMethodDecl *Method1 = dyn_cast<CXXMethodDecl>(FD1); |
| 4229 | CXXMethodDecl *Method2 = dyn_cast<CXXMethodDecl>(FD2); |
Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4230 | |
Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4231 | // C++11 [temp.func.order]p3: |
Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4232 | // [...] If only one of the function templates is a non-static |
| 4233 | // member, that function template is considered to have a new |
| 4234 | // first parameter inserted in its function parameter list. The |
| 4235 | // new parameter is of type "reference to cv A," where cv are |
| 4236 | // the cv-qualifiers of the function template (if any) and A is |
| 4237 | // the class of which the function template is a member. |
| 4238 | // |
Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4239 | // Note that we interpret this to mean "if one of the function |
| 4240 | // templates is a non-static member and the other is a non-member"; |
| 4241 | // otherwise, the ordering rules for static functions against non-static |
| 4242 | // functions don't make any sense. |
| 4243 | // |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4244 | // C++98/03 doesn't have this provision but we've extended DR532 to cover |
| 4245 | // it as wording was broken prior to it. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4246 | SmallVector<QualType, 4> Args1; |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4247 | |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4248 | unsigned NumComparedArguments = NumCallArguments1; |
| 4249 | |
| 4250 | if (!Method2 && Method1 && !Method1->isStatic()) { |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4251 | // Compare 'this' from Method1 against first parameter from Method2. |
| 4252 | AddImplicitObjectParameterType(S.Context, Method1, Args1); |
| 4253 | ++NumComparedArguments; |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4254 | } else if (!Method1 && Method2 && !Method2->isStatic()) { |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4255 | // Compare 'this' from Method2 against first parameter from Method1. |
| 4256 | AddImplicitObjectParameterType(S.Context, Method2, Args2); |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4257 | } |
| 4258 | |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4259 | Args1.insert(Args1.end(), Proto1->param_type_begin(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4260 | Proto1->param_type_end()); |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4261 | Args2.insert(Args2.end(), Proto2->param_type_begin(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4262 | Proto2->param_type_end()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4263 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4264 | // C++ [temp.func.order]p5: |
| 4265 | // The presence of unused ellipsis and default arguments has no effect on |
| 4266 | // the partial ordering of function templates. |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4267 | if (Args1.size() > NumComparedArguments) |
| 4268 | Args1.resize(NumComparedArguments); |
| 4269 | if (Args2.size() > NumComparedArguments) |
| 4270 | Args2.resize(NumComparedArguments); |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4271 | if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(), |
| 4272 | Args1.data(), Args1.size(), Info, Deduced, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4273 | TDF_None, /*PartialOrdering=*/true)) |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 4274 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4275 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4276 | break; |
| 4277 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4278 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4279 | case TPOC_Conversion: |
| 4280 | // - In the context of a call to a conversion operator, the return types |
| 4281 | // of the conversion function templates are used. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4282 | if (DeduceTemplateArgumentsByTypeMatch( |
| 4283 | S, TemplateParams, Proto2->getReturnType(), Proto1->getReturnType(), |
| 4284 | Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4285 | /*PartialOrdering=*/true)) |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4286 | return false; |
| 4287 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4288 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4289 | case TPOC_Other: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4290 | // - 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] | 4291 | // is used. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4292 | if (DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 4293 | FD2->getType(), FD1->getType(), |
| 4294 | Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4295 | /*PartialOrdering=*/true)) |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4296 | return false; |
| 4297 | break; |
| 4298 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4299 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4300 | // C++0x [temp.deduct.partial]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4301 | // In most cases, all template parameters must have values in order for |
| 4302 | // deduction to succeed, but for partial ordering purposes a template |
| 4303 | // 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] | 4304 | // types being used for partial ordering. [ Note: a template parameter used |
| 4305 | // in a non-deduced context is considered used. -end note] |
| 4306 | unsigned ArgIdx = 0, NumArgs = Deduced.size(); |
| 4307 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4308 | if (Deduced[ArgIdx].isNull()) |
| 4309 | break; |
| 4310 | |
| 4311 | if (ArgIdx == NumArgs) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4312 | // All template arguments were deduced. FT1 is at least as specialized |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4313 | // as FT2. |
| 4314 | return true; |
| 4315 | } |
| 4316 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4317 | // Figure out which template parameters were used. |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4318 | llvm::SmallBitVector UsedParameters(TemplateParams->size()); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4319 | switch (TPOC) { |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4320 | case TPOC_Call: |
| 4321 | for (unsigned I = 0, N = Args2.size(); I != N; ++I) |
| 4322 | ::MarkUsedTemplateParameters(S.Context, Args2[I], false, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4323 | TemplateParams->getDepth(), |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4324 | UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4325 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4326 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4327 | case TPOC_Conversion: |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4328 | ::MarkUsedTemplateParameters(S.Context, Proto2->getReturnType(), false, |
| 4329 | TemplateParams->getDepth(), UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4330 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4331 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4332 | case TPOC_Other: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4333 | ::MarkUsedTemplateParameters(S.Context, FD2->getType(), false, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4334 | TemplateParams->getDepth(), |
| 4335 | UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4336 | break; |
| 4337 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4338 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4339 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4340 | // If this argument had no value deduced but was used in one of the types |
| 4341 | // used for partial ordering, then deduction fails. |
| 4342 | if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) |
| 4343 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4344 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4345 | return true; |
| 4346 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4347 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4348 | /// \brief Determine whether this a function template whose parameter-type-list |
| 4349 | /// ends with a function parameter pack. |
| 4350 | static bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) { |
| 4351 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
| 4352 | unsigned NumParams = Function->getNumParams(); |
| 4353 | if (NumParams == 0) |
| 4354 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4355 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4356 | ParmVarDecl *Last = Function->getParamDecl(NumParams - 1); |
| 4357 | if (!Last->isParameterPack()) |
| 4358 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4359 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4360 | // Make sure that no previous parameter is a parameter pack. |
| 4361 | while (--NumParams > 0) { |
| 4362 | if (Function->getParamDecl(NumParams - 1)->isParameterPack()) |
| 4363 | return false; |
| 4364 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4365 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4366 | return true; |
| 4367 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4368 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4369 | /// \brief Returns the more specialized function template according |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4370 | /// to the rules of function template partial ordering (C++ [temp.func.order]). |
| 4371 | /// |
| 4372 | /// \param FT1 the first function template |
| 4373 | /// |
| 4374 | /// \param FT2 the second function template |
| 4375 | /// |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4376 | /// \param TPOC the context in which we are performing partial ordering of |
| 4377 | /// function templates. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4378 | /// |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4379 | /// \param NumCallArguments1 The number of arguments in the call to FT1, used |
| 4380 | /// only when \c TPOC is \c TPOC_Call. |
| 4381 | /// |
| 4382 | /// \param NumCallArguments2 The number of arguments in the call to FT2, used |
| 4383 | /// only when \c TPOC is \c TPOC_Call. |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4384 | /// |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4385 | /// \returns the more specialized function template. If neither |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4386 | /// template is more specialized, returns NULL. |
| 4387 | FunctionTemplateDecl * |
| 4388 | Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, |
| 4389 | FunctionTemplateDecl *FT2, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4390 | SourceLocation Loc, |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4391 | TemplatePartialOrderingContext TPOC, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4392 | unsigned NumCallArguments1, |
| 4393 | unsigned NumCallArguments2) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4394 | bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4395 | NumCallArguments1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4396 | bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4397 | NumCallArguments2); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4398 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4399 | if (Better1 != Better2) // We have a clear winner |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4400 | return Better1 ? FT1 : FT2; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4401 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4402 | if (!Better1 && !Better2) // Neither is better than the other |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4403 | return nullptr; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4404 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4405 | // FIXME: This mimics what GCC implements, but doesn't match up with the |
| 4406 | // proposed resolution for core issue 692. This area needs to be sorted out, |
| 4407 | // but for now we attempt to maintain compatibility. |
| 4408 | bool Variadic1 = isVariadicFunctionTemplate(FT1); |
| 4409 | bool Variadic2 = isVariadicFunctionTemplate(FT2); |
| 4410 | if (Variadic1 != Variadic2) |
| 4411 | return Variadic1? FT2 : FT1; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4412 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4413 | return nullptr; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4414 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4415 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4416 | /// \brief Determine if the two templates are equivalent. |
| 4417 | static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) { |
| 4418 | if (T1 == T2) |
| 4419 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4420 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4421 | if (!T1 || !T2) |
| 4422 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4423 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4424 | return T1->getCanonicalDecl() == T2->getCanonicalDecl(); |
| 4425 | } |
| 4426 | |
| 4427 | /// \brief Retrieve the most specialized of the given function template |
| 4428 | /// specializations. |
| 4429 | /// |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4430 | /// \param SpecBegin the start iterator of the function template |
| 4431 | /// specializations that we will be comparing. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4432 | /// |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4433 | /// \param SpecEnd the end iterator of the function template |
| 4434 | /// specializations, paired with \p SpecBegin. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4435 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4436 | /// \param Loc the location where the ambiguity or no-specializations |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4437 | /// diagnostic should occur. |
| 4438 | /// |
| 4439 | /// \param NoneDiag partial diagnostic used to diagnose cases where there are |
| 4440 | /// no matching candidates. |
| 4441 | /// |
| 4442 | /// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one |
| 4443 | /// occurs. |
| 4444 | /// |
| 4445 | /// \param CandidateDiag partial diagnostic used for each function template |
| 4446 | /// specialization that is a candidate in the ambiguous ordering. One parameter |
| 4447 | /// in this diagnostic should be unbound, which will correspond to the string |
| 4448 | /// describing the template arguments for the function template specialization. |
| 4449 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4450 | /// \returns the most specialized function template specialization, if |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4451 | /// found. Otherwise, returns SpecEnd. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4452 | UnresolvedSetIterator Sema::getMostSpecialized( |
| 4453 | UnresolvedSetIterator SpecBegin, UnresolvedSetIterator SpecEnd, |
| 4454 | TemplateSpecCandidateSet &FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4455 | SourceLocation Loc, const PartialDiagnostic &NoneDiag, |
| 4456 | const PartialDiagnostic &AmbigDiag, const PartialDiagnostic &CandidateDiag, |
| 4457 | bool Complain, QualType TargetType) { |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4458 | if (SpecBegin == SpecEnd) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4459 | if (Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4460 | Diag(Loc, NoneDiag); |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4461 | FailedCandidates.NoteCandidates(*this, Loc); |
| 4462 | } |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4463 | return SpecEnd; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4464 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4465 | |
| 4466 | if (SpecBegin + 1 == SpecEnd) |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4467 | return SpecBegin; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4468 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4469 | // Find the function template that is better than all of the templates it |
| 4470 | // has been compared to. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4471 | UnresolvedSetIterator Best = SpecBegin; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4472 | FunctionTemplateDecl *BestTemplate |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4473 | = cast<FunctionDecl>(*Best)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4474 | assert(BestTemplate && "Not a function template specialization?"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4475 | for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) { |
| 4476 | FunctionTemplateDecl *Challenger |
| 4477 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4478 | assert(Challenger && "Not a function template specialization?"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4479 | if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4480 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4481 | Challenger)) { |
| 4482 | Best = I; |
| 4483 | BestTemplate = Challenger; |
| 4484 | } |
| 4485 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4486 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4487 | // Make sure that the "best" function template is more specialized than all |
| 4488 | // of the others. |
| 4489 | bool Ambiguous = false; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4490 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4491 | FunctionTemplateDecl *Challenger |
| 4492 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4493 | if (I != Best && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4494 | !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4495 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4496 | BestTemplate)) { |
| 4497 | Ambiguous = true; |
| 4498 | break; |
| 4499 | } |
| 4500 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4501 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4502 | if (!Ambiguous) { |
| 4503 | // We found an answer. Return it. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4504 | return Best; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4505 | } |
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 | // Diagnose the ambiguity. |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4508 | if (Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4509 | Diag(Loc, AmbigDiag); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4510 | |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4511 | // FIXME: Can we order the candidates in some sane way? |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4512 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4513 | PartialDiagnostic PD = CandidateDiag; |
Saleem Abdulrasool | 78704fb | 2016-12-22 04:26:57 +0000 | [diff] [blame] | 4514 | const auto *FD = cast<FunctionDecl>(*I); |
| 4515 | PD << FD << getTemplateArgumentBindingsText( |
| 4516 | FD->getPrimaryTemplate()->getTemplateParameters(), |
| 4517 | *FD->getTemplateSpecializationArgs()); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4518 | if (!TargetType.isNull()) |
Saleem Abdulrasool | 78704fb | 2016-12-22 04:26:57 +0000 | [diff] [blame] | 4519 | HandleFunctionTypeMismatch(PD, FD->getType(), TargetType); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4520 | Diag((*I)->getLocation(), PD); |
| 4521 | } |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4522 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4523 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4524 | return SpecEnd; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4525 | } |
| 4526 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4527 | /// Determine whether one partial specialization, P1, is at least as |
| 4528 | /// specialized than another, P2. |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4529 | /// |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4530 | /// \param PartialSpecializationDecl The kind of P2, which must be a |
| 4531 | /// {Class,Var}TemplatePartialSpecializationDecl. |
| 4532 | /// \param T1 The injected-class-name of P1 (faked for a variable template). |
| 4533 | /// \param T2 The injected-class-name of P2 (faked for a variable template). |
| 4534 | /// \param Loc The location at which the comparison is required. |
| 4535 | template<typename PartialSpecializationDecl> |
| 4536 | static bool isAtLeastAsSpecializedAs(Sema &S, QualType T1, QualType T2, |
| 4537 | PartialSpecializationDecl *P2, |
| 4538 | SourceLocation Loc) { |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4539 | // C++ [temp.class.order]p1: |
| 4540 | // For two class template partial specializations, the first is at least as |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4541 | // specialized as the second if, given the following rewrite to two |
| 4542 | // function templates, the first function template is at least as |
| 4543 | // specialized as the second according to the ordering rules for function |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4544 | // templates (14.6.6.2): |
| 4545 | // - the first function template has the same template parameters as the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4546 | // first partial specialization and has a single function parameter |
| 4547 | // whose type is a class template specialization with the template |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4548 | // arguments of the first partial specialization, and |
| 4549 | // - the second function template has the same template parameters as the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4550 | // second partial specialization and has a single function parameter |
| 4551 | // whose type is a class template specialization with the template |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4552 | // arguments of the second partial specialization. |
| 4553 | // |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 4554 | // Rather than synthesize function templates, we merely perform the |
| 4555 | // equivalent partial ordering by performing deduction directly on |
| 4556 | // the template arguments of the class template partial |
| 4557 | // specializations. This computation is slightly simpler than the |
| 4558 | // general problem of function template partial ordering, because |
| 4559 | // class template partial specializations are more constrained. We |
| 4560 | // know that every template parameter is deducible from the class |
| 4561 | // template partial specialization's template arguments, for |
| 4562 | // example. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4563 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4564 | TemplateDeductionInfo Info(Loc); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4565 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4566 | // Determine whether P1 is at least as specialized as P2. |
| 4567 | Deduced.resize(P2->getTemplateParameters()->size()); |
| 4568 | if (DeduceTemplateArgumentsByTypeMatch(S, P2->getTemplateParameters(), |
| 4569 | T2, T1, Info, Deduced, TDF_None, |
| 4570 | /*PartialOrdering=*/true)) |
| 4571 | return false; |
| 4572 | |
| 4573 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), |
| 4574 | Deduced.end()); |
| 4575 | Sema::InstantiatingTemplate Inst(S, Loc, P2, DeducedArgs, Info); |
| 4576 | auto *TST1 = T1->castAs<TemplateSpecializationType>(); |
| 4577 | if (FinishTemplateArgumentDeduction( |
| 4578 | S, P2, TemplateArgumentList(TemplateArgumentList::OnStack, |
| 4579 | TST1->template_arguments()), |
| 4580 | Deduced, Info)) |
| 4581 | return false; |
| 4582 | |
| 4583 | return true; |
| 4584 | } |
| 4585 | |
| 4586 | /// \brief Returns the more specialized class template partial specialization |
| 4587 | /// according to the rules of partial ordering of class template partial |
| 4588 | /// specializations (C++ [temp.class.order]). |
| 4589 | /// |
| 4590 | /// \param PS1 the first class template partial specialization |
| 4591 | /// |
| 4592 | /// \param PS2 the second class template partial specialization |
| 4593 | /// |
| 4594 | /// \returns the more specialized class template partial specialization. If |
| 4595 | /// neither partial specialization is more specialized, returns NULL. |
| 4596 | ClassTemplatePartialSpecializationDecl * |
| 4597 | Sema::getMoreSpecializedPartialSpecialization( |
| 4598 | ClassTemplatePartialSpecializationDecl *PS1, |
| 4599 | ClassTemplatePartialSpecializationDecl *PS2, |
| 4600 | SourceLocation Loc) { |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4601 | QualType PT1 = PS1->getInjectedSpecializationType(); |
| 4602 | QualType PT2 = PS2->getInjectedSpecializationType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4603 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4604 | bool Better1 = isAtLeastAsSpecializedAs(*this, PT1, PT2, PS2, Loc); |
| 4605 | bool Better2 = isAtLeastAsSpecializedAs(*this, PT2, PT1, PS1, Loc); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4606 | |
| 4607 | if (Better1 == Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4608 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4609 | |
| 4610 | return Better1 ? PS1 : PS2; |
| 4611 | } |
| 4612 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4613 | VarTemplatePartialSpecializationDecl * |
| 4614 | Sema::getMoreSpecializedPartialSpecialization( |
| 4615 | VarTemplatePartialSpecializationDecl *PS1, |
| 4616 | VarTemplatePartialSpecializationDecl *PS2, SourceLocation Loc) { |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4617 | // Pretend the variable template specializations are class template |
| 4618 | // specializations and form a fake injected class name type for comparison. |
Richard Smith | f04fd0b | 2013-12-12 23:14:16 +0000 | [diff] [blame] | 4619 | assert(PS1->getSpecializedTemplate() == PS2->getSpecializedTemplate() && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4620 | "the partial specializations being compared should specialize" |
| 4621 | " the same template."); |
| 4622 | TemplateName Name(PS1->getSpecializedTemplate()); |
| 4623 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4624 | QualType PT1 = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4625 | CanonTemplate, PS1->getTemplateArgs().asArray()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4626 | QualType PT2 = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4627 | CanonTemplate, PS2->getTemplateArgs().asArray()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4628 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4629 | bool Better1 = isAtLeastAsSpecializedAs(*this, PT1, PT2, PS2, Loc); |
| 4630 | bool Better2 = isAtLeastAsSpecializedAs(*this, PT2, PT1, PS1, Loc); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4631 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4632 | if (Better1 == Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4633 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4634 | |
Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4635 | return Better1 ? PS1 : PS2; |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4636 | } |
| 4637 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4638 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4639 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4640 | const TemplateArgument &TemplateArg, |
| 4641 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4642 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4643 | llvm::SmallBitVector &Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4644 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4645 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4646 | /// expression. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4647 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4648 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4649 | const Expr *E, |
| 4650 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4651 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4652 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 4653 | // We can deduce from a pack expansion. |
| 4654 | if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E)) |
| 4655 | E = Expansion->getPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4656 | |
Richard Smith | 3434900 | 2012-07-09 03:07:20 +0000 | [diff] [blame] | 4657 | // Skip through any implicit casts we added while type-checking, and any |
| 4658 | // substitutions performed by template alias expansion. |
| 4659 | while (1) { |
| 4660 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 4661 | E = ICE->getSubExpr(); |
| 4662 | else if (const SubstNonTypeTemplateParmExpr *Subst = |
| 4663 | dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 4664 | E = Subst->getReplacement(); |
| 4665 | else |
| 4666 | break; |
| 4667 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4668 | |
| 4669 | // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4670 | // find other occurrences of template parameters. |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4671 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
Douglas Gregor | 04b1152 | 2010-01-14 18:13:22 +0000 | [diff] [blame] | 4672 | if (!DRE) |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4673 | return; |
| 4674 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4675 | const NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4676 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 4677 | if (!NTTP) |
| 4678 | return; |
| 4679 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4680 | if (NTTP->getDepth() == Depth) |
| 4681 | Used[NTTP->getIndex()] = true; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 4682 | |
| 4683 | // In C++1z mode, additional arguments may be deduced from the type of a |
| 4684 | // non-type argument. |
| 4685 | if (Ctx.getLangOpts().CPlusPlus1z) |
| 4686 | MarkUsedTemplateParameters(Ctx, NTTP->getType(), OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4687 | } |
| 4688 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4689 | /// \brief Mark the template parameters that are used by the given |
| 4690 | /// nested name specifier. |
| 4691 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4692 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4693 | NestedNameSpecifier *NNS, |
| 4694 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4695 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4696 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4697 | if (!NNS) |
| 4698 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4699 | |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4700 | MarkUsedTemplateParameters(Ctx, NNS->getPrefix(), OnlyDeduced, Depth, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4701 | Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4702 | MarkUsedTemplateParameters(Ctx, QualType(NNS->getAsType(), 0), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4703 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4704 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4705 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4706 | /// \brief Mark the template parameters that are used by the given |
| 4707 | /// template name. |
| 4708 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4709 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4710 | TemplateName Name, |
| 4711 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4712 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4713 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4714 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 4715 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4716 | = dyn_cast<TemplateTemplateParmDecl>(Template)) { |
| 4717 | if (TTP->getDepth() == Depth) |
| 4718 | Used[TTP->getIndex()] = true; |
| 4719 | } |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4720 | return; |
| 4721 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4722 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4723 | if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4724 | MarkUsedTemplateParameters(Ctx, QTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4725 | Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4726 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4727 | MarkUsedTemplateParameters(Ctx, DTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4728 | Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4729 | } |
| 4730 | |
| 4731 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4732 | /// type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4733 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4734 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4735 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4736 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4737 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4738 | if (T.isNull()) |
| 4739 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4740 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4741 | // Non-dependent types have nothing deducible |
| 4742 | if (!T->isDependentType()) |
| 4743 | return; |
| 4744 | |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4745 | T = Ctx.getCanonicalType(T); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4746 | switch (T->getTypeClass()) { |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4747 | case Type::Pointer: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4748 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4749 | cast<PointerType>(T)->getPointeeType(), |
| 4750 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4751 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4752 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4753 | break; |
| 4754 | |
| 4755 | case Type::BlockPointer: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4756 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4757 | cast<BlockPointerType>(T)->getPointeeType(), |
| 4758 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4759 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4760 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4761 | break; |
| 4762 | |
| 4763 | case Type::LValueReference: |
| 4764 | case Type::RValueReference: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4765 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4766 | cast<ReferenceType>(T)->getPointeeType(), |
| 4767 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4768 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4769 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4770 | break; |
| 4771 | |
| 4772 | case Type::MemberPointer: { |
| 4773 | const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4774 | MarkUsedTemplateParameters(Ctx, MemPtr->getPointeeType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4775 | Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4776 | MarkUsedTemplateParameters(Ctx, QualType(MemPtr->getClass(), 0), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4777 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4778 | break; |
| 4779 | } |
| 4780 | |
| 4781 | case Type::DependentSizedArray: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4782 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4783 | cast<DependentSizedArrayType>(T)->getSizeExpr(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4784 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4785 | // Fall through to check the element type |
| 4786 | |
| 4787 | case Type::ConstantArray: |
| 4788 | case Type::IncompleteArray: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4789 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4790 | cast<ArrayType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4791 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4792 | break; |
| 4793 | |
| 4794 | case Type::Vector: |
| 4795 | case Type::ExtVector: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4796 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4797 | cast<VectorType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4798 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4799 | break; |
| 4800 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4801 | case Type::DependentSizedExtVector: { |
| 4802 | const DependentSizedExtVectorType *VecType |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4803 | = cast<DependentSizedExtVectorType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4804 | MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4805 | Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4806 | MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4807 | Depth, Used); |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4808 | break; |
| 4809 | } |
| 4810 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4811 | case Type::FunctionProto: { |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4812 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4813 | MarkUsedTemplateParameters(Ctx, Proto->getReturnType(), OnlyDeduced, Depth, |
| 4814 | Used); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4815 | for (unsigned I = 0, N = Proto->getNumParams(); I != N; ++I) |
| 4816 | MarkUsedTemplateParameters(Ctx, Proto->getParamType(I), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4817 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4818 | break; |
| 4819 | } |
| 4820 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4821 | case Type::TemplateTypeParm: { |
| 4822 | const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T); |
| 4823 | if (TTP->getDepth() == Depth) |
| 4824 | Used[TTP->getIndex()] = true; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4825 | break; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4826 | } |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4827 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4828 | case Type::SubstTemplateTypeParmPack: { |
| 4829 | const SubstTemplateTypeParmPackType *Subst |
| 4830 | = cast<SubstTemplateTypeParmPackType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4831 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4832 | QualType(Subst->getReplacedParameter(), 0), |
| 4833 | OnlyDeduced, Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4834 | MarkUsedTemplateParameters(Ctx, Subst->getArgumentPack(), |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4835 | OnlyDeduced, Depth, Used); |
| 4836 | break; |
| 4837 | } |
| 4838 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4839 | case Type::InjectedClassName: |
| 4840 | T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); |
| 4841 | // fall through |
| 4842 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4843 | case Type::TemplateSpecialization: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4844 | const TemplateSpecializationType *Spec |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4845 | = cast<TemplateSpecializationType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4846 | MarkUsedTemplateParameters(Ctx, Spec->getTemplateName(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4847 | Depth, Used); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4848 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4849 | // C++0x [temp.deduct.type]p9: |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 4850 | // If the template argument list of P contains a pack expansion that is |
| 4851 | // not the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4852 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4853 | if (OnlyDeduced && |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 4854 | hasPackExpansionBeforeEnd(Spec->template_arguments())) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4855 | break; |
| 4856 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4857 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4858 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4859 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4860 | break; |
| 4861 | } |
| 4862 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4863 | case Type::Complex: |
| 4864 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4865 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4866 | cast<ComplexType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4867 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4868 | break; |
| 4869 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4870 | case Type::Atomic: |
| 4871 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4872 | MarkUsedTemplateParameters(Ctx, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4873 | cast<AtomicType>(T)->getValueType(), |
| 4874 | OnlyDeduced, Depth, Used); |
| 4875 | break; |
| 4876 | |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4877 | case Type::DependentName: |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4878 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4879 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4880 | cast<DependentNameType>(T)->getQualifier(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4881 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4882 | break; |
| 4883 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4884 | case Type::DependentTemplateSpecialization: { |
Richard Smith | 50d5b97 | 2015-12-30 20:56:05 +0000 | [diff] [blame] | 4885 | // C++14 [temp.deduct.type]p5: |
| 4886 | // The non-deduced contexts are: |
| 4887 | // -- The nested-name-specifier of a type that was specified using a |
| 4888 | // qualified-id |
| 4889 | // |
| 4890 | // C++14 [temp.deduct.type]p6: |
| 4891 | // When a type name is specified in a way that includes a non-deduced |
| 4892 | // context, all of the types that comprise that type name are also |
| 4893 | // non-deduced. |
| 4894 | if (OnlyDeduced) |
| 4895 | break; |
| 4896 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4897 | const DependentTemplateSpecializationType *Spec |
| 4898 | = cast<DependentTemplateSpecializationType>(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4899 | |
Richard Smith | 50d5b97 | 2015-12-30 20:56:05 +0000 | [diff] [blame] | 4900 | MarkUsedTemplateParameters(Ctx, Spec->getQualifier(), |
| 4901 | OnlyDeduced, Depth, Used); |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4902 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4903 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4904 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4905 | Used); |
| 4906 | break; |
| 4907 | } |
| 4908 | |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4909 | case Type::TypeOf: |
| 4910 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4911 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4912 | cast<TypeOfType>(T)->getUnderlyingType(), |
| 4913 | OnlyDeduced, Depth, Used); |
| 4914 | break; |
| 4915 | |
| 4916 | case Type::TypeOfExpr: |
| 4917 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4918 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4919 | cast<TypeOfExprType>(T)->getUnderlyingExpr(), |
| 4920 | OnlyDeduced, Depth, Used); |
| 4921 | break; |
| 4922 | |
| 4923 | case Type::Decltype: |
| 4924 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4925 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4926 | cast<DecltypeType>(T)->getUnderlyingExpr(), |
| 4927 | OnlyDeduced, Depth, Used); |
| 4928 | break; |
| 4929 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4930 | case Type::UnaryTransform: |
| 4931 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4932 | MarkUsedTemplateParameters(Ctx, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 4933 | cast<UnaryTransformType>(T)->getUnderlyingType(), |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4934 | OnlyDeduced, Depth, Used); |
| 4935 | break; |
| 4936 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4937 | case Type::PackExpansion: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4938 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4939 | cast<PackExpansionType>(T)->getPattern(), |
| 4940 | OnlyDeduced, Depth, Used); |
| 4941 | break; |
| 4942 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4943 | case Type::Auto: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4944 | MarkUsedTemplateParameters(Ctx, |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4945 | cast<AutoType>(T)->getDeducedType(), |
| 4946 | OnlyDeduced, Depth, Used); |
| 4947 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4948 | // None of these types have any template parameters in them. |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4949 | case Type::Builtin: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4950 | case Type::VariableArray: |
| 4951 | case Type::FunctionNoProto: |
| 4952 | case Type::Record: |
| 4953 | case Type::Enum: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4954 | case Type::ObjCInterface: |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4955 | case Type::ObjCObject: |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 4956 | case Type::ObjCObjectPointer: |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4957 | case Type::UnresolvedUsing: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 4958 | case Type::Pipe: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4959 | #define TYPE(Class, Base) |
| 4960 | #define ABSTRACT_TYPE(Class, Base) |
| 4961 | #define DEPENDENT_TYPE(Class, Base) |
| 4962 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 4963 | #include "clang/AST/TypeNodes.def" |
| 4964 | break; |
| 4965 | } |
| 4966 | } |
| 4967 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4968 | /// \brief Mark the template parameters that are used by this |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4969 | /// template argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4970 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4971 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4972 | const TemplateArgument &TemplateArg, |
| 4973 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4974 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4975 | llvm::SmallBitVector &Used) { |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4976 | switch (TemplateArg.getKind()) { |
| 4977 | case TemplateArgument::Null: |
| 4978 | case TemplateArgument::Integral: |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4979 | case TemplateArgument::Declaration: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4980 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4981 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4982 | case TemplateArgument::NullPtr: |
| 4983 | MarkUsedTemplateParameters(Ctx, TemplateArg.getNullPtrType(), OnlyDeduced, |
| 4984 | Depth, Used); |
| 4985 | break; |
| 4986 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4987 | case TemplateArgument::Type: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4988 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4989 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4990 | break; |
| 4991 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4992 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4993 | case TemplateArgument::TemplateExpansion: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4994 | MarkUsedTemplateParameters(Ctx, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4995 | TemplateArg.getAsTemplateOrTemplatePattern(), |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4996 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4997 | break; |
| 4998 | |
| 4999 | case TemplateArgument::Expression: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5000 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsExpr(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5001 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5002 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5003 | |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5004 | case TemplateArgument::Pack: |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 5005 | for (const auto &P : TemplateArg.pack_elements()) |
| 5006 | MarkUsedTemplateParameters(Ctx, P, OnlyDeduced, Depth, Used); |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5007 | break; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5008 | } |
| 5009 | } |
| 5010 | |
James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 5011 | /// \brief Mark which template parameters can be deduced from a given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5012 | /// template argument list. |
| 5013 | /// |
| 5014 | /// \param TemplateArgs the template argument list from which template |
| 5015 | /// parameters will be deduced. |
| 5016 | /// |
James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 5017 | /// \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] | 5018 | /// to indicate when the corresponding template parameter will be |
| 5019 | /// deduced. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5020 | void |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5021 | Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5022 | bool OnlyDeduced, unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5023 | llvm::SmallBitVector &Used) { |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5024 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5025 | // If the template argument list of P contains a pack expansion that is not |
| 5026 | // the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5027 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5028 | if (OnlyDeduced && |
Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 5029 | hasPackExpansionBeforeEnd(TemplateArgs.asArray())) |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5030 | return; |
| 5031 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5032 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5033 | ::MarkUsedTemplateParameters(Context, TemplateArgs[I], OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5034 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5035 | } |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5036 | |
| 5037 | /// \brief Marks all of the template parameters that will be deduced by a |
| 5038 | /// call to the given function template. |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 5039 | void Sema::MarkDeducedTemplateParameters( |
| 5040 | ASTContext &Ctx, const FunctionTemplateDecl *FunctionTemplate, |
| 5041 | llvm::SmallBitVector &Deduced) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5042 | TemplateParameterList *TemplateParams |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5043 | = FunctionTemplate->getTemplateParameters(); |
| 5044 | Deduced.clear(); |
| 5045 | Deduced.resize(TemplateParams->size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5046 | |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5047 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 5048 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5049 | ::MarkUsedTemplateParameters(Ctx, Function->getParamDecl(I)->getType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5050 | true, TemplateParams->getDepth(), Deduced); |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5051 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5052 | |
| 5053 | bool hasDeducibleTemplateParameters(Sema &S, |
| 5054 | FunctionTemplateDecl *FunctionTemplate, |
| 5055 | QualType T) { |
| 5056 | if (!T->isDependentType()) |
| 5057 | return false; |
| 5058 | |
| 5059 | TemplateParameterList *TemplateParams |
| 5060 | = FunctionTemplate->getTemplateParameters(); |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5061 | llvm::SmallBitVector Deduced(TemplateParams->size()); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 5062 | ::MarkUsedTemplateParameters(S.Context, T, true, TemplateParams->getDepth(), |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5063 | Deduced); |
| 5064 | |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5065 | return Deduced.any(); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5066 | } |