| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 1 | //===- SemaTemplateDeduction.cpp - Template Argument Deduction ------------===// |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 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. |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 7 | // |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 9 | // |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 10 | // This file implements C++ template argument deduction. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 13 | |
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 14 | #include "clang/Sema/TemplateDeduction.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "TreeTransform.h" |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 16 | #include "TypeLocBuilder.h" |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
| Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTLambda.h" |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 19 | #include "clang/AST/Decl.h" |
| 20 | #include "clang/AST/DeclAccessPair.h" |
| 21 | #include "clang/AST/DeclBase.h" |
| 22 | #include "clang/AST/DeclCXX.h" |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclTemplate.h" |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclarationName.h" |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 25 | #include "clang/AST/Expr.h" |
| 26 | #include "clang/AST/ExprCXX.h" |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 27 | #include "clang/AST/NestedNameSpecifier.h" |
| 28 | #include "clang/AST/TemplateBase.h" |
| 29 | #include "clang/AST/TemplateName.h" |
| 30 | #include "clang/AST/Type.h" |
| 31 | #include "clang/AST/TypeLoc.h" |
| 32 | #include "clang/AST/UnresolvedSet.h" |
| 33 | #include "clang/Basic/AddressSpaces.h" |
| 34 | #include "clang/Basic/ExceptionSpecificationType.h" |
| 35 | #include "clang/Basic/LLVM.h" |
| 36 | #include "clang/Basic/LangOptions.h" |
| 37 | #include "clang/Basic/PartialDiagnostic.h" |
| 38 | #include "clang/Basic/SourceLocation.h" |
| 39 | #include "clang/Basic/Specifiers.h" |
| 40 | #include "clang/Sema/Ownership.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 41 | #include "clang/Sema/Sema.h" |
| 42 | #include "clang/Sema/Template.h" |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 43 | #include "llvm/ADT/APInt.h" |
| 44 | #include "llvm/ADT/APSInt.h" |
| 45 | #include "llvm/ADT/ArrayRef.h" |
| 46 | #include "llvm/ADT/DenseMap.h" |
| 47 | #include "llvm/ADT/FoldingSet.h" |
| 48 | #include "llvm/ADT/Optional.h" |
| Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 49 | #include "llvm/ADT/SmallBitVector.h" |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 50 | #include "llvm/ADT/SmallPtrSet.h" |
| 51 | #include "llvm/ADT/SmallVector.h" |
| 52 | #include "llvm/Support/Casting.h" |
| 53 | #include "llvm/Support/Compiler.h" |
| 54 | #include "llvm/Support/ErrorHandling.h" |
| Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 55 | #include <algorithm> |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 56 | #include <cassert> |
| 57 | #include <tuple> |
| 58 | #include <utility> |
| Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 59 | |
| 60 | namespace clang { |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 61 | |
| Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 62 | /// \brief Various flags that control template argument deduction. |
| 63 | /// |
| 64 | /// These flags can be bitwise-OR'd together. |
| 65 | enum TemplateDeductionFlags { |
| 66 | /// \brief No template argument deduction flags, which indicates the |
| 67 | /// strictest results for template argument deduction (as used for, e.g., |
| 68 | /// matching class template partial specializations). |
| 69 | TDF_None = 0, |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 70 | |
| Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 71 | /// \brief Within template argument deduction from a function call, we are |
| 72 | /// matching with a parameter type for which the original parameter was |
| 73 | /// a reference. |
| 74 | TDF_ParamWithReferenceType = 0x1, |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 75 | |
| Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 76 | /// \brief Within template argument deduction from a function call, we |
| 77 | /// are matching in a case where we ignore cv-qualifiers. |
| 78 | TDF_IgnoreQualifiers = 0x02, |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 79 | |
| Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 80 | /// \brief Within template argument deduction from a function call, |
| 81 | /// we are matching in a case where we can perform template argument |
| Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 82 | /// 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] | 83 | TDF_DerivedClass = 0x04, |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 84 | |
| Douglas Gregor | 406f634 | 2009-09-14 20:00:47 +0000 | [diff] [blame] | 85 | /// \brief Allow non-dependent types to differ, e.g., when performing |
| 86 | /// template argument deduction from a function call where conversions |
| 87 | /// may apply. |
| Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 88 | TDF_SkipNonDependent = 0x08, |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 89 | |
| Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 90 | /// \brief Whether we are performing template argument deduction for |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 91 | /// parameters and arguments in a top-level template argument |
| Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 92 | TDF_TopLevelParameterTypeList = 0x10, |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 93 | |
| Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 94 | /// \brief Within template argument deduction from overload resolution per |
| 95 | /// C++ [over.over] allow matching function types that are compatible in |
| Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 96 | /// terms of noreturn and default calling convention adjustments, or |
| 97 | /// similarly matching a declared template specialization against a |
| 98 | /// possible template, per C++ [temp.deduct.decl]. In either case, permit |
| 99 | /// deduction where the parameter is a function type that can be converted |
| 100 | /// to the argument type. |
| 101 | TDF_AllowCompatibleFunctionType = 0x20, |
| Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 102 | }; |
| Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 103 | } |
| Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 104 | |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 105 | using namespace clang; |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 106 | using namespace sema; |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 107 | |
| Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 108 | /// \brief Compare two APSInts, extending and switching the sign as |
| 109 | /// necessary to compare their values regardless of underlying type. |
| 110 | static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) { |
| 111 | if (Y.getBitWidth() > X.getBitWidth()) |
| Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 112 | X = X.extend(Y.getBitWidth()); |
| Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 113 | else if (Y.getBitWidth() < X.getBitWidth()) |
| Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 114 | Y = Y.extend(X.getBitWidth()); |
| Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 115 | |
| 116 | // If there is a signedness mismatch, correct it. |
| 117 | if (X.isSigned() != Y.isSigned()) { |
| 118 | // If the signed value is negative, then the values cannot be the same. |
| 119 | if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative())) |
| 120 | return false; |
| 121 | |
| 122 | Y.setIsSigned(true); |
| 123 | X.setIsSigned(true); |
| 124 | } |
| 125 | |
| 126 | return X == Y; |
| 127 | } |
| 128 | |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 129 | static Sema::TemplateDeductionResult |
| Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 130 | DeduceTemplateArguments(Sema &S, |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 131 | TemplateParameterList *TemplateParams, |
| 132 | const TemplateArgument &Param, |
| Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 133 | TemplateArgument Arg, |
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 134 | TemplateDeductionInfo &Info, |
| Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 135 | SmallVectorImpl<DeducedTemplateArgument> &Deduced); |
| Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 136 | |
| Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 137 | static Sema::TemplateDeductionResult |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 138 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 139 | TemplateParameterList *TemplateParams, |
| 140 | QualType Param, |
| 141 | QualType Arg, |
| 142 | TemplateDeductionInfo &Info, |
| 143 | SmallVectorImpl<DeducedTemplateArgument> & |
| 144 | Deduced, |
| 145 | unsigned TDF, |
| Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 146 | bool PartialOrdering = false, |
| 147 | bool DeducedFromArrayBound = false); |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 148 | |
| 149 | static Sema::TemplateDeductionResult |
| Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 150 | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
| Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 151 | ArrayRef<TemplateArgument> Params, |
| 152 | ArrayRef<TemplateArgument> Args, |
| Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 153 | TemplateDeductionInfo &Info, |
| Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 154 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 155 | bool NumberOfArgumentsMustMatch); |
| Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 156 | |
| Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 157 | static void MarkUsedTemplateParameters(ASTContext &Ctx, |
| 158 | const TemplateArgument &TemplateArg, |
| 159 | bool OnlyDeduced, unsigned Depth, |
| 160 | llvm::SmallBitVector &Used); |
| 161 | |
| 162 | static void MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
| 163 | bool OnlyDeduced, unsigned Level, |
| 164 | llvm::SmallBitVector &Deduced); |
| 165 | |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 166 | /// \brief If the given expression is of a form that permits the deduction |
| 167 | /// of a non-type template parameter, return the declaration of that |
| 168 | /// non-type template parameter. |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 169 | static NonTypeTemplateParmDecl * |
| 170 | getDeducedParameterFromExpr(TemplateDeductionInfo &Info, Expr *E) { |
| Richard Smith | 7ebb07c | 2012-07-08 04:37:51 +0000 | [diff] [blame] | 171 | // If we are within an alias template, the expression may have undergone |
| 172 | // any number of parameter substitutions already. |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 173 | while (true) { |
| Richard Smith | 7ebb07c | 2012-07-08 04:37:51 +0000 | [diff] [blame] | 174 | if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E)) |
| 175 | E = IC->getSubExpr(); |
| 176 | else if (SubstNonTypeTemplateParmExpr *Subst = |
| 177 | dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 178 | E = Subst->getReplacement(); |
| 179 | else |
| 180 | break; |
| 181 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 182 | |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 183 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 184 | if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) |
| 185 | if (NTTP->getDepth() == Info.getDeducedDepth()) |
| 186 | return NTTP; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 187 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 188 | return nullptr; |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 191 | /// \brief Determine whether two declaration pointers refer to the same |
| 192 | /// declaration. |
| 193 | static bool isSameDeclaration(Decl *X, Decl *Y) { |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 194 | if (NamedDecl *NX = dyn_cast<NamedDecl>(X)) |
| 195 | X = NX->getUnderlyingDecl(); |
| 196 | if (NamedDecl *NY = dyn_cast<NamedDecl>(Y)) |
| 197 | Y = NY->getUnderlyingDecl(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 198 | |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 199 | return X->getCanonicalDecl() == Y->getCanonicalDecl(); |
| 200 | } |
| 201 | |
| 202 | /// \brief Verify that the given, deduced template arguments are compatible. |
| 203 | /// |
| 204 | /// \returns The deduced template argument, or a NULL template argument if |
| 205 | /// the deduced template arguments were incompatible. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 206 | static DeducedTemplateArgument |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 207 | checkDeducedTemplateArguments(ASTContext &Context, |
| 208 | const DeducedTemplateArgument &X, |
| 209 | const DeducedTemplateArgument &Y) { |
| 210 | // We have no deduction for one or both of the arguments; they're compatible. |
| 211 | if (X.isNull()) |
| 212 | return Y; |
| 213 | if (Y.isNull()) |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 214 | return X; |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 215 | |
| Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 216 | // If we have two non-type template argument values deduced for the same |
| 217 | // parameter, they must both match the type of the parameter, and thus must |
| 218 | // match each other's type. As we're only keeping one of them, we must check |
| 219 | // for that now. The exception is that if either was deduced from an array |
| 220 | // bound, the type is permitted to differ. |
| 221 | if (!X.wasDeducedFromArrayBound() && !Y.wasDeducedFromArrayBound()) { |
| 222 | QualType XType = X.getNonTypeTemplateArgumentType(); |
| 223 | if (!XType.isNull()) { |
| 224 | QualType YType = Y.getNonTypeTemplateArgumentType(); |
| 225 | if (YType.isNull() || !Context.hasSameType(XType, YType)) |
| 226 | return DeducedTemplateArgument(); |
| 227 | } |
| 228 | } |
| 229 | |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 230 | switch (X.getKind()) { |
| 231 | case TemplateArgument::Null: |
| 232 | llvm_unreachable("Non-deduced template arguments handled above"); |
| 233 | |
| 234 | case TemplateArgument::Type: |
| 235 | // If two template type arguments have the same type, they're compatible. |
| 236 | if (Y.getKind() == TemplateArgument::Type && |
| 237 | Context.hasSameType(X.getAsType(), Y.getAsType())) |
| 238 | return X; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 239 | |
| Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 240 | // If one of the two arguments was deduced from an array bound, the other |
| 241 | // supersedes it. |
| 242 | if (X.wasDeducedFromArrayBound() != Y.wasDeducedFromArrayBound()) |
| 243 | return X.wasDeducedFromArrayBound() ? Y : X; |
| 244 | |
| 245 | // The arguments are not compatible. |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 246 | return DeducedTemplateArgument(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 247 | |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 248 | case TemplateArgument::Integral: |
| 249 | // If we deduced a constant in one case and either a dependent expression or |
| 250 | // declaration in another case, keep the integral constant. |
| 251 | // If both are integral constants with the same value, keep that value. |
| 252 | if (Y.getKind() == TemplateArgument::Expression || |
| 253 | Y.getKind() == TemplateArgument::Declaration || |
| 254 | (Y.getKind() == TemplateArgument::Integral && |
| Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 255 | hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()))) |
| Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 256 | return X.wasDeducedFromArrayBound() ? Y : X; |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 257 | |
| 258 | // All other combinations are incompatible. |
| 259 | return DeducedTemplateArgument(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 260 | |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 261 | case TemplateArgument::Template: |
| 262 | if (Y.getKind() == TemplateArgument::Template && |
| 263 | Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate())) |
| 264 | return X; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 265 | |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 266 | // All other combinations are incompatible. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 267 | return DeducedTemplateArgument(); |
| Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 268 | |
| 269 | case TemplateArgument::TemplateExpansion: |
| 270 | if (Y.getKind() == TemplateArgument::TemplateExpansion && |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 271 | Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(), |
| Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 272 | Y.getAsTemplateOrTemplatePattern())) |
| 273 | return X; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 274 | |
| Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 275 | // All other combinations are incompatible. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 276 | return DeducedTemplateArgument(); |
| Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 277 | |
| Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 278 | case TemplateArgument::Expression: { |
| 279 | if (Y.getKind() != TemplateArgument::Expression) |
| 280 | return checkDeducedTemplateArguments(Context, Y, X); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 281 | |
| Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 282 | // Compare the expressions for equality |
| 283 | llvm::FoldingSetNodeID ID1, ID2; |
| 284 | X.getAsExpr()->Profile(ID1, Context, true); |
| 285 | Y.getAsExpr()->Profile(ID2, Context, true); |
| 286 | if (ID1 == ID2) |
| 287 | return X.wasDeducedFromArrayBound() ? Y : X; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 288 | |
| Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 289 | // Differing dependent expressions are incompatible. |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 290 | return DeducedTemplateArgument(); |
| Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 291 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 292 | |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 293 | case TemplateArgument::Declaration: |
| Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 294 | assert(!X.wasDeducedFromArrayBound()); |
| 295 | |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 296 | // If we deduced a declaration and a dependent expression, keep the |
| 297 | // declaration. |
| 298 | if (Y.getKind() == TemplateArgument::Expression) |
| 299 | return X; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 300 | |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 301 | // If we deduced a declaration and an integral constant, keep the |
| Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 302 | // integral constant and whichever type did not come from an array |
| 303 | // bound. |
| 304 | if (Y.getKind() == TemplateArgument::Integral) { |
| 305 | if (Y.wasDeducedFromArrayBound()) |
| 306 | return TemplateArgument(Context, Y.getAsIntegral(), |
| 307 | X.getParamTypeForDecl()); |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 308 | return Y; |
| Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 309 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 310 | |
| Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 311 | // If we deduced two declarations, make sure that they refer to the |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 312 | // same declaration. |
| 313 | if (Y.getKind() == TemplateArgument::Declaration && |
| David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 314 | isSameDeclaration(X.getAsDecl(), Y.getAsDecl())) |
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 315 | return X; |
| 316 | |
| 317 | // All other combinations are incompatible. |
| 318 | return DeducedTemplateArgument(); |
| 319 | |
| 320 | case TemplateArgument::NullPtr: |
| 321 | // If we deduced a null pointer and a dependent expression, keep the |
| 322 | // null pointer. |
| 323 | if (Y.getKind() == TemplateArgument::Expression) |
| 324 | return X; |
| 325 | |
| 326 | // If we deduced a null pointer and an integral constant, keep the |
| 327 | // integral constant. |
| 328 | if (Y.getKind() == TemplateArgument::Integral) |
| 329 | return Y; |
| 330 | |
| Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 331 | // If we deduced two null pointers, they are the same. |
| 332 | if (Y.getKind() == TemplateArgument::NullPtr) |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 333 | return X; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 334 | |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 335 | // All other combinations are incompatible. |
| 336 | return DeducedTemplateArgument(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 337 | |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 338 | case TemplateArgument::Pack: { |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 339 | if (Y.getKind() != TemplateArgument::Pack || |
| 340 | X.pack_size() != Y.pack_size()) |
| 341 | return DeducedTemplateArgument(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 342 | |
| Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 343 | llvm::SmallVector<TemplateArgument, 8> NewPack; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 344 | for (TemplateArgument::pack_iterator XA = X.pack_begin(), |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 345 | XAEnd = X.pack_end(), |
| 346 | YA = Y.pack_begin(); |
| 347 | XA != XAEnd; ++XA, ++YA) { |
| Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 348 | TemplateArgument Merged = checkDeducedTemplateArguments( |
| 349 | Context, DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()), |
| 350 | DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound())); |
| 351 | if (Merged.isNull()) |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 352 | return DeducedTemplateArgument(); |
| Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 353 | NewPack.push_back(Merged); |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 354 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 355 | |
| Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 356 | return DeducedTemplateArgument( |
| 357 | TemplateArgument::CreatePackCopy(Context, NewPack), |
| 358 | X.wasDeducedFromArrayBound() && Y.wasDeducedFromArrayBound()); |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 359 | } |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 360 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 361 | |
| David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 362 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 365 | /// \brief Deduce the value of the given non-type template parameter |
| Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 366 | /// as the given deduced template argument. All non-type template parameter |
| 367 | /// deduction is funneled through here. |
| Benjamin Kramer | 7320b99 | 2016-06-15 14:20:56 +0000 | [diff] [blame] | 368 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 369 | Sema &S, TemplateParameterList *TemplateParams, |
| Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 370 | NonTypeTemplateParmDecl *NTTP, const DeducedTemplateArgument &NewDeduced, |
| 371 | QualType ValueType, TemplateDeductionInfo &Info, |
| Benjamin Kramer | 7320b99 | 2016-06-15 14:20:56 +0000 | [diff] [blame] | 372 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 373 | assert(NTTP->getDepth() == Info.getDeducedDepth() && |
| 374 | "deducing non-type template argument with wrong depth"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 375 | |
| Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 376 | DeducedTemplateArgument Result = checkDeducedTemplateArguments( |
| 377 | S.Context, Deduced[NTTP->getIndex()], NewDeduced); |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 378 | if (Result.isNull()) { |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 379 | Info.Param = NTTP; |
| 380 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 381 | Info.SecondArg = NewDeduced; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 382 | return Sema::TDK_Inconsistent; |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 383 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 384 | |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 385 | Deduced[NTTP->getIndex()] = Result; |
| Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 386 | if (!S.getLangOpts().CPlusPlus17) |
| Richard Smith | d92eddf | 2016-12-27 06:14:37 +0000 | [diff] [blame] | 387 | return Sema::TDK_Success; |
| 388 | |
| Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 389 | if (NTTP->isExpandedParameterPack()) |
| 390 | // FIXME: We may still need to deduce parts of the type here! But we |
| 391 | // don't have any way to find which slice of the type to use, and the |
| 392 | // type stored on the NTTP itself is nonsense. Perhaps the type of an |
| 393 | // expanded NTTP should be a pack expansion type? |
| 394 | return Sema::TDK_Success; |
| 395 | |
| Richard Smith | 7bfcc05 | 2017-12-01 21:24:36 +0000 | [diff] [blame] | 396 | // Get the type of the parameter for deduction. If it's a (dependent) array |
| 397 | // or function type, we will not have decayed it yet, so do that now. |
| 398 | QualType ParamType = S.Context.getAdjustedParameterType(NTTP->getType()); |
| Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 399 | if (auto *Expansion = dyn_cast<PackExpansionType>(ParamType)) |
| 400 | ParamType = Expansion->getPattern(); |
| 401 | |
| Richard Smith | d92eddf | 2016-12-27 06:14:37 +0000 | [diff] [blame] | 402 | // FIXME: It's not clear how deduction of a parameter of reference |
| 403 | // type from an argument (of non-reference type) should be performed. |
| 404 | // For now, we just remove reference types from both sides and let |
| 405 | // the final check for matching types sort out the mess. |
| 406 | return DeduceTemplateArgumentsByTypeMatch( |
| Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 407 | S, TemplateParams, ParamType.getNonReferenceType(), |
| Richard Smith | d92eddf | 2016-12-27 06:14:37 +0000 | [diff] [blame] | 408 | ValueType.getNonReferenceType(), Info, Deduced, TDF_SkipNonDependent, |
| 409 | /*PartialOrdering=*/false, |
| 410 | /*ArrayBound=*/NewDeduced.wasDeducedFromArrayBound()); |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | /// \brief Deduce the value of the given non-type template parameter |
| Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 414 | /// from the given integral constant. |
| 415 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| 416 | Sema &S, TemplateParameterList *TemplateParams, |
| 417 | NonTypeTemplateParmDecl *NTTP, const llvm::APSInt &Value, |
| 418 | QualType ValueType, bool DeducedFromArrayBound, TemplateDeductionInfo &Info, |
| 419 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 420 | return DeduceNonTypeTemplateArgument( |
| 421 | S, TemplateParams, NTTP, |
| 422 | DeducedTemplateArgument(S.Context, Value, ValueType, |
| 423 | DeducedFromArrayBound), |
| 424 | ValueType, Info, Deduced); |
| 425 | } |
| 426 | |
| 427 | /// \brief Deduce the value of the given non-type template parameter |
| Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 428 | /// from the given null pointer template argument type. |
| 429 | static Sema::TemplateDeductionResult DeduceNullPtrTemplateArgument( |
| Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 430 | Sema &S, TemplateParameterList *TemplateParams, |
| 431 | NonTypeTemplateParmDecl *NTTP, QualType NullPtrType, |
| Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 432 | TemplateDeductionInfo &Info, |
| 433 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 434 | Expr *Value = |
| 435 | S.ImpCastExprToType(new (S.Context) CXXNullPtrLiteralExpr( |
| 436 | S.Context.NullPtrTy, NTTP->getLocation()), |
| 437 | NullPtrType, CK_NullToPointer) |
| 438 | .get(); |
| Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 439 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 440 | DeducedTemplateArgument(Value), |
| 441 | Value->getType(), Info, Deduced); |
| Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /// \brief Deduce the value of the given non-type template parameter |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 445 | /// from the given type- or value-dependent expression. |
| 446 | /// |
| 447 | /// \returns true if deduction succeeded, false otherwise. |
| Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 448 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| 449 | Sema &S, TemplateParameterList *TemplateParams, |
| 450 | NonTypeTemplateParmDecl *NTTP, Expr *Value, TemplateDeductionInfo &Info, |
| 451 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 452 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 453 | DeducedTemplateArgument(Value), |
| 454 | Value->getType(), Info, Deduced); |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 457 | /// \brief Deduce the value of the given non-type template parameter |
| 458 | /// from the given declaration. |
| 459 | /// |
| 460 | /// \returns true if deduction succeeded, false otherwise. |
| Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 461 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| 462 | Sema &S, TemplateParameterList *TemplateParams, |
| 463 | NonTypeTemplateParmDecl *NTTP, ValueDecl *D, QualType T, |
| 464 | TemplateDeductionInfo &Info, |
| 465 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 466 | D = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; |
| Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 467 | TemplateArgument New(D, T); |
| Richard Smith | 5d10289 | 2016-12-27 03:59:58 +0000 | [diff] [blame] | 468 | return DeduceNonTypeTemplateArgument( |
| 469 | S, TemplateParams, NTTP, DeducedTemplateArgument(New), T, Info, Deduced); |
| Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 470 | } |
| 471 | |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 472 | static Sema::TemplateDeductionResult |
| Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 473 | DeduceTemplateArguments(Sema &S, |
| Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 474 | TemplateParameterList *TemplateParams, |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 475 | TemplateName Param, |
| 476 | TemplateName Arg, |
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 477 | TemplateDeductionInfo &Info, |
| Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 478 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 479 | TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); |
| Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 480 | if (!ParamDecl) { |
| 481 | // The parameter type is dependent and is not a template template parameter, |
| 482 | // so there is nothing that we can deduce. |
| 483 | return Sema::TDK_Success; |
| 484 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 485 | |
| Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 486 | if (TemplateTemplateParmDecl *TempParam |
| 487 | = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) { |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 488 | // If we're not deducing at this depth, there's nothing to deduce. |
| 489 | if (TempParam->getDepth() != Info.getDeducedDepth()) |
| 490 | return Sema::TDK_Success; |
| 491 | |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 492 | DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg)); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 493 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 494 | Deduced[TempParam->getIndex()], |
| 495 | NewDeduced); |
| 496 | if (Result.isNull()) { |
| 497 | Info.Param = TempParam; |
| 498 | Info.FirstArg = Deduced[TempParam->getIndex()]; |
| 499 | Info.SecondArg = NewDeduced; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 500 | return Sema::TDK_Inconsistent; |
| Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 501 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 502 | |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 503 | Deduced[TempParam->getIndex()] = Result; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 504 | return Sema::TDK_Success; |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 505 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 506 | |
| Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 507 | // Verify that the two template names are equivalent. |
| Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 508 | if (S.Context.hasSameTemplateName(Param, Arg)) |
| Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 509 | return Sema::TDK_Success; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 510 | |
| Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 511 | // Mismatch of non-dependent template parameter to argument. |
| 512 | Info.FirstArg = TemplateArgument(Param); |
| 513 | Info.SecondArg = TemplateArgument(Arg); |
| 514 | return Sema::TDK_NonDeducedMismatch; |
| Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 515 | } |
| 516 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 517 | /// \brief Deduce the template arguments by comparing the template parameter |
| Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 518 | /// type (which is a template-id) with the template argument type. |
| 519 | /// |
| Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 520 | /// \param S the Sema |
| Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 521 | /// |
| 522 | /// \param TemplateParams the template parameters that we are deducing |
| 523 | /// |
| 524 | /// \param Param the parameter type |
| 525 | /// |
| 526 | /// \param Arg the argument type |
| 527 | /// |
| 528 | /// \param Info information about the template argument deduction itself |
| 529 | /// |
| 530 | /// \param Deduced the deduced template arguments |
| 531 | /// |
| 532 | /// \returns the result of template argument deduction so far. Note that a |
| 533 | /// "success" result means that template argument deduction has not yet failed, |
| 534 | /// but it may still fail, later, for other reasons. |
| 535 | static Sema::TemplateDeductionResult |
| Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 536 | DeduceTemplateArguments(Sema &S, |
| Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 537 | TemplateParameterList *TemplateParams, |
| 538 | const TemplateSpecializationType *Param, |
| 539 | QualType Arg, |
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 540 | TemplateDeductionInfo &Info, |
| Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 541 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 542 | assert(Arg.isCanonical() && "Argument type must be canonical"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 543 | |
| Richard Smith | 1337318 | 2018-01-04 01:24:17 +0000 | [diff] [blame] | 544 | // Treat an injected-class-name as its underlying template-id. |
| 545 | if (auto *Injected = dyn_cast<InjectedClassNameType>(Arg)) |
| 546 | Arg = Injected->getInjectedSpecializationType(); |
| 547 | |
| Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 548 | // Check whether the template argument is a dependent template-id. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 549 | if (const TemplateSpecializationType *SpecArg |
| Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 550 | = dyn_cast<TemplateSpecializationType>(Arg)) { |
| 551 | // Perform template argument deduction for the template name. |
| 552 | if (Sema::TemplateDeductionResult Result |
| Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 553 | = DeduceTemplateArguments(S, TemplateParams, |
| Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 554 | Param->getTemplateName(), |
| 555 | SpecArg->getTemplateName(), |
| 556 | Info, Deduced)) |
| 557 | return Result; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 558 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 559 | |
| Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 560 | // Perform template argument deduction on each template |
| Douglas Gregor | d80ea20 | 2010-12-22 18:55:49 +0000 | [diff] [blame] | 561 | // argument. Ignore any missing/extra arguments, since they could be |
| 562 | // filled in by default arguments. |
| Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 563 | return DeduceTemplateArguments(S, TemplateParams, |
| 564 | Param->template_arguments(), |
| 565 | SpecArg->template_arguments(), Info, Deduced, |
| Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 566 | /*NumberOfArgumentsMustMatch=*/false); |
| Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 567 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 568 | |
| Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 569 | // If the argument type is a class template specialization, we |
| 570 | // perform template argument deduction using its template |
| 571 | // arguments. |
| 572 | const RecordType *RecordArg = dyn_cast<RecordType>(Arg); |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 573 | if (!RecordArg) { |
| 574 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 575 | Info.SecondArg = TemplateArgument(Arg); |
| Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 576 | return Sema::TDK_NonDeducedMismatch; |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 577 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 578 | |
| 579 | ClassTemplateSpecializationDecl *SpecArg |
| Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 580 | = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 581 | if (!SpecArg) { |
| 582 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 583 | Info.SecondArg = TemplateArgument(Arg); |
| Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 584 | return Sema::TDK_NonDeducedMismatch; |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 585 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 586 | |
| Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 587 | // Perform template argument deduction for the template name. |
| 588 | if (Sema::TemplateDeductionResult Result |
| Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 589 | = DeduceTemplateArguments(S, |
| Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 590 | TemplateParams, |
| Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 591 | Param->getTemplateName(), |
| 592 | TemplateName(SpecArg->getSpecializedTemplate()), |
| 593 | Info, Deduced)) |
| 594 | return Result; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 595 | |
| Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 596 | // Perform template argument deduction for the template arguments. |
| Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 597 | return DeduceTemplateArguments(S, TemplateParams, Param->template_arguments(), |
| 598 | SpecArg->getTemplateArgs().asArray(), Info, |
| 599 | Deduced, /*NumberOfArgumentsMustMatch=*/true); |
| Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 602 | /// \brief Determines whether the given type is an opaque type that |
| 603 | /// might be more qualified when instantiated. |
| 604 | static bool IsPossiblyOpaquelyQualifiedType(QualType T) { |
| 605 | switch (T->getTypeClass()) { |
| 606 | case Type::TypeOfExpr: |
| 607 | case Type::TypeOf: |
| 608 | case Type::DependentName: |
| 609 | case Type::Decltype: |
| 610 | case Type::UnresolvedUsing: |
| John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 611 | case Type::TemplateTypeParm: |
| John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 612 | return true; |
| 613 | |
| 614 | case Type::ConstantArray: |
| 615 | case Type::IncompleteArray: |
| 616 | case Type::VariableArray: |
| 617 | case Type::DependentSizedArray: |
| 618 | return IsPossiblyOpaquelyQualifiedType( |
| 619 | cast<ArrayType>(T)->getElementType()); |
| 620 | |
| 621 | default: |
| 622 | return false; |
| 623 | } |
| 624 | } |
| 625 | |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 626 | /// \brief Retrieve the depth and index of a template parameter. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 627 | static std::pair<unsigned, unsigned> |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 628 | getDepthAndIndex(NamedDecl *ND) { |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 629 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND)) |
| 630 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 631 | |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 632 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND)) |
| 633 | return std::make_pair(NTTP->getDepth(), NTTP->getIndex()); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 634 | |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 635 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND); |
| 636 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| 637 | } |
| 638 | |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 639 | /// \brief Retrieve the depth and index of an unexpanded parameter pack. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 640 | static std::pair<unsigned, unsigned> |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 641 | getDepthAndIndex(UnexpandedParameterPack UPP) { |
| 642 | if (const TemplateTypeParmType *TTP |
| 643 | = UPP.first.dyn_cast<const TemplateTypeParmType *>()) |
| 644 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 645 | |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 646 | return getDepthAndIndex(UPP.first.get<NamedDecl *>()); |
| 647 | } |
| 648 | |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 649 | /// \brief Helper function to build a TemplateParameter when we don't |
| 650 | /// know its type statically. |
| 651 | static TemplateParameter makeTemplateParameter(Decl *D) { |
| 652 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) |
| 653 | return TemplateParameter(TTP); |
| Craig Topper | 4b482ee | 2013-07-08 04:24:47 +0000 | [diff] [blame] | 654 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 655 | return TemplateParameter(NTTP); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 656 | |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 657 | return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); |
| 658 | } |
| 659 | |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 660 | /// A pack that we're currently deducing. |
| 661 | struct clang::DeducedPack { |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 662 | // The index of the pack. |
| 663 | unsigned Index; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 664 | |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 665 | // The old value of the pack before we started deducing it. |
| 666 | DeducedTemplateArgument Saved; |
| Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 667 | |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 668 | // A deferred value of this pack from an inner deduction, that couldn't be |
| 669 | // deduced because this deduction hadn't happened yet. |
| 670 | DeducedTemplateArgument DeferredDeduction; |
| 671 | |
| 672 | // The new value of the pack. |
| 673 | SmallVector<DeducedTemplateArgument, 4> New; |
| 674 | |
| 675 | // The outer deduction for this pack, if any. |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 676 | DeducedPack *Outer = nullptr; |
| 677 | |
| 678 | DeducedPack(unsigned Index) : Index(Index) {} |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 679 | }; |
| 680 | |
| Benjamin Kramer | d5748c7 | 2015-03-23 12:31:05 +0000 | [diff] [blame] | 681 | namespace { |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 682 | |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 683 | /// A scope in which we're performing pack deduction. |
| 684 | class PackDeductionScope { |
| 685 | public: |
| 686 | PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, |
| 687 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 688 | TemplateDeductionInfo &Info, TemplateArgument Pattern) |
| 689 | : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { |
| Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 690 | // Dig out the partially-substituted pack, if there is one. |
| 691 | const TemplateArgument *PartialPackArgs = nullptr; |
| 692 | unsigned NumPartialPackArgs = 0; |
| 693 | std::pair<unsigned, unsigned> PartialPackDepthIndex(-1u, -1u); |
| 694 | if (auto *Scope = S.CurrentInstantiationScope) |
| 695 | if (auto *Partial = Scope->getPartiallySubstitutedPack( |
| 696 | &PartialPackArgs, &NumPartialPackArgs)) |
| 697 | PartialPackDepthIndex = getDepthAndIndex(Partial); |
| 698 | |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 699 | // Compute the set of template parameter indices that correspond to |
| 700 | // parameter packs expanded by the pack expansion. |
| 701 | { |
| 702 | llvm::SmallBitVector SawIndices(TemplateParams->size()); |
| Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 703 | |
| 704 | auto AddPack = [&](unsigned Index) { |
| 705 | if (SawIndices[Index]) |
| 706 | return; |
| 707 | SawIndices[Index] = true; |
| 708 | |
| 709 | // Save the deduced template argument for the parameter pack expanded |
| 710 | // by this pack expansion, then clear out the deduction. |
| 711 | DeducedPack Pack(Index); |
| 712 | Pack.Saved = Deduced[Index]; |
| 713 | Deduced[Index] = TemplateArgument(); |
| 714 | |
| 715 | Packs.push_back(Pack); |
| 716 | }; |
| 717 | |
| 718 | // First look for unexpanded packs in the pattern. |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 719 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 720 | S.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
| 721 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 722 | unsigned Depth, Index; |
| 723 | std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); |
| Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 724 | if (Depth == Info.getDeducedDepth()) |
| 725 | AddPack(Index); |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 726 | } |
| Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 727 | assert(!Packs.empty() && "Pack expansion without unexpanded packs?"); |
| 728 | |
| 729 | // This pack expansion will have been partially expanded iff the only |
| 730 | // unexpanded parameter pack within it is the partially-substituted pack. |
| 731 | IsPartiallyExpanded = |
| 732 | Packs.size() == 1 && |
| 733 | PartialPackDepthIndex == |
| 734 | std::make_pair(Info.getDeducedDepth(), Packs.front().Index); |
| 735 | |
| 736 | // Skip over the pack elements that were expanded into separate arguments. |
| 737 | if (IsPartiallyExpanded) |
| 738 | PackElements += NumPartialPackArgs; |
| 739 | |
| 740 | // We can also have deduced template parameters that do not actually |
| 741 | // appear in the pattern, but can be deduced by it (the type of a non-type |
| 742 | // template parameter pack, in particular). These won't have prevented us |
| 743 | // from partially expanding the pack. |
| 744 | llvm::SmallBitVector Used(TemplateParams->size()); |
| 745 | MarkUsedTemplateParameters(S.Context, Pattern, /*OnlyDeduced*/true, |
| 746 | Info.getDeducedDepth(), Used); |
| 747 | for (int Index = Used.find_first(); Index != -1; |
| 748 | Index = Used.find_next(Index)) |
| 749 | if (TemplateParams->getParam(Index)->isParameterPack()) |
| 750 | AddPack(Index); |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 751 | } |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 752 | |
| 753 | for (auto &Pack : Packs) { |
| 754 | if (Info.PendingDeducedPacks.size() > Pack.Index) |
| 755 | Pack.Outer = Info.PendingDeducedPacks[Pack.Index]; |
| 756 | else |
| 757 | Info.PendingDeducedPacks.resize(Pack.Index + 1); |
| 758 | Info.PendingDeducedPacks[Pack.Index] = &Pack; |
| 759 | |
| Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 760 | if (PartialPackDepthIndex == |
| 761 | std::make_pair(Info.getDeducedDepth(), Pack.Index)) { |
| 762 | Pack.New.append(PartialPackArgs, PartialPackArgs + NumPartialPackArgs); |
| 763 | // We pre-populate the deduced value of the partially-substituted |
| 764 | // pack with the specified value. This is not entirely correct: the |
| 765 | // value is supposed to have been substituted, not deduced, but the |
| 766 | // cases where this is observable require an exact type match anyway. |
| 767 | // |
| 768 | // FIXME: If we could represent a "depth i, index j, pack elem k" |
| 769 | // parameter, we could substitute the partially-substituted pack |
| 770 | // everywhere and avoid this. |
| 771 | if (Pack.New.size() > PackElements) |
| 772 | Deduced[Pack.Index] = Pack.New[PackElements]; |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 773 | } |
| Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 774 | } |
| 775 | } |
| Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 776 | |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 777 | ~PackDeductionScope() { |
| 778 | for (auto &Pack : Packs) |
| 779 | Info.PendingDeducedPacks[Pack.Index] = Pack.Outer; |
| Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 780 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 781 | |
| Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 782 | /// Determine whether this pack has already been partially expanded into a |
| 783 | /// sequence of (prior) function parameters / template arguments. |
| Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 784 | bool isPartiallyExpanded() { return IsPartiallyExpanded; } |
| Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 785 | |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 786 | /// Move to deducing the next element in each pack that is being deduced. |
| 787 | void nextPackElement() { |
| 788 | // Capture the deduced template arguments for each parameter pack expanded |
| 789 | // by this pack expansion, add them to the list of arguments we've deduced |
| 790 | // for that pack, then clear out the deduced argument. |
| 791 | for (auto &Pack : Packs) { |
| 792 | DeducedTemplateArgument &DeducedArg = Deduced[Pack.Index]; |
| Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 793 | if (!Pack.New.empty() || !DeducedArg.isNull()) { |
| 794 | while (Pack.New.size() < PackElements) |
| 795 | Pack.New.push_back(DeducedTemplateArgument()); |
| Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 796 | if (Pack.New.size() == PackElements) |
| 797 | Pack.New.push_back(DeducedArg); |
| 798 | else |
| 799 | Pack.New[PackElements] = DeducedArg; |
| 800 | DeducedArg = Pack.New.size() > PackElements + 1 |
| 801 | ? Pack.New[PackElements + 1] |
| 802 | : DeducedTemplateArgument(); |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 803 | } |
| 804 | } |
| Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 805 | ++PackElements; |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | /// \brief Finish template argument deduction for a set of argument packs, |
| 809 | /// producing the argument packs and checking for consistency with prior |
| 810 | /// deductions. |
| Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 811 | Sema::TemplateDeductionResult finish() { |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 812 | // Build argument packs for each of the parameter packs expanded by this |
| 813 | // pack expansion. |
| 814 | for (auto &Pack : Packs) { |
| 815 | // Put back the old value for this pack. |
| 816 | Deduced[Pack.Index] = Pack.Saved; |
| 817 | |
| 818 | // Build or find a new value for this pack. |
| 819 | DeducedTemplateArgument NewPack; |
| Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 820 | if (PackElements && Pack.New.empty()) { |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 821 | if (Pack.DeferredDeduction.isNull()) { |
| 822 | // We were not able to deduce anything for this parameter pack |
| 823 | // (because it only appeared in non-deduced contexts), so just |
| 824 | // restore the saved argument pack. |
| 825 | continue; |
| 826 | } |
| 827 | |
| 828 | NewPack = Pack.DeferredDeduction; |
| 829 | Pack.DeferredDeduction = TemplateArgument(); |
| 830 | } else if (Pack.New.empty()) { |
| 831 | // If we deduced an empty argument pack, create it now. |
| 832 | NewPack = DeducedTemplateArgument(TemplateArgument::getEmptyPack()); |
| 833 | } else { |
| 834 | TemplateArgument *ArgumentPack = |
| 835 | new (S.Context) TemplateArgument[Pack.New.size()]; |
| 836 | std::copy(Pack.New.begin(), Pack.New.end(), ArgumentPack); |
| 837 | NewPack = DeducedTemplateArgument( |
| Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 838 | TemplateArgument(llvm::makeArrayRef(ArgumentPack, Pack.New.size())), |
| Richard Smith | 7fa88bb | 2017-02-21 07:22:31 +0000 | [diff] [blame] | 839 | // FIXME: This is wrong, it's possible that some pack elements are |
| 840 | // deduced from an array bound and others are not: |
| 841 | // template<typename ...T, T ...V> void g(const T (&...p)[V]); |
| 842 | // g({1, 2, 3}, {{}, {}}); |
| 843 | // ... should deduce T = {int, size_t (from array bound)}. |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 844 | Pack.New[0].wasDeducedFromArrayBound()); |
| 845 | } |
| 846 | |
| 847 | // Pick where we're going to put the merged pack. |
| 848 | DeducedTemplateArgument *Loc; |
| 849 | if (Pack.Outer) { |
| 850 | if (Pack.Outer->DeferredDeduction.isNull()) { |
| 851 | // Defer checking this pack until we have a complete pack to compare |
| 852 | // it against. |
| 853 | Pack.Outer->DeferredDeduction = NewPack; |
| 854 | continue; |
| 855 | } |
| 856 | Loc = &Pack.Outer->DeferredDeduction; |
| 857 | } else { |
| 858 | Loc = &Deduced[Pack.Index]; |
| 859 | } |
| 860 | |
| 861 | // Check the new pack matches any previous value. |
| 862 | DeducedTemplateArgument OldPack = *Loc; |
| 863 | DeducedTemplateArgument Result = |
| 864 | checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 865 | |
| 866 | // If we deferred a deduction of this pack, check that one now too. |
| 867 | if (!Result.isNull() && !Pack.DeferredDeduction.isNull()) { |
| 868 | OldPack = Result; |
| 869 | NewPack = Pack.DeferredDeduction; |
| 870 | Result = checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 871 | } |
| 872 | |
| 873 | if (Result.isNull()) { |
| 874 | Info.Param = |
| 875 | makeTemplateParameter(TemplateParams->getParam(Pack.Index)); |
| 876 | Info.FirstArg = OldPack; |
| 877 | Info.SecondArg = NewPack; |
| 878 | return Sema::TDK_Inconsistent; |
| 879 | } |
| 880 | |
| 881 | *Loc = Result; |
| 882 | } |
| 883 | |
| 884 | return Sema::TDK_Success; |
| 885 | } |
| 886 | |
| 887 | private: |
| 888 | Sema &S; |
| 889 | TemplateParameterList *TemplateParams; |
| 890 | SmallVectorImpl<DeducedTemplateArgument> &Deduced; |
| 891 | TemplateDeductionInfo &Info; |
| Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 892 | unsigned PackElements = 0; |
| Richard Smith | 130cc44 | 2017-02-21 23:49:18 +0000 | [diff] [blame] | 893 | bool IsPartiallyExpanded = false; |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 894 | |
| 895 | SmallVector<DeducedPack, 2> Packs; |
| 896 | }; |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 897 | |
| Benjamin Kramer | d5748c7 | 2015-03-23 12:31:05 +0000 | [diff] [blame] | 898 | } // namespace |
| Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 899 | |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 900 | /// \brief Deduce the template arguments by comparing the list of parameter |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 901 | /// types to the list of argument types, as in the parameter-type-lists of |
| 902 | /// function types (C++ [temp.deduct.type]p10). |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 903 | /// |
| 904 | /// \param S The semantic analysis object within which we are deducing |
| 905 | /// |
| 906 | /// \param TemplateParams The template parameters that we are deducing |
| 907 | /// |
| 908 | /// \param Params The list of parameter types |
| 909 | /// |
| 910 | /// \param NumParams The number of types in \c Params |
| 911 | /// |
| 912 | /// \param Args The list of argument types |
| 913 | /// |
| 914 | /// \param NumArgs The number of types in \c Args |
| 915 | /// |
| 916 | /// \param Info information about the template argument deduction itself |
| 917 | /// |
| 918 | /// \param Deduced the deduced template arguments |
| 919 | /// |
| 920 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
| 921 | /// how template argument deduction is performed. |
| 922 | /// |
| Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 923 | /// \param PartialOrdering If true, we are performing template argument |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 924 | /// deduction for during partial ordering for a call |
| Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 925 | /// (C++0x [temp.deduct.partial]). |
| 926 | /// |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 927 | /// \returns the result of template argument deduction so far. Note that a |
| 928 | /// "success" result means that template argument deduction has not yet failed, |
| 929 | /// but it may still fail, later, for other reasons. |
| 930 | static Sema::TemplateDeductionResult |
| 931 | DeduceTemplateArguments(Sema &S, |
| 932 | TemplateParameterList *TemplateParams, |
| 933 | const QualType *Params, unsigned NumParams, |
| 934 | const QualType *Args, unsigned NumArgs, |
| 935 | TemplateDeductionInfo &Info, |
| Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 936 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 937 | unsigned TDF, |
| Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 938 | bool PartialOrdering = false) { |
| Douglas Gregor | 86bea35 | 2011-01-05 23:23:17 +0000 | [diff] [blame] | 939 | // Fast-path check to see if we have too many/too few arguments. |
| 940 | if (NumParams != NumArgs && |
| 941 | !(NumParams && isa<PackExpansionType>(Params[NumParams - 1])) && |
| 942 | !(NumArgs && isa<PackExpansionType>(Args[NumArgs - 1]))) |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 943 | return Sema::TDK_MiscellaneousDeductionFailure; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 944 | |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 945 | // C++0x [temp.deduct.type]p10: |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 946 | // Similarly, if P has a form that contains (T), then each parameter type |
| 947 | // Pi of the respective parameter-type- list of P is compared with the |
| 948 | // corresponding parameter type Ai of the corresponding parameter-type-list |
| 949 | // of A. [...] |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 950 | unsigned ArgIdx = 0, ParamIdx = 0; |
| 951 | for (; ParamIdx != NumParams; ++ParamIdx) { |
| 952 | // Check argument types. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 953 | const PackExpansionType *Expansion |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 954 | = dyn_cast<PackExpansionType>(Params[ParamIdx]); |
| 955 | if (!Expansion) { |
| 956 | // Simple case: compare the parameter and argument types at this point. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 957 | |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 958 | // Make sure we have an argument. |
| 959 | if (ArgIdx >= NumArgs) |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 960 | return Sema::TDK_MiscellaneousDeductionFailure; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 961 | |
| Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 962 | if (isa<PackExpansionType>(Args[ArgIdx])) { |
| 963 | // C++0x [temp.deduct.type]p22: |
| 964 | // If the original function parameter associated with A is a function |
| 965 | // parameter pack and the function parameter associated with P is not |
| 966 | // a function parameter pack, then template argument deduction fails. |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 967 | return Sema::TDK_MiscellaneousDeductionFailure; |
| Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 968 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 969 | |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 970 | if (Sema::TemplateDeductionResult Result |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 971 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 972 | Params[ParamIdx], Args[ArgIdx], |
| 973 | Info, Deduced, TDF, |
| Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 974 | PartialOrdering)) |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 975 | return Result; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 976 | |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 977 | ++ArgIdx; |
| 978 | continue; |
| 979 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 980 | |
| Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 981 | // C++0x [temp.deduct.type]p5: |
| 982 | // The non-deduced contexts are: |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 983 | // - 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] | 984 | // parameter-declaration-clause. |
| 985 | if (ParamIdx + 1 < NumParams) |
| 986 | return Sema::TDK_Success; |
| 987 | |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 988 | // C++0x [temp.deduct.type]p10: |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 989 | // If the parameter-declaration corresponding to Pi is a function |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 990 | // parameter pack, then the type of its declarator- id is compared with |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 991 | // each remaining parameter type in the parameter-type-list of A. Each |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 992 | // comparison deduces template arguments for subsequent positions in the |
| 993 | // template parameter packs expanded by the function parameter pack. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 994 | |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 995 | QualType Pattern = Expansion->getPattern(); |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 996 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 997 | |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 998 | for (; ArgIdx < NumArgs; ++ArgIdx) { |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 999 | // Deduce template arguments from the pattern. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1000 | if (Sema::TemplateDeductionResult Result |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1001 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, Pattern, |
| 1002 | Args[ArgIdx], Info, Deduced, |
| Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1003 | TDF, PartialOrdering)) |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1004 | return Result; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1005 | |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1006 | PackScope.nextPackElement(); |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1007 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1008 | |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1009 | // Build argument packs for each of the parameter packs expanded by this |
| 1010 | // pack expansion. |
| Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 1011 | if (auto Result = PackScope.finish()) |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1012 | return Result; |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1013 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1014 | |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1015 | // Make sure we don't have any extra arguments. |
| 1016 | if (ArgIdx < NumArgs) |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 1017 | return Sema::TDK_MiscellaneousDeductionFailure; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1018 | |
| Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 1019 | return Sema::TDK_Success; |
| 1020 | } |
| 1021 | |
| Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1022 | /// \brief Determine whether the parameter has qualifiers that are either |
| 1023 | /// inconsistent with or a superset of the argument's qualifiers. |
| 1024 | static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType, |
| 1025 | QualType ArgType) { |
| 1026 | Qualifiers ParamQs = ParamType.getQualifiers(); |
| 1027 | Qualifiers ArgQs = ArgType.getQualifiers(); |
| 1028 | |
| 1029 | if (ParamQs == ArgQs) |
| 1030 | return false; |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1031 | |
| Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1032 | // Mismatched (but not missing) Objective-C GC attributes. |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1033 | if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() && |
| Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1034 | ParamQs.hasObjCGCAttr()) |
| 1035 | return true; |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1036 | |
| Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1037 | // Mismatched (but not missing) address spaces. |
| 1038 | if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() && |
| 1039 | ParamQs.hasAddressSpace()) |
| 1040 | return true; |
| 1041 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1042 | // Mismatched (but not missing) Objective-C lifetime qualifiers. |
| 1043 | if (ParamQs.getObjCLifetime() != ArgQs.getObjCLifetime() && |
| 1044 | ParamQs.hasObjCLifetime()) |
| 1045 | return true; |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1046 | |
| Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1047 | // CVR qualifier superset. |
| 1048 | return (ParamQs.getCVRQualifiers() != ArgQs.getCVRQualifiers()) && |
| 1049 | ((ParamQs.getCVRQualifiers() | ArgQs.getCVRQualifiers()) |
| 1050 | == ParamQs.getCVRQualifiers()); |
| 1051 | } |
| 1052 | |
| Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1053 | /// \brief Compare types for equality with respect to possibly compatible |
| 1054 | /// function types (noreturn adjustment, implicit calling conventions). If any |
| 1055 | /// of parameter and argument is not a function, just perform type comparison. |
| 1056 | /// |
| 1057 | /// \param Param the template parameter type. |
| 1058 | /// |
| 1059 | /// \param Arg the argument type. |
| 1060 | bool Sema::isSameOrCompatibleFunctionType(CanQualType Param, |
| 1061 | CanQualType Arg) { |
| 1062 | const FunctionType *ParamFunction = Param->getAs<FunctionType>(), |
| 1063 | *ArgFunction = Arg->getAs<FunctionType>(); |
| 1064 | |
| 1065 | // Just compare if not functions. |
| 1066 | if (!ParamFunction || !ArgFunction) |
| 1067 | return Param == Arg; |
| 1068 | |
| Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 1069 | // Noreturn and noexcept adjustment. |
| Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1070 | QualType AdjustedParam; |
| Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 1071 | if (IsFunctionConversion(Param, Arg, AdjustedParam)) |
| Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1072 | return Arg == Context.getCanonicalType(AdjustedParam); |
| 1073 | |
| 1074 | // FIXME: Compatible calling conventions. |
| 1075 | |
| 1076 | return Param == Arg; |
| 1077 | } |
| 1078 | |
| Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1079 | /// Get the index of the first template parameter that was originally from the |
| 1080 | /// innermost template-parameter-list. This is 0 except when we concatenate |
| 1081 | /// the template parameter lists of a class template and a constructor template |
| 1082 | /// when forming an implicit deduction guide. |
| 1083 | static unsigned getFirstInnerIndex(FunctionTemplateDecl *FTD) { |
| Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1084 | auto *Guide = dyn_cast<CXXDeductionGuideDecl>(FTD->getTemplatedDecl()); |
| 1085 | if (!Guide || !Guide->isImplicit()) |
| Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1086 | return 0; |
| Richard Smith | bc49120 | 2017-02-17 20:05:37 +0000 | [diff] [blame] | 1087 | return Guide->getDeducedTemplate()->getTemplateParameters()->size(); |
| Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | /// Determine whether a type denotes a forwarding reference. |
| 1091 | static bool isForwardingReference(QualType Param, unsigned FirstInnerIndex) { |
| 1092 | // C++1z [temp.deduct.call]p3: |
| 1093 | // A forwarding reference is an rvalue reference to a cv-unqualified |
| 1094 | // template parameter that does not represent a template parameter of a |
| 1095 | // class template. |
| 1096 | if (auto *ParamRef = Param->getAs<RValueReferenceType>()) { |
| 1097 | if (ParamRef->getPointeeType().getQualifiers()) |
| 1098 | return false; |
| 1099 | auto *TypeParm = ParamRef->getPointeeType()->getAs<TemplateTypeParmType>(); |
| 1100 | return TypeParm && TypeParm->getIndex() >= FirstInnerIndex; |
| 1101 | } |
| 1102 | return false; |
| 1103 | } |
| 1104 | |
| Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1105 | /// \brief Deduce the template arguments by comparing the parameter type and |
| 1106 | /// the argument type (C++ [temp.deduct.type]). |
| 1107 | /// |
| Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1108 | /// \param S the semantic analysis object within which we are deducing |
| Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1109 | /// |
| 1110 | /// \param TemplateParams the template parameters that we are deducing |
| 1111 | /// |
| 1112 | /// \param ParamIn the parameter type |
| 1113 | /// |
| 1114 | /// \param ArgIn the argument type |
| 1115 | /// |
| 1116 | /// \param Info information about the template argument deduction itself |
| 1117 | /// |
| 1118 | /// \param Deduced the deduced template arguments |
| 1119 | /// |
| Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1120 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1121 | /// how template argument deduction is performed. |
| Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1122 | /// |
| Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1123 | /// \param PartialOrdering Whether we're performing template argument deduction |
| 1124 | /// in the context of partial ordering (C++0x [temp.deduct.partial]). |
| 1125 | /// |
| Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1126 | /// \returns the result of template argument deduction so far. Note that a |
| 1127 | /// "success" result means that template argument deduction has not yet failed, |
| 1128 | /// but it may still fail, later, for other reasons. |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1129 | static Sema::TemplateDeductionResult |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1130 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 1131 | TemplateParameterList *TemplateParams, |
| 1132 | QualType ParamIn, QualType ArgIn, |
| 1133 | TemplateDeductionInfo &Info, |
| 1134 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 1135 | unsigned TDF, |
| Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1136 | bool PartialOrdering, |
| 1137 | bool DeducedFromArrayBound) { |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1138 | // We only want to look at the canonical types, since typedefs and |
| 1139 | // sugar are not part of template argument deduction. |
| Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1140 | QualType Param = S.Context.getCanonicalType(ParamIn); |
| 1141 | QualType Arg = S.Context.getCanonicalType(ArgIn); |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1142 | |
| Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1143 | // If the argument type is a pack expansion, look at its pattern. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1144 | // This isn't explicitly called out |
| Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1145 | if (const PackExpansionType *ArgExpansion |
| 1146 | = dyn_cast<PackExpansionType>(Arg)) |
| 1147 | Arg = ArgExpansion->getPattern(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1148 | |
| Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1149 | if (PartialOrdering) { |
| Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1150 | // C++11 [temp.deduct.partial]p5: |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1151 | // Before the partial ordering is done, certain transformations are |
| 1152 | // performed on the types used for partial ordering: |
| 1153 | // - 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] | 1154 | const ReferenceType *ParamRef = Param->getAs<ReferenceType>(); |
| 1155 | if (ParamRef) |
| 1156 | Param = ParamRef->getPointeeType(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1157 | |
| Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1158 | // - If A is a reference type, A is replaced by the type referred to. |
| 1159 | const ReferenceType *ArgRef = Arg->getAs<ReferenceType>(); |
| 1160 | if (ArgRef) |
| 1161 | Arg = ArgRef->getPointeeType(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1162 | |
| Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1163 | if (ParamRef && ArgRef && S.Context.hasSameUnqualifiedType(Param, Arg)) { |
| 1164 | // C++11 [temp.deduct.partial]p9: |
| 1165 | // If, for a given type, deduction succeeds in both directions (i.e., |
| 1166 | // the types are identical after the transformations above) and both |
| 1167 | // P and A were reference types [...]: |
| 1168 | // - if [one type] was an lvalue reference and [the other type] was |
| 1169 | // not, [the other type] is not considered to be at least as |
| 1170 | // specialized as [the first type] |
| 1171 | // - if [one type] is more cv-qualified than [the other type], |
| 1172 | // [the other type] is not considered to be at least as specialized |
| 1173 | // as [the first type] |
| 1174 | // Objective-C ARC adds: |
| 1175 | // - [one type] has non-trivial lifetime, [the other type] has |
| 1176 | // __unsafe_unretained lifetime, and the types are otherwise |
| 1177 | // identical |
| Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1178 | // |
| Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1179 | // A is "considered to be at least as specialized" as P iff deduction |
| 1180 | // succeeds, so we model this as a deduction failure. Note that |
| 1181 | // [the first type] is P and [the other type] is A here; the standard |
| 1182 | // gets this backwards. |
| Douglas Gregor | 85894a8 | 2011-04-30 17:07:52 +0000 | [diff] [blame] | 1183 | Qualifiers ParamQuals = Param.getQualifiers(); |
| 1184 | Qualifiers ArgQuals = Arg.getQualifiers(); |
| Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1185 | if ((ParamRef->isLValueReferenceType() && |
| 1186 | !ArgRef->isLValueReferenceType()) || |
| 1187 | ParamQuals.isStrictSupersetOf(ArgQuals) || |
| 1188 | (ParamQuals.hasNonTrivialObjCLifetime() && |
| 1189 | ArgQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone && |
| 1190 | ParamQuals.withoutObjCLifetime() == |
| 1191 | ArgQuals.withoutObjCLifetime())) { |
| 1192 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1193 | Info.SecondArg = TemplateArgument(ArgIn); |
| 1194 | return Sema::TDK_NonDeducedMismatch; |
| Douglas Gregor | 6beabee | 2014-01-02 19:42:02 +0000 | [diff] [blame] | 1195 | } |
| Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1196 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1197 | |
| Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1198 | // C++11 [temp.deduct.partial]p7: |
| Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1199 | // Remove any top-level cv-qualifiers: |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1200 | // - 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] | 1201 | // version of P. |
| 1202 | Param = Param.getUnqualifiedType(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1203 | // - 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] | 1204 | // version of A. |
| 1205 | Arg = Arg.getUnqualifiedType(); |
| 1206 | } else { |
| 1207 | // C++0x [temp.deduct.call]p4 bullet 1: |
| 1208 | // - If the original P is a reference type, the deduced A (i.e., the type |
| 1209 | // referred to by the reference) can be more cv-qualified than the |
| 1210 | // transformed A. |
| 1211 | if (TDF & TDF_ParamWithReferenceType) { |
| 1212 | Qualifiers Quals; |
| 1213 | QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals); |
| 1214 | Quals.setCVRQualifiers(Quals.getCVRQualifiers() & |
| John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 1215 | Arg.getCVRQualifiers()); |
| Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1216 | Param = S.Context.getQualifiedType(UnqualParam, Quals); |
| 1217 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1218 | |
| Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1219 | if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) { |
| 1220 | // C++0x [temp.deduct.type]p10: |
| 1221 | // If P and A are function types that originated from deduction when |
| 1222 | // taking the address of a function template (14.8.2.2) or when deducing |
| 1223 | // 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] | 1224 | // Ai are parameters of the top-level parameter-type-list of P and A, |
| Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1225 | // respectively, Pi is adjusted if it is a forwarding reference and Ai |
| 1226 | // is an lvalue reference, in |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1227 | // 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] | 1228 | // type (i.e., T&& is changed to simply T). [ Note: As a result, when |
| 1229 | // 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] | 1230 | // deduced as X&. - end note ] |
| Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1231 | TDF &= ~TDF_TopLevelParameterTypeList; |
| Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 1232 | if (isForwardingReference(Param, 0) && Arg->isLValueReferenceType()) |
| 1233 | Param = Param->getPointeeType(); |
| Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1234 | } |
| Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1235 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1236 | |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1237 | // C++ [temp.deduct.type]p9: |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1238 | // A template type argument T, a template template argument TT or a |
| 1239 | // 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] | 1240 | // the following forms: |
| 1241 | // |
| 1242 | // T |
| 1243 | // cv-list T |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1244 | if (const TemplateTypeParmType *TemplateTypeParm |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1245 | = Param->getAs<TemplateTypeParmType>()) { |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1246 | // Just skip any attempts to deduce from a placeholder type or a parameter |
| 1247 | // at a different depth. |
| 1248 | if (Arg->isPlaceholderType() || |
| 1249 | Info.getDeducedDepth() != TemplateTypeParm->getDepth()) |
| Douglas Gregor | 4ea5dec | 2011-09-22 15:57:07 +0000 | [diff] [blame] | 1250 | return Sema::TDK_Success; |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1251 | |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1252 | unsigned Index = TemplateTypeParm->getIndex(); |
| Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1253 | bool RecanonicalizeArg = false; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1254 | |
| Douglas Gregor | 6045482 | 2009-07-22 20:02:25 +0000 | [diff] [blame] | 1255 | // If the argument type is an array type, move the qualifiers up to the |
| 1256 | // 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] | 1257 | if (isa<ArrayType>(Arg)) { |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1258 | Qualifiers Quals; |
| Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1259 | Arg = S.Context.getUnqualifiedArrayType(Arg, Quals); |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1260 | if (Quals) { |
| Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1261 | Arg = S.Context.getQualifiedType(Arg, Quals); |
| Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1262 | RecanonicalizeArg = true; |
| 1263 | } |
| 1264 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1266 | // The argument type can not be less qualified than the parameter |
| 1267 | // type. |
| Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1268 | if (!(TDF & TDF_IgnoreQualifiers) && |
| 1269 | hasInconsistentOrSupersetQualifiersOf(Param, Arg)) { |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1270 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1271 | Info.FirstArg = TemplateArgument(Param); |
| John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1272 | Info.SecondArg = TemplateArgument(Arg); |
| John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1273 | return Sema::TDK_Underqualified; |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1274 | } |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1275 | |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1276 | assert(TemplateTypeParm->getDepth() == Info.getDeducedDepth() && |
| 1277 | "saw template type parameter with wrong depth"); |
| Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1278 | assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function"); |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1279 | QualType DeducedType = Arg; |
| John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1280 | |
| Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1281 | // Remove any qualifiers on the parameter from the deduced type. |
| 1282 | // We checked the qualifiers for consistency above. |
| 1283 | Qualifiers DeducedQs = DeducedType.getQualifiers(); |
| 1284 | Qualifiers ParamQs = Param.getQualifiers(); |
| 1285 | DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers()); |
| 1286 | if (ParamQs.hasObjCGCAttr()) |
| 1287 | DeducedQs.removeObjCGCAttr(); |
| 1288 | if (ParamQs.hasAddressSpace()) |
| 1289 | DeducedQs.removeAddressSpace(); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1290 | if (ParamQs.hasObjCLifetime()) |
| 1291 | DeducedQs.removeObjCLifetime(); |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1292 | |
| Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1293 | // Objective-C ARC: |
| Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1294 | // If template deduction would produce a lifetime qualifier on a type |
| 1295 | // that is not a lifetime type, template argument deduction fails. |
| 1296 | if (ParamQs.hasObjCLifetime() && !DeducedType->isObjCLifetimeType() && |
| 1297 | !DeducedType->isDependentType()) { |
| 1298 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1299 | Info.FirstArg = TemplateArgument(Param); |
| 1300 | Info.SecondArg = TemplateArgument(Arg); |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1301 | return Sema::TDK_Underqualified; |
| Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1302 | } |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1303 | |
| Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1304 | // Objective-C ARC: |
| Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1305 | // If template deduction would produce an argument type with lifetime type |
| 1306 | // but no lifetime qualifier, the __strong lifetime qualifier is inferred. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1307 | if (S.getLangOpts().ObjCAutoRefCount && |
| Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1308 | DeducedType->isObjCLifetimeType() && |
| 1309 | !DeducedQs.hasObjCLifetime()) |
| 1310 | DeducedQs.setObjCLifetime(Qualifiers::OCL_Strong); |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1311 | |
| Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1312 | DeducedType = S.Context.getQualifiedType(DeducedType.getUnqualifiedType(), |
| 1313 | DeducedQs); |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1314 | |
| Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1315 | if (RecanonicalizeArg) |
| Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1316 | DeducedType = S.Context.getCanonicalType(DeducedType); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1317 | |
| Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1318 | DeducedTemplateArgument NewDeduced(DeducedType, DeducedFromArrayBound); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1319 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1320 | Deduced[Index], |
| 1321 | NewDeduced); |
| 1322 | if (Result.isNull()) { |
| 1323 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1324 | Info.FirstArg = Deduced[Index]; |
| 1325 | Info.SecondArg = NewDeduced; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1326 | return Sema::TDK_Inconsistent; |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1327 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1328 | |
| Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1329 | Deduced[Index] = Result; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1330 | return Sema::TDK_Success; |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1333 | // Set up the template argument deduction information for a failure. |
| John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1334 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1335 | Info.SecondArg = TemplateArgument(ArgIn); |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1336 | |
| Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 1337 | // If the parameter is an already-substituted template parameter |
| 1338 | // pack, do nothing: we don't know which of its arguments to look |
| 1339 | // at, so we have to wait until all of the parameter packs in this |
| 1340 | // expansion have arguments. |
| 1341 | if (isa<SubstTemplateTypeParmPackType>(Param)) |
| 1342 | return Sema::TDK_Success; |
| 1343 | |
| Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1344 | // Check the cv-qualifiers on the parameter and argument types. |
| Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1345 | CanQualType CanParam = S.Context.getCanonicalType(Param); |
| 1346 | CanQualType CanArg = S.Context.getCanonicalType(Arg); |
| Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1347 | if (!(TDF & TDF_IgnoreQualifiers)) { |
| 1348 | if (TDF & TDF_ParamWithReferenceType) { |
| Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1349 | if (hasInconsistentOrSupersetQualifiersOf(Param, Arg)) |
| Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1350 | return Sema::TDK_NonDeducedMismatch; |
| John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 1351 | } else if (!IsPossiblyOpaquelyQualifiedType(Param)) { |
| Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1352 | if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1353 | return Sema::TDK_NonDeducedMismatch; |
| Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1354 | } |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1355 | |
| Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1356 | // If the parameter type is not dependent, there is nothing to deduce. |
| 1357 | if (!Param->isDependentType()) { |
| Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1358 | if (!(TDF & TDF_SkipNonDependent)) { |
| Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 1359 | bool NonDeduced = |
| 1360 | (TDF & TDF_AllowCompatibleFunctionType) |
| 1361 | ? !S.isSameOrCompatibleFunctionType(CanParam, CanArg) |
| 1362 | : Param != Arg; |
| Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1363 | if (NonDeduced) { |
| 1364 | return Sema::TDK_NonDeducedMismatch; |
| 1365 | } |
| 1366 | } |
| Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1367 | return Sema::TDK_Success; |
| 1368 | } |
| Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1369 | } else if (!Param->isDependentType()) { |
| 1370 | CanQualType ParamUnqualType = CanParam.getUnqualifiedType(), |
| 1371 | ArgUnqualType = CanArg.getUnqualifiedType(); |
| Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 1372 | bool Success = |
| 1373 | (TDF & TDF_AllowCompatibleFunctionType) |
| 1374 | ? S.isSameOrCompatibleFunctionType(ParamUnqualType, ArgUnqualType) |
| 1375 | : ParamUnqualType == ArgUnqualType; |
| Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1376 | if (Success) |
| 1377 | return Sema::TDK_Success; |
| Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1378 | } |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1379 | |
| Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1380 | switch (Param->getTypeClass()) { |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1381 | // Non-canonical types cannot appear here. |
| 1382 | #define NON_CANONICAL_TYPE(Class, Base) \ |
| 1383 | case Type::Class: llvm_unreachable("deducing non-canonical type: " #Class); |
| 1384 | #define TYPE(Class, Base) |
| 1385 | #include "clang/AST/TypeNodes.def" |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1386 | |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1387 | case Type::TemplateTypeParm: |
| 1388 | case Type::SubstTemplateTypeParmPack: |
| 1389 | llvm_unreachable("Type nodes handled above"); |
| Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1390 | |
| 1391 | // These types cannot be dependent, so simply check whether the types are |
| 1392 | // the same. |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1393 | case Type::Builtin: |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1394 | case Type::VariableArray: |
| 1395 | case Type::Vector: |
| 1396 | case Type::FunctionNoProto: |
| 1397 | case Type::Record: |
| 1398 | case Type::Enum: |
| 1399 | case Type::ObjCObject: |
| 1400 | case Type::ObjCInterface: |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 1401 | case Type::ObjCObjectPointer: |
| Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1402 | if (TDF & TDF_SkipNonDependent) |
| 1403 | return Sema::TDK_Success; |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1404 | |
| Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1405 | if (TDF & TDF_IgnoreQualifiers) { |
| 1406 | Param = Param.getUnqualifiedType(); |
| 1407 | Arg = Arg.getUnqualifiedType(); |
| 1408 | } |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1409 | |
| Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1410 | return Param == Arg? Sema::TDK_Success : Sema::TDK_NonDeducedMismatch; |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1411 | |
| 1412 | // _Complex T [placeholder extension] |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1413 | case Type::Complex: |
| 1414 | if (const ComplexType *ComplexArg = Arg->getAs<ComplexType>()) |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1415 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1416 | cast<ComplexType>(Param)->getElementType(), |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1417 | ComplexArg->getElementType(), |
| 1418 | Info, Deduced, TDF); |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1419 | |
| 1420 | return Sema::TDK_NonDeducedMismatch; |
| Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1421 | |
| 1422 | // _Atomic T [extension] |
| 1423 | case Type::Atomic: |
| 1424 | if (const AtomicType *AtomicArg = Arg->getAs<AtomicType>()) |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1425 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1426 | cast<AtomicType>(Param)->getValueType(), |
| 1427 | AtomicArg->getValueType(), |
| 1428 | Info, Deduced, TDF); |
| 1429 | |
| 1430 | return Sema::TDK_NonDeducedMismatch; |
| 1431 | |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1432 | // T * |
| Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1433 | case Type::Pointer: { |
| John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1434 | QualType PointeeType; |
| 1435 | if (const PointerType *PointerArg = Arg->getAs<PointerType>()) { |
| 1436 | PointeeType = PointerArg->getPointeeType(); |
| 1437 | } else if (const ObjCObjectPointerType *PointerArg |
| 1438 | = Arg->getAs<ObjCObjectPointerType>()) { |
| 1439 | PointeeType = PointerArg->getPointeeType(); |
| 1440 | } else { |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1441 | return Sema::TDK_NonDeducedMismatch; |
| John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1442 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1443 | |
| Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1444 | unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1445 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1446 | cast<PointerType>(Param)->getPointeeType(), |
| John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1447 | PointeeType, |
| Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1448 | Info, Deduced, SubTDF); |
| Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1449 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1450 | |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1451 | // T & |
| Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1452 | case Type::LValueReference: { |
| Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 1453 | const LValueReferenceType *ReferenceArg = |
| 1454 | Arg->getAs<LValueReferenceType>(); |
| Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1455 | if (!ReferenceArg) |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1456 | return Sema::TDK_NonDeducedMismatch; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1457 | |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1458 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1459 | cast<LValueReferenceType>(Param)->getPointeeType(), |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1460 | ReferenceArg->getPointeeType(), Info, Deduced, 0); |
| Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1461 | } |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1462 | |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1463 | // T && [C++0x] |
| Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1464 | case Type::RValueReference: { |
| Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 1465 | const RValueReferenceType *ReferenceArg = |
| 1466 | Arg->getAs<RValueReferenceType>(); |
| Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1467 | if (!ReferenceArg) |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1468 | return Sema::TDK_NonDeducedMismatch; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1469 | |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1470 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1471 | cast<RValueReferenceType>(Param)->getPointeeType(), |
| 1472 | ReferenceArg->getPointeeType(), |
| 1473 | Info, Deduced, 0); |
| Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1474 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1475 | |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1476 | // T [] (implied, but not stated explicitly) |
| Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1477 | case Type::IncompleteArray: { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1478 | const IncompleteArrayType *IncompleteArrayArg = |
| Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1479 | S.Context.getAsIncompleteArrayType(Arg); |
| Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1480 | if (!IncompleteArrayArg) |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1481 | return Sema::TDK_NonDeducedMismatch; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | |
| John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1483 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1484 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1485 | S.Context.getAsIncompleteArrayType(Param)->getElementType(), |
| 1486 | IncompleteArrayArg->getElementType(), |
| 1487 | Info, Deduced, SubTDF); |
| Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1488 | } |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1489 | |
| 1490 | // T [integer-constant] |
| Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1491 | case Type::ConstantArray: { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1492 | const ConstantArrayType *ConstantArrayArg = |
| Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1493 | S.Context.getAsConstantArrayType(Arg); |
| Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1494 | if (!ConstantArrayArg) |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1495 | return Sema::TDK_NonDeducedMismatch; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1496 | |
| 1497 | const ConstantArrayType *ConstantArrayParm = |
| Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1498 | S.Context.getAsConstantArrayType(Param); |
| Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1499 | if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1500 | return Sema::TDK_NonDeducedMismatch; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | |
| John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1502 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1503 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1504 | ConstantArrayParm->getElementType(), |
| 1505 | ConstantArrayArg->getElementType(), |
| 1506 | Info, Deduced, SubTDF); |
| Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1509 | // type [i] |
| 1510 | case Type::DependentSizedArray: { |
| Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1511 | const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg); |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1512 | if (!ArrayArg) |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1513 | return Sema::TDK_NonDeducedMismatch; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1514 | |
| John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1515 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
| 1516 | |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1517 | // Check the element type of the arrays |
| 1518 | const DependentSizedArrayType *DependentArrayParm |
| Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1519 | = S.Context.getAsDependentSizedArrayType(Param); |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1520 | if (Sema::TemplateDeductionResult Result |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1521 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1522 | DependentArrayParm->getElementType(), |
| 1523 | ArrayArg->getElementType(), |
| 1524 | Info, Deduced, SubTDF)) |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1525 | return Result; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1526 | |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1527 | // Determine the array bound is something we can deduce. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1528 | NonTypeTemplateParmDecl *NTTP |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1529 | = getDeducedParameterFromExpr(Info, DependentArrayParm->getSizeExpr()); |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1530 | if (!NTTP) |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1531 | return Sema::TDK_Success; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | |
| 1533 | // We can perform template argument deduction for the given non-type |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1534 | // template parameter. |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1535 | assert(NTTP->getDepth() == Info.getDeducedDepth() && |
| 1536 | "saw non-type template parameter with wrong depth"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1537 | if (const ConstantArrayType *ConstantArrayArg |
| Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1538 | = dyn_cast<ConstantArrayType>(ArrayArg)) { |
| 1539 | llvm::APSInt Size(ConstantArrayArg->getSize()); |
| Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1540 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, Size, |
| Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1541 | S.Context.getSizeType(), |
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1542 | /*ArrayBound=*/true, |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1543 | Info, Deduced); |
| Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1544 | } |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1545 | if (const DependentSizedArrayType *DependentArrayArg |
| 1546 | = dyn_cast<DependentSizedArrayType>(ArrayArg)) |
| Douglas Gregor | 7a49ead | 2010-12-22 23:15:38 +0000 | [diff] [blame] | 1547 | if (DependentArrayArg->getSizeExpr()) |
| Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1548 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| Douglas Gregor | 7a49ead | 2010-12-22 23:15:38 +0000 | [diff] [blame] | 1549 | DependentArrayArg->getSizeExpr(), |
| 1550 | Info, Deduced); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1551 | |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1552 | // Incomplete type does not match a dependently-sized array type |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1553 | return Sema::TDK_NonDeducedMismatch; |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1554 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | |
| 1556 | // type(*)(T) |
| 1557 | // T(*)() |
| 1558 | // T(*)(T) |
| Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1559 | case Type::FunctionProto: { |
| Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1560 | unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1561 | const FunctionProtoType *FunctionProtoArg = |
| Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1562 | dyn_cast<FunctionProtoType>(Arg); |
| 1563 | if (!FunctionProtoArg) |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1564 | return Sema::TDK_NonDeducedMismatch; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1565 | |
| 1566 | const FunctionProtoType *FunctionProtoParam = |
| Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1567 | cast<FunctionProtoType>(Param); |
| Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1568 | |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1569 | if (FunctionProtoParam->getTypeQuals() |
| Douglas Gregor | 54e462a | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1570 | != FunctionProtoArg->getTypeQuals() || |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1571 | FunctionProtoParam->getRefQualifier() |
| Douglas Gregor | 54e462a | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1572 | != FunctionProtoArg->getRefQualifier() || |
| 1573 | FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1574 | return Sema::TDK_NonDeducedMismatch; |
| Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1575 | |
| Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1576 | // Check return types. |
| Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 1577 | if (auto Result = DeduceTemplateArgumentsByTypeMatch( |
| 1578 | S, TemplateParams, FunctionProtoParam->getReturnType(), |
| 1579 | FunctionProtoArg->getReturnType(), Info, Deduced, 0)) |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1580 | return Result; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1581 | |
| Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 1582 | // Check parameter types. |
| 1583 | if (auto Result = DeduceTemplateArguments( |
| 1584 | S, TemplateParams, FunctionProtoParam->param_type_begin(), |
| 1585 | FunctionProtoParam->getNumParams(), |
| 1586 | FunctionProtoArg->param_type_begin(), |
| 1587 | FunctionProtoArg->getNumParams(), Info, Deduced, SubTDF)) |
| 1588 | return Result; |
| 1589 | |
| 1590 | if (TDF & TDF_AllowCompatibleFunctionType) |
| 1591 | return Sema::TDK_Success; |
| 1592 | |
| 1593 | // FIXME: Per core-2016/10/1019 (no corresponding core issue yet), permit |
| 1594 | // deducing through the noexcept-specifier if it's part of the canonical |
| 1595 | // type. libstdc++ relies on this. |
| 1596 | Expr *NoexceptExpr = FunctionProtoParam->getNoexceptExpr(); |
| 1597 | if (NonTypeTemplateParmDecl *NTTP = |
| 1598 | NoexceptExpr ? getDeducedParameterFromExpr(Info, NoexceptExpr) |
| 1599 | : nullptr) { |
| 1600 | assert(NTTP->getDepth() == Info.getDeducedDepth() && |
| 1601 | "saw non-type template parameter with wrong depth"); |
| 1602 | |
| 1603 | llvm::APSInt Noexcept(1); |
| 1604 | switch (FunctionProtoArg->canThrow(S.Context)) { |
| 1605 | case CT_Cannot: |
| 1606 | Noexcept = 1; |
| 1607 | LLVM_FALLTHROUGH; |
| 1608 | |
| 1609 | case CT_Can: |
| 1610 | // We give E in noexcept(E) the "deduced from array bound" treatment. |
| 1611 | // FIXME: Should we? |
| 1612 | return DeduceNonTypeTemplateArgument( |
| 1613 | S, TemplateParams, NTTP, Noexcept, S.Context.BoolTy, |
| 1614 | /*ArrayBound*/true, Info, Deduced); |
| 1615 | |
| 1616 | case CT_Dependent: |
| 1617 | if (Expr *ArgNoexceptExpr = FunctionProtoArg->getNoexceptExpr()) |
| 1618 | return DeduceNonTypeTemplateArgument( |
| 1619 | S, TemplateParams, NTTP, ArgNoexceptExpr, Info, Deduced); |
| 1620 | // Can't deduce anything from throw(T...). |
| 1621 | break; |
| 1622 | } |
| 1623 | } |
| 1624 | // FIXME: Detect non-deduced exception specification mismatches? |
| 1625 | |
| 1626 | return Sema::TDK_Success; |
| Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1627 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1628 | |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 1629 | case Type::InjectedClassName: |
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1630 | // Treat a template's injected-class-name as if the template |
| 1631 | // specialization type had been used. |
| John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1632 | Param = cast<InjectedClassNameType>(Param) |
| 1633 | ->getInjectedSpecializationType(); |
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1634 | assert(isa<TemplateSpecializationType>(Param) && |
| 1635 | "injected class name is not a template specialization type"); |
| Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 1636 | LLVM_FALLTHROUGH; |
| John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1637 | |
| Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1638 | // template-name<T> (where template-name refers to a class template) |
| Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1639 | // template-name<i> |
| Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1640 | // TT<T> |
| 1641 | // TT<i> |
| 1642 | // TT<> |
| Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1643 | case Type::TemplateSpecialization: { |
| Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1644 | const TemplateSpecializationType *SpecParam = |
| 1645 | cast<TemplateSpecializationType>(Param); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1646 | |
| Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1647 | // When Arg cannot be a derived class, we can just try to deduce template |
| 1648 | // arguments from the template-id. |
| 1649 | const RecordType *RecordT = Arg->getAs<RecordType>(); |
| 1650 | if (!(TDF & TDF_DerivedClass) || !RecordT) |
| 1651 | return DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg, Info, |
| 1652 | Deduced); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1653 | |
| Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1654 | SmallVector<DeducedTemplateArgument, 8> DeducedOrig(Deduced.begin(), |
| 1655 | Deduced.end()); |
| Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1656 | |
| Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1657 | Sema::TemplateDeductionResult Result = DeduceTemplateArguments( |
| 1658 | S, TemplateParams, SpecParam, Arg, Info, Deduced); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1659 | |
| Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1660 | if (Result == Sema::TDK_Success) |
| 1661 | return Result; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1662 | |
| Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1663 | // We cannot inspect base classes as part of deduction when the type |
| 1664 | // is incomplete, so either instantiate any templates necessary to |
| 1665 | // complete the type, or skip over it if it cannot be completed. |
| 1666 | if (!S.isCompleteType(Info.getLocation(), Arg)) |
| 1667 | return Result; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1668 | |
| Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1669 | // C++14 [temp.deduct.call] p4b3: |
| 1670 | // If P is a class and P has the form simple-template-id, then the |
| 1671 | // transformed A can be a derived class of the deduced A. Likewise if |
| 1672 | // P is a pointer to a class of the form simple-template-id, the |
| 1673 | // transformed A can be a pointer to a derived class pointed to by the |
| 1674 | // deduced A. |
| 1675 | // |
| 1676 | // These alternatives are considered only if type deduction would |
| 1677 | // otherwise fail. If they yield more than one possible deduced A, the |
| 1678 | // type deduction fails. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1679 | |
| Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1680 | // Reset the incorrectly deduced argument from above. |
| 1681 | Deduced = DeducedOrig; |
| 1682 | |
| 1683 | // Use data recursion to crawl through the list of base classes. |
| 1684 | // Visited contains the set of nodes we have already visited, while |
| 1685 | // ToVisit is our stack of records that we still need to visit. |
| 1686 | llvm::SmallPtrSet<const RecordType *, 8> Visited; |
| 1687 | SmallVector<const RecordType *, 8> ToVisit; |
| 1688 | ToVisit.push_back(RecordT); |
| Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1689 | bool Successful = false; |
| Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1690 | SmallVector<DeducedTemplateArgument, 8> SuccessfulDeduced; |
| Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1691 | while (!ToVisit.empty()) { |
| 1692 | // Retrieve the next class in the inheritance hierarchy. |
| 1693 | const RecordType *NextT = ToVisit.pop_back_val(); |
| Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1694 | |
| Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1695 | // If we have already seen this type, skip it. |
| 1696 | if (!Visited.insert(NextT).second) |
| 1697 | continue; |
| Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1698 | |
| Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1699 | // If this is a base class, try to perform template argument |
| 1700 | // deduction from it. |
| 1701 | if (NextT != RecordT) { |
| 1702 | TemplateDeductionInfo BaseInfo(Info.getLocation()); |
| 1703 | Sema::TemplateDeductionResult BaseResult = |
| 1704 | DeduceTemplateArguments(S, TemplateParams, SpecParam, |
| 1705 | QualType(NextT, 0), BaseInfo, Deduced); |
| 1706 | |
| 1707 | // If template argument deduction for this base was successful, |
| 1708 | // note that we had some success. Otherwise, ignore any deductions |
| 1709 | // from this base class. |
| 1710 | if (BaseResult == Sema::TDK_Success) { |
| Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1711 | // If we've already seen some success, then deduction fails due to |
| 1712 | // an ambiguity (temp.deduct.call p5). |
| 1713 | if (Successful) |
| 1714 | return Sema::TDK_MiscellaneousDeductionFailure; |
| 1715 | |
| Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1716 | Successful = true; |
| Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1717 | std::swap(SuccessfulDeduced, Deduced); |
| 1718 | |
| Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1719 | Info.Param = BaseInfo.Param; |
| 1720 | Info.FirstArg = BaseInfo.FirstArg; |
| 1721 | Info.SecondArg = BaseInfo.SecondArg; |
| Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | Deduced = DeducedOrig; |
| Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1725 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1726 | |
| Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1727 | // Visit base classes |
| 1728 | CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl()); |
| 1729 | for (const auto &Base : Next->bases()) { |
| 1730 | assert(Base.getType()->isRecordType() && |
| 1731 | "Base class that isn't a record?"); |
| 1732 | ToVisit.push_back(Base.getType()->getAs<RecordType>()); |
| 1733 | } |
| 1734 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1735 | |
| Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1736 | if (Successful) { |
| 1737 | std::swap(SuccessfulDeduced, Deduced); |
| Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1738 | return Sema::TDK_Success; |
| Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1739 | } |
| Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1740 | |
| Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1741 | return Result; |
| Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
| Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1744 | // T type::* |
| 1745 | // T T::* |
| 1746 | // T (type::*)() |
| 1747 | // type (T::*)() |
| 1748 | // type (type::*)(T) |
| 1749 | // type (T::*)(T) |
| 1750 | // T (type::*)(T) |
| 1751 | // T (T::*)() |
| 1752 | // T (T::*)(T) |
| 1753 | case Type::MemberPointer: { |
| 1754 | const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); |
| 1755 | const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); |
| 1756 | if (!MemPtrArg) |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1757 | return Sema::TDK_NonDeducedMismatch; |
| Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1758 | |
| David Majnemer | a381cda | 2015-11-30 20:34:28 +0000 | [diff] [blame] | 1759 | QualType ParamPointeeType = MemPtrParam->getPointeeType(); |
| 1760 | if (ParamPointeeType->isFunctionType()) |
| 1761 | S.adjustMemberFunctionCC(ParamPointeeType, /*IsStatic=*/true, |
| 1762 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
| 1763 | QualType ArgPointeeType = MemPtrArg->getPointeeType(); |
| 1764 | if (ArgPointeeType->isFunctionType()) |
| 1765 | S.adjustMemberFunctionCC(ArgPointeeType, /*IsStatic=*/true, |
| 1766 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
| 1767 | |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1768 | if (Sema::TemplateDeductionResult Result |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1769 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| David Majnemer | a381cda | 2015-11-30 20:34:28 +0000 | [diff] [blame] | 1770 | ParamPointeeType, |
| 1771 | ArgPointeeType, |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1772 | Info, Deduced, |
| 1773 | TDF & TDF_IgnoreQualifiers)) |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1774 | return Result; |
| 1775 | |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1776 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1777 | QualType(MemPtrParam->getClass(), 0), |
| 1778 | QualType(MemPtrArg->getClass(), 0), |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1779 | Info, Deduced, |
| Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1780 | TDF & TDF_IgnoreQualifiers); |
| Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1781 | } |
| 1782 | |
| Anders Carlsson | 15f1dd1 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1783 | // (clang extension) |
| 1784 | // |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1785 | // type(^)(T) |
| 1786 | // T(^)() |
| 1787 | // T(^)(T) |
| Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1788 | case Type::BlockPointer: { |
| 1789 | const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); |
| 1790 | const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1791 | |
| Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1792 | if (!BlockPtrArg) |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1793 | return Sema::TDK_NonDeducedMismatch; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1794 | |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1795 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1796 | BlockPtrParam->getPointeeType(), |
| 1797 | BlockPtrArg->getPointeeType(), |
| 1798 | Info, Deduced, 0); |
| Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1801 | // (clang extension) |
| 1802 | // |
| 1803 | // T __attribute__(((ext_vector_type(<integral constant>)))) |
| 1804 | case Type::ExtVector: { |
| 1805 | const ExtVectorType *VectorParam = cast<ExtVectorType>(Param); |
| 1806 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1807 | // Make sure that the vectors have the same number of elements. |
| 1808 | if (VectorParam->getNumElements() != VectorArg->getNumElements()) |
| 1809 | return Sema::TDK_NonDeducedMismatch; |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1810 | |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1811 | // Perform deduction on the element types. |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1812 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1813 | VectorParam->getElementType(), |
| 1814 | VectorArg->getElementType(), |
| 1815 | Info, Deduced, TDF); |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1816 | } |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1817 | |
| 1818 | if (const DependentSizedExtVectorType *VectorArg |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1819 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1820 | // We can't check the number of elements, since the argument has a |
| 1821 | // dependent number of elements. This can only occur during partial |
| 1822 | // ordering. |
| 1823 | |
| 1824 | // Perform deduction on the element types. |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1825 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1826 | VectorParam->getElementType(), |
| 1827 | VectorArg->getElementType(), |
| 1828 | Info, Deduced, TDF); |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1829 | } |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1830 | |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1831 | return Sema::TDK_NonDeducedMismatch; |
| 1832 | } |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1833 | |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1834 | // (clang extension) |
| 1835 | // |
| 1836 | // T __attribute__(((ext_vector_type(N)))) |
| 1837 | case Type::DependentSizedExtVector: { |
| 1838 | const DependentSizedExtVectorType *VectorParam |
| 1839 | = cast<DependentSizedExtVectorType>(Param); |
| 1840 | |
| 1841 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1842 | // Perform deduction on the element types. |
| 1843 | if (Sema::TemplateDeductionResult Result |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1844 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1845 | VectorParam->getElementType(), |
| 1846 | VectorArg->getElementType(), |
| 1847 | Info, Deduced, TDF)) |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1848 | return Result; |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1849 | |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1850 | // Perform deduction on the vector size, if we can. |
| 1851 | NonTypeTemplateParmDecl *NTTP |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1852 | = getDeducedParameterFromExpr(Info, VectorParam->getSizeExpr()); |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1853 | if (!NTTP) |
| 1854 | return Sema::TDK_Success; |
| 1855 | |
| 1856 | llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); |
| 1857 | ArgSize = VectorArg->getNumElements(); |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1858 | // Note that we use the "array bound" rules here; just like in that |
| 1859 | // case, we don't have any particular type for the vector size, but |
| 1860 | // we can provide one if necessary. |
| Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1861 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1862 | S.Context.IntTy, true, Info, |
| Richard Smith | 593d6a1 | 2016-12-23 01:30:39 +0000 | [diff] [blame] | 1863 | Deduced); |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1864 | } |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1865 | |
| 1866 | if (const DependentSizedExtVectorType *VectorArg |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1867 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1868 | // Perform deduction on the element types. |
| 1869 | if (Sema::TemplateDeductionResult Result |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1870 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1871 | VectorParam->getElementType(), |
| 1872 | VectorArg->getElementType(), |
| 1873 | Info, Deduced, TDF)) |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1874 | return Result; |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1875 | |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1876 | // Perform deduction on the vector size, if we can. |
| 1877 | NonTypeTemplateParmDecl *NTTP |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 1878 | = getDeducedParameterFromExpr(Info, VectorParam->getSizeExpr()); |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1879 | if (!NTTP) |
| 1880 | return Sema::TDK_Success; |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1881 | |
| Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1882 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 1883 | VectorArg->getSizeExpr(), |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1884 | Info, Deduced); |
| 1885 | } |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1886 | |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1887 | return Sema::TDK_NonDeducedMismatch; |
| 1888 | } |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1889 | |
| Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 1890 | // (clang extension) |
| 1891 | // |
| 1892 | // T __attribute__(((address_space(N)))) |
| 1893 | case Type::DependentAddressSpace: { |
| 1894 | const DependentAddressSpaceType *AddressSpaceParam = |
| 1895 | cast<DependentAddressSpaceType>(Param); |
| 1896 | |
| 1897 | if (const DependentAddressSpaceType *AddressSpaceArg = |
| 1898 | dyn_cast<DependentAddressSpaceType>(Arg)) { |
| 1899 | // Perform deduction on the pointer type. |
| 1900 | if (Sema::TemplateDeductionResult Result = |
| 1901 | DeduceTemplateArgumentsByTypeMatch( |
| 1902 | S, TemplateParams, AddressSpaceParam->getPointeeType(), |
| 1903 | AddressSpaceArg->getPointeeType(), Info, Deduced, TDF)) |
| 1904 | return Result; |
| 1905 | |
| 1906 | // Perform deduction on the address space, if we can. |
| 1907 | NonTypeTemplateParmDecl *NTTP = getDeducedParameterFromExpr( |
| 1908 | Info, AddressSpaceParam->getAddrSpaceExpr()); |
| 1909 | if (!NTTP) |
| 1910 | return Sema::TDK_Success; |
| 1911 | |
| 1912 | return DeduceNonTypeTemplateArgument( |
| 1913 | S, TemplateParams, NTTP, AddressSpaceArg->getAddrSpaceExpr(), Info, |
| 1914 | Deduced); |
| 1915 | } |
| 1916 | |
| Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 1917 | if (isTargetAddressSpace(Arg.getAddressSpace())) { |
| Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 1918 | llvm::APSInt ArgAddressSpace(S.Context.getTypeSize(S.Context.IntTy), |
| 1919 | false); |
| Alexander Richardson | 6d98943 | 2017-10-15 18:48:14 +0000 | [diff] [blame] | 1920 | ArgAddressSpace = toTargetAddressSpace(Arg.getAddressSpace()); |
| Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 1921 | |
| 1922 | // Perform deduction on the pointer types. |
| 1923 | if (Sema::TemplateDeductionResult Result = |
| 1924 | DeduceTemplateArgumentsByTypeMatch( |
| 1925 | S, TemplateParams, AddressSpaceParam->getPointeeType(), |
| 1926 | S.Context.removeAddrSpaceQualType(Arg), Info, Deduced, TDF)) |
| 1927 | return Result; |
| 1928 | |
| 1929 | // Perform deduction on the address space, if we can. |
| 1930 | NonTypeTemplateParmDecl *NTTP = getDeducedParameterFromExpr( |
| 1931 | Info, AddressSpaceParam->getAddrSpaceExpr()); |
| 1932 | if (!NTTP) |
| 1933 | return Sema::TDK_Success; |
| 1934 | |
| 1935 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 1936 | ArgAddressSpace, S.Context.IntTy, |
| 1937 | true, Info, Deduced); |
| 1938 | } |
| 1939 | |
| 1940 | return Sema::TDK_NonDeducedMismatch; |
| 1941 | } |
| 1942 | |
| Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1943 | case Type::TypeOfExpr: |
| 1944 | case Type::TypeOf: |
| Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 1945 | case Type::DependentName: |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1946 | case Type::UnresolvedUsing: |
| 1947 | case Type::Decltype: |
| 1948 | case Type::UnaryTransform: |
| 1949 | case Type::Auto: |
| Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 1950 | case Type::DeducedTemplateSpecialization: |
| Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1951 | case Type::DependentTemplateSpecialization: |
| 1952 | case Type::PackExpansion: |
| Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 1953 | case Type::Pipe: |
| Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1954 | // No template argument deduction for these types |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1955 | return Sema::TDK_Success; |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
| David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1958 | llvm_unreachable("Invalid Type Class!"); |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1959 | } |
| 1960 | |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1961 | static Sema::TemplateDeductionResult |
| Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1962 | DeduceTemplateArguments(Sema &S, |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1963 | TemplateParameterList *TemplateParams, |
| 1964 | const TemplateArgument &Param, |
| Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1965 | TemplateArgument Arg, |
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1966 | TemplateDeductionInfo &Info, |
| Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 1967 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1968 | // If the template argument is a pack expansion, perform template argument |
| 1969 | // deduction against the pattern of that expansion. This only occurs during |
| 1970 | // partial ordering. |
| 1971 | if (Arg.isPackExpansion()) |
| 1972 | Arg = Arg.getPackExpansionPattern(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1973 | |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1974 | switch (Param.getKind()) { |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1975 | case TemplateArgument::Null: |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1976 | llvm_unreachable("Null template argument in parameter list"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1977 | |
| 1978 | case TemplateArgument::Type: |
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1979 | if (Arg.getKind() == TemplateArgument::Type) |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1980 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1981 | Param.getAsType(), |
| 1982 | Arg.getAsType(), |
| 1983 | Info, Deduced, 0); |
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1984 | Info.FirstArg = Param; |
| 1985 | Info.SecondArg = Arg; |
| 1986 | return Sema::TDK_NonDeducedMismatch; |
| Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1987 | |
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1988 | case TemplateArgument::Template: |
| Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1989 | if (Arg.getKind() == TemplateArgument::Template) |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1990 | return DeduceTemplateArguments(S, TemplateParams, |
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1991 | Param.getAsTemplate(), |
| Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1992 | Arg.getAsTemplate(), Info, Deduced); |
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1993 | Info.FirstArg = Param; |
| 1994 | Info.SecondArg = Arg; |
| 1995 | return Sema::TDK_NonDeducedMismatch; |
| Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1996 | |
| 1997 | case TemplateArgument::TemplateExpansion: |
| 1998 | llvm_unreachable("caller should handle pack expansions"); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1999 | |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2000 | case TemplateArgument::Declaration: |
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2001 | if (Arg.getKind() == TemplateArgument::Declaration && |
| David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 2002 | isSameDeclaration(Param.getAsDecl(), Arg.getAsDecl())) |
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2003 | return Sema::TDK_Success; |
| 2004 | |
| 2005 | Info.FirstArg = Param; |
| 2006 | Info.SecondArg = Arg; |
| 2007 | return Sema::TDK_NonDeducedMismatch; |
| 2008 | |
| 2009 | case TemplateArgument::NullPtr: |
| 2010 | if (Arg.getKind() == TemplateArgument::NullPtr && |
| 2011 | S.Context.hasSameType(Param.getNullPtrType(), Arg.getNullPtrType())) |
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2012 | return Sema::TDK_Success; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2013 | |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2014 | Info.FirstArg = Param; |
| 2015 | Info.SecondArg = Arg; |
| 2016 | return Sema::TDK_NonDeducedMismatch; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2017 | |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2018 | case TemplateArgument::Integral: |
| 2019 | if (Arg.getKind() == TemplateArgument::Integral) { |
| Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 2020 | if (hasSameExtendedValue(Param.getAsIntegral(), Arg.getAsIntegral())) |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2021 | return Sema::TDK_Success; |
| 2022 | |
| 2023 | Info.FirstArg = Param; |
| 2024 | Info.SecondArg = Arg; |
| 2025 | return Sema::TDK_NonDeducedMismatch; |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2026 | } |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2027 | |
| 2028 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 2029 | Info.FirstArg = Param; |
| 2030 | Info.SecondArg = Arg; |
| 2031 | return Sema::TDK_NonDeducedMismatch; |
| 2032 | } |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2033 | |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2034 | Info.FirstArg = Param; |
| 2035 | Info.SecondArg = Arg; |
| 2036 | return Sema::TDK_NonDeducedMismatch; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2037 | |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 2038 | case TemplateArgument::Expression: |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2039 | if (NonTypeTemplateParmDecl *NTTP |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2040 | = getDeducedParameterFromExpr(Info, Param.getAsExpr())) { |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2041 | if (Arg.getKind() == TemplateArgument::Integral) |
| Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 2042 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 2043 | Arg.getAsIntegral(), |
| Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 2044 | Arg.getIntegralType(), |
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2045 | /*ArrayBound=*/false, |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2046 | Info, Deduced); |
| Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 2047 | if (Arg.getKind() == TemplateArgument::NullPtr) |
| Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 2048 | return DeduceNullPtrTemplateArgument(S, TemplateParams, NTTP, |
| 2049 | Arg.getNullPtrType(), |
| Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 2050 | Info, Deduced); |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2051 | if (Arg.getKind() == TemplateArgument::Expression) |
| Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 2052 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 2053 | Arg.getAsExpr(), Info, Deduced); |
| Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 2054 | if (Arg.getKind() == TemplateArgument::Declaration) |
| Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 2055 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 2056 | Arg.getAsDecl(), |
| 2057 | Arg.getParamTypeForDecl(), |
| Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 2058 | Info, Deduced); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2059 | |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2060 | Info.FirstArg = Param; |
| 2061 | Info.SecondArg = Arg; |
| 2062 | return Sema::TDK_NonDeducedMismatch; |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2063 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2064 | |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2065 | // Can't deduce anything, but that's okay. |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2066 | return Sema::TDK_Success; |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 2067 | |
| Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 2068 | case TemplateArgument::Pack: |
| Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2069 | llvm_unreachable("Argument packs should be expanded by the caller!"); |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2070 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2071 | |
| David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2072 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2073 | } |
| 2074 | |
| Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2075 | /// \brief Determine whether there is a template argument to be used for |
| 2076 | /// deduction. |
| 2077 | /// |
| 2078 | /// This routine "expands" argument packs in-place, overriding its input |
| 2079 | /// parameters so that \c Args[ArgIdx] will be the available template argument. |
| 2080 | /// |
| 2081 | /// \returns true if there is another template argument (which will be at |
| 2082 | /// \c Args[ArgIdx]), false otherwise. |
| Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2083 | static bool hasTemplateArgumentForDeduction(ArrayRef<TemplateArgument> &Args, |
| 2084 | unsigned &ArgIdx) { |
| 2085 | if (ArgIdx == Args.size()) |
| Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2086 | return false; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2087 | |
| Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2088 | const TemplateArgument &Arg = Args[ArgIdx]; |
| 2089 | if (Arg.getKind() != TemplateArgument::Pack) |
| 2090 | return true; |
| 2091 | |
| Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2092 | assert(ArgIdx == Args.size() - 1 && "Pack not at the end of argument list?"); |
| 2093 | Args = Arg.pack_elements(); |
| Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2094 | ArgIdx = 0; |
| Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2095 | return ArgIdx < Args.size(); |
| Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
| Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 2098 | /// \brief Determine whether the given set of template arguments has a pack |
| 2099 | /// expansion that is not the last template argument. |
| Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2100 | static bool hasPackExpansionBeforeEnd(ArrayRef<TemplateArgument> Args) { |
| 2101 | bool FoundPackExpansion = false; |
| 2102 | for (const auto &A : Args) { |
| 2103 | if (FoundPackExpansion) |
| Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 2104 | return true; |
| Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2105 | |
| 2106 | if (A.getKind() == TemplateArgument::Pack) |
| 2107 | return hasPackExpansionBeforeEnd(A.pack_elements()); |
| 2108 | |
| 2109 | if (A.isPackExpansion()) |
| 2110 | FoundPackExpansion = true; |
| Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 2111 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2112 | |
| Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 2113 | return false; |
| 2114 | } |
| 2115 | |
| Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2116 | static Sema::TemplateDeductionResult |
| Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 2117 | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
| Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2118 | ArrayRef<TemplateArgument> Params, |
| 2119 | ArrayRef<TemplateArgument> Args, |
| Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2120 | TemplateDeductionInfo &Info, |
| Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 2121 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2122 | bool NumberOfArgumentsMustMatch) { |
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2123 | // C++0x [temp.deduct.type]p9: |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2124 | // If the template argument list of P contains a pack expansion that is not |
| 2125 | // the last template argument, the entire template argument list is a |
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2126 | // non-deduced context. |
| Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2127 | if (hasPackExpansionBeforeEnd(Params)) |
| Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 2128 | return Sema::TDK_Success; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2129 | |
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2130 | // C++0x [temp.deduct.type]p9: |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2131 | // If P has a form that contains <T> or <i>, then each argument Pi of the |
| 2132 | // respective template argument list P is compared with the corresponding |
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2133 | // argument Ai of the corresponding template argument list of A. |
| Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2134 | unsigned ArgIdx = 0, ParamIdx = 0; |
| Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2135 | for (; hasTemplateArgumentForDeduction(Params, ParamIdx); ++ParamIdx) { |
| Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2136 | if (!Params[ParamIdx].isPackExpansion()) { |
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2137 | // The simple case: deduce template arguments by matching Pi and Ai. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2138 | |
| Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2139 | // Check whether we have enough arguments. |
| Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2140 | if (!hasTemplateArgumentForDeduction(Args, ArgIdx)) |
| Richard Smith | ec7176e | 2017-01-05 02:31:32 +0000 | [diff] [blame] | 2141 | return NumberOfArgumentsMustMatch |
| 2142 | ? Sema::TDK_MiscellaneousDeductionFailure |
| 2143 | : Sema::TDK_Success; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2144 | |
| Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 2145 | // C++1z [temp.deduct.type]p9: |
| 2146 | // During partial ordering, if Ai was originally a pack expansion [and] |
| 2147 | // Pi is not a pack expansion, template argument deduction fails. |
| 2148 | if (Args[ArgIdx].isPackExpansion()) |
| Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 2149 | return Sema::TDK_MiscellaneousDeductionFailure; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2150 | |
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2151 | // Perform deduction for this Pi/Ai pair. |
| Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2152 | if (Sema::TemplateDeductionResult Result |
| Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 2153 | = DeduceTemplateArguments(S, TemplateParams, |
| 2154 | Params[ParamIdx], Args[ArgIdx], |
| 2155 | Info, Deduced)) |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2156 | return Result; |
| 2157 | |
| Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2158 | // Move to the next argument. |
| 2159 | ++ArgIdx; |
| 2160 | continue; |
| 2161 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2162 | |
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2163 | // The parameter is a pack expansion. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2164 | |
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2165 | // C++0x [temp.deduct.type]p9: |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2166 | // If Pi is a pack expansion, then the pattern of Pi is compared with |
| 2167 | // each remaining argument in the template argument list of A. Each |
| 2168 | // comparison deduces template arguments for subsequent positions in the |
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2169 | // template parameter packs expanded by Pi. |
| 2170 | TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2171 | |
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2172 | // FIXME: If there are no remaining arguments, we can bail out early |
| 2173 | // and set any deduced parameter packs to an empty argument pack. |
| 2174 | // The latter part of this is a (minor) correctness issue. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2175 | |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 2176 | // Prepare to deduce the packs within the pattern. |
| 2177 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2178 | |
| 2179 | // Keep track of the deduced template arguments for each parameter pack |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2180 | // expanded by this pack expansion (the outer index) and for each |
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2181 | // template argument (the inner SmallVectors). |
| Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2182 | for (; hasTemplateArgumentForDeduction(Args, ArgIdx); ++ArgIdx) { |
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2183 | // Deduce template arguments from the pattern. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2184 | if (Sema::TemplateDeductionResult Result |
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2185 | = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx], |
| 2186 | Info, Deduced)) |
| 2187 | return Result; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2188 | |
| Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 2189 | PackScope.nextPackElement(); |
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2190 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2191 | |
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2192 | // Build argument packs for each of the parameter packs expanded by this |
| 2193 | // pack expansion. |
| Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 2194 | if (auto Result = PackScope.finish()) |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2195 | return Result; |
| Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2196 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2197 | |
| Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 2198 | return Sema::TDK_Success; |
| 2199 | } |
| 2200 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2201 | static Sema::TemplateDeductionResult |
| Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2202 | DeduceTemplateArguments(Sema &S, |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2203 | TemplateParameterList *TemplateParams, |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2204 | const TemplateArgumentList &ParamList, |
| 2205 | const TemplateArgumentList &ArgList, |
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2206 | TemplateDeductionInfo &Info, |
| Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2207 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 2208 | return DeduceTemplateArguments(S, TemplateParams, ParamList.asArray(), |
| Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 2209 | ArgList.asArray(), Info, Deduced, |
| 2210 | /*NumberOfArgumentsMustMatch*/false); |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
| Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2213 | /// \brief Determine whether two template arguments are the same. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2214 | static bool isSameTemplateArg(ASTContext &Context, |
| Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2215 | TemplateArgument X, |
| 2216 | const TemplateArgument &Y, |
| 2217 | bool PackExpansionMatchesPack = false) { |
| 2218 | // If we're checking deduced arguments (X) against original arguments (Y), |
| 2219 | // we will have flattened packs to non-expansions in X. |
| 2220 | if (PackExpansionMatchesPack && X.isPackExpansion() && !Y.isPackExpansion()) |
| 2221 | X = X.getPackExpansionPattern(); |
| 2222 | |
| Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2223 | if (X.getKind() != Y.getKind()) |
| 2224 | return false; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2225 | |
| Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2226 | switch (X.getKind()) { |
| 2227 | case TemplateArgument::Null: |
| David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2228 | llvm_unreachable("Comparing NULL template argument"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2229 | |
| Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2230 | case TemplateArgument::Type: |
| 2231 | return Context.getCanonicalType(X.getAsType()) == |
| 2232 | Context.getCanonicalType(Y.getAsType()); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2233 | |
| Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2234 | case TemplateArgument::Declaration: |
| David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 2235 | return isSameDeclaration(X.getAsDecl(), Y.getAsDecl()); |
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2236 | |
| 2237 | case TemplateArgument::NullPtr: |
| 2238 | return Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType()); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2239 | |
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2240 | case TemplateArgument::Template: |
| Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2241 | case TemplateArgument::TemplateExpansion: |
| 2242 | return Context.getCanonicalTemplateName( |
| 2243 | X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() == |
| 2244 | Context.getCanonicalTemplateName( |
| 2245 | Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2246 | |
| Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2247 | case TemplateArgument::Integral: |
| Richard Smith | 993f203 | 2016-12-25 20:21:12 +0000 | [diff] [blame] | 2248 | return hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2249 | |
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2250 | case TemplateArgument::Expression: { |
| 2251 | llvm::FoldingSetNodeID XID, YID; |
| 2252 | X.getAsExpr()->Profile(XID, Context, true); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2253 | Y.getAsExpr()->Profile(YID, Context, true); |
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2254 | return XID == YID; |
| 2255 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2256 | |
| Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2257 | case TemplateArgument::Pack: |
| 2258 | if (X.pack_size() != Y.pack_size()) |
| 2259 | return false; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2260 | |
| 2261 | for (TemplateArgument::pack_iterator XP = X.pack_begin(), |
| 2262 | XPEnd = X.pack_end(), |
| Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2263 | YP = Y.pack_begin(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2264 | XP != XPEnd; ++XP, ++YP) |
| Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2265 | if (!isSameTemplateArg(Context, *XP, *YP, PackExpansionMatchesPack)) |
| Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2266 | return false; |
| 2267 | |
| 2268 | return true; |
| 2269 | } |
| 2270 | |
| David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2271 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
| Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2272 | } |
| 2273 | |
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2274 | /// \brief Allocate a TemplateArgumentLoc where all locations have |
| 2275 | /// been initialized to the given location. |
| 2276 | /// |
| James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2277 | /// \param Arg The template argument we are producing template argument |
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2278 | /// location information for. |
| 2279 | /// |
| 2280 | /// \param NTTPType For a declaration template argument, the type of |
| 2281 | /// the non-type template parameter that corresponds to this template |
| Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2282 | /// argument. Can be null if no type sugar is available to add to the |
| 2283 | /// type from the template argument. |
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2284 | /// |
| 2285 | /// \param Loc The source location to use for the resulting template |
| 2286 | /// argument. |
| Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2287 | TemplateArgumentLoc |
| 2288 | Sema::getTrivialTemplateArgumentLoc(const TemplateArgument &Arg, |
| 2289 | QualType NTTPType, SourceLocation Loc) { |
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2290 | switch (Arg.getKind()) { |
| 2291 | case TemplateArgument::Null: |
| 2292 | llvm_unreachable("Can't get a NULL template argument here"); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2293 | |
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2294 | case TemplateArgument::Type: |
| Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2295 | return TemplateArgumentLoc( |
| 2296 | Arg, Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc)); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2297 | |
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2298 | case TemplateArgument::Declaration: { |
| Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2299 | if (NTTPType.isNull()) |
| 2300 | NTTPType = Arg.getParamTypeForDecl(); |
| Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2301 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
| 2302 | .getAs<Expr>(); |
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2303 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2304 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2305 | |
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2306 | case TemplateArgument::NullPtr: { |
| Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2307 | if (NTTPType.isNull()) |
| 2308 | NTTPType = Arg.getNullPtrType(); |
| Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2309 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
| 2310 | .getAs<Expr>(); |
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2311 | return TemplateArgumentLoc(TemplateArgument(NTTPType, /*isNullPtr*/true), |
| 2312 | E); |
| 2313 | } |
| 2314 | |
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2315 | case TemplateArgument::Integral: { |
| Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2316 | Expr *E = |
| 2317 | BuildExpressionFromIntegralTemplateArgument(Arg, Loc).getAs<Expr>(); |
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2318 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2319 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2320 | |
| Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2321 | case TemplateArgument::Template: |
| 2322 | case TemplateArgument::TemplateExpansion: { |
| 2323 | NestedNameSpecifierLocBuilder Builder; |
| 2324 | TemplateName Template = Arg.getAsTemplate(); |
| 2325 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) |
| Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2326 | Builder.MakeTrivial(Context, DTN->getQualifier(), Loc); |
| Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 2327 | else if (QualifiedTemplateName *QTN = |
| 2328 | Template.getAsQualifiedTemplateName()) |
| Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2329 | Builder.MakeTrivial(Context, QTN->getQualifier(), Loc); |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2330 | |
| Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2331 | if (Arg.getKind() == TemplateArgument::Template) |
| Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2332 | return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(Context), |
| Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2333 | Loc); |
| Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2334 | |
| 2335 | return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(Context), |
| Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2336 | Loc, Loc); |
| 2337 | } |
| Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2338 | |
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2339 | case TemplateArgument::Expression: |
| 2340 | return TemplateArgumentLoc(Arg, Arg.getAsExpr()); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2341 | |
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2342 | case TemplateArgument::Pack: |
| 2343 | return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo()); |
| 2344 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2345 | |
| David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2346 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2347 | } |
| 2348 | |
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2349 | /// \brief Convert the given deduced template argument and add it to the set of |
| 2350 | /// fully-converted template arguments. |
| Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2351 | static bool |
| 2352 | ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param, |
| 2353 | DeducedTemplateArgument Arg, |
| 2354 | NamedDecl *Template, |
| Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2355 | TemplateDeductionInfo &Info, |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2356 | bool IsDeduced, |
| Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2357 | SmallVectorImpl<TemplateArgument> &Output) { |
| Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2358 | auto ConvertArg = [&](DeducedTemplateArgument Arg, |
| 2359 | unsigned ArgumentPackIndex) { |
| 2360 | // Convert the deduced template argument into a template |
| 2361 | // argument that we can check, almost as if the user had written |
| 2362 | // the template argument explicitly. |
| 2363 | TemplateArgumentLoc ArgLoc = |
| Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2364 | S.getTrivialTemplateArgumentLoc(Arg, QualType(), Info.getLocation()); |
| Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2365 | |
| 2366 | // Check the template argument, converting it as necessary. |
| 2367 | return S.CheckTemplateArgument( |
| 2368 | Param, ArgLoc, Template, Template->getLocation(), |
| 2369 | Template->getSourceRange().getEnd(), ArgumentPackIndex, Output, |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2370 | IsDeduced |
| Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2371 | ? (Arg.wasDeducedFromArrayBound() ? Sema::CTAK_DeducedFromArrayBound |
| 2372 | : Sema::CTAK_Deduced) |
| 2373 | : Sema::CTAK_Specified); |
| 2374 | }; |
| 2375 | |
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2376 | if (Arg.getKind() == TemplateArgument::Pack) { |
| 2377 | // This is a template argument pack, so check each of its arguments against |
| 2378 | // the template parameter. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2379 | SmallVector<TemplateArgument, 2> PackedArgsBuilder; |
| Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 2380 | for (const auto &P : Arg.pack_elements()) { |
| Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2381 | // When converting the deduced template argument, append it to the |
| 2382 | // general output list. We need to do this so that the template argument |
| 2383 | // checking logic has all of the prior template arguments available. |
| Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 2384 | DeducedTemplateArgument InnerArg(P); |
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2385 | InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound()); |
| Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2386 | assert(InnerArg.getKind() != TemplateArgument::Pack && |
| 2387 | "deduced nested pack"); |
| Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 2388 | if (P.isNull()) { |
| 2389 | // We deduced arguments for some elements of this pack, but not for |
| 2390 | // all of them. This happens if we get a conditionally-non-deduced |
| 2391 | // context in a pack expansion (such as an overload set in one of the |
| 2392 | // arguments). |
| 2393 | S.Diag(Param->getLocation(), |
| 2394 | diag::err_template_arg_deduced_incomplete_pack) |
| 2395 | << Arg << Param; |
| 2396 | return true; |
| 2397 | } |
| Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2398 | if (ConvertArg(InnerArg, PackedArgsBuilder.size())) |
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2399 | return true; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2400 | |
| Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2401 | // Move the converted template argument into our argument pack. |
| Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 2402 | PackedArgsBuilder.push_back(Output.pop_back_val()); |
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2403 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2404 | |
| Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2405 | // 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] | 2406 | // itself, in case that substitution fails. |
| 2407 | if (PackedArgsBuilder.empty()) { |
| Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2408 | LocalInstantiationScope Scope(S); |
| Richard Smith | e824775 | 2016-12-22 07:24:39 +0000 | [diff] [blame] | 2409 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Output); |
| Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2410 | MultiLevelTemplateArgumentList Args(TemplateArgs); |
| 2411 | |
| 2412 | if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2413 | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
| 2414 | NTTP, Output, |
| 2415 | Template->getSourceRange()); |
| Simon Pilgrim | 6f3e1ea | 2016-12-26 18:11:49 +0000 | [diff] [blame] | 2416 | if (Inst.isInvalid() || |
| Richard Smith | 9341790 | 2016-12-23 02:00:24 +0000 | [diff] [blame] | 2417 | S.SubstType(NTTP->getType(), Args, NTTP->getLocation(), |
| 2418 | NTTP->getDeclName()).isNull()) |
| 2419 | return true; |
| 2420 | } else if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 2421 | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
| 2422 | TTP, Output, |
| 2423 | Template->getSourceRange()); |
| 2424 | if (Inst.isInvalid() || !S.SubstDecl(TTP, S.CurContext, Args)) |
| 2425 | return true; |
| 2426 | } |
| 2427 | // For type parameters, no substitution is ever required. |
| Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2428 | } |
| Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2429 | |
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2430 | // Create the resulting argument pack. |
| Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 2431 | Output.push_back( |
| 2432 | TemplateArgument::CreatePackCopy(S.Context, PackedArgsBuilder)); |
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2433 | return false; |
| 2434 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2435 | |
| Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2436 | return ConvertArg(Arg, 0); |
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2437 | } |
| 2438 | |
| Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2439 | // FIXME: This should not be a template, but |
| 2440 | // ClassTemplatePartialSpecializationDecl sadly does not derive from |
| 2441 | // TemplateDecl. |
| 2442 | template<typename TemplateDeclT> |
| 2443 | static Sema::TemplateDeductionResult ConvertDeducedTemplateArguments( |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2444 | Sema &S, TemplateDeclT *Template, bool IsDeduced, |
| Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2445 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2446 | TemplateDeductionInfo &Info, SmallVectorImpl<TemplateArgument> &Builder, |
| 2447 | LocalInstantiationScope *CurrentInstantiationScope = nullptr, |
| Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 2448 | unsigned NumAlreadyConverted = 0, bool PartialOverloading = false) { |
| Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2449 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| 2450 | |
| 2451 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 2452 | NamedDecl *Param = TemplateParams->getParam(I); |
| 2453 | |
| 2454 | if (!Deduced[I].isNull()) { |
| 2455 | if (I < NumAlreadyConverted) { |
| Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2456 | // We may have had explicitly-specified template arguments for a |
| 2457 | // template parameter pack (that may or may not have been extended |
| 2458 | // via additional deduced arguments). |
| Richard Smith | 9c0c986 | 2017-01-05 20:27:28 +0000 | [diff] [blame] | 2459 | if (Param->isParameterPack() && CurrentInstantiationScope && |
| 2460 | CurrentInstantiationScope->getPartiallySubstitutedPack() == Param) { |
| 2461 | // Forget the partially-substituted pack; its substitution is now |
| 2462 | // complete. |
| 2463 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2464 | // We still need to check the argument in case it was extended by |
| 2465 | // deduction. |
| 2466 | } else { |
| 2467 | // We have already fully type-checked and converted this |
| 2468 | // argument, because it was explicitly-specified. Just record the |
| 2469 | // presence of this argument. |
| 2470 | Builder.push_back(Deduced[I]); |
| 2471 | continue; |
| Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2472 | } |
| Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
| Richard Smith | 9c0c986 | 2017-01-05 20:27:28 +0000 | [diff] [blame] | 2475 | // We may have deduced this argument, so it still needs to be |
| Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2476 | // checked and converted. |
| 2477 | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Template, Info, |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2478 | IsDeduced, Builder)) { |
| Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2479 | Info.Param = makeTemplateParameter(Param); |
| 2480 | // FIXME: These template arguments are temporary. Free them! |
| 2481 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2482 | return Sema::TDK_SubstitutionFailure; |
| 2483 | } |
| 2484 | |
| 2485 | continue; |
| 2486 | } |
| 2487 | |
| 2488 | // C++0x [temp.arg.explicit]p3: |
| 2489 | // A trailing template parameter pack (14.5.3) not otherwise deduced will |
| 2490 | // be deduced to an empty sequence of template arguments. |
| 2491 | // FIXME: Where did the word "trailing" come from? |
| 2492 | if (Param->isTemplateParameterPack()) { |
| 2493 | // We may have had explicitly-specified template arguments for this |
| 2494 | // template parameter pack. If so, our empty deduction extends the |
| 2495 | // explicitly-specified set (C++0x [temp.arg.explicit]p9). |
| 2496 | const TemplateArgument *ExplicitArgs; |
| 2497 | unsigned NumExplicitArgs; |
| 2498 | if (CurrentInstantiationScope && |
| 2499 | CurrentInstantiationScope->getPartiallySubstitutedPack( |
| 2500 | &ExplicitArgs, &NumExplicitArgs) == Param) { |
| 2501 | Builder.push_back(TemplateArgument( |
| 2502 | llvm::makeArrayRef(ExplicitArgs, NumExplicitArgs))); |
| 2503 | |
| 2504 | // Forget the partially-substituted pack; its substitution is now |
| 2505 | // complete. |
| 2506 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2507 | } else { |
| 2508 | // Go through the motions of checking the empty argument pack against |
| 2509 | // the parameter pack. |
| 2510 | DeducedTemplateArgument DeducedPack(TemplateArgument::getEmptyPack()); |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2511 | if (ConvertDeducedTemplateArgument(S, Param, DeducedPack, Template, |
| 2512 | Info, IsDeduced, Builder)) { |
| Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2513 | Info.Param = makeTemplateParameter(Param); |
| 2514 | // FIXME: These template arguments are temporary. Free them! |
| 2515 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2516 | return Sema::TDK_SubstitutionFailure; |
| 2517 | } |
| 2518 | } |
| 2519 | continue; |
| 2520 | } |
| 2521 | |
| 2522 | // Substitute into the default template argument, if available. |
| 2523 | bool HasDefaultArg = false; |
| 2524 | TemplateDecl *TD = dyn_cast<TemplateDecl>(Template); |
| 2525 | if (!TD) { |
| Richard Smith | f8ba3fd | 2017-06-02 22:53:06 +0000 | [diff] [blame] | 2526 | assert(isa<ClassTemplatePartialSpecializationDecl>(Template) || |
| 2527 | isa<VarTemplatePartialSpecializationDecl>(Template)); |
| Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2528 | return Sema::TDK_Incomplete; |
| 2529 | } |
| 2530 | |
| 2531 | TemplateArgumentLoc DefArg = S.SubstDefaultTemplateArgumentIfAvailable( |
| 2532 | TD, TD->getLocation(), TD->getSourceRange().getEnd(), Param, Builder, |
| 2533 | HasDefaultArg); |
| 2534 | |
| 2535 | // If there was no default argument, deduction is incomplete. |
| 2536 | if (DefArg.getArgument().isNull()) { |
| 2537 | Info.Param = makeTemplateParameter( |
| 2538 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
| 2539 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 2540 | if (PartialOverloading) break; |
| 2541 | |
| Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2542 | return HasDefaultArg ? Sema::TDK_SubstitutionFailure |
| 2543 | : Sema::TDK_Incomplete; |
| 2544 | } |
| 2545 | |
| 2546 | // Check whether we can actually use the default argument. |
| 2547 | if (S.CheckTemplateArgument(Param, DefArg, TD, TD->getLocation(), |
| 2548 | TD->getSourceRange().getEnd(), 0, Builder, |
| 2549 | Sema::CTAK_Specified)) { |
| 2550 | Info.Param = makeTemplateParameter( |
| 2551 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
| 2552 | // FIXME: These template arguments are temporary. Free them! |
| 2553 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2554 | return Sema::TDK_SubstitutionFailure; |
| 2555 | } |
| 2556 | |
| 2557 | // If we get here, we successfully used the default template argument. |
| 2558 | } |
| 2559 | |
| 2560 | return Sema::TDK_Success; |
| 2561 | } |
| 2562 | |
| Benjamin Kramer | 357c9e1 | 2017-02-11 12:21:17 +0000 | [diff] [blame] | 2563 | static DeclContext *getAsDeclContextOrEnclosing(Decl *D) { |
| Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2564 | if (auto *DC = dyn_cast<DeclContext>(D)) |
| 2565 | return DC; |
| 2566 | return D->getDeclContext(); |
| 2567 | } |
| 2568 | |
| 2569 | template<typename T> struct IsPartialSpecialization { |
| 2570 | static constexpr bool value = false; |
| 2571 | }; |
| 2572 | template<> |
| 2573 | struct IsPartialSpecialization<ClassTemplatePartialSpecializationDecl> { |
| 2574 | static constexpr bool value = true; |
| 2575 | }; |
| 2576 | template<> |
| 2577 | struct IsPartialSpecialization<VarTemplatePartialSpecializationDecl> { |
| 2578 | static constexpr bool value = true; |
| 2579 | }; |
| 2580 | |
| 2581 | /// Complete template argument deduction for a partial specialization. |
| 2582 | template <typename T> |
| 2583 | static typename std::enable_if<IsPartialSpecialization<T>::value, |
| 2584 | Sema::TemplateDeductionResult>::type |
| 2585 | FinishTemplateArgumentDeduction( |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2586 | Sema &S, T *Partial, bool IsPartialOrdering, |
| 2587 | const TemplateArgumentList &TemplateArgs, |
| Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2588 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2589 | TemplateDeductionInfo &Info) { |
| Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2590 | // Unevaluated SFINAE context. |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2591 | EnterExpressionEvaluationContext Unevaluated( |
| 2592 | S, Sema::ExpressionEvaluationContext::Unevaluated); |
| Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2593 | Sema::SFINAETrap Trap(S); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2594 | |
| Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2595 | Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Partial)); |
| Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2596 | |
| 2597 | // C++ [temp.deduct.type]p2: |
| 2598 | // [...] or if any template argument remains neither deduced nor |
| 2599 | // explicitly specified, template argument deduction fails. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2600 | SmallVector<TemplateArgument, 4> Builder; |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2601 | if (auto Result = ConvertDeducedTemplateArguments( |
| 2602 | S, Partial, IsPartialOrdering, Deduced, Info, Builder)) |
| Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 2603 | return Result; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2604 | |
| Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2605 | // Form the template argument list from the deduced template arguments. |
| 2606 | TemplateArgumentList *DeducedArgumentList |
| David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2607 | = TemplateArgumentList::CreateCopy(S.Context, Builder); |
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2608 | |
| Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2609 | Info.reset(DeducedArgumentList); |
| 2610 | |
| 2611 | // Substitute the deduced template arguments into the template |
| 2612 | // arguments of the class template partial specialization, and |
| 2613 | // verify that the instantiated template arguments are both valid |
| 2614 | // and are equivalent to the template arguments originally provided |
| 2615 | // to the class template. |
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2616 | LocalInstantiationScope InstScope(S); |
| Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2617 | auto *Template = Partial->getSpecializedTemplate(); |
| 2618 | const ASTTemplateArgumentListInfo *PartialTemplArgInfo = |
| 2619 | Partial->getTemplateArgsAsWritten(); |
| 2620 | const TemplateArgumentLoc *PartialTemplateArgs = |
| 2621 | PartialTemplArgInfo->getTemplateArgs(); |
| Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2622 | |
| Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2623 | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, |
| 2624 | PartialTemplArgInfo->RAngleLoc); |
| Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2625 | |
| Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2626 | if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs, |
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2627 | InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { |
| 2628 | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; |
| 2629 | if (ParamIdx >= Partial->getTemplateParameters()->size()) |
| 2630 | ParamIdx = Partial->getTemplateParameters()->size() - 1; |
| 2631 | |
| Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2632 | Decl *Param = const_cast<NamedDecl *>( |
| 2633 | Partial->getTemplateParameters()->getParam(ParamIdx)); |
| Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2634 | Info.Param = makeTemplateParameter(Param); |
| 2635 | Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); |
| 2636 | return Sema::TDK_SubstitutionFailure; |
| Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2637 | } |
| 2638 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2639 | SmallVector<TemplateArgument, 4> ConvertedInstArgs; |
| Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2640 | if (S.CheckTemplateArgumentList(Template, Partial->getLocation(), InstArgs, |
| 2641 | false, ConvertedInstArgs)) |
| Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2642 | return Sema::TDK_SubstitutionFailure; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2643 | |
| Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 2644 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2645 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2646 | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; |
| Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2647 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { |
| Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 2648 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
| Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2649 | Info.FirstArg = TemplateArgs[I]; |
| 2650 | Info.SecondArg = InstArg; |
| 2651 | return Sema::TDK_NonDeducedMismatch; |
| 2652 | } |
| 2653 | } |
| 2654 | |
| 2655 | if (Trap.hasErrorOccurred()) |
| 2656 | return Sema::TDK_SubstitutionFailure; |
| 2657 | |
| 2658 | return Sema::TDK_Success; |
| 2659 | } |
| 2660 | |
| Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2661 | /// Complete template argument deduction for a class or variable template, |
| 2662 | /// when partial ordering against a partial specialization. |
| 2663 | // FIXME: Factor out duplication with partial specialization version above. |
| Benjamin Kramer | 357c9e1 | 2017-02-11 12:21:17 +0000 | [diff] [blame] | 2664 | static Sema::TemplateDeductionResult FinishTemplateArgumentDeduction( |
| Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2665 | Sema &S, TemplateDecl *Template, bool PartialOrdering, |
| 2666 | const TemplateArgumentList &TemplateArgs, |
| 2667 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2668 | TemplateDeductionInfo &Info) { |
| 2669 | // Unevaluated SFINAE context. |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2670 | EnterExpressionEvaluationContext Unevaluated( |
| 2671 | S, Sema::ExpressionEvaluationContext::Unevaluated); |
| Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 2672 | Sema::SFINAETrap Trap(S); |
| 2673 | |
| 2674 | Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Template)); |
| 2675 | |
| 2676 | // C++ [temp.deduct.type]p2: |
| 2677 | // [...] or if any template argument remains neither deduced nor |
| 2678 | // explicitly specified, template argument deduction fails. |
| 2679 | SmallVector<TemplateArgument, 4> Builder; |
| 2680 | if (auto Result = ConvertDeducedTemplateArguments( |
| 2681 | S, Template, /*IsDeduced*/PartialOrdering, Deduced, Info, Builder)) |
| 2682 | return Result; |
| 2683 | |
| 2684 | // Check that we produced the correct argument list. |
| 2685 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| 2686 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
| 2687 | TemplateArgument InstArg = Builder[I]; |
| 2688 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg, |
| 2689 | /*PackExpansionMatchesPack*/true)) { |
| 2690 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
| 2691 | Info.FirstArg = TemplateArgs[I]; |
| 2692 | Info.SecondArg = InstArg; |
| 2693 | return Sema::TDK_NonDeducedMismatch; |
| 2694 | } |
| 2695 | } |
| 2696 | |
| 2697 | if (Trap.hasErrorOccurred()) |
| 2698 | return Sema::TDK_SubstitutionFailure; |
| 2699 | |
| 2700 | return Sema::TDK_Success; |
| 2701 | } |
| 2702 | |
| 2703 | |
| Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2704 | /// \brief Perform template argument deduction to determine whether |
| Larisse Voufo | 833b05a | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 2705 | /// the given template arguments match the given class template |
| Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2706 | /// partial specialization per C++ [temp.class.spec.match]. |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2707 | Sema::TemplateDeductionResult |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2708 | Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2709 | const TemplateArgumentList &TemplateArgs, |
| 2710 | TemplateDeductionInfo &Info) { |
| Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 2711 | if (Partial->isInvalidDecl()) |
| 2712 | return TDK_Invalid; |
| 2713 | |
| Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2714 | // C++ [temp.class.spec.match]p2: |
| 2715 | // A partial specialization matches a given actual template |
| 2716 | // argument list if the template arguments of the partial |
| 2717 | // specialization can be deduced from the actual template argument |
| 2718 | // list (14.8.2). |
| Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2719 | |
| 2720 | // Unevaluated SFINAE context. |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2721 | EnterExpressionEvaluationContext Unevaluated( |
| 2722 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
| Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2723 | SFINAETrap Trap(*this); |
| Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2724 | |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2725 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2726 | Deduced.resize(Partial->getTemplateParameters()->size()); |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2727 | if (TemplateDeductionResult Result |
| Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2728 | = ::DeduceTemplateArguments(*this, |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2729 | Partial->getTemplateParameters(), |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2730 | Partial->getTemplateArgs(), |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2731 | TemplateArgs, Info, Deduced)) |
| 2732 | return Result; |
| Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 2733 | |
| Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2734 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
| Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2735 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2736 | Info); |
| Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2737 | if (Inst.isInvalid()) |
| Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2738 | return TDK_InstantiationDepth; |
| Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2739 | |
| Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2740 | if (Trap.hasErrorOccurred()) |
| Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2741 | return Sema::TDK_SubstitutionFailure; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2742 | |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2743 | return ::FinishTemplateArgumentDeduction( |
| 2744 | *this, Partial, /*PartialOrdering=*/false, TemplateArgs, Deduced, Info); |
| Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2745 | } |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2746 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2747 | /// \brief Perform template argument deduction to determine whether |
| 2748 | /// the given template arguments match the given variable template |
| 2749 | /// partial specialization per C++ [temp.class.spec.match]. |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2750 | Sema::TemplateDeductionResult |
| 2751 | Sema::DeduceTemplateArguments(VarTemplatePartialSpecializationDecl *Partial, |
| 2752 | const TemplateArgumentList &TemplateArgs, |
| 2753 | TemplateDeductionInfo &Info) { |
| 2754 | if (Partial->isInvalidDecl()) |
| 2755 | return TDK_Invalid; |
| 2756 | |
| 2757 | // C++ [temp.class.spec.match]p2: |
| 2758 | // A partial specialization matches a given actual template |
| 2759 | // argument list if the template arguments of the partial |
| 2760 | // specialization can be deduced from the actual template argument |
| 2761 | // list (14.8.2). |
| 2762 | |
| 2763 | // Unevaluated SFINAE context. |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2764 | EnterExpressionEvaluationContext Unevaluated( |
| 2765 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2766 | SFINAETrap Trap(*this); |
| 2767 | |
| 2768 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 2769 | Deduced.resize(Partial->getTemplateParameters()->size()); |
| 2770 | if (TemplateDeductionResult Result = ::DeduceTemplateArguments( |
| 2771 | *this, Partial->getTemplateParameters(), Partial->getTemplateArgs(), |
| 2772 | TemplateArgs, Info, Deduced)) |
| 2773 | return Result; |
| 2774 | |
| 2775 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
| Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2776 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2777 | Info); |
| Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2778 | if (Inst.isInvalid()) |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2779 | return TDK_InstantiationDepth; |
| 2780 | |
| 2781 | if (Trap.hasErrorOccurred()) |
| 2782 | return Sema::TDK_SubstitutionFailure; |
| 2783 | |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 2784 | return ::FinishTemplateArgumentDeduction( |
| 2785 | *this, Partial, /*PartialOrdering=*/false, TemplateArgs, Deduced, Info); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
| Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2788 | /// \brief Determine whether the given type T is a simple-template-id type. |
| 2789 | static bool isSimpleTemplateIdType(QualType T) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2790 | if (const TemplateSpecializationType *Spec |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2791 | = T->getAs<TemplateSpecializationType>()) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2792 | return Spec->getTemplateName().getAsTemplateDecl() != nullptr; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2793 | |
| Richard Smith | 1363e8f | 2017-09-07 07:22:36 +0000 | [diff] [blame] | 2794 | // C++17 [temp.local]p2: |
| 2795 | // the injected-class-name [...] is equivalent to the template-name followed |
| 2796 | // by the template-arguments of the class template specialization or partial |
| 2797 | // specialization enclosed in <> |
| 2798 | // ... which means it's equivalent to a simple-template-id. |
| 2799 | // |
| 2800 | // This only arises during class template argument deduction for a copy |
| 2801 | // deduction candidate, where it permits slicing. |
| 2802 | if (T->getAs<InjectedClassNameType>()) |
| 2803 | return true; |
| 2804 | |
| Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2805 | return false; |
| 2806 | } |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2807 | |
| 2808 | /// \brief Substitute the explicitly-provided template arguments into the |
| 2809 | /// given function template according to C++ [temp.arg.explicit]. |
| 2810 | /// |
| 2811 | /// \param FunctionTemplate the function template into which the explicit |
| 2812 | /// template arguments will be substituted. |
| 2813 | /// |
| James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2814 | /// \param ExplicitTemplateArgs the explicitly-specified template |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2815 | /// arguments. |
| 2816 | /// |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2817 | /// \param Deduced the deduced template arguments, which will be populated |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2818 | /// with the converted and checked explicit template arguments. |
| 2819 | /// |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2820 | /// \param ParamTypes will be populated with the instantiated function |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2821 | /// parameters. |
| 2822 | /// |
| 2823 | /// \param FunctionType if non-NULL, the result type of the function template |
| 2824 | /// will also be instantiated and the pointed-to value will be updated with |
| 2825 | /// the instantiated function type. |
| 2826 | /// |
| 2827 | /// \param Info if substitution fails for any reason, this object will be |
| 2828 | /// populated with more information about the failure. |
| 2829 | /// |
| 2830 | /// \returns TDK_Success if substitution was successful, or some failure |
| 2831 | /// condition. |
| 2832 | Sema::TemplateDeductionResult |
| 2833 | Sema::SubstituteExplicitTemplateArguments( |
| 2834 | FunctionTemplateDecl *FunctionTemplate, |
| Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2835 | TemplateArgumentListInfo &ExplicitTemplateArgs, |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2836 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2837 | SmallVectorImpl<QualType> &ParamTypes, |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2838 | QualType *FunctionType, |
| 2839 | TemplateDeductionInfo &Info) { |
| 2840 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 2841 | TemplateParameterList *TemplateParams |
| 2842 | = FunctionTemplate->getTemplateParameters(); |
| 2843 | |
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2844 | if (ExplicitTemplateArgs.size() == 0) { |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2845 | // No arguments to substitute; just copy over the parameter types and |
| 2846 | // fill in the function type. |
| David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2847 | for (auto P : Function->parameters()) |
| Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 2848 | ParamTypes.push_back(P->getType()); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2849 | |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2850 | if (FunctionType) |
| 2851 | *FunctionType = Function->getType(); |
| 2852 | return TDK_Success; |
| 2853 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2854 | |
| Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2855 | // Unevaluated SFINAE context. |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 2856 | EnterExpressionEvaluationContext Unevaluated( |
| 2857 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2858 | SFINAETrap Trap(*this); |
| 2859 | |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2860 | // C++ [temp.arg.explicit]p3: |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2861 | // Template arguments that are present shall be specified in the |
| 2862 | // declaration order of their corresponding template-parameters. The |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2863 | // template argument list shall not specify more template-arguments than |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2864 | // there are corresponding template-parameters. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2865 | SmallVector<TemplateArgument, 4> Builder; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2866 | |
| 2867 | // Enter a new template instantiation context where we check the |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2868 | // explicitly-specified template arguments against this function template, |
| 2869 | // and then substitute them into the function parameter types. |
| Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 2870 | SmallVector<TemplateArgument, 4> DeducedArgs; |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 2871 | InstantiatingTemplate Inst( |
| 2872 | *this, Info.getLocation(), FunctionTemplate, DeducedArgs, |
| 2873 | CodeSynthesisContext::ExplicitTemplateArgumentSubstitution, Info); |
| Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2874 | if (Inst.isInvalid()) |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2875 | return TDK_InstantiationDepth; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2876 | |
| Richard Smith | 11255ec | 2017-01-18 19:19:22 +0000 | [diff] [blame] | 2877 | if (CheckTemplateArgumentList(FunctionTemplate, SourceLocation(), |
| 2878 | ExplicitTemplateArgs, true, Builder, false) || |
| 2879 | Trap.hasErrorOccurred()) { |
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2880 | unsigned Index = Builder.size(); |
| Douglas Gregor | 62c281a | 2010-05-09 01:26:06 +0000 | [diff] [blame] | 2881 | if (Index >= TemplateParams->size()) |
| 2882 | Index = TemplateParams->size() - 1; |
| 2883 | Info.Param = makeTemplateParameter(TemplateParams->getParam(Index)); |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2884 | return TDK_InvalidExplicitArguments; |
| Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 2885 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2886 | |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2887 | // Form the template argument list from the explicitly-specified |
| 2888 | // template arguments. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2889 | TemplateArgumentList *ExplicitArgumentList |
| David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2890 | = TemplateArgumentList::CreateCopy(Context, Builder); |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2891 | Info.reset(ExplicitArgumentList); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2892 | |
| John McCall | 036855a | 2010-10-12 19:40:14 +0000 | [diff] [blame] | 2893 | // Template argument deduction and the final substitution should be |
| 2894 | // done in the context of the templated declaration. Explicit |
| 2895 | // argument substitution, on the other hand, needs to happen in the |
| 2896 | // calling context. |
| 2897 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
| 2898 | |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2899 | // If we deduced template arguments for a template parameter pack, |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2900 | // note that the template argument pack is partially substituted and record |
| 2901 | // the explicit template arguments. They'll be used as part of deduction |
| 2902 | // for this template parameter pack. |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2903 | for (unsigned I = 0, N = Builder.size(); I != N; ++I) { |
| 2904 | const TemplateArgument &Arg = Builder[I]; |
| 2905 | if (Arg.getKind() == TemplateArgument::Pack) { |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2906 | CurrentInstantiationScope->SetPartiallySubstitutedPack( |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2907 | TemplateParams->getParam(I), |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2908 | Arg.pack_begin(), |
| 2909 | Arg.pack_size()); |
| 2910 | break; |
| 2911 | } |
| 2912 | } |
| 2913 | |
| Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2914 | const FunctionProtoType *Proto |
| 2915 | = Function->getType()->getAs<FunctionProtoType>(); |
| 2916 | assert(Proto && "Function template does not have a prototype?"); |
| 2917 | |
| Richard Smith | 70b1304 | 2015-01-09 01:19:56 +0000 | [diff] [blame] | 2918 | // Isolate our substituted parameters from our caller. |
| 2919 | LocalInstantiationScope InstScope(*this, /*MergeWithOuterScope*/true); |
| 2920 | |
| John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2921 | ExtParameterInfoBuilder ExtParamInfos; |
| 2922 | |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2923 | // Instantiate the types of each of the function parameters given the |
| Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2924 | // explicitly-specified template arguments. If the function has a trailing |
| 2925 | // return type, substitute it after the arguments to ensure we substitute |
| 2926 | // in lexical order. |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2927 | if (Proto->hasTrailingReturn()) { |
| David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2928 | if (SubstParmTypes(Function->getLocation(), Function->parameters(), |
| John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2929 | Proto->getExtParameterInfosOrNull(), |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2930 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2931 | ParamTypes, /*params*/ nullptr, ExtParamInfos)) |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2932 | return TDK_SubstitutionFailure; |
| 2933 | } |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2934 | |
| Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2935 | // Instantiate the return type. |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2936 | QualType ResultType; |
| 2937 | { |
| 2938 | // C++11 [expr.prim.general]p3: |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2939 | // If a declaration declares a member function or member function |
| 2940 | // 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] | 2941 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2942 | // and the end of the function-definition, member-declarator, or |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2943 | // declarator. |
| 2944 | unsigned ThisTypeQuals = 0; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2945 | CXXRecordDecl *ThisContext = nullptr; |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2946 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
| 2947 | ThisContext = Method->getParent(); |
| 2948 | ThisTypeQuals = Method->getTypeQualifiers(); |
| 2949 | } |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2950 | |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2951 | CXXThisScopeRAII ThisScope(*this, ThisContext, ThisTypeQuals, |
| Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2952 | getLangOpts().CPlusPlus11); |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2953 | |
| 2954 | ResultType = |
| 2955 | SubstType(Proto->getReturnType(), |
| 2956 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 2957 | Function->getTypeSpecStartLoc(), Function->getDeclName()); |
| Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2958 | if (ResultType.isNull() || Trap.hasErrorOccurred()) |
| 2959 | return TDK_SubstitutionFailure; |
| 2960 | } |
| John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2961 | |
| Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2962 | // Instantiate the types of each of the function parameters given the |
| 2963 | // explicitly-specified template arguments if we didn't do so earlier. |
| 2964 | if (!Proto->hasTrailingReturn() && |
| David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2965 | SubstParmTypes(Function->getLocation(), Function->parameters(), |
| John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2966 | Proto->getExtParameterInfosOrNull(), |
| Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2967 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2968 | ParamTypes, /*params*/ nullptr, ExtParamInfos)) |
| Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2969 | return TDK_SubstitutionFailure; |
| 2970 | |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2971 | if (FunctionType) { |
| John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2972 | auto EPI = Proto->getExtProtoInfo(); |
| 2973 | EPI.ExtParameterInfos = ExtParamInfos.getPointerOrNull(ParamTypes.size()); |
| Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 2974 | |
| 2975 | // In C++1z onwards, exception specifications are part of the function type, |
| 2976 | // so substitution into the type must also substitute into the exception |
| 2977 | // specification. |
| 2978 | SmallVector<QualType, 4> ExceptionStorage; |
| Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 2979 | if (getLangOpts().CPlusPlus17 && |
| Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 2980 | SubstExceptionSpec( |
| 2981 | Function->getLocation(), EPI.ExceptionSpec, ExceptionStorage, |
| 2982 | MultiLevelTemplateArgumentList(*ExplicitArgumentList))) |
| 2983 | return TDK_SubstitutionFailure; |
| 2984 | |
| Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2985 | *FunctionType = BuildFunctionType(ResultType, ParamTypes, |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2986 | Function->getLocation(), |
| Eli Friedman | d8725a9 | 2010-08-05 02:54:05 +0000 | [diff] [blame] | 2987 | Function->getDeclName(), |
| John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2988 | EPI); |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2989 | if (FunctionType->isNull() || Trap.hasErrorOccurred()) |
| 2990 | return TDK_SubstitutionFailure; |
| 2991 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2992 | |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2993 | // C++ [temp.arg.explicit]p2: |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2994 | // Trailing template arguments that can be deduced (14.8.2) may be |
| 2995 | // omitted from the list of explicit template-arguments. If all of the |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2996 | // template arguments can be deduced, they may all be omitted; in this |
| 2997 | // case, the empty template argument list <> itself may also be omitted. |
| 2998 | // |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2999 | // Take all of the explicitly-specified arguments and put them into |
| 3000 | // the set of deduced template arguments. Explicitly-specified |
| 3001 | // parameter packs, however, will be set to NULL since the deduction |
| 3002 | // mechanisms handle explicitly-specified argument packs directly. |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3003 | Deduced.reserve(TemplateParams->size()); |
| Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 3004 | for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) { |
| 3005 | const TemplateArgument &Arg = ExplicitArgumentList->get(I); |
| 3006 | if (Arg.getKind() == TemplateArgument::Pack) |
| 3007 | Deduced.push_back(DeducedTemplateArgument()); |
| 3008 | else |
| 3009 | Deduced.push_back(Arg); |
| 3010 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3011 | |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3012 | return TDK_Success; |
| 3013 | } |
| 3014 | |
| Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3015 | /// \brief Check whether the deduced argument type for a call to a function |
| 3016 | /// template matches the actual argument type per C++ [temp.deduct.call]p4. |
| Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3017 | static Sema::TemplateDeductionResult |
| 3018 | CheckOriginalCallArgDeduction(Sema &S, TemplateDeductionInfo &Info, |
| 3019 | Sema::OriginalCallArg OriginalArg, |
| Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3020 | QualType DeducedA) { |
| 3021 | ASTContext &Context = S.Context; |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3022 | |
| Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3023 | auto Failed = [&]() -> Sema::TemplateDeductionResult { |
| 3024 | Info.FirstArg = TemplateArgument(DeducedA); |
| 3025 | Info.SecondArg = TemplateArgument(OriginalArg.OriginalArgType); |
| 3026 | Info.CallArgIndex = OriginalArg.ArgIdx; |
| 3027 | return OriginalArg.DecomposedParam ? Sema::TDK_DeducedMismatchNested |
| 3028 | : Sema::TDK_DeducedMismatch; |
| 3029 | }; |
| 3030 | |
| Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3031 | QualType A = OriginalArg.OriginalArgType; |
| 3032 | QualType OriginalParamType = OriginalArg.OriginalParamType; |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3033 | |
| Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3034 | // Check for type equality (top-level cv-qualifiers are ignored). |
| 3035 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3036 | return Sema::TDK_Success; |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3037 | |
| Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3038 | // Strip off references on the argument types; they aren't needed for |
| 3039 | // the following checks. |
| 3040 | if (const ReferenceType *DeducedARef = DeducedA->getAs<ReferenceType>()) |
| 3041 | DeducedA = DeducedARef->getPointeeType(); |
| 3042 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
| 3043 | A = ARef->getPointeeType(); |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3044 | |
| Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3045 | // C++ [temp.deduct.call]p4: |
| 3046 | // [...] However, there are three cases that allow a difference: |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3047 | // - If the original P is a reference type, the deduced A (i.e., the |
| 3048 | // type referred to by the reference) can be more cv-qualified than |
| Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3049 | // the transformed A. |
| 3050 | if (const ReferenceType *OriginalParamRef |
| 3051 | = OriginalParamType->getAs<ReferenceType>()) { |
| 3052 | // We don't want to keep the reference around any more. |
| 3053 | OriginalParamType = OriginalParamRef->getPointeeType(); |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3054 | |
| Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 3055 | // FIXME: Resolve core issue (no number yet): if the original P is a |
| 3056 | // reference type and the transformed A is function type "noexcept F", |
| 3057 | // the deduced A can be F. |
| 3058 | QualType Tmp; |
| 3059 | if (A->isFunctionType() && S.IsFunctionConversion(A, DeducedA, Tmp)) |
| Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3060 | return Sema::TDK_Success; |
| Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 3061 | |
| Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3062 | Qualifiers AQuals = A.getQualifiers(); |
| 3063 | Qualifiers DeducedAQuals = DeducedA.getQualifiers(); |
| Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 3064 | |
| Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 3065 | // Under Objective-C++ ARC, the deduced type may have implicitly |
| 3066 | // been given strong or (when dealing with a const reference) |
| 3067 | // unsafe_unretained lifetime. If so, update the original |
| 3068 | // qualifiers to include this lifetime. |
| Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 3069 | if (S.getLangOpts().ObjCAutoRefCount && |
| Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 3070 | ((DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_Strong && |
| 3071 | AQuals.getObjCLifetime() == Qualifiers::OCL_None) || |
| 3072 | (DeducedAQuals.hasConst() && |
| 3073 | DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone))) { |
| 3074 | AQuals.setObjCLifetime(DeducedAQuals.getObjCLifetime()); |
| Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 3075 | } |
| 3076 | |
| Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3077 | if (AQuals == DeducedAQuals) { |
| 3078 | // Qualifiers match; there's nothing to do. |
| 3079 | } else if (!DeducedAQuals.compatiblyIncludes(AQuals)) { |
| Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3080 | return Failed(); |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3081 | } else { |
| Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3082 | // Qualifiers are compatible, so have the argument type adopt the |
| 3083 | // deduced argument type's qualifiers as if we had performed the |
| 3084 | // qualification conversion. |
| 3085 | A = Context.getQualifiedType(A.getUnqualifiedType(), DeducedAQuals); |
| 3086 | } |
| 3087 | } |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3088 | |
| 3089 | // - The transformed A can be another pointer or pointer to member |
| Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3090 | // type that can be converted to the deduced A via a function pointer |
| 3091 | // conversion and/or a qualification conversion. |
| Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 3092 | // |
| Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 3093 | // Also allow conversions which merely strip __attribute__((noreturn)) from |
| 3094 | // function types (recursively). |
| Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3095 | bool ObjCLifetimeConversion = false; |
| Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 3096 | QualType ResultTy; |
| Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3097 | if ((A->isAnyPointerType() || A->isMemberPointerType()) && |
| Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 3098 | (S.IsQualificationConversion(A, DeducedA, false, |
| 3099 | ObjCLifetimeConversion) || |
| Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 3100 | S.IsFunctionConversion(A, DeducedA, ResultTy))) |
| Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3101 | return Sema::TDK_Success; |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3102 | |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3103 | // - 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] | 3104 | // transformed A can be a derived class of the deduced A. [...] |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3105 | // [...] Likewise, if P is a pointer to a class of the form |
| 3106 | // simple-template-id, the transformed A can be a pointer to a |
| Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3107 | // derived class pointed to by the deduced A. |
| 3108 | if (const PointerType *OriginalParamPtr |
| 3109 | = OriginalParamType->getAs<PointerType>()) { |
| 3110 | if (const PointerType *DeducedAPtr = DeducedA->getAs<PointerType>()) { |
| 3111 | if (const PointerType *APtr = A->getAs<PointerType>()) { |
| 3112 | if (A->getPointeeType()->isRecordType()) { |
| 3113 | OriginalParamType = OriginalParamPtr->getPointeeType(); |
| 3114 | DeducedA = DeducedAPtr->getPointeeType(); |
| 3115 | A = APtr->getPointeeType(); |
| 3116 | } |
| 3117 | } |
| 3118 | } |
| 3119 | } |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3120 | |
| Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3121 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3122 | return Sema::TDK_Success; |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3123 | |
| Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3124 | if (A->isRecordType() && isSimpleTemplateIdType(OriginalParamType) && |
| Richard Smith | 148bc6a | 2018-02-20 23:47:12 +0000 | [diff] [blame] | 3125 | S.IsDerivedFrom(Info.getLocation(), A, DeducedA)) |
| Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3126 | return Sema::TDK_Success; |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3127 | |
| Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3128 | return Failed(); |
| Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 3129 | } |
| 3130 | |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3131 | /// Find the pack index for a particular parameter index in an instantiation of |
| 3132 | /// a function template with specific arguments. |
| 3133 | /// |
| 3134 | /// \return The pack index for whichever pack produced this parameter, or -1 |
| 3135 | /// if this was not produced by a parameter. Intended to be used as the |
| 3136 | /// ArgumentPackSubstitutionIndex for further substitutions. |
| 3137 | // FIXME: We should track this in OriginalCallArgs so we don't need to |
| 3138 | // reconstruct it here. |
| 3139 | static unsigned getPackIndexForParam(Sema &S, |
| 3140 | FunctionTemplateDecl *FunctionTemplate, |
| 3141 | const MultiLevelTemplateArgumentList &Args, |
| 3142 | unsigned ParamIdx) { |
| 3143 | unsigned Idx = 0; |
| 3144 | for (auto *PD : FunctionTemplate->getTemplatedDecl()->parameters()) { |
| 3145 | if (PD->isParameterPack()) { |
| 3146 | unsigned NumExpansions = |
| 3147 | S.getNumArgumentsInExpansion(PD->getType(), Args).getValueOr(1); |
| 3148 | if (Idx + NumExpansions > ParamIdx) |
| 3149 | return ParamIdx - Idx; |
| 3150 | Idx += NumExpansions; |
| 3151 | } else { |
| 3152 | if (Idx == ParamIdx) |
| 3153 | return -1; // Not a pack expansion |
| 3154 | ++Idx; |
| 3155 | } |
| 3156 | } |
| 3157 | |
| 3158 | llvm_unreachable("parameter index would not be produced from template"); |
| 3159 | } |
| 3160 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3161 | /// \brief Finish template argument deduction for a function template, |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3162 | /// checking the deduced template arguments for completeness and forming |
| 3163 | /// the function template specialization. |
| Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3164 | /// |
| 3165 | /// \param OriginalCallArgs If non-NULL, the original call arguments against |
| 3166 | /// which the deduced argument types should be compared. |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3167 | Sema::TemplateDeductionResult Sema::FinishTemplateArgumentDeduction( |
| 3168 | FunctionTemplateDecl *FunctionTemplate, |
| 3169 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3170 | unsigned NumExplicitlySpecified, FunctionDecl *&Specialization, |
| 3171 | TemplateDeductionInfo &Info, |
| 3172 | SmallVectorImpl<OriginalCallArg> const *OriginalCallArgs, |
| 3173 | bool PartialOverloading, llvm::function_ref<bool()> CheckNonDependent) { |
| Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3174 | // Unevaluated SFINAE context. |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 3175 | EnterExpressionEvaluationContext Unevaluated( |
| 3176 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3177 | SFINAETrap Trap(*this); |
| 3178 | |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3179 | // Enter a new template instantiation context while we instantiate the |
| 3180 | // actual function declaration. |
| Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3181 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
| Richard Smith | 696e312 | 2017-02-23 01:43:54 +0000 | [diff] [blame] | 3182 | InstantiatingTemplate Inst( |
| 3183 | *this, Info.getLocation(), FunctionTemplate, DeducedArgs, |
| 3184 | CodeSynthesisContext::DeducedTemplateArgumentSubstitution, Info); |
| Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 3185 | if (Inst.isInvalid()) |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3186 | return TDK_InstantiationDepth; |
| 3187 | |
| John McCall | e23b871 | 2010-04-29 01:18:58 +0000 | [diff] [blame] | 3188 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
| John McCall | 80e58cd | 2010-04-29 00:35:03 +0000 | [diff] [blame] | 3189 | |
| Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3190 | // C++ [temp.deduct.type]p2: |
| 3191 | // [...] or if any template argument remains neither deduced nor |
| 3192 | // explicitly specified, template argument deduction fails. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3193 | SmallVector<TemplateArgument, 4> Builder; |
| Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 3194 | if (auto Result = ConvertDeducedTemplateArguments( |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 3195 | *this, FunctionTemplate, /*IsDeduced*/true, Deduced, Info, Builder, |
| Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame] | 3196 | CurrentInstantiationScope, NumExplicitlySpecified, |
| 3197 | PartialOverloading)) |
| 3198 | return Result; |
| Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3199 | |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3200 | // C++ [temp.deduct.call]p10: [DR1391] |
| 3201 | // If deduction succeeds for all parameters that contain |
| 3202 | // template-parameters that participate in template argument deduction, |
| 3203 | // and all template arguments are explicitly specified, deduced, or |
| 3204 | // obtained from default template arguments, remaining parameters are then |
| 3205 | // compared with the corresponding arguments. For each remaining parameter |
| 3206 | // P with a type that was non-dependent before substitution of any |
| 3207 | // explicitly-specified template arguments, if the corresponding argument |
| 3208 | // A cannot be implicitly converted to P, deduction fails. |
| 3209 | if (CheckNonDependent()) |
| 3210 | return TDK_NonDependentConversionFailure; |
| 3211 | |
| Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3212 | // Form the template argument list from the deduced template arguments. |
| 3213 | TemplateArgumentList *DeducedArgumentList |
| David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 3214 | = TemplateArgumentList::CreateCopy(Context, Builder); |
| Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 3215 | Info.reset(DeducedArgumentList); |
| 3216 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3217 | // Substitute the deduced template arguments into the function template |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3218 | // declaration to produce the function template specialization. |
| Douglas Gregor | 1614237 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 3219 | DeclContext *Owner = FunctionTemplate->getDeclContext(); |
| 3220 | if (FunctionTemplate->getFriendObjectKind()) |
| 3221 | Owner = FunctionTemplate->getLexicalDeclContext(); |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3222 | MultiLevelTemplateArgumentList SubstArgs(*DeducedArgumentList); |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3223 | Specialization = cast_or_null<FunctionDecl>( |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3224 | SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner, SubstArgs)); |
| Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 3225 | if (!Specialization || Specialization->isInvalidDecl()) |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3226 | return TDK_SubstitutionFailure; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3227 | |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3228 | assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == |
| Douglas Gregor | 31fae89 | 2009-09-15 18:26:13 +0000 | [diff] [blame] | 3229 | FunctionTemplate->getCanonicalDecl()); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3230 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3231 | // If the template argument list is owned by the function template |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3232 | // specialization, release it. |
| Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 3233 | if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList && |
| 3234 | !Trap.hasErrorOccurred()) |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3235 | Info.take(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3236 | |
| Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 3237 | // There may have been an error that did not prevent us from constructing a |
| 3238 | // declaration. Mark the declaration invalid and return with a substitution |
| 3239 | // failure. |
| 3240 | if (Trap.hasErrorOccurred()) { |
| 3241 | Specialization->setInvalidDecl(true); |
| 3242 | return TDK_SubstitutionFailure; |
| 3243 | } |
| 3244 | |
| Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3245 | if (OriginalCallArgs) { |
| 3246 | // C++ [temp.deduct.call]p4: |
| 3247 | // In general, the deduction process attempts to find template argument |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3248 | // 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] | 3249 | // is transformed as described above). [...] |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3250 | llvm::SmallDenseMap<std::pair<unsigned, QualType>, QualType> DeducedATypes; |
| Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3251 | for (unsigned I = 0, N = OriginalCallArgs->size(); I != N; ++I) { |
| 3252 | OriginalCallArg OriginalArg = (*OriginalCallArgs)[I]; |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3253 | |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3254 | auto ParamIdx = OriginalArg.ArgIdx; |
| Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3255 | if (ParamIdx >= Specialization->getNumParams()) |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3256 | // FIXME: This presumably means a pack ended up smaller than we |
| 3257 | // expected while deducing. Should this not result in deduction |
| 3258 | // failure? Can it even happen? |
| Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3259 | continue; |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3260 | |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3261 | QualType DeducedA; |
| 3262 | if (!OriginalArg.DecomposedParam) { |
| 3263 | // P is one of the function parameters, just look up its substituted |
| 3264 | // type. |
| 3265 | DeducedA = Specialization->getParamDecl(ParamIdx)->getType(); |
| 3266 | } else { |
| 3267 | // P is a decomposed element of a parameter corresponding to a |
| 3268 | // braced-init-list argument. Substitute back into P to find the |
| 3269 | // deduced A. |
| 3270 | QualType &CacheEntry = |
| 3271 | DeducedATypes[{ParamIdx, OriginalArg.OriginalParamType}]; |
| 3272 | if (CacheEntry.isNull()) { |
| 3273 | ArgumentPackSubstitutionIndexRAII PackIndex( |
| 3274 | *this, getPackIndexForParam(*this, FunctionTemplate, SubstArgs, |
| 3275 | ParamIdx)); |
| 3276 | CacheEntry = |
| 3277 | SubstType(OriginalArg.OriginalParamType, SubstArgs, |
| 3278 | Specialization->getTypeSpecStartLoc(), |
| 3279 | Specialization->getDeclName()); |
| 3280 | } |
| 3281 | DeducedA = CacheEntry; |
| 3282 | } |
| 3283 | |
| Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 3284 | if (auto TDK = |
| 3285 | CheckOriginalCallArgDeduction(*this, Info, OriginalArg, DeducedA)) |
| 3286 | return TDK; |
| Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3287 | } |
| 3288 | } |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3289 | |
| Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3290 | // If we suppressed any diagnostics while performing template argument |
| 3291 | // deduction, and if we haven't already instantiated this declaration, |
| 3292 | // keep track of these diagnostics. They'll be emitted if this specialization |
| 3293 | // is actually used. |
| 3294 | if (Info.diag_begin() != Info.diag_end()) { |
| Craig Topper | 79be4cd | 2013-07-05 04:33:53 +0000 | [diff] [blame] | 3295 | SuppressedDiagnosticsMap::iterator |
| Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3296 | Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl()); |
| 3297 | if (Pos == SuppressedDiagnostics.end()) |
| 3298 | SuppressedDiagnostics[Specialization->getCanonicalDecl()] |
| 3299 | .append(Info.diag_begin(), Info.diag_end()); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3300 | } |
| Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3301 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3302 | return TDK_Success; |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3303 | } |
| 3304 | |
| John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3305 | /// Gets the type of a function for template-argument-deducton |
| 3306 | /// purposes when it's considered as part of an overload set. |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3307 | static QualType GetTypeOfFunction(Sema &S, const OverloadExpr::FindResult &R, |
| John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3308 | FunctionDecl *Fn) { |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3309 | // We may need to deduce the return type of the function now. |
| Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3310 | if (S.getLangOpts().CPlusPlus14 && Fn->getReturnType()->isUndeducedType() && |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3311 | S.DeduceReturnType(Fn, R.Expression->getExprLoc(), /*Diagnose*/ false)) |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 3312 | return {}; |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3313 | |
| John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3314 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
| John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3315 | if (Method->isInstance()) { |
| 3316 | // An instance method that's referenced in a form that doesn't |
| 3317 | // look like a member pointer is just invalid. |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 3318 | if (!R.HasFormOfMemberPointer) |
| 3319 | return {}; |
| John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3320 | |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3321 | return S.Context.getMemberPointerType(Fn->getType(), |
| 3322 | S.Context.getTypeDeclType(Method->getParent()).getTypePtr()); |
| John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3323 | } |
| 3324 | |
| 3325 | if (!R.IsAddressOfOperand) return Fn->getType(); |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3326 | return S.Context.getPointerType(Fn->getType()); |
| John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
| 3329 | /// Apply the deduction rules for overload sets. |
| 3330 | /// |
| 3331 | /// \return the null type if this argument should be treated as an |
| 3332 | /// undeduced context |
| 3333 | static QualType |
| 3334 | ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams, |
| Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3335 | Expr *Arg, QualType ParamType, |
| 3336 | bool ParamWasReference) { |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3337 | |
| John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3338 | OverloadExpr::FindResult R = OverloadExpr::find(Arg); |
| John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3339 | |
| John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3340 | OverloadExpr *Ovl = R.Expression; |
| John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3341 | |
| Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3342 | // C++0x [temp.deduct.call]p4 |
| 3343 | unsigned TDF = 0; |
| 3344 | if (ParamWasReference) |
| 3345 | TDF |= TDF_ParamWithReferenceType; |
| 3346 | if (R.IsAddressOfOperand) |
| 3347 | TDF |= TDF_IgnoreQualifiers; |
| 3348 | |
| John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3349 | // C++0x [temp.deduct.call]p6: |
| 3350 | // When P is a function type, pointer to function type, or pointer |
| 3351 | // to member function type: |
| 3352 | |
| 3353 | if (!ParamType->isFunctionType() && |
| 3354 | !ParamType->isFunctionPointerType() && |
| Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3355 | !ParamType->isMemberFunctionPointerType()) { |
| 3356 | if (Ovl->hasExplicitTemplateArgs()) { |
| 3357 | // But we can still look for an explicit specialization. |
| 3358 | if (FunctionDecl *ExplicitSpec |
| 3359 | = S.ResolveSingleFunctionTemplateSpecialization(Ovl)) |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3360 | return GetTypeOfFunction(S, R, ExplicitSpec); |
| Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3361 | } |
| John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3362 | |
| George Burgess IV | cc2f355 | 2016-03-19 21:51:45 +0000 | [diff] [blame] | 3363 | DeclAccessPair DAP; |
| 3364 | if (FunctionDecl *Viable = |
| 3365 | S.resolveAddressOfOnlyViableOverloadCandidate(Arg, DAP)) |
| 3366 | return GetTypeOfFunction(S, R, Viable); |
| 3367 | |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 3368 | return {}; |
| Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3369 | } |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3370 | |
| Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3371 | // Gather the explicit template arguments, if any. |
| 3372 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 3373 | if (Ovl->hasExplicitTemplateArgs()) |
| James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 3374 | Ovl->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
| John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3375 | QualType Match; |
| John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 3376 | for (UnresolvedSetIterator I = Ovl->decls_begin(), |
| 3377 | E = Ovl->decls_end(); I != E; ++I) { |
| John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3378 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 3379 | |
| Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3380 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) { |
| 3381 | // - If the argument is an overload set containing one or more |
| 3382 | // function templates, the parameter is treated as a |
| 3383 | // non-deduced context. |
| 3384 | if (!Ovl->hasExplicitTemplateArgs()) |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 3385 | return {}; |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3386 | |
| 3387 | // Otherwise, see if we can resolve a function type |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3388 | FunctionDecl *Specialization = nullptr; |
| Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3389 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
| Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3390 | if (S.DeduceTemplateArguments(FunTmpl, &ExplicitTemplateArgs, |
| 3391 | Specialization, Info)) |
| 3392 | continue; |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3393 | |
| Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3394 | D = Specialization; |
| 3395 | } |
| John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3396 | |
| 3397 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3398 | QualType ArgType = GetTypeOfFunction(S, R, Fn); |
| John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3399 | if (ArgType.isNull()) continue; |
| John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3400 | |
| Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3401 | // Function-to-pointer conversion. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3402 | if (!ParamWasReference && ParamType->isPointerType() && |
| Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3403 | ArgType->isFunctionType()) |
| 3404 | ArgType = S.Context.getPointerType(ArgType); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3405 | |
| John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3406 | // - If the argument is an overload set (not containing function |
| 3407 | // templates), trial argument deduction is attempted using each |
| 3408 | // of the members of the set. If deduction succeeds for only one |
| 3409 | // of the overload set members, that member is used as the |
| 3410 | // argument value for the deduction. If deduction succeeds for |
| 3411 | // more than one member of the overload set the parameter is |
| 3412 | // treated as a non-deduced context. |
| 3413 | |
| 3414 | // We do all of this in a fresh context per C++0x [temp.deduct.type]p2: |
| 3415 | // Type deduction is done independently for each P/A pair, and |
| 3416 | // the deduced template argument values are then combined. |
| 3417 | // So we do not reject deductions which were made elsewhere. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3418 | SmallVector<DeducedTemplateArgument, 8> |
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3419 | Deduced(TemplateParams->size()); |
| Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3420 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
| John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3421 | Sema::TemplateDeductionResult Result |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3422 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
| 3423 | ArgType, Info, Deduced, TDF); |
| John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3424 | if (Result) continue; |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 3425 | if (!Match.isNull()) |
| 3426 | return {}; |
| John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3427 | Match = ArgType; |
| 3428 | } |
| 3429 | |
| 3430 | return Match; |
| 3431 | } |
| 3432 | |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3433 | /// \brief Perform the adjustments to the parameter and argument types |
| Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3434 | /// described in C++ [temp.deduct.call]. |
| 3435 | /// |
| 3436 | /// \returns true if the caller should not attempt to perform any template |
| Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3437 | /// argument deduction based on this P/A pair because the argument is an |
| 3438 | /// overloaded function set that could not be resolved. |
| Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3439 | static bool AdjustFunctionParmAndArgTypesForDeduction( |
| 3440 | Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, |
| 3441 | QualType &ParamType, QualType &ArgType, Expr *Arg, unsigned &TDF) { |
| Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3442 | // C++0x [temp.deduct.call]p3: |
| NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3443 | // 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] | 3444 | // are ignored for type deduction. |
| Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3445 | if (ParamType.hasQualifiers()) |
| 3446 | ParamType = ParamType.getUnqualifiedType(); |
| Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3447 | |
| 3448 | // [...] If P is a reference type, the type referred to by P is |
| 3449 | // used for type deduction. |
| Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3450 | const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>(); |
| Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3451 | if (ParamRefType) |
| 3452 | ParamType = ParamRefType->getPointeeType(); |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3453 | |
| Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3454 | // Overload sets usually make this parameter an undeduced context, |
| 3455 | // but there are sometimes special circumstances. Typically |
| 3456 | // involving a template-id-expr. |
| Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3457 | if (ArgType == S.Context.OverloadTy) { |
| 3458 | ArgType = ResolveOverloadForDeduction(S, TemplateParams, |
| 3459 | Arg, ParamType, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3460 | ParamRefType != nullptr); |
| Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3461 | if (ArgType.isNull()) |
| 3462 | return true; |
| 3463 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3464 | |
| Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3465 | if (ParamRefType) { |
| Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3466 | // If the argument has incomplete array type, try to complete its type. |
| Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3467 | if (ArgType->isIncompleteArrayType()) { |
| 3468 | S.completeExprArrayBound(Arg); |
| Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3469 | ArgType = Arg->getType(); |
| Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3470 | } |
| Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3471 | |
| Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3472 | // C++1z [temp.deduct.call]p3: |
| 3473 | // If P is a forwarding reference and the argument is an lvalue, the type |
| 3474 | // "lvalue reference to A" is used in place of A for type deduction. |
| 3475 | if (isForwardingReference(QualType(ParamRefType, 0), FirstInnerIndex) && |
| Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3476 | Arg->isLValue()) |
| 3477 | ArgType = S.Context.getLValueReferenceType(ArgType); |
| 3478 | } else { |
| 3479 | // C++ [temp.deduct.call]p2: |
| 3480 | // If P is not a reference type: |
| 3481 | // - If A is an array type, the pointer type produced by the |
| 3482 | // array-to-pointer standard conversion (4.2) is used in place of |
| 3483 | // A for type deduction; otherwise, |
| 3484 | if (ArgType->isArrayType()) |
| 3485 | ArgType = S.Context.getArrayDecayedType(ArgType); |
| 3486 | // - If A is a function type, the pointer type produced by the |
| 3487 | // function-to-pointer standard conversion (4.3) is used in place |
| 3488 | // of A for type deduction; otherwise, |
| 3489 | else if (ArgType->isFunctionType()) |
| 3490 | ArgType = S.Context.getPointerType(ArgType); |
| 3491 | else { |
| NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3492 | // - 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] | 3493 | // type are ignored for type deduction. |
| Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3494 | ArgType = ArgType.getUnqualifiedType(); |
| Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3495 | } |
| 3496 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3497 | |
| Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3498 | // C++0x [temp.deduct.call]p4: |
| 3499 | // In general, the deduction process attempts to find template argument |
| 3500 | // values that will make the deduced A identical to A (after the type A |
| 3501 | // is transformed as described above). [...] |
| 3502 | TDF = TDF_SkipNonDependent; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3503 | |
| Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3504 | // - If the original P is a reference type, the deduced A (i.e., the |
| 3505 | // type referred to by the reference) can be more cv-qualified than |
| 3506 | // the transformed A. |
| 3507 | if (ParamRefType) |
| 3508 | TDF |= TDF_ParamWithReferenceType; |
| 3509 | // - The transformed A can be another pointer or pointer to member |
| 3510 | // type that can be converted to the deduced A via a qualification |
| 3511 | // conversion (4.4). |
| 3512 | if (ArgType->isPointerType() || ArgType->isMemberPointerType() || |
| 3513 | ArgType->isObjCObjectPointerType()) |
| 3514 | TDF |= TDF_IgnoreQualifiers; |
| 3515 | // - If P is a class and P has the form simple-template-id, then the |
| 3516 | // transformed A can be a derived class of the deduced A. Likewise, |
| 3517 | // if P is a pointer to a class of the form simple-template-id, the |
| 3518 | // transformed A can be a pointer to a derived class pointed to by |
| 3519 | // the deduced A. |
| 3520 | if (isSimpleTemplateIdType(ParamType) || |
| 3521 | (isa<PointerType>(ParamType) && |
| 3522 | isSimpleTemplateIdType( |
| 3523 | ParamType->getAs<PointerType>()->getPointeeType()))) |
| 3524 | TDF |= TDF_DerivedClass; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3525 | |
| Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3526 | return false; |
| 3527 | } |
| 3528 | |
| Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 3529 | static bool |
| 3530 | hasDeducibleTemplateParameters(Sema &S, FunctionTemplateDecl *FunctionTemplate, |
| 3531 | QualType T); |
| Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3532 | |
| Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3533 | static Sema::TemplateDeductionResult DeduceTemplateArgumentsFromCallArgument( |
| Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3534 | Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, |
| 3535 | QualType ParamType, Expr *Arg, TemplateDeductionInfo &Info, |
| Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3536 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3537 | SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3538 | bool DecomposedParam, unsigned ArgIdx, unsigned TDF); |
| Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3539 | |
| 3540 | /// \brief Attempt template argument deduction from an initializer list |
| 3541 | /// deemed to be an argument in a function call. |
| Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3542 | static Sema::TemplateDeductionResult DeduceFromInitializerList( |
| 3543 | Sema &S, TemplateParameterList *TemplateParams, QualType AdjustedParamType, |
| 3544 | InitListExpr *ILE, TemplateDeductionInfo &Info, |
| 3545 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3546 | SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, unsigned ArgIdx, |
| 3547 | unsigned TDF) { |
| Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3548 | // C++ [temp.deduct.call]p1: (CWG 1591) |
| 3549 | // If removing references and cv-qualifiers from P gives |
| 3550 | // std::initializer_list<P0> or P0[N] for some P0 and N and the argument is |
| 3551 | // a non-empty initializer list, then deduction is performed instead for |
| 3552 | // each element of the initializer list, taking P0 as a function template |
| 3553 | // parameter type and the initializer element as its argument |
| 3554 | // |
| Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3555 | // We've already removed references and cv-qualifiers here. |
| Richard Smith | 9c5534c | 2017-01-05 04:16:30 +0000 | [diff] [blame] | 3556 | if (!ILE->getNumInits()) |
| 3557 | return Sema::TDK_Success; |
| 3558 | |
| Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3559 | QualType ElTy; |
| 3560 | auto *ArrTy = S.Context.getAsArrayType(AdjustedParamType); |
| 3561 | if (ArrTy) |
| 3562 | ElTy = ArrTy->getElementType(); |
| 3563 | else if (!S.isStdInitializerList(AdjustedParamType, &ElTy)) { |
| 3564 | // Otherwise, an initializer list argument causes the parameter to be |
| 3565 | // considered a non-deduced context |
| 3566 | return Sema::TDK_Success; |
| Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3567 | } |
| Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3568 | |
| Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3569 | // Deduction only needs to be done for dependent types. |
| 3570 | if (ElTy->isDependentType()) { |
| 3571 | for (Expr *E : ILE->inits()) { |
| Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3572 | if (auto Result = DeduceTemplateArgumentsFromCallArgument( |
| Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3573 | S, TemplateParams, 0, ElTy, E, Info, Deduced, OriginalCallArgs, true, |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3574 | ArgIdx, TDF)) |
| Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3575 | return Result; |
| Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3576 | } |
| 3577 | } |
| Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3578 | |
| 3579 | // in the P0[N] case, if N is a non-type template parameter, N is deduced |
| 3580 | // from the length of the initializer list. |
| Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3581 | if (auto *DependentArrTy = dyn_cast_or_null<DependentSizedArrayType>(ArrTy)) { |
| Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3582 | // Determine the array bound is something we can deduce. |
| 3583 | if (NonTypeTemplateParmDecl *NTTP = |
| Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3584 | getDeducedParameterFromExpr(Info, DependentArrTy->getSizeExpr())) { |
| Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3585 | // We can perform template argument deduction for the given non-type |
| 3586 | // template parameter. |
| Richard Smith | 7fa88bb | 2017-02-21 07:22:31 +0000 | [diff] [blame] | 3587 | // C++ [temp.deduct.type]p13: |
| 3588 | // The type of N in the type T[N] is std::size_t. |
| 3589 | QualType T = S.Context.getSizeType(); |
| 3590 | llvm::APInt Size(S.Context.getIntWidth(T), ILE->getNumInits()); |
| Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3591 | if (auto Result = DeduceNonTypeTemplateArgument( |
| Richard Smith | 7fa88bb | 2017-02-21 07:22:31 +0000 | [diff] [blame] | 3592 | S, TemplateParams, NTTP, llvm::APSInt(Size), T, |
| Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3593 | /*ArrayBound=*/true, Info, Deduced)) |
| 3594 | return Result; |
| Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3595 | } |
| 3596 | } |
| Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3597 | |
| 3598 | return Sema::TDK_Success; |
| Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3599 | } |
| 3600 | |
| Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3601 | /// \brief Perform template argument deduction per [temp.deduct.call] for a |
| 3602 | /// single parameter / argument pair. |
| 3603 | static Sema::TemplateDeductionResult DeduceTemplateArgumentsFromCallArgument( |
| Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3604 | Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, |
| 3605 | QualType ParamType, Expr *Arg, TemplateDeductionInfo &Info, |
| Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3606 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3607 | SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3608 | bool DecomposedParam, unsigned ArgIdx, unsigned TDF) { |
| Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3609 | QualType ArgType = Arg->getType(); |
| Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3610 | QualType OrigParamType = ParamType; |
| 3611 | |
| 3612 | // If P is a reference type [...] |
| 3613 | // If P is a cv-qualified type [...] |
| Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3614 | if (AdjustFunctionParmAndArgTypesForDeduction( |
| 3615 | S, TemplateParams, FirstInnerIndex, ParamType, ArgType, Arg, TDF)) |
| Richard Smith | 363ae81 | 2017-01-04 22:03:59 +0000 | [diff] [blame] | 3616 | return Sema::TDK_Success; |
| 3617 | |
| Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3618 | // If [...] the argument is a non-empty initializer list [...] |
| 3619 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) |
| 3620 | return DeduceFromInitializerList(S, TemplateParams, ParamType, ILE, Info, |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3621 | Deduced, OriginalCallArgs, ArgIdx, TDF); |
| Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3622 | |
| 3623 | // [...] the deduction process attempts to find template argument values |
| 3624 | // that will make the deduced A identical to A |
| 3625 | // |
| 3626 | // Keep track of the argument type and corresponding parameter index, |
| 3627 | // so we can check for compatibility between the deduced A and A. |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3628 | OriginalCallArgs.push_back( |
| 3629 | Sema::OriginalCallArg(OrigParamType, DecomposedParam, ArgIdx, ArgType)); |
| Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3630 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
| Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3631 | ArgType, Info, Deduced, TDF); |
| Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3632 | } |
| 3633 | |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3634 | /// \brief Perform template argument deduction from a function call |
| 3635 | /// (C++ [temp.deduct.call]). |
| 3636 | /// |
| 3637 | /// \param FunctionTemplate the function template for which we are performing |
| 3638 | /// template argument deduction. |
| 3639 | /// |
| James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3640 | /// \param ExplicitTemplateArgs the explicit template arguments provided |
| Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 3641 | /// for this call. |
| Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3642 | /// |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3643 | /// \param Args the function call arguments |
| 3644 | /// |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3645 | /// \param Specialization if template argument deduction was successful, |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3646 | /// this will be set to the function template specialization produced by |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3647 | /// template argument deduction. |
| 3648 | /// |
| 3649 | /// \param Info the argument will be updated to provide additional information |
| 3650 | /// about template argument deduction. |
| 3651 | /// |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3652 | /// \param CheckNonDependent A callback to invoke to check conversions for |
| 3653 | /// non-dependent parameters, between deduction and substitution, per DR1391. |
| 3654 | /// If this returns true, substitution will be skipped and we return |
| 3655 | /// TDK_NonDependentConversionFailure. The callback is passed the parameter |
| 3656 | /// types (after substituting explicit template arguments). |
| 3657 | /// |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3658 | /// \returns the result of template argument deduction. |
| Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 3659 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3660 | FunctionTemplateDecl *FunctionTemplate, |
| 3661 | TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args, |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3662 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3663 | bool PartialOverloading, |
| 3664 | llvm::function_ref<bool(ArrayRef<QualType>)> CheckNonDependent) { |
| Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3665 | if (FunctionTemplate->isInvalidDecl()) |
| 3666 | return TDK_Invalid; |
| 3667 | |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3668 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3669 | unsigned NumParams = Function->getNumParams(); |
| Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3670 | |
| Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3671 | unsigned FirstInnerIndex = getFirstInnerIndex(FunctionTemplate); |
| 3672 | |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3673 | // C++ [temp.deduct.call]p1: |
| 3674 | // Template argument deduction is done by comparing each function template |
| 3675 | // parameter type (call it P) with the type of the corresponding argument |
| 3676 | // of the call (call it A) as described below. |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3677 | if (Args.size() < Function->getMinRequiredArguments() && !PartialOverloading) |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3678 | return TDK_TooFewArguments; |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3679 | else if (TooManyArguments(NumParams, Args.size(), PartialOverloading)) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3680 | const FunctionProtoType *Proto |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3681 | = Function->getType()->getAs<FunctionProtoType>(); |
| Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3682 | if (Proto->isTemplateVariadic()) |
| 3683 | /* Do nothing */; |
| Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3684 | else if (!Proto->isVariadic()) |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3685 | return TDK_TooManyArguments; |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3686 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3687 | |
| Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3688 | // The types of the parameters from which we will perform template argument |
| 3689 | // deduction. |
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3690 | LocalInstantiationScope InstScope(*this); |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3691 | TemplateParameterList *TemplateParams |
| 3692 | = FunctionTemplate->getTemplateParameters(); |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3693 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3694 | SmallVector<QualType, 8> ParamTypes; |
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3695 | unsigned NumExplicitlySpecified = 0; |
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3696 | if (ExplicitTemplateArgs) { |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3697 | TemplateDeductionResult Result = |
| 3698 | SubstituteExplicitTemplateArguments(FunctionTemplate, |
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3699 | *ExplicitTemplateArgs, |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3700 | Deduced, |
| 3701 | ParamTypes, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3702 | nullptr, |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3703 | Info); |
| 3704 | if (Result) |
| 3705 | return Result; |
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3706 | |
| 3707 | NumExplicitlySpecified = Deduced.size(); |
| Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3708 | } else { |
| 3709 | // Just fill in the parameter types from the function declaration. |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3710 | for (unsigned I = 0; I != NumParams; ++I) |
| Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3711 | ParamTypes.push_back(Function->getParamDecl(I)->getType()); |
| 3712 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3713 | |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3714 | SmallVector<OriginalCallArg, 8> OriginalCallArgs; |
| Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3715 | |
| 3716 | // Deduce an argument of type ParamType from an expression with index ArgIdx. |
| 3717 | auto DeduceCallArgument = [&](QualType ParamType, unsigned ArgIdx) { |
| Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3718 | // C++ [demp.deduct.call]p1: (DR1391) |
| 3719 | // Template argument deduction is done by comparing each function template |
| 3720 | // parameter that contains template-parameters that participate in |
| 3721 | // template argument deduction ... |
| Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 3722 | if (!hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
| Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3723 | return Sema::TDK_Success; |
| 3724 | |
| Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 3725 | // ... with the type of the corresponding argument |
| 3726 | return DeduceTemplateArgumentsFromCallArgument( |
| Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 3727 | *this, TemplateParams, FirstInnerIndex, ParamType, Args[ArgIdx], Info, Deduced, |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 3728 | OriginalCallArgs, /*Decomposed*/false, ArgIdx, /*TDF*/ 0); |
| Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3729 | }; |
| 3730 | |
| Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3731 | // Deduce template arguments from the function parameters. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3732 | Deduced.resize(TemplateParams->size()); |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3733 | SmallVector<QualType, 8> ParamTypesForArgChecking; |
| Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3734 | for (unsigned ParamIdx = 0, NumParamTypes = ParamTypes.size(), ArgIdx = 0; |
| Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3735 | ParamIdx != NumParamTypes; ++ParamIdx) { |
| Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3736 | QualType ParamType = ParamTypes[ParamIdx]; |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3737 | |
| Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3738 | const PackExpansionType *ParamExpansion = |
| 3739 | dyn_cast<PackExpansionType>(ParamType); |
| Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3740 | if (!ParamExpansion) { |
| 3741 | // Simple case: matching a function parameter to a function argument. |
| Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3742 | if (ArgIdx >= Args.size()) |
| Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3743 | break; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3744 | |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3745 | ParamTypesForArgChecking.push_back(ParamType); |
| Richard Smith | a7d5ec9 | 2017-01-04 19:47:19 +0000 | [diff] [blame] | 3746 | if (auto Result = DeduceCallArgument(ParamType, ArgIdx++)) |
| Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3747 | return Result; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3748 | |
| Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3749 | continue; |
| Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3750 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3751 | |
| Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3752 | QualType ParamPattern = ParamExpansion->getPattern(); |
| 3753 | PackDeductionScope PackScope(*this, TemplateParams, Deduced, Info, |
| 3754 | ParamPattern); |
| 3755 | |
| Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3756 | // C++0x [temp.deduct.call]p1: |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3757 | // For a function parameter pack that occurs at the end of the |
| 3758 | // parameter-declaration-list, the type A of each remaining argument of |
| 3759 | // the call is compared with the type P of the declarator-id of the |
| 3760 | // function parameter pack. Each comparison deduces template arguments |
| 3761 | // for subsequent positions in the template parameter packs expanded by |
| Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3762 | // the function parameter pack. When a function parameter pack appears |
| 3763 | // in a non-deduced context [not at the end of the list], the type of |
| 3764 | // that parameter pack is never deduced. |
| 3765 | // |
| 3766 | // FIXME: The above rule allows the size of the parameter pack to change |
| 3767 | // after we skip it (in the non-deduced case). That makes no sense, so |
| 3768 | // we instead notionally deduce the pack against N arguments, where N is |
| 3769 | // the length of the explicitly-specified pack if it's expanded by the |
| 3770 | // parameter pack and 0 otherwise, and we treat each deduction as a |
| 3771 | // non-deduced context. |
| 3772 | if (ParamIdx + 1 == NumParamTypes) { |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3773 | for (; ArgIdx < Args.size(); PackScope.nextPackElement(), ++ArgIdx) { |
| 3774 | ParamTypesForArgChecking.push_back(ParamPattern); |
| Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3775 | if (auto Result = DeduceCallArgument(ParamPattern, ArgIdx)) |
| 3776 | return Result; |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3777 | } |
| Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3778 | } else { |
| 3779 | // If the parameter type contains an explicitly-specified pack that we |
| 3780 | // could not expand, skip the number of parameters notionally created |
| 3781 | // by the expansion. |
| 3782 | Optional<unsigned> NumExpansions = ParamExpansion->getNumExpansions(); |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3783 | if (NumExpansions && !PackScope.isPartiallyExpanded()) { |
| Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3784 | for (unsigned I = 0; I != *NumExpansions && ArgIdx < Args.size(); |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3785 | ++I, ++ArgIdx) { |
| 3786 | ParamTypesForArgChecking.push_back(ParamPattern); |
| Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3787 | // FIXME: Should we add OriginalCallArgs for these? What if the |
| 3788 | // corresponding argument is a list? |
| 3789 | PackScope.nextPackElement(); |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3790 | } |
| 3791 | } |
| Richard Smith | de0d34a | 2017-01-09 07:14:40 +0000 | [diff] [blame] | 3792 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3793 | |
| Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3794 | // Build argument packs for each of the parameter packs expanded by this |
| 3795 | // pack expansion. |
| Richard Smith | 539e8e3 | 2017-01-04 01:48:55 +0000 | [diff] [blame] | 3796 | if (auto Result = PackScope.finish()) |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3797 | return Result; |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3798 | } |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3799 | |
| Richard Smith | 6eedfe7 | 2017-01-09 08:01:21 +0000 | [diff] [blame] | 3800 | return FinishTemplateArgumentDeduction( |
| 3801 | FunctionTemplate, Deduced, NumExplicitlySpecified, Specialization, Info, |
| 3802 | &OriginalCallArgs, PartialOverloading, |
| 3803 | [&]() { return CheckNonDependent(ParamTypesForArgChecking); }); |
| Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3804 | } |
| 3805 | |
| Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3806 | QualType Sema::adjustCCAndNoReturn(QualType ArgFunctionType, |
| Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3807 | QualType FunctionType, |
| 3808 | bool AdjustExceptionSpec) { |
| Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3809 | if (ArgFunctionType.isNull()) |
| 3810 | return ArgFunctionType; |
| 3811 | |
| 3812 | const FunctionProtoType *FunctionTypeP = |
| 3813 | FunctionType->castAs<FunctionProtoType>(); |
| Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3814 | const FunctionProtoType *ArgFunctionTypeP = |
| 3815 | ArgFunctionType->getAs<FunctionProtoType>(); |
| Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3816 | |
| 3817 | FunctionProtoType::ExtProtoInfo EPI = ArgFunctionTypeP->getExtProtoInfo(); |
| 3818 | bool Rebuild = false; |
| 3819 | |
| 3820 | CallingConv CC = FunctionTypeP->getCallConv(); |
| 3821 | if (EPI.ExtInfo.getCC() != CC) { |
| 3822 | EPI.ExtInfo = EPI.ExtInfo.withCallingConv(CC); |
| 3823 | Rebuild = true; |
| 3824 | } |
| 3825 | |
| 3826 | bool NoReturn = FunctionTypeP->getNoReturnAttr(); |
| 3827 | if (EPI.ExtInfo.getNoReturn() != NoReturn) { |
| 3828 | EPI.ExtInfo = EPI.ExtInfo.withNoReturn(NoReturn); |
| 3829 | Rebuild = true; |
| 3830 | } |
| 3831 | |
| 3832 | if (AdjustExceptionSpec && (FunctionTypeP->hasExceptionSpec() || |
| 3833 | ArgFunctionTypeP->hasExceptionSpec())) { |
| 3834 | EPI.ExceptionSpec = FunctionTypeP->getExtProtoInfo().ExceptionSpec; |
| 3835 | Rebuild = true; |
| 3836 | } |
| 3837 | |
| 3838 | if (!Rebuild) |
| Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3839 | return ArgFunctionType; |
| 3840 | |
| Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3841 | return Context.getFunctionType(ArgFunctionTypeP->getReturnType(), |
| 3842 | ArgFunctionTypeP->getParamTypes(), EPI); |
| Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3843 | } |
| 3844 | |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3845 | /// \brief Deduce template arguments when taking the address of a function |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3846 | /// template (C++ [temp.deduct.funcaddr]) or matching a specialization to |
| 3847 | /// a template. |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3848 | /// |
| 3849 | /// \param FunctionTemplate the function template for which we are performing |
| 3850 | /// template argument deduction. |
| 3851 | /// |
| James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3852 | /// \param ExplicitTemplateArgs the explicitly-specified template |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3853 | /// arguments. |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3854 | /// |
| 3855 | /// \param ArgFunctionType the function type that will be used as the |
| 3856 | /// "argument" type (A) when performing template argument deduction from the |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3857 | /// function template's function type. This type may be NULL, if there is no |
| 3858 | /// argument type to compare against, in C++0x [temp.arg.explicit]p3. |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3859 | /// |
| 3860 | /// \param Specialization if template argument deduction was successful, |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3861 | /// this will be set to the function template specialization produced by |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3862 | /// template argument deduction. |
| 3863 | /// |
| 3864 | /// \param Info the argument will be updated to provide additional information |
| 3865 | /// about template argument deduction. |
| 3866 | /// |
| Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3867 | /// \param IsAddressOfFunction If \c true, we are deducing as part of taking |
| 3868 | /// the address of a function template per [temp.deduct.funcaddr] and |
| 3869 | /// [over.over]. If \c false, we are looking up a function template |
| 3870 | /// specialization based on its signature, per [temp.deduct.decl]. |
| 3871 | /// |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3872 | /// \returns the result of template argument deduction. |
| Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3873 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3874 | FunctionTemplateDecl *FunctionTemplate, |
| 3875 | TemplateArgumentListInfo *ExplicitTemplateArgs, QualType ArgFunctionType, |
| 3876 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 3877 | bool IsAddressOfFunction) { |
| Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3878 | if (FunctionTemplate->isInvalidDecl()) |
| 3879 | return TDK_Invalid; |
| 3880 | |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3881 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 3882 | TemplateParameterList *TemplateParams |
| 3883 | = FunctionTemplate->getTemplateParameters(); |
| 3884 | QualType FunctionType = Function->getType(); |
| Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3885 | |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3886 | // Substitute any explicit template arguments. |
| John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3887 | LocalInstantiationScope InstScope(*this); |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3888 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3889 | unsigned NumExplicitlySpecified = 0; |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3890 | SmallVector<QualType, 4> ParamTypes; |
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3891 | if (ExplicitTemplateArgs) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3892 | if (TemplateDeductionResult Result |
| 3893 | = SubstituteExplicitTemplateArguments(FunctionTemplate, |
| John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3894 | *ExplicitTemplateArgs, |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3895 | Deduced, ParamTypes, |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3896 | &FunctionType, Info)) |
| 3897 | return Result; |
| Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3898 | |
| 3899 | NumExplicitlySpecified = Deduced.size(); |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3900 | } |
| 3901 | |
| Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 3902 | // When taking the address of a function, we require convertibility of |
| 3903 | // the resulting function type. Otherwise, we allow arbitrary mismatches |
| 3904 | // of calling convention and noreturn. |
| 3905 | if (!IsAddressOfFunction) |
| 3906 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, FunctionType, |
| 3907 | /*AdjustExceptionSpec*/false); |
| 3908 | |
| Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3909 | // Unevaluated SFINAE context. |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 3910 | EnterExpressionEvaluationContext Unevaluated( |
| 3911 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3912 | SFINAETrap Trap(*this); |
| 3913 | |
| John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3914 | Deduced.resize(TemplateParams->size()); |
| 3915 | |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3916 | // 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] | 3917 | // type so that we treat it as a non-deduced context in what follows. If we |
| 3918 | // are looking up by signature, the signature type should also have a deduced |
| 3919 | // return type, which we instead expect to exactly match. |
| Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3920 | bool HasDeducedReturnType = false; |
| Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3921 | if (getLangOpts().CPlusPlus14 && IsAddressOfFunction && |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3922 | Function->getReturnType()->getContainedAutoType()) { |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3923 | FunctionType = SubstAutoType(FunctionType, Context.DependentTy); |
| Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3924 | HasDeducedReturnType = true; |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3927 | if (!ArgFunctionType.isNull()) { |
| Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 3928 | unsigned TDF = |
| 3929 | TDF_TopLevelParameterTypeList | TDF_AllowCompatibleFunctionType; |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3930 | // Deduce template arguments from the function type. |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3931 | if (TemplateDeductionResult Result |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3932 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3933 | FunctionType, ArgFunctionType, |
| 3934 | Info, Deduced, TDF)) |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3935 | return Result; |
| 3936 | } |
| Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3937 | |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3938 | if (TemplateDeductionResult Result |
| Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3939 | = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
| 3940 | NumExplicitlySpecified, |
| 3941 | Specialization, Info)) |
| 3942 | return Result; |
| 3943 | |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3944 | // If the function has a deduced return type, deduce it now, so we can check |
| 3945 | // that the deduced function type matches the requested type. |
| Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3946 | if (HasDeducedReturnType && |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3947 | Specialization->getReturnType()->isUndeducedType() && |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3948 | DeduceReturnType(Specialization, Info.getLocation(), false)) |
| 3949 | return TDK_MiscellaneousDeductionFailure; |
| 3950 | |
| Richard Smith | 9095e5b | 2016-11-01 01:31:23 +0000 | [diff] [blame] | 3951 | // If the function has a dependent exception specification, resolve it now, |
| 3952 | // so we can check that the exception specification matches. |
| 3953 | auto *SpecializationFPT = |
| 3954 | Specialization->getType()->castAs<FunctionProtoType>(); |
| Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 3955 | if (getLangOpts().CPlusPlus17 && |
| Richard Smith | 9095e5b | 2016-11-01 01:31:23 +0000 | [diff] [blame] | 3956 | isUnresolvedExceptionSpec(SpecializationFPT->getExceptionSpecType()) && |
| 3957 | !ResolveExceptionSpec(Info.getLocation(), SpecializationFPT)) |
| 3958 | return TDK_MiscellaneousDeductionFailure; |
| 3959 | |
| Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 3960 | // Adjust the exception specification of the argument to match the |
| Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3961 | // substituted and resolved type we just formed. (Calling convention and |
| 3962 | // noreturn can't be dependent, so we don't actually need this for them |
| 3963 | // right now.) |
| 3964 | QualType SpecializationType = Specialization->getType(); |
| 3965 | if (!IsAddressOfFunction) |
| 3966 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, SpecializationType, |
| 3967 | /*AdjustExceptionSpec*/true); |
| 3968 | |
| Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3969 | // 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] | 3970 | // specialization with respect to arguments of compatible pointer to function |
| 3971 | // types, template argument deduction fails. |
| 3972 | if (!ArgFunctionType.isNull()) { |
| Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3973 | if (IsAddressOfFunction && |
| 3974 | !isSameOrCompatibleFunctionType( |
| 3975 | Context.getCanonicalType(SpecializationType), |
| 3976 | Context.getCanonicalType(ArgFunctionType))) |
| Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3977 | return TDK_MiscellaneousDeductionFailure; |
| Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3978 | |
| 3979 | if (!IsAddressOfFunction && |
| 3980 | !Context.hasSameType(SpecializationType, ArgFunctionType)) |
| Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3981 | return TDK_MiscellaneousDeductionFailure; |
| 3982 | } |
| Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3983 | |
| 3984 | return TDK_Success; |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3985 | } |
| 3986 | |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3987 | /// \brief Deduce template arguments for a templated conversion |
| 3988 | /// function (C++ [temp.deduct.conv]) and, if successful, produce a |
| 3989 | /// conversion function template specialization. |
| 3990 | Sema::TemplateDeductionResult |
| Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3991 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *ConversionTemplate, |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3992 | QualType ToType, |
| 3993 | CXXConversionDecl *&Specialization, |
| 3994 | TemplateDeductionInfo &Info) { |
| Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3995 | if (ConversionTemplate->isInvalidDecl()) |
| Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3996 | return TDK_Invalid; |
| 3997 | |
| Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3998 | CXXConversionDecl *ConversionGeneric |
| Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3999 | = cast<CXXConversionDecl>(ConversionTemplate->getTemplatedDecl()); |
| 4000 | |
| Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 4001 | QualType FromType = ConversionGeneric->getConversionType(); |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4002 | |
| 4003 | // Canonicalize the types for deduction. |
| 4004 | QualType P = Context.getCanonicalType(FromType); |
| 4005 | QualType A = Context.getCanonicalType(ToType); |
| 4006 | |
| Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 4007 | // C++0x [temp.deduct.conv]p2: |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4008 | // If P is a reference type, the type referred to by P is used for |
| 4009 | // type deduction. |
| 4010 | if (const ReferenceType *PRef = P->getAs<ReferenceType>()) |
| 4011 | P = PRef->getPointeeType(); |
| 4012 | |
| Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 4013 | // C++0x [temp.deduct.conv]p4: |
| 4014 | // [...] 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] | 4015 | // for type deduction. |
| 4016 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
| Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 4017 | A = ARef->getPointeeType().getUnqualifiedType(); |
| 4018 | // C++ [temp.deduct.conv]p3: |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4019 | // |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4020 | // If A is not a reference type: |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4021 | else { |
| 4022 | assert(!A->isReferenceType() && "Reference types were handled above"); |
| 4023 | |
| 4024 | // - If P is an array type, the pointer type produced by the |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4025 | // array-to-pointer standard conversion (4.2) is used in place |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4026 | // of P for type deduction; otherwise, |
| 4027 | if (P->isArrayType()) |
| 4028 | P = Context.getArrayDecayedType(P); |
| 4029 | // - If P is a function type, the pointer type produced by the |
| 4030 | // function-to-pointer standard conversion (4.3) is used in |
| 4031 | // place of P for type deduction; otherwise, |
| 4032 | else if (P->isFunctionType()) |
| 4033 | P = Context.getPointerType(P); |
| 4034 | // - 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] | 4035 | // P's type are ignored for type deduction. |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4036 | else |
| 4037 | P = P.getUnqualifiedType(); |
| 4038 | |
| Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 4039 | // C++0x [temp.deduct.conv]p4: |
| NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4040 | // 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] | 4041 | // 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] | 4042 | // referred to by A is used for type deduction. |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4043 | A = A.getUnqualifiedType(); |
| 4044 | } |
| 4045 | |
| Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 4046 | // Unevaluated SFINAE context. |
| Faisal Vali | d143a0c | 2017-04-01 21:30:49 +0000 | [diff] [blame] | 4047 | EnterExpressionEvaluationContext Unevaluated( |
| 4048 | *this, Sema::ExpressionEvaluationContext::Unevaluated); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4049 | SFINAETrap Trap(*this); |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4050 | |
| 4051 | // C++ [temp.deduct.conv]p1: |
| 4052 | // Template argument deduction is done by comparing the return |
| 4053 | // type of the template conversion function (call it P) with the |
| 4054 | // type that is required as the result of the conversion (call it |
| 4055 | // A) as described in 14.8.2.4. |
| 4056 | TemplateParameterList *TemplateParams |
| Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 4057 | = ConversionTemplate->getTemplateParameters(); |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4058 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4059 | Deduced.resize(TemplateParams->size()); |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4060 | |
| 4061 | // C++0x [temp.deduct.conv]p4: |
| 4062 | // In general, the deduction process attempts to find template |
| 4063 | // argument values that will make the deduced A identical to |
| 4064 | // A. However, there are two cases that allow a difference: |
| 4065 | unsigned TDF = 0; |
| 4066 | // - If the original A is a reference type, A can be more |
| 4067 | // cv-qualified than the deduced A (i.e., the type referred to |
| 4068 | // by the reference) |
| 4069 | if (ToType->isReferenceType()) |
| 4070 | TDF |= TDF_ParamWithReferenceType; |
| 4071 | // - The deduced A can be another pointer or pointer to member |
| NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4072 | // type that can be converted to A via a qualification |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4073 | // conversion. |
| 4074 | // |
| 4075 | // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when |
| 4076 | // both P and A are pointers or member pointers. In this case, we |
| 4077 | // just ignore cv-qualifiers completely). |
| 4078 | if ((P->isPointerType() && A->isPointerType()) || |
| Douglas Gregor | 9f05ed5 | 2011-08-30 00:37:54 +0000 | [diff] [blame] | 4079 | (P->isMemberPointerType() && A->isMemberPointerType())) |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4080 | TDF |= TDF_IgnoreQualifiers; |
| 4081 | if (TemplateDeductionResult Result |
| Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4082 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 4083 | P, A, Info, Deduced, TDF)) |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4084 | return Result; |
| Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 4085 | |
| 4086 | // Create an Instantiation Scope for finalizing the operator. |
| 4087 | LocalInstantiationScope InstScope(*this); |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4088 | // Finish template argument deduction. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4089 | FunctionDecl *ConversionSpecialized = nullptr; |
| Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 4090 | TemplateDeductionResult Result |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 4091 | = FinishTemplateArgumentDeduction(ConversionTemplate, Deduced, 0, |
| Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 4092 | ConversionSpecialized, Info); |
| 4093 | Specialization = cast_or_null<CXXConversionDecl>(ConversionSpecialized); |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4094 | return Result; |
| 4095 | } |
| 4096 | |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4097 | /// \brief Deduce template arguments for a function template when there is |
| 4098 | /// nothing to deduce against (C++0x [temp.arg.explicit]p3). |
| 4099 | /// |
| 4100 | /// \param FunctionTemplate the function template for which we are performing |
| 4101 | /// template argument deduction. |
| 4102 | /// |
| James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 4103 | /// \param ExplicitTemplateArgs the explicitly-specified template |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4104 | /// arguments. |
| 4105 | /// |
| 4106 | /// \param Specialization if template argument deduction was successful, |
| 4107 | /// this will be set to the function template specialization produced by |
| 4108 | /// template argument deduction. |
| 4109 | /// |
| 4110 | /// \param Info the argument will be updated to provide additional information |
| 4111 | /// about template argument deduction. |
| 4112 | /// |
| Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4113 | /// \param IsAddressOfFunction If \c true, we are deducing as part of taking |
| 4114 | /// the address of a function template in a context where we do not have a |
| 4115 | /// target type, per [over.over]. If \c false, we are looking up a function |
| 4116 | /// template specialization based on its signature, which only happens when |
| 4117 | /// deducing a function parameter type from an argument that is a template-id |
| 4118 | /// naming a function template specialization. |
| 4119 | /// |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4120 | /// \returns the result of template argument deduction. |
| Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4121 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 4122 | FunctionTemplateDecl *FunctionTemplate, |
| 4123 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 4124 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 4125 | bool IsAddressOfFunction) { |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4126 | return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
| Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 4127 | QualType(), Specialization, Info, |
| Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4128 | IsAddressOfFunction); |
| Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4129 | } |
| 4130 | |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4131 | namespace { |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 4132 | |
| Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4133 | /// Substitute the 'auto' specifier or deduced template specialization type |
| 4134 | /// specifier within a type for a given replacement type. |
| 4135 | class SubstituteDeducedTypeTransform : |
| 4136 | public TreeTransform<SubstituteDeducedTypeTransform> { |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4137 | QualType Replacement; |
| Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4138 | bool UseTypeSugar; |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 4139 | |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4140 | public: |
| Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4141 | SubstituteDeducedTypeTransform(Sema &SemaRef, QualType Replacement, |
| 4142 | bool UseTypeSugar = true) |
| 4143 | : TreeTransform<SubstituteDeducedTypeTransform>(SemaRef), |
| 4144 | Replacement(Replacement), UseTypeSugar(UseTypeSugar) {} |
| 4145 | |
| 4146 | QualType TransformDesugared(TypeLocBuilder &TLB, DeducedTypeLoc TL) { |
| 4147 | assert(isa<TemplateTypeParmType>(Replacement) && |
| 4148 | "unexpected unsugared replacement kind"); |
| 4149 | QualType Result = Replacement; |
| 4150 | TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result); |
| 4151 | NewTL.setNameLoc(TL.getNameLoc()); |
| 4152 | return Result; |
| 4153 | } |
| Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 4154 | |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4155 | QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) { |
| 4156 | // If we're building the type pattern to deduce against, don't wrap the |
| 4157 | // substituted type in an AutoType. Certain template deduction rules |
| 4158 | // apply only when a template type parameter appears directly (and not if |
| 4159 | // the parameter is found through desugaring). For instance: |
| 4160 | // auto &&lref = lvalue; |
| 4161 | // must transform into "rvalue reference to T" not "rvalue reference to |
| 4162 | // auto type deduced as T" in order for [temp.deduct.call]p3 to apply. |
| Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4163 | // |
| 4164 | // FIXME: Is this still necessary? |
| 4165 | if (!UseTypeSugar) |
| 4166 | return TransformDesugared(TLB, TL); |
| 4167 | |
| 4168 | QualType Result = SemaRef.Context.getAutoType( |
| 4169 | Replacement, TL.getTypePtr()->getKeyword(), Replacement.isNull()); |
| 4170 | auto NewTL = TLB.push<AutoTypeLoc>(Result); |
| 4171 | NewTL.setNameLoc(TL.getNameLoc()); |
| 4172 | return Result; |
| 4173 | } |
| 4174 | |
| 4175 | QualType TransformDeducedTemplateSpecializationType( |
| 4176 | TypeLocBuilder &TLB, DeducedTemplateSpecializationTypeLoc TL) { |
| 4177 | if (!UseTypeSugar) |
| 4178 | return TransformDesugared(TLB, TL); |
| 4179 | |
| 4180 | QualType Result = SemaRef.Context.getDeducedTemplateSpecializationType( |
| 4181 | TL.getTypePtr()->getTemplateName(), |
| 4182 | Replacement, Replacement.isNull()); |
| 4183 | auto NewTL = TLB.push<DeducedTemplateSpecializationTypeLoc>(Result); |
| 4184 | NewTL.setNameLoc(TL.getNameLoc()); |
| 4185 | return Result; |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4186 | } |
| Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 4187 | |
| 4188 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 4189 | // Lambdas never need to be transformed. |
| 4190 | return E; |
| 4191 | } |
| Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4192 | |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4193 | QualType Apply(TypeLoc TL) { |
| 4194 | // Create some scratch storage for the transformed type locations. |
| 4195 | // FIXME: We're just going to throw this information away. Don't build it. |
| 4196 | TypeLocBuilder TLB; |
| 4197 | TLB.reserve(TL.getFullDataSize()); |
| 4198 | return TransformType(TLB, TL); |
| Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4199 | } |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4200 | }; |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 4201 | |
| 4202 | } // namespace |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4203 | |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4204 | Sema::DeduceAutoResult |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4205 | Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *&Init, QualType &Result, |
| 4206 | Optional<unsigned> DependentDeductionDepth) { |
| 4207 | return DeduceAutoType(Type->getTypeLoc(), Init, Result, |
| 4208 | DependentDeductionDepth); |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
| Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4211 | /// Attempt to produce an informative diagostic explaining why auto deduction |
| 4212 | /// failed. |
| 4213 | /// \return \c true if diagnosed, \c false if not. |
| 4214 | static bool diagnoseAutoDeductionFailure(Sema &S, |
| 4215 | Sema::TemplateDeductionResult TDK, |
| 4216 | TemplateDeductionInfo &Info, |
| 4217 | ArrayRef<SourceRange> Ranges) { |
| 4218 | switch (TDK) { |
| 4219 | case Sema::TDK_Inconsistent: { |
| 4220 | // Inconsistent deduction means we were deducing from an initializer list. |
| 4221 | auto D = S.Diag(Info.getLocation(), diag::err_auto_inconsistent_deduction); |
| 4222 | D << Info.FirstArg << Info.SecondArg; |
| 4223 | for (auto R : Ranges) |
| 4224 | D << R; |
| 4225 | return true; |
| 4226 | } |
| 4227 | |
| 4228 | // FIXME: Are there other cases for which a custom diagnostic is more useful |
| 4229 | // than the basic "types don't match" diagnostic? |
| 4230 | |
| 4231 | default: |
| 4232 | return false; |
| 4233 | } |
| 4234 | } |
| 4235 | |
| Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4236 | /// \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] | 4237 | /// |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4238 | /// Note that this is done even if the initializer is dependent. (This is |
| 4239 | /// necessary to support partial ordering of templates using 'auto'.) |
| 4240 | /// A dependent type will be produced when deducing from a dependent type. |
| 4241 | /// |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4242 | /// \param Type the type pattern using the auto type-specifier. |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4243 | /// \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] | 4244 | /// \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] | 4245 | /// deduced type. |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4246 | /// \param DependentDeductionDepth Set if we should permit deduction in |
| 4247 | /// dependent cases. This is necessary for template partial ordering with |
| 4248 | /// 'auto' template parameters. The value specified is the template |
| 4249 | /// parameter depth at which we should perform 'auto' deduction. |
| Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4250 | Sema::DeduceAutoResult |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4251 | Sema::DeduceAutoType(TypeLoc Type, Expr *&Init, QualType &Result, |
| 4252 | Optional<unsigned> DependentDeductionDepth) { |
| John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4253 | if (Init->getType()->isNonOverloadPlaceholderType()) { |
| Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4254 | ExprResult NonPlaceholder = CheckPlaceholderExpr(Init); |
| 4255 | if (NonPlaceholder.isInvalid()) |
| 4256 | return DAR_FailedAlreadyDiagnosed; |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4257 | Init = NonPlaceholder.get(); |
| John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4258 | } |
| 4259 | |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4260 | if (!DependentDeductionDepth && |
| 4261 | (Type.getType()->isDependentType() || Init->isTypeDependent())) { |
| Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4262 | Result = SubstituteDeducedTypeTransform(*this, QualType()).Apply(Type); |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4263 | assert(!Result.isNull() && "substituting DependentTy can't fail"); |
| Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4264 | return DAR_Succeeded; |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4265 | } |
| 4266 | |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4267 | // Find the depth of template parameter to synthesize. |
| 4268 | unsigned Depth = DependentDeductionDepth.getValueOr(0); |
| 4269 | |
| Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4270 | // If this is a 'decltype(auto)' specifier, do the decltype dance. |
| 4271 | // Since 'decltype(auto)' can only occur at the top of the type, we |
| 4272 | // don't need to go digging for it. |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4273 | if (const AutoType *AT = Type.getType()->getAs<AutoType>()) { |
| Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4274 | if (AT->isDecltypeAuto()) { |
| 4275 | if (isa<InitListExpr>(Init)) { |
| 4276 | Diag(Init->getLocStart(), diag::err_decltype_auto_initializer_list); |
| 4277 | return DAR_FailedAlreadyDiagnosed; |
| 4278 | } |
| 4279 | |
| Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 4280 | QualType Deduced = BuildDecltypeType(Init, Init->getLocStart(), false); |
| David Majnemer | 3c20ab2 | 2015-07-01 00:29:28 +0000 | [diff] [blame] | 4281 | if (Deduced.isNull()) |
| 4282 | return DAR_FailedAlreadyDiagnosed; |
| Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4283 | // FIXME: Support a non-canonical deduced type for 'auto'. |
| 4284 | Deduced = Context.getCanonicalType(Deduced); |
| Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4285 | Result = SubstituteDeducedTypeTransform(*this, Deduced).Apply(Type); |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4286 | if (Result.isNull()) |
| 4287 | return DAR_FailedAlreadyDiagnosed; |
| Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4288 | return DAR_Succeeded; |
| Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4289 | } else if (!getLangOpts().CPlusPlus) { |
| 4290 | if (isa<InitListExpr>(Init)) { |
| 4291 | Diag(Init->getLocStart(), diag::err_auto_init_list_from_c); |
| 4292 | return DAR_FailedAlreadyDiagnosed; |
| 4293 | } |
| Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4294 | } |
| 4295 | } |
| 4296 | |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4297 | SourceLocation Loc = Init->getExprLoc(); |
| 4298 | |
| 4299 | LocalInstantiationScope InstScope(*this); |
| 4300 | |
| 4301 | // Build template<class TemplParam> void Func(FuncParam); |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4302 | TemplateTypeParmDecl *TemplParam = TemplateTypeParmDecl::Create( |
| 4303 | Context, nullptr, SourceLocation(), Loc, Depth, 0, nullptr, false, false); |
| Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4304 | QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0); |
| 4305 | NamedDecl *TemplParamPtr = TemplParam; |
| Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 4306 | FixedSizeTemplateParameterListStorage<1, false> TemplateParamsSt( |
| 4307 | Loc, Loc, TemplParamPtr, Loc, nullptr); |
| Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 4308 | |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4309 | QualType FuncParam = |
| Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4310 | SubstituteDeducedTypeTransform(*this, TemplArg, /*UseTypeSugar*/false) |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4311 | .Apply(Type); |
| Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4312 | assert(!FuncParam.isNull() && |
| 4313 | "substituting template parameter for 'auto' failed"); |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4314 | |
| 4315 | // Deduce type of TemplParam in Func(Init) |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4316 | SmallVector<DeducedTemplateArgument, 1> Deduced; |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4317 | Deduced.resize(1); |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4318 | |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4319 | TemplateDeductionInfo Info(Loc, Depth); |
| 4320 | |
| 4321 | // If deduction failed, don't diagnose if the initializer is dependent; it |
| 4322 | // might acquire a matching type in the instantiation. |
| Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4323 | auto DeductionFailed = [&](TemplateDeductionResult TDK, |
| 4324 | ArrayRef<SourceRange> Ranges) -> DeduceAutoResult { |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4325 | if (Init->isTypeDependent()) { |
| Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4326 | Result = SubstituteDeducedTypeTransform(*this, QualType()).Apply(Type); |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4327 | assert(!Result.isNull() && "substituting DependentTy can't fail"); |
| 4328 | return DAR_Succeeded; |
| 4329 | } |
| Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4330 | if (diagnoseAutoDeductionFailure(*this, TDK, Info, Ranges)) |
| 4331 | return DAR_FailedAlreadyDiagnosed; |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4332 | return DAR_Failed; |
| 4333 | }; |
| Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4334 | |
| Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 4335 | SmallVector<OriginalCallArg, 4> OriginalCallArgs; |
| 4336 | |
| Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 4337 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
| Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4338 | if (InitList) { |
| Richard Smith | c8a32e5 | 2017-01-05 23:12:16 +0000 | [diff] [blame] | 4339 | // Notionally, we substitute std::initializer_list<T> for 'auto' and deduce |
| 4340 | // against that. Such deduction only succeeds if removing cv-qualifiers and |
| 4341 | // references results in std::initializer_list<T>. |
| 4342 | if (!Type.getType().getNonReferenceType()->getAs<AutoType>()) |
| 4343 | return DAR_Failed; |
| 4344 | |
| Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4345 | SourceRange DeducedFromInitRange; |
| Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4346 | for (unsigned i = 0, e = InitList->getNumInits(); i < e; ++i) { |
| Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4347 | Expr *Init = InitList->getInit(i); |
| 4348 | |
| 4349 | if (auto TDK = DeduceTemplateArgumentsFromCallArgument( |
| 4350 | *this, TemplateParamsSt.get(), 0, TemplArg, Init, |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 4351 | Info, Deduced, OriginalCallArgs, /*Decomposed*/ true, |
| 4352 | /*ArgIdx*/ 0, /*TDF*/ 0)) |
| Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4353 | return DeductionFailed(TDK, {DeducedFromInitRange, |
| 4354 | Init->getSourceRange()}); |
| 4355 | |
| 4356 | if (DeducedFromInitRange.isInvalid() && |
| 4357 | Deduced[0].getKind() != TemplateArgument::Null) |
| 4358 | DeducedFromInitRange = Init->getSourceRange(); |
| Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4359 | } |
| 4360 | } else { |
| Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4361 | if (!getLangOpts().CPlusPlus && Init->refersToBitField()) { |
| 4362 | Diag(Loc, diag::err_auto_bitfield); |
| 4363 | return DAR_FailedAlreadyDiagnosed; |
| 4364 | } |
| 4365 | |
| Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4366 | if (auto TDK = DeduceTemplateArgumentsFromCallArgument( |
| Richard Smith | 3291877 | 2017-02-14 00:25:28 +0000 | [diff] [blame] | 4367 | *this, TemplateParamsSt.get(), 0, FuncParam, Init, Info, Deduced, |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 4368 | OriginalCallArgs, /*Decomposed*/ false, /*ArgIdx*/ 0, /*TDF*/ 0)) |
| Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4369 | return DeductionFailed(TDK, {}); |
| Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4370 | } |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4371 | |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4372 | // Could be null if somehow 'auto' appears in a non-deduced context. |
| Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4373 | if (Deduced[0].getKind() != TemplateArgument::Type) |
| Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4374 | return DeductionFailed(TDK_Incomplete, {}); |
| Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4375 | |
| Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4376 | QualType DeducedType = Deduced[0].getAsType(); |
| 4377 | |
| Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4378 | if (InitList) { |
| 4379 | DeducedType = BuildStdInitializerList(DeducedType, Loc); |
| 4380 | if (DeducedType.isNull()) |
| Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4381 | return DAR_FailedAlreadyDiagnosed; |
| Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4382 | } |
| 4383 | |
| Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4384 | Result = SubstituteDeducedTypeTransform(*this, DeducedType).Apply(Type); |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4385 | if (Result.isNull()) |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4386 | return DAR_FailedAlreadyDiagnosed; |
| Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4387 | |
| Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4388 | // Check that the deduced argument type is compatible with the original |
| 4389 | // argument type per C++ [temp.deduct.call]p4. |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 4390 | QualType DeducedA = InitList ? Deduced[0].getAsType() : Result; |
| Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 4391 | for (const OriginalCallArg &OriginalArg : OriginalCallArgs) { |
| Richard Smith | c92d206 | 2017-01-05 23:02:44 +0000 | [diff] [blame] | 4392 | assert((bool)InitList == OriginalArg.DecomposedParam && |
| 4393 | "decomposed non-init-list in auto deduction?"); |
| Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4394 | if (auto TDK = |
| 4395 | CheckOriginalCallArgDeduction(*this, Info, OriginalArg, DeducedA)) { |
| Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 4396 | Result = QualType(); |
| Richard Smith | b1efc9b | 2017-08-30 00:44:08 +0000 | [diff] [blame] | 4397 | return DeductionFailed(TDK, {}); |
| Richard Smith | 707eab6 | 2017-01-05 04:08:31 +0000 | [diff] [blame] | 4398 | } |
| Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4399 | } |
| 4400 | |
| Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4401 | return DAR_Succeeded; |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4402 | } |
| 4403 | |
| Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 4404 | QualType Sema::SubstAutoType(QualType TypeWithAuto, |
| Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4405 | QualType TypeToReplaceAuto) { |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4406 | if (TypeToReplaceAuto->isDependentType()) |
| 4407 | TypeToReplaceAuto = QualType(); |
| Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4408 | return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto) |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4409 | .TransformType(TypeWithAuto); |
| Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4410 | } |
| 4411 | |
| Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4412 | TypeSourceInfo *Sema::SubstAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto, |
| 4413 | QualType TypeToReplaceAuto) { |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4414 | if (TypeToReplaceAuto->isDependentType()) |
| 4415 | TypeToReplaceAuto = QualType(); |
| Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4416 | return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto) |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4417 | .TransformType(TypeWithAuto); |
| Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 4418 | } |
| 4419 | |
| Richard Smith | 33c33c3 | 2017-02-04 01:28:01 +0000 | [diff] [blame] | 4420 | QualType Sema::ReplaceAutoType(QualType TypeWithAuto, |
| 4421 | QualType TypeToReplaceAuto) { |
| Richard Smith | 6043762 | 2017-02-09 19:17:44 +0000 | [diff] [blame] | 4422 | return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto, |
| 4423 | /*UseTypeSugar*/ false) |
| Richard Smith | 33c33c3 | 2017-02-04 01:28:01 +0000 | [diff] [blame] | 4424 | .TransformType(TypeWithAuto); |
| 4425 | } |
| 4426 | |
| Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4427 | void Sema::DiagnoseAutoDeductionFailure(VarDecl *VDecl, Expr *Init) { |
| 4428 | if (isa<InitListExpr>(Init)) |
| 4429 | Diag(VDecl->getLocation(), |
| Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4430 | VDecl->isInitCapture() |
| 4431 | ? diag::err_init_capture_deduction_failure_from_init_list |
| 4432 | : diag::err_auto_var_deduction_failure_from_init_list) |
| Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4433 | << VDecl->getDeclName() << VDecl->getType() << Init->getSourceRange(); |
| 4434 | else |
| Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4435 | Diag(VDecl->getLocation(), |
| 4436 | VDecl->isInitCapture() ? diag::err_init_capture_deduction_failure |
| 4437 | : diag::err_auto_var_deduction_failure) |
| Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4438 | << VDecl->getDeclName() << VDecl->getType() << Init->getType() |
| 4439 | << Init->getSourceRange(); |
| 4440 | } |
| 4441 | |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4442 | bool Sema::DeduceReturnType(FunctionDecl *FD, SourceLocation Loc, |
| 4443 | bool Diagnose) { |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4444 | assert(FD->getReturnType()->isUndeducedType()); |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4445 | |
| Richard Smith | 50e291e | 2018-01-02 23:52:42 +0000 | [diff] [blame] | 4446 | // For a lambda's conversion operator, deduce any 'auto' or 'decltype(auto)' |
| 4447 | // within the return type from the call operator's type. |
| 4448 | if (isLambdaConversionOperator(FD)) { |
| 4449 | CXXRecordDecl *Lambda = cast<CXXMethodDecl>(FD)->getParent(); |
| 4450 | FunctionDecl *CallOp = Lambda->getLambdaCallOperator(); |
| 4451 | |
| 4452 | // For a generic lambda, instantiate the call operator if needed. |
| 4453 | if (auto *Args = FD->getTemplateSpecializationArgs()) { |
| 4454 | CallOp = InstantiateFunctionDeclaration( |
| 4455 | CallOp->getDescribedFunctionTemplate(), Args, Loc); |
| 4456 | if (!CallOp || CallOp->isInvalidDecl()) |
| 4457 | return true; |
| 4458 | |
| 4459 | // We might need to deduce the return type by instantiating the definition |
| 4460 | // of the operator() function. |
| 4461 | if (CallOp->getReturnType()->isUndeducedType()) |
| 4462 | InstantiateFunctionDefinition(Loc, CallOp); |
| 4463 | } |
| 4464 | |
| 4465 | if (CallOp->isInvalidDecl()) |
| 4466 | return true; |
| 4467 | assert(!CallOp->getReturnType()->isUndeducedType() && |
| 4468 | "failed to deduce lambda return type"); |
| 4469 | |
| 4470 | // Build the new return type from scratch. |
| 4471 | QualType RetType = getLambdaConversionFunctionResultType( |
| 4472 | CallOp->getType()->castAs<FunctionProtoType>()); |
| 4473 | if (FD->getReturnType()->getAs<PointerType>()) |
| 4474 | RetType = Context.getPointerType(RetType); |
| 4475 | else { |
| 4476 | assert(FD->getReturnType()->getAs<BlockPointerType>()); |
| 4477 | RetType = Context.getBlockPointerType(RetType); |
| 4478 | } |
| 4479 | Context.adjustDeducedFunctionResultType(FD, RetType); |
| 4480 | return false; |
| 4481 | } |
| 4482 | |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4483 | if (FD->getTemplateInstantiationPattern()) |
| 4484 | InstantiateFunctionDefinition(Loc, FD); |
| 4485 | |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4486 | bool StillUndeduced = FD->getReturnType()->isUndeducedType(); |
| Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4487 | if (StillUndeduced && Diagnose && !FD->isInvalidDecl()) { |
| 4488 | Diag(Loc, diag::err_auto_fn_used_before_defined) << FD; |
| 4489 | Diag(FD->getLocation(), diag::note_callee_decl) << FD; |
| 4490 | } |
| 4491 | |
| 4492 | return StillUndeduced; |
| 4493 | } |
| 4494 | |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4495 | /// \brief If this is a non-static member function, |
| Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 4496 | static void |
| 4497 | AddImplicitObjectParameterType(ASTContext &Context, |
| 4498 | CXXMethodDecl *Method, |
| 4499 | SmallVectorImpl<QualType> &ArgTypes) { |
| Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4500 | // C++11 [temp.func.order]p3: |
| 4501 | // [...] The new parameter is of type "reference to cv A," where cv are |
| 4502 | // the cv-qualifiers of the function template (if any) and A is |
| 4503 | // the class of which the function template is a member. |
| Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4504 | // |
| Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4505 | // The standard doesn't say explicitly, but we pick the appropriate kind of |
| 4506 | // reference type based on [over.match.funcs]p4. |
| Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4507 | QualType ArgTy = Context.getTypeDeclType(Method->getParent()); |
| 4508 | ArgTy = Context.getQualifiedType(ArgTy, |
| 4509 | Qualifiers::fromCVRMask(Method->getTypeQualifiers())); |
| Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4510 | if (Method->getRefQualifier() == RQ_RValue) |
| 4511 | ArgTy = Context.getRValueReferenceType(ArgTy); |
| 4512 | else |
| 4513 | ArgTy = Context.getLValueReferenceType(ArgTy); |
| Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4514 | ArgTypes.push_back(ArgTy); |
| 4515 | } |
| 4516 | |
| Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4517 | /// \brief Determine whether the function template \p FT1 is at least as |
| 4518 | /// specialized as \p FT2. |
| 4519 | static bool isAtLeastAsSpecializedAs(Sema &S, |
| John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4520 | SourceLocation Loc, |
| Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4521 | FunctionTemplateDecl *FT1, |
| 4522 | FunctionTemplateDecl *FT2, |
| 4523 | TemplatePartialOrderingContext TPOC, |
| Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4524 | unsigned NumCallArguments1) { |
| Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4525 | FunctionDecl *FD1 = FT1->getTemplatedDecl(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4526 | FunctionDecl *FD2 = FT2->getTemplatedDecl(); |
| Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4527 | const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); |
| 4528 | const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4529 | |
| Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4530 | assert(Proto1 && Proto2 && "Function templates must have prototypes"); |
| 4531 | TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4532 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4533 | Deduced.resize(TemplateParams->size()); |
| 4534 | |
| 4535 | // C++0x [temp.deduct.partial]p3: |
| 4536 | // The types used to determine the ordering depend on the context in which |
| 4537 | // the partial ordering is done: |
| Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4538 | TemplateDeductionInfo Info(Loc); |
| Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4539 | SmallVector<QualType, 4> Args2; |
| Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4540 | switch (TPOC) { |
| 4541 | case TPOC_Call: { |
| 4542 | // - In the context of a function call, the function parameter types are |
| 4543 | // used. |
| Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4544 | CXXMethodDecl *Method1 = dyn_cast<CXXMethodDecl>(FD1); |
| 4545 | CXXMethodDecl *Method2 = dyn_cast<CXXMethodDecl>(FD2); |
| Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4546 | |
| Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4547 | // C++11 [temp.func.order]p3: |
| Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4548 | // [...] If only one of the function templates is a non-static |
| 4549 | // member, that function template is considered to have a new |
| 4550 | // first parameter inserted in its function parameter list. The |
| 4551 | // new parameter is of type "reference to cv A," where cv are |
| 4552 | // the cv-qualifiers of the function template (if any) and A is |
| 4553 | // the class of which the function template is a member. |
| 4554 | // |
| Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4555 | // Note that we interpret this to mean "if one of the function |
| 4556 | // templates is a non-static member and the other is a non-member"; |
| 4557 | // otherwise, the ordering rules for static functions against non-static |
| 4558 | // functions don't make any sense. |
| 4559 | // |
| Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4560 | // C++98/03 doesn't have this provision but we've extended DR532 to cover |
| 4561 | // it as wording was broken prior to it. |
| Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4562 | SmallVector<QualType, 4> Args1; |
| 4563 | |
| Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4564 | unsigned NumComparedArguments = NumCallArguments1; |
| 4565 | |
| 4566 | if (!Method2 && Method1 && !Method1->isStatic()) { |
| Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4567 | // Compare 'this' from Method1 against first parameter from Method2. |
| 4568 | AddImplicitObjectParameterType(S.Context, Method1, Args1); |
| 4569 | ++NumComparedArguments; |
| Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4570 | } else if (!Method1 && Method2 && !Method2->isStatic()) { |
| Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4571 | // Compare 'this' from Method2 against first parameter from Method1. |
| 4572 | AddImplicitObjectParameterType(S.Context, Method2, Args2); |
| Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4573 | } |
| 4574 | |
| Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4575 | Args1.insert(Args1.end(), Proto1->param_type_begin(), |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4576 | Proto1->param_type_end()); |
| Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4577 | Args2.insert(Args2.end(), Proto2->param_type_begin(), |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4578 | Proto2->param_type_end()); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4579 | |
| Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4580 | // C++ [temp.func.order]p5: |
| 4581 | // The presence of unused ellipsis and default arguments has no effect on |
| 4582 | // the partial ordering of function templates. |
| Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4583 | if (Args1.size() > NumComparedArguments) |
| 4584 | Args1.resize(NumComparedArguments); |
| 4585 | if (Args2.size() > NumComparedArguments) |
| 4586 | Args2.resize(NumComparedArguments); |
| Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4587 | if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(), |
| 4588 | Args1.data(), Args1.size(), Info, Deduced, |
| 4589 | TDF_None, /*PartialOrdering=*/true)) |
| 4590 | return false; |
| 4591 | |
| Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4592 | break; |
| 4593 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4594 | |
| Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4595 | case TPOC_Conversion: |
| 4596 | // - In the context of a call to a conversion operator, the return types |
| 4597 | // of the conversion function templates are used. |
| Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4598 | if (DeduceTemplateArgumentsByTypeMatch( |
| 4599 | S, TemplateParams, Proto2->getReturnType(), Proto1->getReturnType(), |
| 4600 | Info, Deduced, TDF_None, |
| 4601 | /*PartialOrdering=*/true)) |
| 4602 | return false; |
| Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4603 | break; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4604 | |
| Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4605 | case TPOC_Other: |
| NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4606 | // - 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] | 4607 | // is used. |
| Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4608 | if (DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 4609 | FD2->getType(), FD1->getType(), |
| 4610 | Info, Deduced, TDF_None, |
| 4611 | /*PartialOrdering=*/true)) |
| 4612 | return false; |
| Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4613 | break; |
| 4614 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4615 | |
| Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4616 | // C++0x [temp.deduct.partial]p11: |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4617 | // In most cases, all template parameters must have values in order for |
| 4618 | // deduction to succeed, but for partial ordering purposes a template |
| 4619 | // 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] | 4620 | // types being used for partial ordering. [ Note: a template parameter used |
| 4621 | // in a non-deduced context is considered used. -end note] |
| 4622 | unsigned ArgIdx = 0, NumArgs = Deduced.size(); |
| 4623 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4624 | if (Deduced[ArgIdx].isNull()) |
| 4625 | break; |
| 4626 | |
| Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4627 | // FIXME: We fail to implement [temp.deduct.type]p1 along this path. We need |
| 4628 | // to substitute the deduced arguments back into the template and check that |
| 4629 | // we get the right type. |
| Richard Smith | cf82486 | 2016-12-30 04:32:02 +0000 | [diff] [blame] | 4630 | |
| Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4631 | if (ArgIdx == NumArgs) { |
| 4632 | // All template arguments were deduced. FT1 is at least as specialized |
| 4633 | // as FT2. |
| 4634 | return true; |
| Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4635 | } |
| 4636 | |
| Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4637 | // Figure out which template parameters were used. |
| 4638 | llvm::SmallBitVector UsedParameters(TemplateParams->size()); |
| 4639 | switch (TPOC) { |
| 4640 | case TPOC_Call: |
| 4641 | for (unsigned I = 0, N = Args2.size(); I != N; ++I) |
| 4642 | ::MarkUsedTemplateParameters(S.Context, Args2[I], false, |
| 4643 | TemplateParams->getDepth(), |
| 4644 | UsedParameters); |
| 4645 | break; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4646 | |
| Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4647 | case TPOC_Conversion: |
| 4648 | ::MarkUsedTemplateParameters(S.Context, Proto2->getReturnType(), false, |
| 4649 | TemplateParams->getDepth(), UsedParameters); |
| 4650 | break; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4651 | |
| Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4652 | case TPOC_Other: |
| 4653 | ::MarkUsedTemplateParameters(S.Context, FD2->getType(), false, |
| 4654 | TemplateParams->getDepth(), |
| 4655 | UsedParameters); |
| 4656 | break; |
| Richard Smith | 86a1b13 | 2017-02-16 03:49:44 +0000 | [diff] [blame] | 4657 | } |
| 4658 | |
| Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 4659 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4660 | // If this argument had no value deduced but was used in one of the types |
| 4661 | // used for partial ordering, then deduction fails. |
| 4662 | if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) |
| 4663 | return false; |
| 4664 | |
| Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4665 | return true; |
| 4666 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4667 | |
| Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4668 | /// \brief Determine whether this a function template whose parameter-type-list |
| 4669 | /// ends with a function parameter pack. |
| 4670 | static bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) { |
| 4671 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
| 4672 | unsigned NumParams = Function->getNumParams(); |
| 4673 | if (NumParams == 0) |
| 4674 | return false; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4675 | |
| Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4676 | ParmVarDecl *Last = Function->getParamDecl(NumParams - 1); |
| 4677 | if (!Last->isParameterPack()) |
| 4678 | return false; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4679 | |
| Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4680 | // Make sure that no previous parameter is a parameter pack. |
| 4681 | while (--NumParams > 0) { |
| 4682 | if (Function->getParamDecl(NumParams - 1)->isParameterPack()) |
| 4683 | return false; |
| 4684 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4685 | |
| Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4686 | return true; |
| 4687 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4688 | |
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4689 | /// \brief Returns the more specialized function template according |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4690 | /// to the rules of function template partial ordering (C++ [temp.func.order]). |
| 4691 | /// |
| 4692 | /// \param FT1 the first function template |
| 4693 | /// |
| 4694 | /// \param FT2 the second function template |
| 4695 | /// |
| Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4696 | /// \param TPOC the context in which we are performing partial ordering of |
| 4697 | /// function templates. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4698 | /// |
| Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4699 | /// \param NumCallArguments1 The number of arguments in the call to FT1, used |
| 4700 | /// only when \c TPOC is \c TPOC_Call. |
| 4701 | /// |
| 4702 | /// \param NumCallArguments2 The number of arguments in the call to FT2, used |
| 4703 | /// only when \c TPOC is \c TPOC_Call. |
| Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4704 | /// |
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4705 | /// \returns the more specialized function template. If neither |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4706 | /// template is more specialized, returns NULL. |
| 4707 | FunctionTemplateDecl * |
| 4708 | Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, |
| 4709 | FunctionTemplateDecl *FT2, |
| John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4710 | SourceLocation Loc, |
| Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4711 | TemplatePartialOrderingContext TPOC, |
| Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4712 | unsigned NumCallArguments1, |
| 4713 | unsigned NumCallArguments2) { |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4714 | bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, |
| Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4715 | NumCallArguments1); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4716 | bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC, |
| Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4717 | NumCallArguments2); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4718 | |
| Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4719 | if (Better1 != Better2) // We have a clear winner |
| Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4720 | return Better1 ? FT1 : FT2; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4721 | |
| Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4722 | if (!Better1 && !Better2) // Neither is better than the other |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4723 | return nullptr; |
| Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4724 | |
| Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4725 | // FIXME: This mimics what GCC implements, but doesn't match up with the |
| 4726 | // proposed resolution for core issue 692. This area needs to be sorted out, |
| 4727 | // but for now we attempt to maintain compatibility. |
| 4728 | bool Variadic1 = isVariadicFunctionTemplate(FT1); |
| 4729 | bool Variadic2 = isVariadicFunctionTemplate(FT2); |
| 4730 | if (Variadic1 != Variadic2) |
| 4731 | return Variadic1? FT2 : FT1; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4732 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4733 | return nullptr; |
| Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4734 | } |
| Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4735 | |
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4736 | /// \brief Determine if the two templates are equivalent. |
| 4737 | static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) { |
| 4738 | if (T1 == T2) |
| 4739 | return true; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4740 | |
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4741 | if (!T1 || !T2) |
| 4742 | return false; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4743 | |
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4744 | return T1->getCanonicalDecl() == T2->getCanonicalDecl(); |
| 4745 | } |
| 4746 | |
| 4747 | /// \brief Retrieve the most specialized of the given function template |
| 4748 | /// specializations. |
| 4749 | /// |
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4750 | /// \param SpecBegin the start iterator of the function template |
| 4751 | /// specializations that we will be comparing. |
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4752 | /// |
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4753 | /// \param SpecEnd the end iterator of the function template |
| 4754 | /// specializations, paired with \p SpecBegin. |
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4755 | /// |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4756 | /// \param Loc the location where the ambiguity or no-specializations |
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4757 | /// diagnostic should occur. |
| 4758 | /// |
| 4759 | /// \param NoneDiag partial diagnostic used to diagnose cases where there are |
| 4760 | /// no matching candidates. |
| 4761 | /// |
| 4762 | /// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one |
| 4763 | /// occurs. |
| 4764 | /// |
| 4765 | /// \param CandidateDiag partial diagnostic used for each function template |
| 4766 | /// specialization that is a candidate in the ambiguous ordering. One parameter |
| 4767 | /// in this diagnostic should be unbound, which will correspond to the string |
| 4768 | /// describing the template arguments for the function template specialization. |
| 4769 | /// |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4770 | /// \returns the most specialized function template specialization, if |
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4771 | /// found. Otherwise, returns SpecEnd. |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4772 | UnresolvedSetIterator Sema::getMostSpecialized( |
| 4773 | UnresolvedSetIterator SpecBegin, UnresolvedSetIterator SpecEnd, |
| 4774 | TemplateSpecCandidateSet &FailedCandidates, |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4775 | SourceLocation Loc, const PartialDiagnostic &NoneDiag, |
| 4776 | const PartialDiagnostic &AmbigDiag, const PartialDiagnostic &CandidateDiag, |
| 4777 | bool Complain, QualType TargetType) { |
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4778 | if (SpecBegin == SpecEnd) { |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4779 | if (Complain) { |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4780 | Diag(Loc, NoneDiag); |
| Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4781 | FailedCandidates.NoteCandidates(*this, Loc); |
| 4782 | } |
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4783 | return SpecEnd; |
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4784 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4785 | |
| 4786 | if (SpecBegin + 1 == SpecEnd) |
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4787 | return SpecBegin; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4788 | |
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4789 | // Find the function template that is better than all of the templates it |
| 4790 | // has been compared to. |
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4791 | UnresolvedSetIterator Best = SpecBegin; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4792 | FunctionTemplateDecl *BestTemplate |
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4793 | = cast<FunctionDecl>(*Best)->getPrimaryTemplate(); |
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4794 | assert(BestTemplate && "Not a function template specialization?"); |
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4795 | for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) { |
| 4796 | FunctionTemplateDecl *Challenger |
| 4797 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4798 | assert(Challenger && "Not a function template specialization?"); |
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4799 | if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
| Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4800 | Loc, TPOC_Other, 0, 0), |
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4801 | Challenger)) { |
| 4802 | Best = I; |
| 4803 | BestTemplate = Challenger; |
| 4804 | } |
| 4805 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4806 | |
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4807 | // Make sure that the "best" function template is more specialized than all |
| 4808 | // of the others. |
| 4809 | bool Ambiguous = false; |
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4810 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4811 | FunctionTemplateDecl *Challenger |
| 4812 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4813 | if (I != Best && |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4814 | !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
| Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4815 | Loc, TPOC_Other, 0, 0), |
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4816 | BestTemplate)) { |
| 4817 | Ambiguous = true; |
| 4818 | break; |
| 4819 | } |
| 4820 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4821 | |
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4822 | if (!Ambiguous) { |
| 4823 | // We found an answer. Return it. |
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4824 | return Best; |
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4825 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4826 | |
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4827 | // Diagnose the ambiguity. |
| Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4828 | if (Complain) { |
| Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4829 | Diag(Loc, AmbigDiag); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4830 | |
| Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4831 | // FIXME: Can we order the candidates in some sane way? |
| Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4832 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4833 | PartialDiagnostic PD = CandidateDiag; |
| Saleem Abdulrasool | 78704fb | 2016-12-22 04:26:57 +0000 | [diff] [blame] | 4834 | const auto *FD = cast<FunctionDecl>(*I); |
| 4835 | PD << FD << getTemplateArgumentBindingsText( |
| 4836 | FD->getPrimaryTemplate()->getTemplateParameters(), |
| 4837 | *FD->getTemplateSpecializationArgs()); |
| Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4838 | if (!TargetType.isNull()) |
| Saleem Abdulrasool | 78704fb | 2016-12-22 04:26:57 +0000 | [diff] [blame] | 4839 | HandleFunctionTypeMismatch(PD, FD->getType(), TargetType); |
| Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4840 | Diag((*I)->getLocation(), PD); |
| 4841 | } |
| Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4842 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4843 | |
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4844 | return SpecEnd; |
| Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4845 | } |
| 4846 | |
| Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4847 | /// Determine whether one partial specialization, P1, is at least as |
| 4848 | /// specialized than another, P2. |
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4849 | /// |
| Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 4850 | /// \tparam TemplateLikeDecl The kind of P2, which must be a |
| 4851 | /// TemplateDecl or {Class,Var}TemplatePartialSpecializationDecl. |
| Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4852 | /// \param T1 The injected-class-name of P1 (faked for a variable template). |
| 4853 | /// \param T2 The injected-class-name of P2 (faked for a variable template). |
| Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 4854 | template<typename TemplateLikeDecl> |
| Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4855 | static bool isAtLeastAsSpecializedAs(Sema &S, QualType T1, QualType T2, |
| Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 4856 | TemplateLikeDecl *P2, |
| Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 4857 | TemplateDeductionInfo &Info) { |
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4858 | // C++ [temp.class.order]p1: |
| 4859 | // For two class template partial specializations, the first is at least as |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4860 | // specialized as the second if, given the following rewrite to two |
| 4861 | // function templates, the first function template is at least as |
| 4862 | // specialized as the second according to the ordering rules for function |
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4863 | // templates (14.6.6.2): |
| 4864 | // - the first function template has the same template parameters as the |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4865 | // first partial specialization and has a single function parameter |
| 4866 | // whose type is a class template specialization with the template |
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4867 | // arguments of the first partial specialization, and |
| 4868 | // - the second function template has the same template parameters as the |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4869 | // second partial specialization and has a single function parameter |
| 4870 | // whose type is a class template specialization with the template |
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4871 | // arguments of the second partial specialization. |
| 4872 | // |
| Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 4873 | // Rather than synthesize function templates, we merely perform the |
| 4874 | // equivalent partial ordering by performing deduction directly on |
| 4875 | // the template arguments of the class template partial |
| 4876 | // specializations. This computation is slightly simpler than the |
| 4877 | // general problem of function template partial ordering, because |
| 4878 | // class template partial specializations are more constrained. We |
| 4879 | // know that every template parameter is deducible from the class |
| 4880 | // template partial specialization's template arguments, for |
| 4881 | // example. |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4882 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4883 | |
| Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4884 | // Determine whether P1 is at least as specialized as P2. |
| 4885 | Deduced.resize(P2->getTemplateParameters()->size()); |
| 4886 | if (DeduceTemplateArgumentsByTypeMatch(S, P2->getTemplateParameters(), |
| 4887 | T2, T1, Info, Deduced, TDF_None, |
| 4888 | /*PartialOrdering=*/true)) |
| 4889 | return false; |
| 4890 | |
| 4891 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), |
| 4892 | Deduced.end()); |
| Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 4893 | Sema::InstantiatingTemplate Inst(S, Info.getLocation(), P2, DeducedArgs, |
| 4894 | Info); |
| Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4895 | auto *TST1 = T1->castAs<TemplateSpecializationType>(); |
| 4896 | if (FinishTemplateArgumentDeduction( |
| Richard Smith | 87d263e | 2016-12-25 08:05:23 +0000 | [diff] [blame] | 4897 | S, P2, /*PartialOrdering=*/true, |
| 4898 | TemplateArgumentList(TemplateArgumentList::OnStack, |
| 4899 | TST1->template_arguments()), |
| Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4900 | Deduced, Info)) |
| 4901 | return false; |
| 4902 | |
| 4903 | return true; |
| 4904 | } |
| 4905 | |
| 4906 | /// \brief Returns the more specialized class template partial specialization |
| 4907 | /// according to the rules of partial ordering of class template partial |
| 4908 | /// specializations (C++ [temp.class.order]). |
| 4909 | /// |
| 4910 | /// \param PS1 the first class template partial specialization |
| 4911 | /// |
| 4912 | /// \param PS2 the second class template partial specialization |
| 4913 | /// |
| 4914 | /// \returns the more specialized class template partial specialization. If |
| 4915 | /// neither partial specialization is more specialized, returns NULL. |
| 4916 | ClassTemplatePartialSpecializationDecl * |
| 4917 | Sema::getMoreSpecializedPartialSpecialization( |
| 4918 | ClassTemplatePartialSpecializationDecl *PS1, |
| 4919 | ClassTemplatePartialSpecializationDecl *PS2, |
| 4920 | SourceLocation Loc) { |
| John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4921 | QualType PT1 = PS1->getInjectedSpecializationType(); |
| 4922 | QualType PT2 = PS2->getInjectedSpecializationType(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4923 | |
| Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 4924 | TemplateDeductionInfo Info(Loc); |
| 4925 | bool Better1 = isAtLeastAsSpecializedAs(*this, PT1, PT2, PS2, Info); |
| 4926 | bool Better2 = isAtLeastAsSpecializedAs(*this, PT2, PT1, PS1, Info); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4927 | |
| 4928 | if (Better1 == Better2) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4929 | return nullptr; |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4930 | |
| 4931 | return Better1 ? PS1 : PS2; |
| 4932 | } |
| 4933 | |
| Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 4934 | bool Sema::isMoreSpecializedThanPrimary( |
| 4935 | ClassTemplatePartialSpecializationDecl *Spec, TemplateDeductionInfo &Info) { |
| 4936 | ClassTemplateDecl *Primary = Spec->getSpecializedTemplate(); |
| 4937 | QualType PrimaryT = Primary->getInjectedClassNameSpecialization(); |
| 4938 | QualType PartialT = Spec->getInjectedSpecializationType(); |
| 4939 | if (!isAtLeastAsSpecializedAs(*this, PartialT, PrimaryT, Primary, Info)) |
| 4940 | return false; |
| 4941 | if (isAtLeastAsSpecializedAs(*this, PrimaryT, PartialT, Spec, Info)) { |
| 4942 | Info.clearSFINAEDiagnostic(); |
| 4943 | return false; |
| 4944 | } |
| 4945 | return true; |
| 4946 | } |
| 4947 | |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4948 | VarTemplatePartialSpecializationDecl * |
| 4949 | Sema::getMoreSpecializedPartialSpecialization( |
| 4950 | VarTemplatePartialSpecializationDecl *PS1, |
| 4951 | VarTemplatePartialSpecializationDecl *PS2, SourceLocation Loc) { |
| Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4952 | // Pretend the variable template specializations are class template |
| 4953 | // specializations and form a fake injected class name type for comparison. |
| Richard Smith | f04fd0b | 2013-12-12 23:14:16 +0000 | [diff] [blame] | 4954 | assert(PS1->getSpecializedTemplate() == PS2->getSpecializedTemplate() && |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4955 | "the partial specializations being compared should specialize" |
| 4956 | " the same template."); |
| 4957 | TemplateName Name(PS1->getSpecializedTemplate()); |
| 4958 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4959 | QualType PT1 = Context.getTemplateSpecializationType( |
| David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4960 | CanonTemplate, PS1->getTemplateArgs().asArray()); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4961 | QualType PT2 = Context.getTemplateSpecializationType( |
| David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4962 | CanonTemplate, PS2->getTemplateArgs().asArray()); |
| Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4963 | |
| Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 4964 | TemplateDeductionInfo Info(Loc); |
| 4965 | bool Better1 = isAtLeastAsSpecializedAs(*this, PT1, PT2, PS2, Info); |
| 4966 | bool Better2 = isAtLeastAsSpecializedAs(*this, PT2, PT1, PS1, Info); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4967 | |
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4968 | if (Better1 == Better2) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4969 | return nullptr; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4970 | |
| Richard Smith | 0da6dc4 | 2016-12-24 16:40:51 +0000 | [diff] [blame] | 4971 | return Better1 ? PS1 : PS2; |
| Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4972 | } |
| 4973 | |
| Richard Smith | 0e617ec | 2016-12-27 07:56:27 +0000 | [diff] [blame] | 4974 | bool Sema::isMoreSpecializedThanPrimary( |
| 4975 | VarTemplatePartialSpecializationDecl *Spec, TemplateDeductionInfo &Info) { |
| 4976 | TemplateDecl *Primary = Spec->getSpecializedTemplate(); |
| 4977 | // FIXME: Cache the injected template arguments rather than recomputing |
| 4978 | // them for each partial specialization. |
| 4979 | SmallVector<TemplateArgument, 8> PrimaryArgs; |
| 4980 | Context.getInjectedTemplateArgs(Primary->getTemplateParameters(), |
| 4981 | PrimaryArgs); |
| 4982 | |
| 4983 | TemplateName CanonTemplate = |
| 4984 | Context.getCanonicalTemplateName(TemplateName(Primary)); |
| 4985 | QualType PrimaryT = Context.getTemplateSpecializationType( |
| 4986 | CanonTemplate, PrimaryArgs); |
| 4987 | QualType PartialT = Context.getTemplateSpecializationType( |
| 4988 | CanonTemplate, Spec->getTemplateArgs().asArray()); |
| 4989 | if (!isAtLeastAsSpecializedAs(*this, PartialT, PrimaryT, Primary, Info)) |
| 4990 | return false; |
| 4991 | if (isAtLeastAsSpecializedAs(*this, PrimaryT, PartialT, Spec, Info)) { |
| 4992 | Info.clearSFINAEDiagnostic(); |
| 4993 | return false; |
| 4994 | } |
| 4995 | return true; |
| 4996 | } |
| 4997 | |
| Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 4998 | bool Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs( |
| 4999 | TemplateParameterList *P, TemplateDecl *AArg, SourceLocation Loc) { |
| 5000 | // C++1z [temp.arg.template]p4: (DR 150) |
| 5001 | // A template template-parameter P is at least as specialized as a |
| 5002 | // template template-argument A if, given the following rewrite to two |
| 5003 | // function templates... |
| 5004 | |
| 5005 | // Rather than synthesize function templates, we merely perform the |
| 5006 | // equivalent partial ordering by performing deduction directly on |
| 5007 | // the template parameter lists of the template template parameters. |
| 5008 | // |
| 5009 | // Given an invented class template X with the template parameter list of |
| 5010 | // A (including default arguments): |
| 5011 | TemplateName X = Context.getCanonicalTemplateName(TemplateName(AArg)); |
| 5012 | TemplateParameterList *A = AArg->getTemplateParameters(); |
| 5013 | |
| 5014 | // - Each function template has a single function parameter whose type is |
| 5015 | // a specialization of X with template arguments corresponding to the |
| 5016 | // template parameters from the respective function template |
| 5017 | SmallVector<TemplateArgument, 8> AArgs; |
| 5018 | Context.getInjectedTemplateArgs(A, AArgs); |
| 5019 | |
| 5020 | // Check P's arguments against A's parameter list. This will fill in default |
| 5021 | // template arguments as needed. AArgs are already correct by construction. |
| 5022 | // We can't just use CheckTemplateIdType because that will expand alias |
| 5023 | // templates. |
| 5024 | SmallVector<TemplateArgument, 4> PArgs; |
| 5025 | { |
| 5026 | SFINAETrap Trap(*this); |
| 5027 | |
| 5028 | Context.getInjectedTemplateArgs(P, PArgs); |
| 5029 | TemplateArgumentListInfo PArgList(P->getLAngleLoc(), P->getRAngleLoc()); |
| 5030 | for (unsigned I = 0, N = P->size(); I != N; ++I) { |
| 5031 | // Unwrap packs that getInjectedTemplateArgs wrapped around pack |
| 5032 | // expansions, to form an "as written" argument list. |
| 5033 | TemplateArgument Arg = PArgs[I]; |
| 5034 | if (Arg.getKind() == TemplateArgument::Pack) { |
| 5035 | assert(Arg.pack_size() == 1 && Arg.pack_begin()->isPackExpansion()); |
| 5036 | Arg = *Arg.pack_begin(); |
| 5037 | } |
| 5038 | PArgList.addArgument(getTrivialTemplateArgumentLoc( |
| 5039 | Arg, QualType(), P->getParam(I)->getLocation())); |
| 5040 | } |
| 5041 | PArgs.clear(); |
| 5042 | |
| 5043 | // C++1z [temp.arg.template]p3: |
| 5044 | // If the rewrite produces an invalid type, then P is not at least as |
| 5045 | // specialized as A. |
| 5046 | if (CheckTemplateArgumentList(AArg, Loc, PArgList, false, PArgs) || |
| 5047 | Trap.hasErrorOccurred()) |
| 5048 | return false; |
| 5049 | } |
| 5050 | |
| 5051 | QualType AType = Context.getTemplateSpecializationType(X, AArgs); |
| 5052 | QualType PType = Context.getTemplateSpecializationType(X, PArgs); |
| 5053 | |
| Richard Smith | 26b86ea | 2016-12-31 21:41:23 +0000 | [diff] [blame] | 5054 | // ... the function template corresponding to P is at least as specialized |
| 5055 | // as the function template corresponding to A according to the partial |
| 5056 | // ordering rules for function templates. |
| 5057 | TemplateDeductionInfo Info(Loc, A->getDepth()); |
| 5058 | return isAtLeastAsSpecializedAs(*this, PType, AType, AArg, Info); |
| 5059 | } |
| 5060 | |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5061 | /// \brief Mark the template parameters that are used by the given |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5062 | /// expression. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5063 | static void |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5064 | MarkUsedTemplateParameters(ASTContext &Ctx, |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5065 | const Expr *E, |
| 5066 | bool OnlyDeduced, |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5067 | unsigned Depth, |
| Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5068 | llvm::SmallBitVector &Used) { |
| Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 5069 | // We can deduce from a pack expansion. |
| 5070 | if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E)) |
| 5071 | E = Expansion->getPattern(); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5072 | |
| Richard Smith | 3434900 | 2012-07-09 03:07:20 +0000 | [diff] [blame] | 5073 | // Skip through any implicit casts we added while type-checking, and any |
| 5074 | // substitutions performed by template alias expansion. |
| Eugene Zelenko | 82eb70f | 2018-02-22 22:35:17 +0000 | [diff] [blame] | 5075 | while (true) { |
| Richard Smith | 3434900 | 2012-07-09 03:07:20 +0000 | [diff] [blame] | 5076 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 5077 | E = ICE->getSubExpr(); |
| 5078 | else if (const SubstNonTypeTemplateParmExpr *Subst = |
| 5079 | dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 5080 | E = Subst->getReplacement(); |
| 5081 | else |
| 5082 | break; |
| 5083 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5084 | |
| 5085 | // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5086 | // find other occurrences of template parameters. |
| Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 5087 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
| Douglas Gregor | 04b1152 | 2010-01-14 18:13:22 +0000 | [diff] [blame] | 5088 | if (!DRE) |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5089 | return; |
| 5090 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5091 | const NonTypeTemplateParmDecl *NTTP |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5092 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 5093 | if (!NTTP) |
| 5094 | return; |
| 5095 | |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5096 | if (NTTP->getDepth() == Depth) |
| 5097 | Used[NTTP->getIndex()] = true; |
| Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5098 | |
| Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5099 | // In C++17 mode, additional arguments may be deduced from the type of a |
| Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5100 | // non-type argument. |
| Aaron Ballman | c351fba | 2017-12-04 20:27:34 +0000 | [diff] [blame] | 5101 | if (Ctx.getLangOpts().CPlusPlus17) |
| Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5102 | MarkUsedTemplateParameters(Ctx, NTTP->getType(), OnlyDeduced, Depth, Used); |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5103 | } |
| 5104 | |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5105 | /// \brief Mark the template parameters that are used by the given |
| 5106 | /// nested name specifier. |
| 5107 | static void |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5108 | MarkUsedTemplateParameters(ASTContext &Ctx, |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5109 | NestedNameSpecifier *NNS, |
| 5110 | bool OnlyDeduced, |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5111 | unsigned Depth, |
| Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5112 | llvm::SmallBitVector &Used) { |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5113 | if (!NNS) |
| 5114 | return; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5115 | |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5116 | MarkUsedTemplateParameters(Ctx, NNS->getPrefix(), OnlyDeduced, Depth, |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5117 | Used); |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5118 | MarkUsedTemplateParameters(Ctx, QualType(NNS->getAsType(), 0), |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5119 | OnlyDeduced, Depth, Used); |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5120 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5121 | |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5122 | /// \brief Mark the template parameters that are used by the given |
| 5123 | /// template name. |
| 5124 | static void |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5125 | MarkUsedTemplateParameters(ASTContext &Ctx, |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5126 | TemplateName Name, |
| 5127 | bool OnlyDeduced, |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5128 | unsigned Depth, |
| Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5129 | llvm::SmallBitVector &Used) { |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5130 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 5131 | if (TemplateTemplateParmDecl *TTP |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5132 | = dyn_cast<TemplateTemplateParmDecl>(Template)) { |
| 5133 | if (TTP->getDepth() == Depth) |
| 5134 | Used[TTP->getIndex()] = true; |
| 5135 | } |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5136 | return; |
| 5137 | } |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5138 | |
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5139 | if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5140 | MarkUsedTemplateParameters(Ctx, QTN->getQualifier(), OnlyDeduced, |
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5141 | Depth, Used); |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5142 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5143 | MarkUsedTemplateParameters(Ctx, DTN->getQualifier(), OnlyDeduced, |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5144 | Depth, Used); |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5145 | } |
| 5146 | |
| 5147 | /// \brief Mark the template parameters that are used by the given |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5148 | /// type. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5149 | static void |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5150 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5151 | bool OnlyDeduced, |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5152 | unsigned Depth, |
| Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5153 | llvm::SmallBitVector &Used) { |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5154 | if (T.isNull()) |
| 5155 | return; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5156 | |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5157 | // Non-dependent types have nothing deducible |
| 5158 | if (!T->isDependentType()) |
| 5159 | return; |
| 5160 | |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5161 | T = Ctx.getCanonicalType(T); |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5162 | switch (T->getTypeClass()) { |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5163 | case Type::Pointer: |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5164 | MarkUsedTemplateParameters(Ctx, |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5165 | cast<PointerType>(T)->getPointeeType(), |
| 5166 | OnlyDeduced, |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5167 | Depth, |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5168 | Used); |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5169 | break; |
| 5170 | |
| 5171 | case Type::BlockPointer: |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5172 | MarkUsedTemplateParameters(Ctx, |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5173 | cast<BlockPointerType>(T)->getPointeeType(), |
| 5174 | OnlyDeduced, |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5175 | Depth, |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5176 | Used); |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5177 | break; |
| 5178 | |
| 5179 | case Type::LValueReference: |
| 5180 | case Type::RValueReference: |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5181 | MarkUsedTemplateParameters(Ctx, |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5182 | cast<ReferenceType>(T)->getPointeeType(), |
| 5183 | OnlyDeduced, |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5184 | Depth, |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5185 | Used); |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5186 | break; |
| 5187 | |
| 5188 | case Type::MemberPointer: { |
| 5189 | const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5190 | MarkUsedTemplateParameters(Ctx, MemPtr->getPointeeType(), OnlyDeduced, |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5191 | Depth, Used); |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5192 | MarkUsedTemplateParameters(Ctx, QualType(MemPtr->getClass(), 0), |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5193 | OnlyDeduced, Depth, Used); |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5194 | break; |
| 5195 | } |
| 5196 | |
| 5197 | case Type::DependentSizedArray: |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5198 | MarkUsedTemplateParameters(Ctx, |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5199 | cast<DependentSizedArrayType>(T)->getSizeExpr(), |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5200 | OnlyDeduced, Depth, Used); |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5201 | // Fall through to check the element type |
| Galina Kistanova | 3339911 | 2017-06-03 06:35:06 +0000 | [diff] [blame] | 5202 | LLVM_FALLTHROUGH; |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5203 | |
| 5204 | case Type::ConstantArray: |
| 5205 | case Type::IncompleteArray: |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5206 | MarkUsedTemplateParameters(Ctx, |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5207 | cast<ArrayType>(T)->getElementType(), |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5208 | OnlyDeduced, Depth, Used); |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5209 | break; |
| 5210 | |
| 5211 | case Type::Vector: |
| 5212 | case Type::ExtVector: |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5213 | MarkUsedTemplateParameters(Ctx, |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5214 | cast<VectorType>(T)->getElementType(), |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5215 | OnlyDeduced, Depth, Used); |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5216 | break; |
| 5217 | |
| Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5218 | case Type::DependentSizedExtVector: { |
| 5219 | const DependentSizedExtVectorType *VecType |
| Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 5220 | = cast<DependentSizedExtVectorType>(T); |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5221 | MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5222 | Depth, Used); |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5223 | MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5224 | Depth, Used); |
| Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 5225 | break; |
| 5226 | } |
| 5227 | |
| Andrew Gozillon | 572bbb0 | 2017-10-02 06:25:51 +0000 | [diff] [blame] | 5228 | case Type::DependentAddressSpace: { |
| 5229 | const DependentAddressSpaceType *DependentASType = |
| 5230 | cast<DependentAddressSpaceType>(T); |
| 5231 | MarkUsedTemplateParameters(Ctx, DependentASType->getPointeeType(), |
| 5232 | OnlyDeduced, Depth, Used); |
| 5233 | MarkUsedTemplateParameters(Ctx, |
| 5234 | DependentASType->getAddrSpaceExpr(), |
| 5235 | OnlyDeduced, Depth, Used); |
| 5236 | break; |
| 5237 | } |
| 5238 | |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5239 | case Type::FunctionProto: { |
| Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 5240 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 5241 | MarkUsedTemplateParameters(Ctx, Proto->getReturnType(), OnlyDeduced, Depth, |
| 5242 | Used); |
| Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 5243 | for (unsigned I = 0, N = Proto->getNumParams(); I != N; ++I) |
| 5244 | MarkUsedTemplateParameters(Ctx, Proto->getParamType(I), OnlyDeduced, |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5245 | Depth, Used); |
| Richard Smith | cd19815 | 2017-06-07 21:46:22 +0000 | [diff] [blame] | 5246 | if (auto *E = Proto->getNoexceptExpr()) |
| 5247 | MarkUsedTemplateParameters(Ctx, E, OnlyDeduced, Depth, Used); |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5248 | break; |
| 5249 | } |
| 5250 | |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5251 | case Type::TemplateTypeParm: { |
| 5252 | const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T); |
| 5253 | if (TTP->getDepth() == Depth) |
| 5254 | Used[TTP->getIndex()] = true; |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5255 | break; |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5256 | } |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5257 | |
| Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 5258 | case Type::SubstTemplateTypeParmPack: { |
| 5259 | const SubstTemplateTypeParmPackType *Subst |
| 5260 | = cast<SubstTemplateTypeParmPackType>(T); |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5261 | MarkUsedTemplateParameters(Ctx, |
| Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 5262 | QualType(Subst->getReplacedParameter(), 0), |
| 5263 | OnlyDeduced, Depth, Used); |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5264 | MarkUsedTemplateParameters(Ctx, Subst->getArgumentPack(), |
| Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 5265 | OnlyDeduced, Depth, Used); |
| 5266 | break; |
| 5267 | } |
| 5268 | |
| John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 5269 | case Type::InjectedClassName: |
| 5270 | T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); |
| Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 5271 | LLVM_FALLTHROUGH; |
| John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 5272 | |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5273 | case Type::TemplateSpecialization: { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5274 | const TemplateSpecializationType *Spec |
| Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 5275 | = cast<TemplateSpecializationType>(T); |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5276 | MarkUsedTemplateParameters(Ctx, Spec->getTemplateName(), OnlyDeduced, |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5277 | Depth, Used); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5278 | |
| Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5279 | // C++0x [temp.deduct.type]p9: |
| Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 5280 | // If the template argument list of P contains a pack expansion that is |
| 5281 | // not the last template argument, the entire template argument list is a |
| Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5282 | // non-deduced context. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5283 | if (OnlyDeduced && |
| Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 5284 | hasPackExpansionBeforeEnd(Spec->template_arguments())) |
| Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5285 | break; |
| 5286 | |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5287 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5288 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5289 | Used); |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5290 | break; |
| 5291 | } |
| 5292 | |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5293 | case Type::Complex: |
| 5294 | if (!OnlyDeduced) |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5295 | MarkUsedTemplateParameters(Ctx, |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5296 | cast<ComplexType>(T)->getElementType(), |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5297 | OnlyDeduced, Depth, Used); |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5298 | break; |
| 5299 | |
| Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5300 | case Type::Atomic: |
| 5301 | if (!OnlyDeduced) |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5302 | MarkUsedTemplateParameters(Ctx, |
| Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5303 | cast<AtomicType>(T)->getValueType(), |
| 5304 | OnlyDeduced, Depth, Used); |
| 5305 | break; |
| 5306 | |
| Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5307 | case Type::DependentName: |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5308 | if (!OnlyDeduced) |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5309 | MarkUsedTemplateParameters(Ctx, |
| Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 5310 | cast<DependentNameType>(T)->getQualifier(), |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5311 | OnlyDeduced, Depth, Used); |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5312 | break; |
| 5313 | |
| John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5314 | case Type::DependentTemplateSpecialization: { |
| Richard Smith | 50d5b97 | 2015-12-30 20:56:05 +0000 | [diff] [blame] | 5315 | // C++14 [temp.deduct.type]p5: |
| 5316 | // The non-deduced contexts are: |
| 5317 | // -- The nested-name-specifier of a type that was specified using a |
| 5318 | // qualified-id |
| 5319 | // |
| 5320 | // C++14 [temp.deduct.type]p6: |
| 5321 | // When a type name is specified in a way that includes a non-deduced |
| 5322 | // context, all of the types that comprise that type name are also |
| 5323 | // non-deduced. |
| 5324 | if (OnlyDeduced) |
| 5325 | break; |
| 5326 | |
| John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5327 | const DependentTemplateSpecializationType *Spec |
| 5328 | = cast<DependentTemplateSpecializationType>(T); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5329 | |
| Richard Smith | 50d5b97 | 2015-12-30 20:56:05 +0000 | [diff] [blame] | 5330 | MarkUsedTemplateParameters(Ctx, Spec->getQualifier(), |
| 5331 | OnlyDeduced, Depth, Used); |
| Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5332 | |
| John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5333 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5334 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
| John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5335 | Used); |
| 5336 | break; |
| 5337 | } |
| 5338 | |
| John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5339 | case Type::TypeOf: |
| 5340 | if (!OnlyDeduced) |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5341 | MarkUsedTemplateParameters(Ctx, |
| John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5342 | cast<TypeOfType>(T)->getUnderlyingType(), |
| 5343 | OnlyDeduced, Depth, Used); |
| 5344 | break; |
| 5345 | |
| 5346 | case Type::TypeOfExpr: |
| 5347 | if (!OnlyDeduced) |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5348 | MarkUsedTemplateParameters(Ctx, |
| John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5349 | cast<TypeOfExprType>(T)->getUnderlyingExpr(), |
| 5350 | OnlyDeduced, Depth, Used); |
| 5351 | break; |
| 5352 | |
| 5353 | case Type::Decltype: |
| 5354 | if (!OnlyDeduced) |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5355 | MarkUsedTemplateParameters(Ctx, |
| John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5356 | cast<DecltypeType>(T)->getUnderlyingExpr(), |
| 5357 | OnlyDeduced, Depth, Used); |
| 5358 | break; |
| 5359 | |
| Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 5360 | case Type::UnaryTransform: |
| 5361 | if (!OnlyDeduced) |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5362 | MarkUsedTemplateParameters(Ctx, |
| Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5363 | cast<UnaryTransformType>(T)->getUnderlyingType(), |
| Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 5364 | OnlyDeduced, Depth, Used); |
| 5365 | break; |
| 5366 | |
| Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 5367 | case Type::PackExpansion: |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5368 | MarkUsedTemplateParameters(Ctx, |
| Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 5369 | cast<PackExpansionType>(T)->getPattern(), |
| 5370 | OnlyDeduced, Depth, Used); |
| 5371 | break; |
| 5372 | |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5373 | case Type::Auto: |
| Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 5374 | case Type::DeducedTemplateSpecialization: |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5375 | MarkUsedTemplateParameters(Ctx, |
| Richard Smith | 600b526 | 2017-01-26 20:40:47 +0000 | [diff] [blame] | 5376 | cast<DeducedType>(T)->getDeducedType(), |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5377 | OnlyDeduced, Depth, Used); |
| Adrian Prantl | 4b49085 | 2017-12-19 22:21:48 +0000 | [diff] [blame] | 5378 | break; |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5379 | |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5380 | // None of these types have any template parameters in them. |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5381 | case Type::Builtin: |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5382 | case Type::VariableArray: |
| 5383 | case Type::FunctionNoProto: |
| 5384 | case Type::Record: |
| 5385 | case Type::Enum: |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5386 | case Type::ObjCInterface: |
| John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5387 | case Type::ObjCObject: |
| Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 5388 | case Type::ObjCObjectPointer: |
| John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5389 | case Type::UnresolvedUsing: |
| Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5390 | case Type::Pipe: |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5391 | #define TYPE(Class, Base) |
| 5392 | #define ABSTRACT_TYPE(Class, Base) |
| 5393 | #define DEPENDENT_TYPE(Class, Base) |
| 5394 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 5395 | #include "clang/AST/TypeNodes.def" |
| 5396 | break; |
| 5397 | } |
| 5398 | } |
| 5399 | |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5400 | /// \brief Mark the template parameters that are used by this |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5401 | /// template argument. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5402 | static void |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5403 | MarkUsedTemplateParameters(ASTContext &Ctx, |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5404 | const TemplateArgument &TemplateArg, |
| 5405 | bool OnlyDeduced, |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5406 | unsigned Depth, |
| Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5407 | llvm::SmallBitVector &Used) { |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5408 | switch (TemplateArg.getKind()) { |
| 5409 | case TemplateArgument::Null: |
| 5410 | case TemplateArgument::Integral: |
| Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5411 | case TemplateArgument::Declaration: |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5412 | break; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5413 | |
| Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5414 | case TemplateArgument::NullPtr: |
| 5415 | MarkUsedTemplateParameters(Ctx, TemplateArg.getNullPtrType(), OnlyDeduced, |
| 5416 | Depth, Used); |
| 5417 | break; |
| 5418 | |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5419 | case TemplateArgument::Type: |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5420 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsType(), OnlyDeduced, |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5421 | Depth, Used); |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5422 | break; |
| 5423 | |
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5424 | case TemplateArgument::Template: |
| Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 5425 | case TemplateArgument::TemplateExpansion: |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5426 | MarkUsedTemplateParameters(Ctx, |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5427 | TemplateArg.getAsTemplateOrTemplatePattern(), |
| Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5428 | OnlyDeduced, Depth, Used); |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5429 | break; |
| 5430 | |
| 5431 | case TemplateArgument::Expression: |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5432 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsExpr(), OnlyDeduced, |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5433 | Depth, Used); |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5434 | break; |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5435 | |
| Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5436 | case TemplateArgument::Pack: |
| Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 5437 | for (const auto &P : TemplateArg.pack_elements()) |
| 5438 | MarkUsedTemplateParameters(Ctx, P, OnlyDeduced, Depth, Used); |
| Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5439 | break; |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5440 | } |
| 5441 | } |
| 5442 | |
| James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 5443 | /// \brief Mark which template parameters can be deduced from a given |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5444 | /// template argument list. |
| 5445 | /// |
| 5446 | /// \param TemplateArgs the template argument list from which template |
| 5447 | /// parameters will be deduced. |
| 5448 | /// |
| James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 5449 | /// \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] | 5450 | /// to indicate when the corresponding template parameter will be |
| 5451 | /// deduced. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5452 | void |
| Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5453 | Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5454 | bool OnlyDeduced, unsigned Depth, |
| Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5455 | llvm::SmallBitVector &Used) { |
| Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5456 | // C++0x [temp.deduct.type]p9: |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5457 | // If the template argument list of P contains a pack expansion that is not |
| 5458 | // the last template argument, the entire template argument list is a |
| Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5459 | // non-deduced context. |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5460 | if (OnlyDeduced && |
| Richard Smith | 0bda5b5 | 2016-12-23 23:46:56 +0000 | [diff] [blame] | 5461 | hasPackExpansionBeforeEnd(TemplateArgs.asArray())) |
| Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5462 | return; |
| 5463 | |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5464 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5465 | ::MarkUsedTemplateParameters(Context, TemplateArgs[I], OnlyDeduced, |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5466 | Depth, Used); |
| Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5467 | } |
| Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5468 | |
| 5469 | /// \brief Marks all of the template parameters that will be deduced by a |
| 5470 | /// call to the given function template. |
| Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 5471 | void Sema::MarkDeducedTemplateParameters( |
| 5472 | ASTContext &Ctx, const FunctionTemplateDecl *FunctionTemplate, |
| 5473 | llvm::SmallBitVector &Deduced) { |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5474 | TemplateParameterList *TemplateParams |
| Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5475 | = FunctionTemplate->getTemplateParameters(); |
| 5476 | Deduced.clear(); |
| 5477 | Deduced.resize(TemplateParams->size()); |
| NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5478 | |
| Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5479 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 5480 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) |
| Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5481 | ::MarkUsedTemplateParameters(Ctx, Function->getParamDecl(I)->getType(), |
| Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5482 | true, TemplateParams->getDepth(), Deduced); |
| Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5483 | } |
| Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5484 | |
| Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 5485 | bool hasDeducibleTemplateParameters(Sema &S, |
| 5486 | FunctionTemplateDecl *FunctionTemplate, |
| Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5487 | QualType T) { |
| 5488 | if (!T->isDependentType()) |
| 5489 | return false; |
| 5490 | |
| Richard Smith | f0393bf | 2017-02-16 04:22:56 +0000 | [diff] [blame] | 5491 | TemplateParameterList *TemplateParams |
| 5492 | = FunctionTemplate->getTemplateParameters(); |
| 5493 | llvm::SmallBitVector Deduced(TemplateParams->size()); |
| 5494 | ::MarkUsedTemplateParameters(S.Context, T, true, TemplateParams->getDepth(), |
| 5495 | Deduced); |
| Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5496 | |
| Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5497 | return Deduced.any(); |
| Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5498 | } |