Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1 | //===------- SemaTemplateDeduction.cpp - Template Argument Deduction ------===/ |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | //===----------------------------------------------------------------------===/ |
| 8 | // |
| 9 | // This file implements C++ template argument deduction. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===/ |
| 12 | |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 13 | #include "clang/Sema/TemplateDeduction.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "TreeTransform.h" |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTLambda.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
| 20 | #include "clang/AST/ExprCXX.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "clang/AST/StmtVisitor.h" |
| 22 | #include "clang/Sema/DeclSpec.h" |
| 23 | #include "clang/Sema/Sema.h" |
| 24 | #include "clang/Sema/Template.h" |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallBitVector.h" |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 27 | |
| 28 | namespace clang { |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 29 | using namespace sema; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 30 | /// \brief Various flags that control template argument deduction. |
| 31 | /// |
| 32 | /// These flags can be bitwise-OR'd together. |
| 33 | enum TemplateDeductionFlags { |
| 34 | /// \brief No template argument deduction flags, which indicates the |
| 35 | /// strictest results for template argument deduction (as used for, e.g., |
| 36 | /// matching class template partial specializations). |
| 37 | TDF_None = 0, |
| 38 | /// \brief Within template argument deduction from a function call, we are |
| 39 | /// matching with a parameter type for which the original parameter was |
| 40 | /// a reference. |
| 41 | TDF_ParamWithReferenceType = 0x1, |
| 42 | /// \brief Within template argument deduction from a function call, we |
| 43 | /// are matching in a case where we ignore cv-qualifiers. |
| 44 | TDF_IgnoreQualifiers = 0x02, |
| 45 | /// \brief Within template argument deduction from a function call, |
| 46 | /// we are matching in a case where we can perform template argument |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 47 | /// deduction from a template-id of a derived class of the argument type. |
Douglas Gregor | 406f634 | 2009-09-14 20:00:47 +0000 | [diff] [blame] | 48 | TDF_DerivedClass = 0x04, |
| 49 | /// \brief Allow non-dependent types to differ, e.g., when performing |
| 50 | /// template argument deduction from a function call where conversions |
| 51 | /// may apply. |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 52 | TDF_SkipNonDependent = 0x08, |
| 53 | /// \brief Whether we are performing template argument deduction for |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 54 | /// parameters and arguments in a top-level template argument |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 55 | TDF_TopLevelParameterTypeList = 0x10, |
| 56 | /// \brief Within template argument deduction from overload resolution per |
| 57 | /// C++ [over.over] allow matching function types that are compatible in |
| 58 | /// terms of noreturn and default calling convention adjustments. |
| 59 | TDF_InOverloadResolution = 0x20 |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 60 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 61 | } |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 62 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 63 | using namespace clang; |
| 64 | |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 65 | /// \brief Compare two APSInts, extending and switching the sign as |
| 66 | /// necessary to compare their values regardless of underlying type. |
| 67 | static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) { |
| 68 | if (Y.getBitWidth() > X.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 69 | X = X.extend(Y.getBitWidth()); |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 70 | else if (Y.getBitWidth() < X.getBitWidth()) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 71 | Y = Y.extend(X.getBitWidth()); |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 72 | |
| 73 | // If there is a signedness mismatch, correct it. |
| 74 | if (X.isSigned() != Y.isSigned()) { |
| 75 | // If the signed value is negative, then the values cannot be the same. |
| 76 | if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative())) |
| 77 | return false; |
| 78 | |
| 79 | Y.setIsSigned(true); |
| 80 | X.setIsSigned(true); |
| 81 | } |
| 82 | |
| 83 | return X == Y; |
| 84 | } |
| 85 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 86 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 87 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 88 | TemplateParameterList *TemplateParams, |
| 89 | const TemplateArgument &Param, |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 90 | TemplateArgument Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 91 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 92 | SmallVectorImpl<DeducedTemplateArgument> &Deduced); |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 93 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 94 | static Sema::TemplateDeductionResult |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 95 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 96 | TemplateParameterList *TemplateParams, |
| 97 | QualType Param, |
| 98 | QualType Arg, |
| 99 | TemplateDeductionInfo &Info, |
| 100 | SmallVectorImpl<DeducedTemplateArgument> & |
| 101 | Deduced, |
| 102 | unsigned TDF, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 103 | bool PartialOrdering = false, |
| 104 | bool DeducedFromArrayBound = false); |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 105 | |
| 106 | static Sema::TemplateDeductionResult |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 107 | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 108 | const TemplateArgument *Params, unsigned NumParams, |
| 109 | const TemplateArgument *Args, unsigned NumArgs, |
| 110 | TemplateDeductionInfo &Info, |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 111 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 112 | bool NumberOfArgumentsMustMatch); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 113 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 114 | /// \brief If the given expression is of a form that permits the deduction |
| 115 | /// of a non-type template parameter, return the declaration of that |
| 116 | /// non-type template parameter. |
| 117 | static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) { |
Richard Smith | 7ebb07c | 2012-07-08 04:37:51 +0000 | [diff] [blame] | 118 | // If we are within an alias template, the expression may have undergone |
| 119 | // any number of parameter substitutions already. |
| 120 | while (1) { |
| 121 | if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E)) |
| 122 | E = IC->getSubExpr(); |
| 123 | else if (SubstNonTypeTemplateParmExpr *Subst = |
| 124 | dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 125 | E = Subst->getReplacement(); |
| 126 | else |
| 127 | break; |
| 128 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 130 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 131 | return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 133 | return nullptr; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 136 | /// \brief Determine whether two declaration pointers refer to the same |
| 137 | /// declaration. |
| 138 | static bool isSameDeclaration(Decl *X, Decl *Y) { |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 139 | if (NamedDecl *NX = dyn_cast<NamedDecl>(X)) |
| 140 | X = NX->getUnderlyingDecl(); |
| 141 | if (NamedDecl *NY = dyn_cast<NamedDecl>(Y)) |
| 142 | Y = NY->getUnderlyingDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 143 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 144 | return X->getCanonicalDecl() == Y->getCanonicalDecl(); |
| 145 | } |
| 146 | |
| 147 | /// \brief Verify that the given, deduced template arguments are compatible. |
| 148 | /// |
| 149 | /// \returns The deduced template argument, or a NULL template argument if |
| 150 | /// the deduced template arguments were incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 151 | static DeducedTemplateArgument |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 152 | checkDeducedTemplateArguments(ASTContext &Context, |
| 153 | const DeducedTemplateArgument &X, |
| 154 | const DeducedTemplateArgument &Y) { |
| 155 | // We have no deduction for one or both of the arguments; they're compatible. |
| 156 | if (X.isNull()) |
| 157 | return Y; |
| 158 | if (Y.isNull()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 159 | return X; |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 160 | |
| 161 | switch (X.getKind()) { |
| 162 | case TemplateArgument::Null: |
| 163 | llvm_unreachable("Non-deduced template arguments handled above"); |
| 164 | |
| 165 | case TemplateArgument::Type: |
| 166 | // If two template type arguments have the same type, they're compatible. |
| 167 | if (Y.getKind() == TemplateArgument::Type && |
| 168 | Context.hasSameType(X.getAsType(), Y.getAsType())) |
| 169 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 170 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 171 | // If one of the two arguments was deduced from an array bound, the other |
| 172 | // supersedes it. |
| 173 | if (X.wasDeducedFromArrayBound() != Y.wasDeducedFromArrayBound()) |
| 174 | return X.wasDeducedFromArrayBound() ? Y : X; |
| 175 | |
| 176 | // The arguments are not compatible. |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 177 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 178 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 179 | case TemplateArgument::Integral: |
| 180 | // If we deduced a constant in one case and either a dependent expression or |
| 181 | // declaration in another case, keep the integral constant. |
| 182 | // If both are integral constants with the same value, keep that value. |
| 183 | if (Y.getKind() == TemplateArgument::Expression || |
| 184 | Y.getKind() == TemplateArgument::Declaration || |
| 185 | (Y.getKind() == TemplateArgument::Integral && |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 186 | hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 187 | return DeducedTemplateArgument(X, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 188 | X.wasDeducedFromArrayBound() && |
| 189 | Y.wasDeducedFromArrayBound()); |
| 190 | |
| 191 | // All other combinations are incompatible. |
| 192 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 193 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 194 | case TemplateArgument::Template: |
| 195 | if (Y.getKind() == TemplateArgument::Template && |
| 196 | Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate())) |
| 197 | return X; |
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 | // All other combinations are incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 200 | return DeducedTemplateArgument(); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 201 | |
| 202 | case TemplateArgument::TemplateExpansion: |
| 203 | if (Y.getKind() == TemplateArgument::TemplateExpansion && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 204 | Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(), |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 205 | Y.getAsTemplateOrTemplatePattern())) |
| 206 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 207 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 208 | // All other combinations are incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 209 | return DeducedTemplateArgument(); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 210 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 211 | case TemplateArgument::Expression: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 212 | // If we deduced a dependent expression in one case and either an integral |
| 213 | // constant or a declaration in another case, keep the integral constant |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 214 | // or declaration. |
| 215 | if (Y.getKind() == TemplateArgument::Integral || |
| 216 | Y.getKind() == TemplateArgument::Declaration) |
| 217 | return DeducedTemplateArgument(Y, X.wasDeducedFromArrayBound() && |
| 218 | Y.wasDeducedFromArrayBound()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 219 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 220 | if (Y.getKind() == TemplateArgument::Expression) { |
| 221 | // Compare the expressions for equality |
| 222 | llvm::FoldingSetNodeID ID1, ID2; |
| 223 | X.getAsExpr()->Profile(ID1, Context, true); |
| 224 | Y.getAsExpr()->Profile(ID2, Context, true); |
| 225 | if (ID1 == ID2) |
| 226 | return X; |
| 227 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 228 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 229 | // All other combinations are incompatible. |
| 230 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 231 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 232 | case TemplateArgument::Declaration: |
| 233 | // If we deduced a declaration and a dependent expression, keep the |
| 234 | // declaration. |
| 235 | if (Y.getKind() == TemplateArgument::Expression) |
| 236 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 237 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 238 | // If we deduced a declaration and an integral constant, keep the |
| 239 | // integral constant. |
| 240 | if (Y.getKind() == TemplateArgument::Integral) |
| 241 | return Y; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 242 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 243 | // If we deduced two declarations, make sure they they refer to the |
| 244 | // same declaration. |
| 245 | if (Y.getKind() == TemplateArgument::Declaration && |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 246 | isSameDeclaration(X.getAsDecl(), Y.getAsDecl())) |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 247 | return X; |
| 248 | |
| 249 | // All other combinations are incompatible. |
| 250 | return DeducedTemplateArgument(); |
| 251 | |
| 252 | case TemplateArgument::NullPtr: |
| 253 | // If we deduced a null pointer and a dependent expression, keep the |
| 254 | // null pointer. |
| 255 | if (Y.getKind() == TemplateArgument::Expression) |
| 256 | return X; |
| 257 | |
| 258 | // If we deduced a null pointer and an integral constant, keep the |
| 259 | // integral constant. |
| 260 | if (Y.getKind() == TemplateArgument::Integral) |
| 261 | return Y; |
| 262 | |
| 263 | // If we deduced two null pointers, make sure they have the same type. |
| 264 | if (Y.getKind() == TemplateArgument::NullPtr && |
| 265 | Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType())) |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 266 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 267 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 268 | // All other combinations are incompatible. |
| 269 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 270 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 271 | case TemplateArgument::Pack: |
| 272 | if (Y.getKind() != TemplateArgument::Pack || |
| 273 | X.pack_size() != Y.pack_size()) |
| 274 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 275 | |
| 276 | for (TemplateArgument::pack_iterator XA = X.pack_begin(), |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 277 | XAEnd = X.pack_end(), |
| 278 | YA = Y.pack_begin(); |
| 279 | XA != XAEnd; ++XA, ++YA) { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 280 | // FIXME: Do we need to merge the results together here? |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 281 | if (checkDeducedTemplateArguments(Context, |
| 282 | DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()), |
Douglas Gregor | f491ee2 | 2011-01-05 21:00:53 +0000 | [diff] [blame] | 283 | DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound())) |
| 284 | .isNull()) |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 285 | return DeducedTemplateArgument(); |
| 286 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 287 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 288 | return X; |
| 289 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 290 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 291 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | /// \brief Deduce the value of the given non-type template parameter |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 295 | /// from the given integral constant. |
Benjamin Kramer | 7320b99 | 2016-06-15 14:20:56 +0000 | [diff] [blame] | 296 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 297 | Sema &S, TemplateParameterList *TemplateParams, |
| 298 | NonTypeTemplateParmDecl *NTTP, const llvm::APSInt &Value, |
Benjamin Kramer | 7320b99 | 2016-06-15 14:20:56 +0000 | [diff] [blame] | 299 | QualType ValueType, bool DeducedFromArrayBound, TemplateDeductionInfo &Info, |
| 300 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 301 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 302 | "Cannot deduce non-type template argument with depth > 0"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 303 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 304 | DeducedTemplateArgument NewDeduced(S.Context, Value, ValueType, |
| 305 | DeducedFromArrayBound); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 306 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 307 | Deduced[NTTP->getIndex()], |
| 308 | NewDeduced); |
| 309 | if (Result.isNull()) { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 310 | Info.Param = NTTP; |
| 311 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 312 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 313 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 314 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 315 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 316 | Deduced[NTTP->getIndex()] = Result; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 317 | return S.getLangOpts().CPlusPlus1z |
| 318 | ? DeduceTemplateArgumentsByTypeMatch( |
| 319 | S, TemplateParams, NTTP->getType(), ValueType, Info, Deduced, |
| 320 | TDF_ParamWithReferenceType | TDF_SkipNonDependent, |
| 321 | /*PartialOrdering=*/false, |
| 322 | /*ArrayBound=*/DeducedFromArrayBound) |
| 323 | : Sema::TDK_Success; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | /// \brief Deduce the value of the given non-type template parameter |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 327 | /// from the given null pointer template argument type. |
| 328 | static Sema::TemplateDeductionResult DeduceNullPtrTemplateArgument( |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 329 | Sema &S, TemplateParameterList *TemplateParams, |
| 330 | NonTypeTemplateParmDecl *NTTP, QualType NullPtrType, |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 331 | TemplateDeductionInfo &Info, |
| 332 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 333 | Expr *Value = |
| 334 | S.ImpCastExprToType(new (S.Context) CXXNullPtrLiteralExpr( |
| 335 | S.Context.NullPtrTy, NTTP->getLocation()), |
| 336 | NullPtrType, CK_NullToPointer) |
| 337 | .get(); |
| 338 | DeducedTemplateArgument NewDeduced(Value); |
| 339 | DeducedTemplateArgument Result = checkDeducedTemplateArguments( |
| 340 | S.Context, Deduced[NTTP->getIndex()], NewDeduced); |
| 341 | |
| 342 | if (Result.isNull()) { |
| 343 | Info.Param = NTTP; |
| 344 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
| 345 | Info.SecondArg = NewDeduced; |
| 346 | return Sema::TDK_Inconsistent; |
| 347 | } |
| 348 | |
| 349 | Deduced[NTTP->getIndex()] = Result; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 350 | return S.getLangOpts().CPlusPlus1z |
| 351 | ? DeduceTemplateArgumentsByTypeMatch( |
| 352 | S, TemplateParams, NTTP->getType(), Value->getType(), Info, |
| 353 | Deduced, TDF_ParamWithReferenceType | TDF_SkipNonDependent) |
| 354 | : Sema::TDK_Success; |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /// \brief Deduce the value of the given non-type template parameter |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 358 | /// from the given type- or value-dependent expression. |
| 359 | /// |
| 360 | /// \returns true if deduction succeeded, false otherwise. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 361 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 362 | DeduceNonTypeTemplateArgument(Sema &S, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 363 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 364 | NonTypeTemplateParmDecl *NTTP, |
| 365 | Expr *Value, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 366 | TemplateDeductionInfo &Info, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 367 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 368 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 369 | "Cannot deduce non-type template argument with depth > 0"); |
| 370 | assert((Value->isTypeDependent() || Value->isValueDependent()) && |
| 371 | "Expression template argument must be type- or value-dependent."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 373 | DeducedTemplateArgument NewDeduced(Value); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 374 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
| 375 | Deduced[NTTP->getIndex()], |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 376 | NewDeduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 377 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 378 | if (Result.isNull()) { |
| 379 | Info.Param = NTTP; |
| 380 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
| 381 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 382 | return Sema::TDK_Inconsistent; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +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; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 386 | return S.getLangOpts().CPlusPlus1z |
| 387 | ? DeduceTemplateArgumentsByTypeMatch( |
| 388 | S, TemplateParams, NTTP->getType(), Value->getType(), Info, |
| 389 | Deduced, TDF_ParamWithReferenceType | TDF_SkipNonDependent) |
| 390 | : Sema::TDK_Success; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 393 | /// \brief Deduce the value of the given non-type template parameter |
| 394 | /// from the given declaration. |
| 395 | /// |
| 396 | /// \returns true if deduction succeeded, false otherwise. |
| 397 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 398 | DeduceNonTypeTemplateArgument(Sema &S, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 399 | TemplateParameterList *TemplateParams, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 400 | NonTypeTemplateParmDecl *NTTP, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 401 | ValueDecl *D, QualType T, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 402 | TemplateDeductionInfo &Info, |
| 403 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 404 | assert(NTTP->getDepth() == 0 && |
| 405 | "Cannot deduce non-type template argument with depth > 0"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 406 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 407 | D = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 408 | TemplateArgument New(D, NTTP->getType()); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 409 | DeducedTemplateArgument NewDeduced(New); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 410 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 411 | Deduced[NTTP->getIndex()], |
| 412 | NewDeduced); |
| 413 | if (Result.isNull()) { |
| 414 | Info.Param = NTTP; |
| 415 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
| 416 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 417 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 418 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 419 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 420 | Deduced[NTTP->getIndex()] = Result; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 421 | return S.getLangOpts().CPlusPlus1z |
| 422 | ? DeduceTemplateArgumentsByTypeMatch( |
| 423 | S, TemplateParams, NTTP->getType(), T, Info, Deduced, |
| 424 | TDF_ParamWithReferenceType | TDF_SkipNonDependent) |
| 425 | : Sema::TDK_Success; |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 428 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 429 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 430 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 431 | TemplateName Param, |
| 432 | TemplateName Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 433 | TemplateDeductionInfo &Info, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 434 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 435 | TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 436 | if (!ParamDecl) { |
| 437 | // The parameter type is dependent and is not a template template parameter, |
| 438 | // so there is nothing that we can deduce. |
| 439 | return Sema::TDK_Success; |
| 440 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 441 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 442 | if (TemplateTemplateParmDecl *TempParam |
| 443 | = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) { |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 444 | DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 445 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 446 | Deduced[TempParam->getIndex()], |
| 447 | NewDeduced); |
| 448 | if (Result.isNull()) { |
| 449 | Info.Param = TempParam; |
| 450 | Info.FirstArg = Deduced[TempParam->getIndex()]; |
| 451 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 452 | return Sema::TDK_Inconsistent; |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 453 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 454 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 455 | Deduced[TempParam->getIndex()] = Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 456 | return Sema::TDK_Success; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 457 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 458 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 459 | // Verify that the two template names are equivalent. |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 460 | if (S.Context.hasSameTemplateName(Param, Arg)) |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 461 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 462 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 463 | // Mismatch of non-dependent template parameter to argument. |
| 464 | Info.FirstArg = TemplateArgument(Param); |
| 465 | Info.SecondArg = TemplateArgument(Arg); |
| 466 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 469 | /// \brief Deduce the template arguments by comparing the template parameter |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 470 | /// type (which is a template-id) with the template argument type. |
| 471 | /// |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 472 | /// \param S the Sema |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 473 | /// |
| 474 | /// \param TemplateParams the template parameters that we are deducing |
| 475 | /// |
| 476 | /// \param Param the parameter type |
| 477 | /// |
| 478 | /// \param Arg the argument type |
| 479 | /// |
| 480 | /// \param Info information about the template argument deduction itself |
| 481 | /// |
| 482 | /// \param Deduced the deduced template arguments |
| 483 | /// |
| 484 | /// \returns the result of template argument deduction so far. Note that a |
| 485 | /// "success" result means that template argument deduction has not yet failed, |
| 486 | /// but it may still fail, later, for other reasons. |
| 487 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 488 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 489 | TemplateParameterList *TemplateParams, |
| 490 | const TemplateSpecializationType *Param, |
| 491 | QualType Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 492 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 493 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 494 | assert(Arg.isCanonical() && "Argument type must be canonical"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 496 | // Check whether the template argument is a dependent template-id. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 497 | if (const TemplateSpecializationType *SpecArg |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 498 | = dyn_cast<TemplateSpecializationType>(Arg)) { |
| 499 | // Perform template argument deduction for the template name. |
| 500 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 501 | = DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 502 | Param->getTemplateName(), |
| 503 | SpecArg->getTemplateName(), |
| 504 | Info, Deduced)) |
| 505 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 506 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 507 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 508 | // Perform template argument deduction on each template |
Douglas Gregor | d80ea20 | 2010-12-22 18:55:49 +0000 | [diff] [blame] | 509 | // argument. Ignore any missing/extra arguments, since they could be |
| 510 | // filled in by default arguments. |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 511 | return DeduceTemplateArguments(S, TemplateParams, Param->getArgs(), |
| 512 | Param->getNumArgs(), SpecArg->getArgs(), |
| 513 | SpecArg->getNumArgs(), Info, Deduced, |
| 514 | /*NumberOfArgumentsMustMatch=*/false); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 515 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 516 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 517 | // If the argument type is a class template specialization, we |
| 518 | // perform template argument deduction using its template |
| 519 | // arguments. |
| 520 | const RecordType *RecordArg = dyn_cast<RecordType>(Arg); |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 521 | if (!RecordArg) { |
| 522 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 523 | Info.SecondArg = TemplateArgument(Arg); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 524 | return Sema::TDK_NonDeducedMismatch; |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 525 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 526 | |
| 527 | ClassTemplateSpecializationDecl *SpecArg |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 528 | = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 529 | if (!SpecArg) { |
| 530 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 531 | Info.SecondArg = TemplateArgument(Arg); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 532 | return Sema::TDK_NonDeducedMismatch; |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 533 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 534 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 535 | // Perform template argument deduction for the template name. |
| 536 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 537 | = DeduceTemplateArguments(S, |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 538 | TemplateParams, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 539 | Param->getTemplateName(), |
| 540 | TemplateName(SpecArg->getSpecializedTemplate()), |
| 541 | Info, Deduced)) |
| 542 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 543 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 544 | // Perform template argument deduction for the template arguments. |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 545 | return DeduceTemplateArguments( |
| 546 | S, TemplateParams, Param->getArgs(), Param->getNumArgs(), |
| 547 | SpecArg->getTemplateArgs().data(), SpecArg->getTemplateArgs().size(), |
| 548 | Info, Deduced, /*NumberOfArgumentsMustMatch=*/true); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 549 | } |
| 550 | |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 551 | /// \brief Determines whether the given type is an opaque type that |
| 552 | /// might be more qualified when instantiated. |
| 553 | static bool IsPossiblyOpaquelyQualifiedType(QualType T) { |
| 554 | switch (T->getTypeClass()) { |
| 555 | case Type::TypeOfExpr: |
| 556 | case Type::TypeOf: |
| 557 | case Type::DependentName: |
| 558 | case Type::Decltype: |
| 559 | case Type::UnresolvedUsing: |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 560 | case Type::TemplateTypeParm: |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 561 | return true; |
| 562 | |
| 563 | case Type::ConstantArray: |
| 564 | case Type::IncompleteArray: |
| 565 | case Type::VariableArray: |
| 566 | case Type::DependentSizedArray: |
| 567 | return IsPossiblyOpaquelyQualifiedType( |
| 568 | cast<ArrayType>(T)->getElementType()); |
| 569 | |
| 570 | default: |
| 571 | return false; |
| 572 | } |
| 573 | } |
| 574 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 575 | /// \brief Retrieve the depth and index of a template parameter. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 576 | static std::pair<unsigned, unsigned> |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 577 | getDepthAndIndex(NamedDecl *ND) { |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 578 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND)) |
| 579 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 580 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 581 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND)) |
| 582 | return std::make_pair(NTTP->getDepth(), NTTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 583 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 584 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND); |
| 585 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| 586 | } |
| 587 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 588 | /// \brief Retrieve the depth and index of an unexpanded parameter pack. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 589 | static std::pair<unsigned, unsigned> |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 590 | getDepthAndIndex(UnexpandedParameterPack UPP) { |
| 591 | if (const TemplateTypeParmType *TTP |
| 592 | = UPP.first.dyn_cast<const TemplateTypeParmType *>()) |
| 593 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 594 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 595 | return getDepthAndIndex(UPP.first.get<NamedDecl *>()); |
| 596 | } |
| 597 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 598 | /// \brief Helper function to build a TemplateParameter when we don't |
| 599 | /// know its type statically. |
| 600 | static TemplateParameter makeTemplateParameter(Decl *D) { |
| 601 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) |
| 602 | return TemplateParameter(TTP); |
Craig Topper | 4b482ee | 2013-07-08 04:24:47 +0000 | [diff] [blame] | 603 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 604 | return TemplateParameter(NTTP); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 605 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 606 | return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); |
| 607 | } |
| 608 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 609 | /// A pack that we're currently deducing. |
| 610 | struct clang::DeducedPack { |
| 611 | DeducedPack(unsigned Index) : Index(Index), Outer(nullptr) {} |
Craig Topper | 0a4e1f5 | 2013-07-08 04:44:01 +0000 | [diff] [blame] | 612 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 613 | // The index of the pack. |
| 614 | unsigned Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 615 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 616 | // The old value of the pack before we started deducing it. |
| 617 | DeducedTemplateArgument Saved; |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 618 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 619 | // A deferred value of this pack from an inner deduction, that couldn't be |
| 620 | // deduced because this deduction hadn't happened yet. |
| 621 | DeducedTemplateArgument DeferredDeduction; |
| 622 | |
| 623 | // The new value of the pack. |
| 624 | SmallVector<DeducedTemplateArgument, 4> New; |
| 625 | |
| 626 | // The outer deduction for this pack, if any. |
| 627 | DeducedPack *Outer; |
| 628 | }; |
| 629 | |
Benjamin Kramer | d5748c7 | 2015-03-23 12:31:05 +0000 | [diff] [blame] | 630 | namespace { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 631 | /// A scope in which we're performing pack deduction. |
| 632 | class PackDeductionScope { |
| 633 | public: |
| 634 | PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, |
| 635 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 636 | TemplateDeductionInfo &Info, TemplateArgument Pattern) |
| 637 | : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { |
| 638 | // Compute the set of template parameter indices that correspond to |
| 639 | // parameter packs expanded by the pack expansion. |
| 640 | { |
| 641 | llvm::SmallBitVector SawIndices(TemplateParams->size()); |
| 642 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 643 | S.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
| 644 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 645 | unsigned Depth, Index; |
| 646 | std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); |
| 647 | if (Depth == 0 && !SawIndices[Index]) { |
| 648 | SawIndices[Index] = true; |
| 649 | |
| 650 | // Save the deduced template argument for the parameter pack expanded |
| 651 | // by this pack expansion, then clear out the deduction. |
| 652 | DeducedPack Pack(Index); |
| 653 | Pack.Saved = Deduced[Index]; |
| 654 | Deduced[Index] = TemplateArgument(); |
| 655 | |
| 656 | Packs.push_back(Pack); |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | assert(!Packs.empty() && "Pack expansion without unexpanded packs?"); |
| 661 | |
| 662 | for (auto &Pack : Packs) { |
| 663 | if (Info.PendingDeducedPacks.size() > Pack.Index) |
| 664 | Pack.Outer = Info.PendingDeducedPacks[Pack.Index]; |
| 665 | else |
| 666 | Info.PendingDeducedPacks.resize(Pack.Index + 1); |
| 667 | Info.PendingDeducedPacks[Pack.Index] = &Pack; |
| 668 | |
| 669 | if (S.CurrentInstantiationScope) { |
| 670 | // If the template argument pack was explicitly specified, add that to |
| 671 | // the set of deduced arguments. |
| 672 | const TemplateArgument *ExplicitArgs; |
| 673 | unsigned NumExplicitArgs; |
| 674 | NamedDecl *PartiallySubstitutedPack = |
| 675 | S.CurrentInstantiationScope->getPartiallySubstitutedPack( |
| 676 | &ExplicitArgs, &NumExplicitArgs); |
| 677 | if (PartiallySubstitutedPack && |
| 678 | getDepthAndIndex(PartiallySubstitutedPack).second == Pack.Index) |
| 679 | Pack.New.append(ExplicitArgs, ExplicitArgs + NumExplicitArgs); |
| 680 | } |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 681 | } |
| 682 | } |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 683 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 684 | ~PackDeductionScope() { |
| 685 | for (auto &Pack : Packs) |
| 686 | Info.PendingDeducedPacks[Pack.Index] = Pack.Outer; |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 687 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 688 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 689 | /// Move to deducing the next element in each pack that is being deduced. |
| 690 | void nextPackElement() { |
| 691 | // Capture the deduced template arguments for each parameter pack expanded |
| 692 | // by this pack expansion, add them to the list of arguments we've deduced |
| 693 | // for that pack, then clear out the deduced argument. |
| 694 | for (auto &Pack : Packs) { |
| 695 | DeducedTemplateArgument &DeducedArg = Deduced[Pack.Index]; |
| 696 | if (!DeducedArg.isNull()) { |
| 697 | Pack.New.push_back(DeducedArg); |
| 698 | DeducedArg = DeducedTemplateArgument(); |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | /// \brief Finish template argument deduction for a set of argument packs, |
| 704 | /// producing the argument packs and checking for consistency with prior |
| 705 | /// deductions. |
| 706 | Sema::TemplateDeductionResult finish(bool HasAnyArguments) { |
| 707 | // Build argument packs for each of the parameter packs expanded by this |
| 708 | // pack expansion. |
| 709 | for (auto &Pack : Packs) { |
| 710 | // Put back the old value for this pack. |
| 711 | Deduced[Pack.Index] = Pack.Saved; |
| 712 | |
| 713 | // Build or find a new value for this pack. |
| 714 | DeducedTemplateArgument NewPack; |
| 715 | if (HasAnyArguments && Pack.New.empty()) { |
| 716 | if (Pack.DeferredDeduction.isNull()) { |
| 717 | // We were not able to deduce anything for this parameter pack |
| 718 | // (because it only appeared in non-deduced contexts), so just |
| 719 | // restore the saved argument pack. |
| 720 | continue; |
| 721 | } |
| 722 | |
| 723 | NewPack = Pack.DeferredDeduction; |
| 724 | Pack.DeferredDeduction = TemplateArgument(); |
| 725 | } else if (Pack.New.empty()) { |
| 726 | // If we deduced an empty argument pack, create it now. |
| 727 | NewPack = DeducedTemplateArgument(TemplateArgument::getEmptyPack()); |
| 728 | } else { |
| 729 | TemplateArgument *ArgumentPack = |
| 730 | new (S.Context) TemplateArgument[Pack.New.size()]; |
| 731 | std::copy(Pack.New.begin(), Pack.New.end(), ArgumentPack); |
| 732 | NewPack = DeducedTemplateArgument( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 733 | TemplateArgument(llvm::makeArrayRef(ArgumentPack, Pack.New.size())), |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 734 | Pack.New[0].wasDeducedFromArrayBound()); |
| 735 | } |
| 736 | |
| 737 | // Pick where we're going to put the merged pack. |
| 738 | DeducedTemplateArgument *Loc; |
| 739 | if (Pack.Outer) { |
| 740 | if (Pack.Outer->DeferredDeduction.isNull()) { |
| 741 | // Defer checking this pack until we have a complete pack to compare |
| 742 | // it against. |
| 743 | Pack.Outer->DeferredDeduction = NewPack; |
| 744 | continue; |
| 745 | } |
| 746 | Loc = &Pack.Outer->DeferredDeduction; |
| 747 | } else { |
| 748 | Loc = &Deduced[Pack.Index]; |
| 749 | } |
| 750 | |
| 751 | // Check the new pack matches any previous value. |
| 752 | DeducedTemplateArgument OldPack = *Loc; |
| 753 | DeducedTemplateArgument Result = |
| 754 | checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 755 | |
| 756 | // If we deferred a deduction of this pack, check that one now too. |
| 757 | if (!Result.isNull() && !Pack.DeferredDeduction.isNull()) { |
| 758 | OldPack = Result; |
| 759 | NewPack = Pack.DeferredDeduction; |
| 760 | Result = checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 761 | } |
| 762 | |
| 763 | if (Result.isNull()) { |
| 764 | Info.Param = |
| 765 | makeTemplateParameter(TemplateParams->getParam(Pack.Index)); |
| 766 | Info.FirstArg = OldPack; |
| 767 | Info.SecondArg = NewPack; |
| 768 | return Sema::TDK_Inconsistent; |
| 769 | } |
| 770 | |
| 771 | *Loc = Result; |
| 772 | } |
| 773 | |
| 774 | return Sema::TDK_Success; |
| 775 | } |
| 776 | |
| 777 | private: |
| 778 | Sema &S; |
| 779 | TemplateParameterList *TemplateParams; |
| 780 | SmallVectorImpl<DeducedTemplateArgument> &Deduced; |
| 781 | TemplateDeductionInfo &Info; |
| 782 | |
| 783 | SmallVector<DeducedPack, 2> Packs; |
| 784 | }; |
Benjamin Kramer | d5748c7 | 2015-03-23 12:31:05 +0000 | [diff] [blame] | 785 | } // namespace |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 786 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 787 | /// \brief Deduce the template arguments by comparing the list of parameter |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 788 | /// types to the list of argument types, as in the parameter-type-lists of |
| 789 | /// function types (C++ [temp.deduct.type]p10). |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 790 | /// |
| 791 | /// \param S The semantic analysis object within which we are deducing |
| 792 | /// |
| 793 | /// \param TemplateParams The template parameters that we are deducing |
| 794 | /// |
| 795 | /// \param Params The list of parameter types |
| 796 | /// |
| 797 | /// \param NumParams The number of types in \c Params |
| 798 | /// |
| 799 | /// \param Args The list of argument types |
| 800 | /// |
| 801 | /// \param NumArgs The number of types in \c Args |
| 802 | /// |
| 803 | /// \param Info information about the template argument deduction itself |
| 804 | /// |
| 805 | /// \param Deduced the deduced template arguments |
| 806 | /// |
| 807 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
| 808 | /// how template argument deduction is performed. |
| 809 | /// |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 810 | /// \param PartialOrdering If true, we are performing template argument |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 811 | /// deduction for during partial ordering for a call |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 812 | /// (C++0x [temp.deduct.partial]). |
| 813 | /// |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 814 | /// \returns the result of template argument deduction so far. Note that a |
| 815 | /// "success" result means that template argument deduction has not yet failed, |
| 816 | /// but it may still fail, later, for other reasons. |
| 817 | static Sema::TemplateDeductionResult |
| 818 | DeduceTemplateArguments(Sema &S, |
| 819 | TemplateParameterList *TemplateParams, |
| 820 | const QualType *Params, unsigned NumParams, |
| 821 | const QualType *Args, unsigned NumArgs, |
| 822 | TemplateDeductionInfo &Info, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 823 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 824 | unsigned TDF, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 825 | bool PartialOrdering = false) { |
Douglas Gregor | 86bea35 | 2011-01-05 23:23:17 +0000 | [diff] [blame] | 826 | // Fast-path check to see if we have too many/too few arguments. |
| 827 | if (NumParams != NumArgs && |
| 828 | !(NumParams && isa<PackExpansionType>(Params[NumParams - 1])) && |
| 829 | !(NumArgs && isa<PackExpansionType>(Args[NumArgs - 1]))) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 830 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 831 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 832 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 833 | // Similarly, if P has a form that contains (T), then each parameter type |
| 834 | // Pi of the respective parameter-type- list of P is compared with the |
| 835 | // corresponding parameter type Ai of the corresponding parameter-type-list |
| 836 | // of A. [...] |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 837 | unsigned ArgIdx = 0, ParamIdx = 0; |
| 838 | for (; ParamIdx != NumParams; ++ParamIdx) { |
| 839 | // Check argument types. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 840 | const PackExpansionType *Expansion |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 841 | = dyn_cast<PackExpansionType>(Params[ParamIdx]); |
| 842 | if (!Expansion) { |
| 843 | // Simple case: compare the parameter and argument types at this point. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 844 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 845 | // Make sure we have an argument. |
| 846 | if (ArgIdx >= NumArgs) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 847 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 848 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 849 | if (isa<PackExpansionType>(Args[ArgIdx])) { |
| 850 | // C++0x [temp.deduct.type]p22: |
| 851 | // If the original function parameter associated with A is a function |
| 852 | // parameter pack and the function parameter associated with P is not |
| 853 | // a function parameter pack, then template argument deduction fails. |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 854 | return Sema::TDK_MiscellaneousDeductionFailure; |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 855 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 856 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 857 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 858 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 859 | Params[ParamIdx], Args[ArgIdx], |
| 860 | Info, Deduced, TDF, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 861 | PartialOrdering)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 862 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 863 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 864 | ++ArgIdx; |
| 865 | continue; |
| 866 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 867 | |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 868 | // C++0x [temp.deduct.type]p5: |
| 869 | // The non-deduced contexts are: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 870 | // - 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] | 871 | // parameter-declaration-clause. |
| 872 | if (ParamIdx + 1 < NumParams) |
| 873 | return Sema::TDK_Success; |
| 874 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 875 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 876 | // If the parameter-declaration corresponding to Pi is a function |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 877 | // parameter pack, then the type of its declarator- id is compared with |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 878 | // each remaining parameter type in the parameter-type-list of A. Each |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 879 | // comparison deduces template arguments for subsequent positions in the |
| 880 | // template parameter packs expanded by the function parameter pack. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 881 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 882 | QualType Pattern = Expansion->getPattern(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 883 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 884 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 885 | bool HasAnyArguments = false; |
| 886 | for (; ArgIdx < NumArgs; ++ArgIdx) { |
| 887 | HasAnyArguments = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 888 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 889 | // Deduce template arguments from the pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 890 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 891 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, Pattern, |
| 892 | Args[ArgIdx], Info, Deduced, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 893 | TDF, PartialOrdering)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 894 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 895 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 896 | PackScope.nextPackElement(); |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 897 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 898 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 899 | // Build argument packs for each of the parameter packs expanded by this |
| 900 | // pack expansion. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 901 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 902 | return Result; |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 903 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 904 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 905 | // Make sure we don't have any extra arguments. |
| 906 | if (ArgIdx < NumArgs) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 907 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 908 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 909 | return Sema::TDK_Success; |
| 910 | } |
| 911 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 912 | /// \brief Determine whether the parameter has qualifiers that are either |
| 913 | /// inconsistent with or a superset of the argument's qualifiers. |
| 914 | static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType, |
| 915 | QualType ArgType) { |
| 916 | Qualifiers ParamQs = ParamType.getQualifiers(); |
| 917 | Qualifiers ArgQs = ArgType.getQualifiers(); |
| 918 | |
| 919 | if (ParamQs == ArgQs) |
| 920 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 921 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 922 | // Mismatched (but not missing) Objective-C GC attributes. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 923 | if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() && |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 924 | ParamQs.hasObjCGCAttr()) |
| 925 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 926 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 927 | // Mismatched (but not missing) address spaces. |
| 928 | if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() && |
| 929 | ParamQs.hasAddressSpace()) |
| 930 | return true; |
| 931 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 932 | // Mismatched (but not missing) Objective-C lifetime qualifiers. |
| 933 | if (ParamQs.getObjCLifetime() != ArgQs.getObjCLifetime() && |
| 934 | ParamQs.hasObjCLifetime()) |
| 935 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 936 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 937 | // CVR qualifier superset. |
| 938 | return (ParamQs.getCVRQualifiers() != ArgQs.getCVRQualifiers()) && |
| 939 | ((ParamQs.getCVRQualifiers() | ArgQs.getCVRQualifiers()) |
| 940 | == ParamQs.getCVRQualifiers()); |
| 941 | } |
| 942 | |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 943 | /// \brief Compare types for equality with respect to possibly compatible |
| 944 | /// function types (noreturn adjustment, implicit calling conventions). If any |
| 945 | /// of parameter and argument is not a function, just perform type comparison. |
| 946 | /// |
| 947 | /// \param Param the template parameter type. |
| 948 | /// |
| 949 | /// \param Arg the argument type. |
| 950 | bool Sema::isSameOrCompatibleFunctionType(CanQualType Param, |
| 951 | CanQualType Arg) { |
| 952 | const FunctionType *ParamFunction = Param->getAs<FunctionType>(), |
| 953 | *ArgFunction = Arg->getAs<FunctionType>(); |
| 954 | |
| 955 | // Just compare if not functions. |
| 956 | if (!ParamFunction || !ArgFunction) |
| 957 | return Param == Arg; |
| 958 | |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 959 | // Noreturn and noexcept adjustment. |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 960 | QualType AdjustedParam; |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 961 | if (IsFunctionConversion(Param, Arg, AdjustedParam)) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 962 | return Arg == Context.getCanonicalType(AdjustedParam); |
| 963 | |
| 964 | // FIXME: Compatible calling conventions. |
| 965 | |
| 966 | return Param == Arg; |
| 967 | } |
| 968 | |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 969 | /// \brief Deduce the template arguments by comparing the parameter type and |
| 970 | /// the argument type (C++ [temp.deduct.type]). |
| 971 | /// |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 972 | /// \param S the semantic analysis object within which we are deducing |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 973 | /// |
| 974 | /// \param TemplateParams the template parameters that we are deducing |
| 975 | /// |
| 976 | /// \param ParamIn the parameter type |
| 977 | /// |
| 978 | /// \param ArgIn the argument type |
| 979 | /// |
| 980 | /// \param Info information about the template argument deduction itself |
| 981 | /// |
| 982 | /// \param Deduced the deduced template arguments |
| 983 | /// |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 984 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 985 | /// how template argument deduction is performed. |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 986 | /// |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 987 | /// \param PartialOrdering Whether we're performing template argument deduction |
| 988 | /// in the context of partial ordering (C++0x [temp.deduct.partial]). |
| 989 | /// |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 990 | /// \returns the result of template argument deduction so far. Note that a |
| 991 | /// "success" result means that template argument deduction has not yet failed, |
| 992 | /// but it may still fail, later, for other reasons. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 993 | static Sema::TemplateDeductionResult |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 994 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 995 | TemplateParameterList *TemplateParams, |
| 996 | QualType ParamIn, QualType ArgIn, |
| 997 | TemplateDeductionInfo &Info, |
| 998 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 999 | unsigned TDF, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1000 | bool PartialOrdering, |
| 1001 | bool DeducedFromArrayBound) { |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1002 | // We only want to look at the canonical types, since typedefs and |
| 1003 | // sugar are not part of template argument deduction. |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1004 | QualType Param = S.Context.getCanonicalType(ParamIn); |
| 1005 | QualType Arg = S.Context.getCanonicalType(ArgIn); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1006 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1007 | // If the argument type is a pack expansion, look at its pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1008 | // This isn't explicitly called out |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1009 | if (const PackExpansionType *ArgExpansion |
| 1010 | = dyn_cast<PackExpansionType>(Arg)) |
| 1011 | Arg = ArgExpansion->getPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1012 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1013 | if (PartialOrdering) { |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1014 | // C++11 [temp.deduct.partial]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1015 | // Before the partial ordering is done, certain transformations are |
| 1016 | // performed on the types used for partial ordering: |
| 1017 | // - 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] | 1018 | const ReferenceType *ParamRef = Param->getAs<ReferenceType>(); |
| 1019 | if (ParamRef) |
| 1020 | Param = ParamRef->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1021 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1022 | // - If A is a reference type, A is replaced by the type referred to. |
| 1023 | const ReferenceType *ArgRef = Arg->getAs<ReferenceType>(); |
| 1024 | if (ArgRef) |
| 1025 | Arg = ArgRef->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1026 | |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1027 | if (ParamRef && ArgRef && S.Context.hasSameUnqualifiedType(Param, Arg)) { |
| 1028 | // C++11 [temp.deduct.partial]p9: |
| 1029 | // If, for a given type, deduction succeeds in both directions (i.e., |
| 1030 | // the types are identical after the transformations above) and both |
| 1031 | // P and A were reference types [...]: |
| 1032 | // - if [one type] was an lvalue reference and [the other type] was |
| 1033 | // not, [the other type] is not considered to be at least as |
| 1034 | // specialized as [the first type] |
| 1035 | // - if [one type] is more cv-qualified than [the other type], |
| 1036 | // [the other type] is not considered to be at least as specialized |
| 1037 | // as [the first type] |
| 1038 | // Objective-C ARC adds: |
| 1039 | // - [one type] has non-trivial lifetime, [the other type] has |
| 1040 | // __unsafe_unretained lifetime, and the types are otherwise |
| 1041 | // identical |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1042 | // |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1043 | // A is "considered to be at least as specialized" as P iff deduction |
| 1044 | // succeeds, so we model this as a deduction failure. Note that |
| 1045 | // [the first type] is P and [the other type] is A here; the standard |
| 1046 | // gets this backwards. |
Douglas Gregor | 85894a8 | 2011-04-30 17:07:52 +0000 | [diff] [blame] | 1047 | Qualifiers ParamQuals = Param.getQualifiers(); |
| 1048 | Qualifiers ArgQuals = Arg.getQualifiers(); |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1049 | if ((ParamRef->isLValueReferenceType() && |
| 1050 | !ArgRef->isLValueReferenceType()) || |
| 1051 | ParamQuals.isStrictSupersetOf(ArgQuals) || |
| 1052 | (ParamQuals.hasNonTrivialObjCLifetime() && |
| 1053 | ArgQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone && |
| 1054 | ParamQuals.withoutObjCLifetime() == |
| 1055 | ArgQuals.withoutObjCLifetime())) { |
| 1056 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1057 | Info.SecondArg = TemplateArgument(ArgIn); |
| 1058 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 6beabee | 2014-01-02 19:42:02 +0000 | [diff] [blame] | 1059 | } |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1060 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1061 | |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1062 | // C++11 [temp.deduct.partial]p7: |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1063 | // Remove any top-level cv-qualifiers: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1064 | // - 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] | 1065 | // version of P. |
| 1066 | Param = Param.getUnqualifiedType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1067 | // - 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] | 1068 | // version of A. |
| 1069 | Arg = Arg.getUnqualifiedType(); |
| 1070 | } else { |
| 1071 | // C++0x [temp.deduct.call]p4 bullet 1: |
| 1072 | // - If the original P is a reference type, the deduced A (i.e., the type |
| 1073 | // referred to by the reference) can be more cv-qualified than the |
| 1074 | // transformed A. |
| 1075 | if (TDF & TDF_ParamWithReferenceType) { |
| 1076 | Qualifiers Quals; |
| 1077 | QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals); |
| 1078 | Quals.setCVRQualifiers(Quals.getCVRQualifiers() & |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 1079 | Arg.getCVRQualifiers()); |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1080 | Param = S.Context.getQualifiedType(UnqualParam, Quals); |
| 1081 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1082 | |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1083 | if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) { |
| 1084 | // C++0x [temp.deduct.type]p10: |
| 1085 | // If P and A are function types that originated from deduction when |
| 1086 | // taking the address of a function template (14.8.2.2) or when deducing |
| 1087 | // 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] | 1088 | // Ai are parameters of the top-level parameter-type-list of P and A, |
| 1089 | // respectively, Pi is adjusted if it is an rvalue reference to a |
| 1090 | // cv-unqualified template parameter and Ai is an lvalue reference, in |
| 1091 | // 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] | 1092 | // type (i.e., T&& is changed to simply T). [ Note: As a result, when |
| 1093 | // 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] | 1094 | // deduced as X&. - end note ] |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1095 | TDF &= ~TDF_TopLevelParameterTypeList; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1096 | |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1097 | if (const RValueReferenceType *ParamRef |
| 1098 | = Param->getAs<RValueReferenceType>()) { |
| 1099 | if (isa<TemplateTypeParmType>(ParamRef->getPointeeType()) && |
| 1100 | !ParamRef->getPointeeType().getQualifiers()) |
| 1101 | if (Arg->isLValueReferenceType()) |
| 1102 | Param = ParamRef->getPointeeType(); |
| 1103 | } |
| 1104 | } |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1105 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1106 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1107 | // C++ [temp.deduct.type]p9: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1108 | // A template type argument T, a template template argument TT or a |
| 1109 | // 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] | 1110 | // the following forms: |
| 1111 | // |
| 1112 | // T |
| 1113 | // cv-list T |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1114 | if (const TemplateTypeParmType *TemplateTypeParm |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1115 | = Param->getAs<TemplateTypeParmType>()) { |
Douglas Gregor | 4ea5dec | 2011-09-22 15:57:07 +0000 | [diff] [blame] | 1116 | // Just skip any attempts to deduce from a placeholder type. |
| 1117 | if (Arg->isPlaceholderType()) |
| 1118 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1119 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1120 | unsigned Index = TemplateTypeParm->getIndex(); |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1121 | bool RecanonicalizeArg = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1122 | |
Douglas Gregor | 6045482 | 2009-07-22 20:02:25 +0000 | [diff] [blame] | 1123 | // If the argument type is an array type, move the qualifiers up to the |
| 1124 | // 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] | 1125 | if (isa<ArrayType>(Arg)) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1126 | Qualifiers Quals; |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1127 | Arg = S.Context.getUnqualifiedArrayType(Arg, Quals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1128 | if (Quals) { |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1129 | Arg = S.Context.getQualifiedType(Arg, Quals); |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1130 | RecanonicalizeArg = true; |
| 1131 | } |
| 1132 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1133 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1134 | // The argument type can not be less qualified than the parameter |
| 1135 | // type. |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1136 | if (!(TDF & TDF_IgnoreQualifiers) && |
| 1137 | hasInconsistentOrSupersetQualifiersOf(Param, Arg)) { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1138 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1139 | Info.FirstArg = TemplateArgument(Param); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1140 | Info.SecondArg = TemplateArgument(Arg); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1141 | return Sema::TDK_Underqualified; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1142 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1143 | |
| 1144 | assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0"); |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1145 | assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function"); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1146 | QualType DeducedType = Arg; |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1147 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1148 | // Remove any qualifiers on the parameter from the deduced type. |
| 1149 | // We checked the qualifiers for consistency above. |
| 1150 | Qualifiers DeducedQs = DeducedType.getQualifiers(); |
| 1151 | Qualifiers ParamQs = Param.getQualifiers(); |
| 1152 | DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers()); |
| 1153 | if (ParamQs.hasObjCGCAttr()) |
| 1154 | DeducedQs.removeObjCGCAttr(); |
| 1155 | if (ParamQs.hasAddressSpace()) |
| 1156 | DeducedQs.removeAddressSpace(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1157 | if (ParamQs.hasObjCLifetime()) |
| 1158 | DeducedQs.removeObjCLifetime(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1159 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1160 | // Objective-C ARC: |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1161 | // If template deduction would produce a lifetime qualifier on a type |
| 1162 | // that is not a lifetime type, template argument deduction fails. |
| 1163 | if (ParamQs.hasObjCLifetime() && !DeducedType->isObjCLifetimeType() && |
| 1164 | !DeducedType->isDependentType()) { |
| 1165 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1166 | Info.FirstArg = TemplateArgument(Param); |
| 1167 | Info.SecondArg = TemplateArgument(Arg); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1168 | return Sema::TDK_Underqualified; |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1169 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1170 | |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1171 | // Objective-C ARC: |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1172 | // If template deduction would produce an argument type with lifetime type |
| 1173 | // but no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1174 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1175 | DeducedType->isObjCLifetimeType() && |
| 1176 | !DeducedQs.hasObjCLifetime()) |
| 1177 | DeducedQs.setObjCLifetime(Qualifiers::OCL_Strong); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1178 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1179 | DeducedType = S.Context.getQualifiedType(DeducedType.getUnqualifiedType(), |
| 1180 | DeducedQs); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1181 | |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1182 | if (RecanonicalizeArg) |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1183 | DeducedType = S.Context.getCanonicalType(DeducedType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1184 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1185 | DeducedTemplateArgument NewDeduced(DeducedType, DeducedFromArrayBound); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1186 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1187 | Deduced[Index], |
| 1188 | NewDeduced); |
| 1189 | if (Result.isNull()) { |
| 1190 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1191 | Info.FirstArg = Deduced[Index]; |
| 1192 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1193 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1194 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1195 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1196 | Deduced[Index] = Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1197 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1200 | // Set up the template argument deduction information for a failure. |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1201 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1202 | Info.SecondArg = TemplateArgument(ArgIn); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1203 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 1204 | // If the parameter is an already-substituted template parameter |
| 1205 | // pack, do nothing: we don't know which of its arguments to look |
| 1206 | // at, so we have to wait until all of the parameter packs in this |
| 1207 | // expansion have arguments. |
| 1208 | if (isa<SubstTemplateTypeParmPackType>(Param)) |
| 1209 | return Sema::TDK_Success; |
| 1210 | |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1211 | // Check the cv-qualifiers on the parameter and argument types. |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1212 | CanQualType CanParam = S.Context.getCanonicalType(Param); |
| 1213 | CanQualType CanArg = S.Context.getCanonicalType(Arg); |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1214 | if (!(TDF & TDF_IgnoreQualifiers)) { |
| 1215 | if (TDF & TDF_ParamWithReferenceType) { |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1216 | if (hasInconsistentOrSupersetQualifiersOf(Param, Arg)) |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1217 | return Sema::TDK_NonDeducedMismatch; |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 1218 | } else if (!IsPossiblyOpaquelyQualifiedType(Param)) { |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1219 | if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1220 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1221 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1222 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1223 | // If the parameter type is not dependent, there is nothing to deduce. |
| 1224 | if (!Param->isDependentType()) { |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1225 | if (!(TDF & TDF_SkipNonDependent)) { |
| 1226 | bool NonDeduced = (TDF & TDF_InOverloadResolution)? |
| 1227 | !S.isSameOrCompatibleFunctionType(CanParam, CanArg) : |
| 1228 | Param != Arg; |
| 1229 | if (NonDeduced) { |
| 1230 | return Sema::TDK_NonDeducedMismatch; |
| 1231 | } |
| 1232 | } |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1233 | return Sema::TDK_Success; |
| 1234 | } |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1235 | } else if (!Param->isDependentType()) { |
| 1236 | CanQualType ParamUnqualType = CanParam.getUnqualifiedType(), |
| 1237 | ArgUnqualType = CanArg.getUnqualifiedType(); |
| 1238 | bool Success = (TDF & TDF_InOverloadResolution)? |
| 1239 | S.isSameOrCompatibleFunctionType(ParamUnqualType, |
| 1240 | ArgUnqualType) : |
| 1241 | ParamUnqualType == ArgUnqualType; |
| 1242 | if (Success) |
| 1243 | return Sema::TDK_Success; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1244 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1245 | |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1246 | switch (Param->getTypeClass()) { |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1247 | // Non-canonical types cannot appear here. |
| 1248 | #define NON_CANONICAL_TYPE(Class, Base) \ |
| 1249 | case Type::Class: llvm_unreachable("deducing non-canonical type: " #Class); |
| 1250 | #define TYPE(Class, Base) |
| 1251 | #include "clang/AST/TypeNodes.def" |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1252 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1253 | case Type::TemplateTypeParm: |
| 1254 | case Type::SubstTemplateTypeParmPack: |
| 1255 | llvm_unreachable("Type nodes handled above"); |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1256 | |
| 1257 | // These types cannot be dependent, so simply check whether the types are |
| 1258 | // the same. |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1259 | case Type::Builtin: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1260 | case Type::VariableArray: |
| 1261 | case Type::Vector: |
| 1262 | case Type::FunctionNoProto: |
| 1263 | case Type::Record: |
| 1264 | case Type::Enum: |
| 1265 | case Type::ObjCObject: |
| 1266 | case Type::ObjCInterface: |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1267 | case Type::ObjCObjectPointer: { |
| 1268 | if (TDF & TDF_SkipNonDependent) |
| 1269 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1270 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1271 | if (TDF & TDF_IgnoreQualifiers) { |
| 1272 | Param = Param.getUnqualifiedType(); |
| 1273 | Arg = Arg.getUnqualifiedType(); |
| 1274 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1275 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1276 | return Param == Arg? Sema::TDK_Success : Sema::TDK_NonDeducedMismatch; |
| 1277 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1278 | |
| 1279 | // _Complex T [placeholder extension] |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1280 | case Type::Complex: |
| 1281 | if (const ComplexType *ComplexArg = Arg->getAs<ComplexType>()) |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1282 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1283 | cast<ComplexType>(Param)->getElementType(), |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1284 | ComplexArg->getElementType(), |
| 1285 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1286 | |
| 1287 | return Sema::TDK_NonDeducedMismatch; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1288 | |
| 1289 | // _Atomic T [extension] |
| 1290 | case Type::Atomic: |
| 1291 | if (const AtomicType *AtomicArg = Arg->getAs<AtomicType>()) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1292 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1293 | cast<AtomicType>(Param)->getValueType(), |
| 1294 | AtomicArg->getValueType(), |
| 1295 | Info, Deduced, TDF); |
| 1296 | |
| 1297 | return Sema::TDK_NonDeducedMismatch; |
| 1298 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1299 | // T * |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1300 | case Type::Pointer: { |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1301 | QualType PointeeType; |
| 1302 | if (const PointerType *PointerArg = Arg->getAs<PointerType>()) { |
| 1303 | PointeeType = PointerArg->getPointeeType(); |
| 1304 | } else if (const ObjCObjectPointerType *PointerArg |
| 1305 | = Arg->getAs<ObjCObjectPointerType>()) { |
| 1306 | PointeeType = PointerArg->getPointeeType(); |
| 1307 | } else { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1308 | return Sema::TDK_NonDeducedMismatch; |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1309 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1310 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1311 | unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1312 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1313 | cast<PointerType>(Param)->getPointeeType(), |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1314 | PointeeType, |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1315 | Info, Deduced, SubTDF); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1316 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1317 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1318 | // T & |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1319 | case Type::LValueReference: { |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 1320 | const LValueReferenceType *ReferenceArg = |
| 1321 | Arg->getAs<LValueReferenceType>(); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1322 | if (!ReferenceArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1323 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1324 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1325 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1326 | cast<LValueReferenceType>(Param)->getPointeeType(), |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1327 | ReferenceArg->getPointeeType(), Info, Deduced, 0); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1328 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1329 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1330 | // T && [C++0x] |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1331 | case Type::RValueReference: { |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 1332 | const RValueReferenceType *ReferenceArg = |
| 1333 | Arg->getAs<RValueReferenceType>(); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1334 | if (!ReferenceArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1335 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1336 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1337 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1338 | cast<RValueReferenceType>(Param)->getPointeeType(), |
| 1339 | ReferenceArg->getPointeeType(), |
| 1340 | Info, Deduced, 0); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1341 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1342 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1343 | // T [] (implied, but not stated explicitly) |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1344 | case Type::IncompleteArray: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1345 | const IncompleteArrayType *IncompleteArrayArg = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1346 | S.Context.getAsIncompleteArrayType(Arg); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1347 | if (!IncompleteArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1348 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1349 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1350 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1351 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1352 | S.Context.getAsIncompleteArrayType(Param)->getElementType(), |
| 1353 | IncompleteArrayArg->getElementType(), |
| 1354 | Info, Deduced, SubTDF); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1355 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1356 | |
| 1357 | // T [integer-constant] |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1358 | case Type::ConstantArray: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1359 | const ConstantArrayType *ConstantArrayArg = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1360 | S.Context.getAsConstantArrayType(Arg); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1361 | if (!ConstantArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1362 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1363 | |
| 1364 | const ConstantArrayType *ConstantArrayParm = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1365 | S.Context.getAsConstantArrayType(Param); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1366 | if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1367 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1368 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1369 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1370 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1371 | ConstantArrayParm->getElementType(), |
| 1372 | ConstantArrayArg->getElementType(), |
| 1373 | Info, Deduced, SubTDF); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1376 | // type [i] |
| 1377 | case Type::DependentSizedArray: { |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1378 | const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1379 | if (!ArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1380 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1381 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1382 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
| 1383 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1384 | // Check the element type of the arrays |
| 1385 | const DependentSizedArrayType *DependentArrayParm |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1386 | = S.Context.getAsDependentSizedArrayType(Param); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1387 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1388 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1389 | DependentArrayParm->getElementType(), |
| 1390 | ArrayArg->getElementType(), |
| 1391 | Info, Deduced, SubTDF)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1392 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1393 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1394 | // Determine the array bound is something we can deduce. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1395 | NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1396 | = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr()); |
| 1397 | if (!NTTP) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1398 | return Sema::TDK_Success; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1399 | |
| 1400 | // We can perform template argument deduction for the given non-type |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1401 | // template parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1402 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1403 | "Cannot deduce non-type template argument at depth > 0"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | if (const ConstantArrayType *ConstantArrayArg |
Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1405 | = dyn_cast<ConstantArrayType>(ArrayArg)) { |
| 1406 | llvm::APSInt Size(ConstantArrayArg->getSize()); |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1407 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, Size, |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1408 | S.Context.getSizeType(), |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1409 | /*ArrayBound=*/true, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1410 | Info, Deduced); |
Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1411 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1412 | if (const DependentSizedArrayType *DependentArrayArg |
| 1413 | = dyn_cast<DependentSizedArrayType>(ArrayArg)) |
Douglas Gregor | 7a49ead | 2010-12-22 23:15:38 +0000 | [diff] [blame] | 1414 | if (DependentArrayArg->getSizeExpr()) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1415 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
Douglas Gregor | 7a49ead | 2010-12-22 23:15:38 +0000 | [diff] [blame] | 1416 | DependentArrayArg->getSizeExpr(), |
| 1417 | Info, Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1419 | // Incomplete type does not match a dependently-sized array type |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1420 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1421 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1422 | |
| 1423 | // type(*)(T) |
| 1424 | // T(*)() |
| 1425 | // T(*)(T) |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1426 | case Type::FunctionProto: { |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1427 | unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | const FunctionProtoType *FunctionProtoArg = |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1429 | dyn_cast<FunctionProtoType>(Arg); |
| 1430 | if (!FunctionProtoArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1431 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1432 | |
| 1433 | const FunctionProtoType *FunctionProtoParam = |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1434 | cast<FunctionProtoType>(Param); |
Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1435 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1436 | if (FunctionProtoParam->getTypeQuals() |
Douglas Gregor | 54e462a | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1437 | != FunctionProtoArg->getTypeQuals() || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1438 | FunctionProtoParam->getRefQualifier() |
Douglas Gregor | 54e462a | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1439 | != FunctionProtoArg->getRefQualifier() || |
| 1440 | FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1441 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1442 | |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1443 | // Check return types. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1444 | if (Sema::TemplateDeductionResult Result = |
| 1445 | DeduceTemplateArgumentsByTypeMatch( |
| 1446 | S, TemplateParams, FunctionProtoParam->getReturnType(), |
| 1447 | FunctionProtoArg->getReturnType(), Info, Deduced, 0)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1448 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1449 | |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1450 | return DeduceTemplateArguments( |
| 1451 | S, TemplateParams, FunctionProtoParam->param_type_begin(), |
| 1452 | FunctionProtoParam->getNumParams(), |
| 1453 | FunctionProtoArg->param_type_begin(), |
| 1454 | FunctionProtoArg->getNumParams(), Info, Deduced, SubTDF); |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1455 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1456 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1457 | case Type::InjectedClassName: { |
| 1458 | // Treat a template's injected-class-name as if the template |
| 1459 | // specialization type had been used. |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1460 | Param = cast<InjectedClassNameType>(Param) |
| 1461 | ->getInjectedSpecializationType(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1462 | assert(isa<TemplateSpecializationType>(Param) && |
| 1463 | "injected class name is not a template specialization type"); |
| 1464 | // fall through |
| 1465 | } |
| 1466 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1467 | // template-name<T> (where template-name refers to a class template) |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1468 | // template-name<i> |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1469 | // TT<T> |
| 1470 | // TT<i> |
| 1471 | // TT<> |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1472 | case Type::TemplateSpecialization: { |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1473 | const TemplateSpecializationType *SpecParam = |
| 1474 | cast<TemplateSpecializationType>(Param); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1475 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1476 | // When Arg cannot be a derived class, we can just try to deduce template |
| 1477 | // arguments from the template-id. |
| 1478 | const RecordType *RecordT = Arg->getAs<RecordType>(); |
| 1479 | if (!(TDF & TDF_DerivedClass) || !RecordT) |
| 1480 | return DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg, Info, |
| 1481 | Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1483 | SmallVector<DeducedTemplateArgument, 8> DeducedOrig(Deduced.begin(), |
| 1484 | Deduced.end()); |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1485 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1486 | Sema::TemplateDeductionResult Result = DeduceTemplateArguments( |
| 1487 | S, TemplateParams, SpecParam, Arg, Info, Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1488 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1489 | if (Result == Sema::TDK_Success) |
| 1490 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1491 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1492 | // We cannot inspect base classes as part of deduction when the type |
| 1493 | // is incomplete, so either instantiate any templates necessary to |
| 1494 | // complete the type, or skip over it if it cannot be completed. |
| 1495 | if (!S.isCompleteType(Info.getLocation(), Arg)) |
| 1496 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1497 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1498 | // C++14 [temp.deduct.call] p4b3: |
| 1499 | // If P is a class and P has the form simple-template-id, then the |
| 1500 | // transformed A can be a derived class of the deduced A. Likewise if |
| 1501 | // P is a pointer to a class of the form simple-template-id, the |
| 1502 | // transformed A can be a pointer to a derived class pointed to by the |
| 1503 | // deduced A. |
| 1504 | // |
| 1505 | // These alternatives are considered only if type deduction would |
| 1506 | // otherwise fail. If they yield more than one possible deduced A, the |
| 1507 | // type deduction fails. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1508 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1509 | // Reset the incorrectly deduced argument from above. |
| 1510 | Deduced = DeducedOrig; |
| 1511 | |
| 1512 | // Use data recursion to crawl through the list of base classes. |
| 1513 | // Visited contains the set of nodes we have already visited, while |
| 1514 | // ToVisit is our stack of records that we still need to visit. |
| 1515 | llvm::SmallPtrSet<const RecordType *, 8> Visited; |
| 1516 | SmallVector<const RecordType *, 8> ToVisit; |
| 1517 | ToVisit.push_back(RecordT); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1518 | bool Successful = false; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1519 | SmallVector<DeducedTemplateArgument, 8> SuccessfulDeduced; |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1520 | while (!ToVisit.empty()) { |
| 1521 | // Retrieve the next class in the inheritance hierarchy. |
| 1522 | const RecordType *NextT = ToVisit.pop_back_val(); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1523 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1524 | // If we have already seen this type, skip it. |
| 1525 | if (!Visited.insert(NextT).second) |
| 1526 | continue; |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1527 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1528 | // If this is a base class, try to perform template argument |
| 1529 | // deduction from it. |
| 1530 | if (NextT != RecordT) { |
| 1531 | TemplateDeductionInfo BaseInfo(Info.getLocation()); |
| 1532 | Sema::TemplateDeductionResult BaseResult = |
| 1533 | DeduceTemplateArguments(S, TemplateParams, SpecParam, |
| 1534 | QualType(NextT, 0), BaseInfo, Deduced); |
| 1535 | |
| 1536 | // If template argument deduction for this base was successful, |
| 1537 | // note that we had some success. Otherwise, ignore any deductions |
| 1538 | // from this base class. |
| 1539 | if (BaseResult == Sema::TDK_Success) { |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1540 | // If we've already seen some success, then deduction fails due to |
| 1541 | // an ambiguity (temp.deduct.call p5). |
| 1542 | if (Successful) |
| 1543 | return Sema::TDK_MiscellaneousDeductionFailure; |
| 1544 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1545 | Successful = true; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1546 | std::swap(SuccessfulDeduced, Deduced); |
| 1547 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1548 | Info.Param = BaseInfo.Param; |
| 1549 | Info.FirstArg = BaseInfo.FirstArg; |
| 1550 | Info.SecondArg = BaseInfo.SecondArg; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | Deduced = DeducedOrig; |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1554 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1556 | // Visit base classes |
| 1557 | CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl()); |
| 1558 | for (const auto &Base : Next->bases()) { |
| 1559 | assert(Base.getType()->isRecordType() && |
| 1560 | "Base class that isn't a record?"); |
| 1561 | ToVisit.push_back(Base.getType()->getAs<RecordType>()); |
| 1562 | } |
| 1563 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1564 | |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1565 | if (Successful) { |
| 1566 | std::swap(SuccessfulDeduced, Deduced); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1567 | return Sema::TDK_Success; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1568 | } |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1569 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1570 | return Result; |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1573 | // T type::* |
| 1574 | // T T::* |
| 1575 | // T (type::*)() |
| 1576 | // type (T::*)() |
| 1577 | // type (type::*)(T) |
| 1578 | // type (T::*)(T) |
| 1579 | // T (type::*)(T) |
| 1580 | // T (T::*)() |
| 1581 | // T (T::*)(T) |
| 1582 | case Type::MemberPointer: { |
| 1583 | const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); |
| 1584 | const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); |
| 1585 | if (!MemPtrArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1586 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1587 | |
David Majnemer | a381cda | 2015-11-30 20:34:28 +0000 | [diff] [blame] | 1588 | QualType ParamPointeeType = MemPtrParam->getPointeeType(); |
| 1589 | if (ParamPointeeType->isFunctionType()) |
| 1590 | S.adjustMemberFunctionCC(ParamPointeeType, /*IsStatic=*/true, |
| 1591 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
| 1592 | QualType ArgPointeeType = MemPtrArg->getPointeeType(); |
| 1593 | if (ArgPointeeType->isFunctionType()) |
| 1594 | S.adjustMemberFunctionCC(ArgPointeeType, /*IsStatic=*/true, |
| 1595 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
| 1596 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1597 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1598 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
David Majnemer | a381cda | 2015-11-30 20:34:28 +0000 | [diff] [blame] | 1599 | ParamPointeeType, |
| 1600 | ArgPointeeType, |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1601 | Info, Deduced, |
| 1602 | TDF & TDF_IgnoreQualifiers)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1603 | return Result; |
| 1604 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1605 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1606 | QualType(MemPtrParam->getClass(), 0), |
| 1607 | QualType(MemPtrArg->getClass(), 0), |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1608 | Info, Deduced, |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1609 | TDF & TDF_IgnoreQualifiers); |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
Anders Carlsson | 15f1dd1 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1612 | // (clang extension) |
| 1613 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1614 | // type(^)(T) |
| 1615 | // T(^)() |
| 1616 | // T(^)(T) |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1617 | case Type::BlockPointer: { |
| 1618 | const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); |
| 1619 | const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1620 | |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1621 | if (!BlockPtrArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1622 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1623 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1624 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1625 | BlockPtrParam->getPointeeType(), |
| 1626 | BlockPtrArg->getPointeeType(), |
| 1627 | Info, Deduced, 0); |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1630 | // (clang extension) |
| 1631 | // |
| 1632 | // T __attribute__(((ext_vector_type(<integral constant>)))) |
| 1633 | case Type::ExtVector: { |
| 1634 | const ExtVectorType *VectorParam = cast<ExtVectorType>(Param); |
| 1635 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1636 | // Make sure that the vectors have the same number of elements. |
| 1637 | if (VectorParam->getNumElements() != VectorArg->getNumElements()) |
| 1638 | return Sema::TDK_NonDeducedMismatch; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1639 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1640 | // Perform deduction on the element types. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1641 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1642 | VectorParam->getElementType(), |
| 1643 | VectorArg->getElementType(), |
| 1644 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1645 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1646 | |
| 1647 | if (const DependentSizedExtVectorType *VectorArg |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1648 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1649 | // We can't check the number of elements, since the argument has a |
| 1650 | // dependent number of elements. This can only occur during partial |
| 1651 | // ordering. |
| 1652 | |
| 1653 | // Perform deduction on the element types. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1654 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1655 | VectorParam->getElementType(), |
| 1656 | VectorArg->getElementType(), |
| 1657 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1658 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1659 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1660 | return Sema::TDK_NonDeducedMismatch; |
| 1661 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1662 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1663 | // (clang extension) |
| 1664 | // |
| 1665 | // T __attribute__(((ext_vector_type(N)))) |
| 1666 | case Type::DependentSizedExtVector: { |
| 1667 | const DependentSizedExtVectorType *VectorParam |
| 1668 | = cast<DependentSizedExtVectorType>(Param); |
| 1669 | |
| 1670 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1671 | // Perform deduction on the element types. |
| 1672 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1673 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1674 | VectorParam->getElementType(), |
| 1675 | VectorArg->getElementType(), |
| 1676 | Info, Deduced, TDF)) |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1677 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1678 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1679 | // Perform deduction on the vector size, if we can. |
| 1680 | NonTypeTemplateParmDecl *NTTP |
| 1681 | = getDeducedParameterFromExpr(VectorParam->getSizeExpr()); |
| 1682 | if (!NTTP) |
| 1683 | return Sema::TDK_Success; |
| 1684 | |
| 1685 | llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); |
| 1686 | ArgSize = VectorArg->getNumElements(); |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1687 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, |
| 1688 | S.Context.IntTy, false, Info, Deduced); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1689 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1690 | |
| 1691 | if (const DependentSizedExtVectorType *VectorArg |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1692 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1693 | // Perform deduction on the element types. |
| 1694 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1695 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1696 | VectorParam->getElementType(), |
| 1697 | VectorArg->getElementType(), |
| 1698 | Info, Deduced, TDF)) |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1699 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1700 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1701 | // Perform deduction on the vector size, if we can. |
| 1702 | NonTypeTemplateParmDecl *NTTP |
| 1703 | = getDeducedParameterFromExpr(VectorParam->getSizeExpr()); |
| 1704 | if (!NTTP) |
| 1705 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1706 | |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1707 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 1708 | VectorArg->getSizeExpr(), |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1709 | Info, Deduced); |
| 1710 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1711 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1712 | return Sema::TDK_NonDeducedMismatch; |
| 1713 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1714 | |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1715 | case Type::TypeOfExpr: |
| 1716 | case Type::TypeOf: |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 1717 | case Type::DependentName: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1718 | case Type::UnresolvedUsing: |
| 1719 | case Type::Decltype: |
| 1720 | case Type::UnaryTransform: |
| 1721 | case Type::Auto: |
| 1722 | case Type::DependentTemplateSpecialization: |
| 1723 | case Type::PackExpansion: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 1724 | case Type::Pipe: |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1725 | // No template argument deduction for these types |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1726 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1729 | llvm_unreachable("Invalid Type Class!"); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1732 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1733 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1734 | TemplateParameterList *TemplateParams, |
| 1735 | const TemplateArgument &Param, |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1736 | TemplateArgument Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1737 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 1738 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1739 | // If the template argument is a pack expansion, perform template argument |
| 1740 | // deduction against the pattern of that expansion. This only occurs during |
| 1741 | // partial ordering. |
| 1742 | if (Arg.isPackExpansion()) |
| 1743 | Arg = Arg.getPackExpansionPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1744 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1745 | switch (Param.getKind()) { |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1746 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1747 | llvm_unreachable("Null template argument in parameter list"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1748 | |
| 1749 | case TemplateArgument::Type: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1750 | if (Arg.getKind() == TemplateArgument::Type) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1751 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1752 | Param.getAsType(), |
| 1753 | Arg.getAsType(), |
| 1754 | Info, Deduced, 0); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1755 | Info.FirstArg = Param; |
| 1756 | Info.SecondArg = Arg; |
| 1757 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1758 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1759 | case TemplateArgument::Template: |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1760 | if (Arg.getKind() == TemplateArgument::Template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1761 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1762 | Param.getAsTemplate(), |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1763 | Arg.getAsTemplate(), Info, Deduced); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1764 | Info.FirstArg = Param; |
| 1765 | Info.SecondArg = Arg; |
| 1766 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1767 | |
| 1768 | case TemplateArgument::TemplateExpansion: |
| 1769 | llvm_unreachable("caller should handle pack expansions"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1770 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1771 | case TemplateArgument::Declaration: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1772 | if (Arg.getKind() == TemplateArgument::Declaration && |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 1773 | isSameDeclaration(Param.getAsDecl(), Arg.getAsDecl())) |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 1774 | return Sema::TDK_Success; |
| 1775 | |
| 1776 | Info.FirstArg = Param; |
| 1777 | Info.SecondArg = Arg; |
| 1778 | return Sema::TDK_NonDeducedMismatch; |
| 1779 | |
| 1780 | case TemplateArgument::NullPtr: |
| 1781 | if (Arg.getKind() == TemplateArgument::NullPtr && |
| 1782 | S.Context.hasSameType(Param.getNullPtrType(), Arg.getNullPtrType())) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1783 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1784 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1785 | Info.FirstArg = Param; |
| 1786 | Info.SecondArg = Arg; |
| 1787 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1788 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1789 | case TemplateArgument::Integral: |
| 1790 | if (Arg.getKind() == TemplateArgument::Integral) { |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1791 | if (hasSameExtendedValue(Param.getAsIntegral(), Arg.getAsIntegral())) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1792 | return Sema::TDK_Success; |
| 1793 | |
| 1794 | Info.FirstArg = Param; |
| 1795 | Info.SecondArg = Arg; |
| 1796 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1797 | } |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1798 | |
| 1799 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 1800 | Info.FirstArg = Param; |
| 1801 | Info.SecondArg = Arg; |
| 1802 | return Sema::TDK_NonDeducedMismatch; |
| 1803 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1804 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1805 | Info.FirstArg = Param; |
| 1806 | Info.SecondArg = Arg; |
| 1807 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1808 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1809 | case TemplateArgument::Expression: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1810 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1811 | = getDeducedParameterFromExpr(Param.getAsExpr())) { |
| 1812 | if (Arg.getKind() == TemplateArgument::Integral) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1813 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1814 | Arg.getAsIntegral(), |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1815 | Arg.getIntegralType(), |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1816 | /*ArrayBound=*/false, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1817 | Info, Deduced); |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 1818 | if (Arg.getKind() == TemplateArgument::NullPtr) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1819 | return DeduceNullPtrTemplateArgument(S, TemplateParams, NTTP, |
| 1820 | Arg.getNullPtrType(), |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame] | 1821 | Info, Deduced); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1822 | if (Arg.getKind() == TemplateArgument::Expression) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1823 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 1824 | Arg.getAsExpr(), Info, Deduced); |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 1825 | if (Arg.getKind() == TemplateArgument::Declaration) |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 1826 | return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, |
| 1827 | Arg.getAsDecl(), |
| 1828 | Arg.getParamTypeForDecl(), |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 1829 | Info, Deduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1830 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1831 | Info.FirstArg = Param; |
| 1832 | Info.SecondArg = Arg; |
| 1833 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1834 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1835 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1836 | // Can't deduce anything, but that's okay. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1837 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1838 | } |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 1839 | case TemplateArgument::Pack: |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1840 | llvm_unreachable("Argument packs should be expanded by the caller!"); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1841 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1842 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1843 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1846 | /// \brief Determine whether there is a template argument to be used for |
| 1847 | /// deduction. |
| 1848 | /// |
| 1849 | /// This routine "expands" argument packs in-place, overriding its input |
| 1850 | /// parameters so that \c Args[ArgIdx] will be the available template argument. |
| 1851 | /// |
| 1852 | /// \returns true if there is another template argument (which will be at |
| 1853 | /// \c Args[ArgIdx]), false otherwise. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1854 | static bool hasTemplateArgumentForDeduction(const TemplateArgument *&Args, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1855 | unsigned &ArgIdx, |
| 1856 | unsigned &NumArgs) { |
| 1857 | if (ArgIdx == NumArgs) |
| 1858 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1859 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1860 | const TemplateArgument &Arg = Args[ArgIdx]; |
| 1861 | if (Arg.getKind() != TemplateArgument::Pack) |
| 1862 | return true; |
| 1863 | |
| 1864 | assert(ArgIdx == NumArgs - 1 && "Pack not at the end of argument list?"); |
| 1865 | Args = Arg.pack_begin(); |
| 1866 | NumArgs = Arg.pack_size(); |
| 1867 | ArgIdx = 0; |
| 1868 | return ArgIdx < NumArgs; |
| 1869 | } |
| 1870 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1871 | /// \brief Determine whether the given set of template arguments has a pack |
| 1872 | /// expansion that is not the last template argument. |
| 1873 | static bool hasPackExpansionBeforeEnd(const TemplateArgument *Args, |
| 1874 | unsigned NumArgs) { |
| 1875 | unsigned ArgIdx = 0; |
| 1876 | while (ArgIdx < NumArgs) { |
| 1877 | const TemplateArgument &Arg = Args[ArgIdx]; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1878 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1879 | // Unwrap argument packs. |
| 1880 | if (Args[ArgIdx].getKind() == TemplateArgument::Pack) { |
| 1881 | Args = Arg.pack_begin(); |
| 1882 | NumArgs = Arg.pack_size(); |
| 1883 | ArgIdx = 0; |
| 1884 | continue; |
| 1885 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1886 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1887 | ++ArgIdx; |
| 1888 | if (ArgIdx == NumArgs) |
| 1889 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1890 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1891 | if (Arg.isPackExpansion()) |
| 1892 | return true; |
| 1893 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1894 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1895 | return false; |
| 1896 | } |
| 1897 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1898 | static Sema::TemplateDeductionResult |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1899 | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1900 | const TemplateArgument *Params, unsigned NumParams, |
| 1901 | const TemplateArgument *Args, unsigned NumArgs, |
| 1902 | TemplateDeductionInfo &Info, |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1903 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 1904 | bool NumberOfArgumentsMustMatch) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1905 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1906 | // If the template argument list of P contains a pack expansion that is not |
| 1907 | // the last template argument, the entire template argument list is a |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1908 | // non-deduced context. |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1909 | if (hasPackExpansionBeforeEnd(Params, NumParams)) |
| 1910 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1911 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1912 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1913 | // If P has a form that contains <T> or <i>, then each argument Pi of the |
| 1914 | // respective template argument list P is compared with the corresponding |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1915 | // argument Ai of the corresponding template argument list of A. |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1916 | unsigned ArgIdx = 0, ParamIdx = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1917 | for (; hasTemplateArgumentForDeduction(Params, ParamIdx, NumParams); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1918 | ++ParamIdx) { |
| 1919 | if (!Params[ParamIdx].isPackExpansion()) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1920 | // The simple case: deduce template arguments by matching Pi and Ai. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1921 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1922 | // Check whether we have enough arguments. |
| 1923 | if (!hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs)) |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1924 | return NumberOfArgumentsMustMatch ? Sema::TDK_TooFewArguments |
| 1925 | : Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1926 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1927 | if (Args[ArgIdx].isPackExpansion()) { |
| 1928 | // FIXME: We follow the logic of C++0x [temp.deduct.type]p22 here, |
| 1929 | // but applied to pack expansions that are template arguments. |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 1930 | return Sema::TDK_MiscellaneousDeductionFailure; |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1931 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1932 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1933 | // Perform deduction for this Pi/Ai pair. |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1934 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1935 | = DeduceTemplateArguments(S, TemplateParams, |
| 1936 | Params[ParamIdx], Args[ArgIdx], |
| 1937 | Info, Deduced)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1938 | return Result; |
| 1939 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1940 | // Move to the next argument. |
| 1941 | ++ArgIdx; |
| 1942 | continue; |
| 1943 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1944 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1945 | // The parameter is a pack expansion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1946 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1947 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1948 | // If Pi is a pack expansion, then the pattern of Pi is compared with |
| 1949 | // each remaining argument in the template argument list of A. Each |
| 1950 | // comparison deduces template arguments for subsequent positions in the |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1951 | // template parameter packs expanded by Pi. |
| 1952 | TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1953 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1954 | // FIXME: If there are no remaining arguments, we can bail out early |
| 1955 | // and set any deduced parameter packs to an empty argument pack. |
| 1956 | // The latter part of this is a (minor) correctness issue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1957 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1958 | // Prepare to deduce the packs within the pattern. |
| 1959 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1960 | |
| 1961 | // Keep track of the deduced template arguments for each parameter pack |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1962 | // expanded by this pack expansion (the outer index) and for each |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1963 | // template argument (the inner SmallVectors). |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1964 | bool HasAnyArguments = false; |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1965 | for (; hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs); ++ArgIdx) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1966 | HasAnyArguments = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1967 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1968 | // Deduce template arguments from the pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1969 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1970 | = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx], |
| 1971 | Info, Deduced)) |
| 1972 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1973 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1974 | PackScope.nextPackElement(); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1975 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1976 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1977 | // Build argument packs for each of the parameter packs expanded by this |
| 1978 | // pack expansion. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1979 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1980 | return Result; |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1981 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1982 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1983 | return Sema::TDK_Success; |
| 1984 | } |
| 1985 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1986 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1987 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1988 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1989 | const TemplateArgumentList &ParamList, |
| 1990 | const TemplateArgumentList &ArgList, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1991 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 1992 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1993 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1994 | ParamList.data(), ParamList.size(), |
| 1995 | ArgList.data(), ArgList.size(), |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1996 | Info, Deduced, false); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1999 | /// \brief Determine whether two template arguments are the same. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2000 | static bool isSameTemplateArg(ASTContext &Context, |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2001 | const TemplateArgument &X, |
| 2002 | const TemplateArgument &Y) { |
| 2003 | if (X.getKind() != Y.getKind()) |
| 2004 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2005 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2006 | switch (X.getKind()) { |
| 2007 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2008 | llvm_unreachable("Comparing NULL template argument"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2010 | case TemplateArgument::Type: |
| 2011 | return Context.getCanonicalType(X.getAsType()) == |
| 2012 | Context.getCanonicalType(Y.getAsType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2013 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2014 | case TemplateArgument::Declaration: |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 2015 | return isSameDeclaration(X.getAsDecl(), Y.getAsDecl()); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2016 | |
| 2017 | case TemplateArgument::NullPtr: |
| 2018 | return Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2019 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2020 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2021 | case TemplateArgument::TemplateExpansion: |
| 2022 | return Context.getCanonicalTemplateName( |
| 2023 | X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() == |
| 2024 | Context.getCanonicalTemplateName( |
| 2025 | Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2026 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2027 | case TemplateArgument::Integral: |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 2028 | return X.getAsIntegral() == Y.getAsIntegral(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2029 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2030 | case TemplateArgument::Expression: { |
| 2031 | llvm::FoldingSetNodeID XID, YID; |
| 2032 | X.getAsExpr()->Profile(XID, Context, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2033 | Y.getAsExpr()->Profile(YID, Context, true); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2034 | return XID == YID; |
| 2035 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2036 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2037 | case TemplateArgument::Pack: |
| 2038 | if (X.pack_size() != Y.pack_size()) |
| 2039 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2040 | |
| 2041 | for (TemplateArgument::pack_iterator XP = X.pack_begin(), |
| 2042 | XPEnd = X.pack_end(), |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2043 | YP = Y.pack_begin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2044 | XP != XPEnd; ++XP, ++YP) |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2045 | if (!isSameTemplateArg(Context, *XP, *YP)) |
| 2046 | return false; |
| 2047 | |
| 2048 | return true; |
| 2049 | } |
| 2050 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2051 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2052 | } |
| 2053 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2054 | /// \brief Allocate a TemplateArgumentLoc where all locations have |
| 2055 | /// been initialized to the given location. |
| 2056 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2057 | /// \param Arg The template argument we are producing template argument |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2058 | /// location information for. |
| 2059 | /// |
| 2060 | /// \param NTTPType For a declaration template argument, the type of |
| 2061 | /// the non-type template parameter that corresponds to this template |
| 2062 | /// argument. |
| 2063 | /// |
| 2064 | /// \param Loc The source location to use for the resulting template |
| 2065 | /// argument. |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2066 | TemplateArgumentLoc |
| 2067 | Sema::getTrivialTemplateArgumentLoc(const TemplateArgument &Arg, |
| 2068 | QualType NTTPType, SourceLocation Loc) { |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2069 | switch (Arg.getKind()) { |
| 2070 | case TemplateArgument::Null: |
| 2071 | llvm_unreachable("Can't get a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2072 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2073 | case TemplateArgument::Type: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2074 | return TemplateArgumentLoc( |
| 2075 | Arg, Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2076 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2077 | case TemplateArgument::Declaration: { |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2078 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
| 2079 | .getAs<Expr>(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2080 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2081 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2082 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2083 | case TemplateArgument::NullPtr: { |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2084 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
| 2085 | .getAs<Expr>(); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2086 | return TemplateArgumentLoc(TemplateArgument(NTTPType, /*isNullPtr*/true), |
| 2087 | E); |
| 2088 | } |
| 2089 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2090 | case TemplateArgument::Integral: { |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2091 | Expr *E = |
| 2092 | BuildExpressionFromIntegralTemplateArgument(Arg, Loc).getAs<Expr>(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2093 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2094 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2095 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2096 | case TemplateArgument::Template: |
| 2097 | case TemplateArgument::TemplateExpansion: { |
| 2098 | NestedNameSpecifierLocBuilder Builder; |
| 2099 | TemplateName Template = Arg.getAsTemplate(); |
| 2100 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2101 | Builder.MakeTrivial(Context, DTN->getQualifier(), Loc); |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 2102 | else if (QualifiedTemplateName *QTN = |
| 2103 | Template.getAsQualifiedTemplateName()) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2104 | Builder.MakeTrivial(Context, QTN->getQualifier(), Loc); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2105 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2106 | if (Arg.getKind() == TemplateArgument::Template) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2107 | return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(Context), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2108 | Loc); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2109 | |
| 2110 | return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(Context), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2111 | Loc, Loc); |
| 2112 | } |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2113 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2114 | case TemplateArgument::Expression: |
| 2115 | return TemplateArgumentLoc(Arg, Arg.getAsExpr()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2116 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2117 | case TemplateArgument::Pack: |
| 2118 | return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo()); |
| 2119 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2120 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2121 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2122 | } |
| 2123 | |
| 2124 | |
| 2125 | /// \brief Convert the given deduced template argument and add it to the set of |
| 2126 | /// fully-converted template arguments. |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2127 | static bool |
| 2128 | ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param, |
| 2129 | DeducedTemplateArgument Arg, |
| 2130 | NamedDecl *Template, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2131 | TemplateDeductionInfo &Info, |
| 2132 | bool InFunctionTemplate, |
| 2133 | SmallVectorImpl<TemplateArgument> &Output) { |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2134 | // First, for a non-type template parameter type that is |
| 2135 | // initialized by a declaration, we need the type of the |
| 2136 | // corresponding non-type template parameter. |
| 2137 | QualType NTTPType; |
| 2138 | if (NonTypeTemplateParmDecl *NTTP = |
| 2139 | dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2140 | NTTPType = NTTP->getType(); |
| 2141 | if (NTTPType->isDependentType()) { |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2142 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Output); |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2143 | NTTPType = S.SubstType(NTTPType, |
| 2144 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2145 | NTTP->getLocation(), |
| 2146 | NTTP->getDeclName()); |
| 2147 | if (NTTPType.isNull()) |
| 2148 | return true; |
| 2149 | } |
| 2150 | } |
| 2151 | |
| 2152 | auto ConvertArg = [&](DeducedTemplateArgument Arg, |
| 2153 | unsigned ArgumentPackIndex) { |
| 2154 | // Convert the deduced template argument into a template |
| 2155 | // argument that we can check, almost as if the user had written |
| 2156 | // the template argument explicitly. |
| 2157 | TemplateArgumentLoc ArgLoc = |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2158 | S.getTrivialTemplateArgumentLoc(Arg, NTTPType, Info.getLocation()); |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2159 | |
| 2160 | // Check the template argument, converting it as necessary. |
| 2161 | return S.CheckTemplateArgument( |
| 2162 | Param, ArgLoc, Template, Template->getLocation(), |
| 2163 | Template->getSourceRange().getEnd(), ArgumentPackIndex, Output, |
| 2164 | InFunctionTemplate |
| 2165 | ? (Arg.wasDeducedFromArrayBound() ? Sema::CTAK_DeducedFromArrayBound |
| 2166 | : Sema::CTAK_Deduced) |
| 2167 | : Sema::CTAK_Specified); |
| 2168 | }; |
| 2169 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2170 | if (Arg.getKind() == TemplateArgument::Pack) { |
| 2171 | // This is a template argument pack, so check each of its arguments against |
| 2172 | // the template parameter. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2173 | SmallVector<TemplateArgument, 2> PackedArgsBuilder; |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 2174 | for (const auto &P : Arg.pack_elements()) { |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2175 | // When converting the deduced template argument, append it to the |
| 2176 | // general output list. We need to do this so that the template argument |
| 2177 | // checking logic has all of the prior template arguments available. |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 2178 | DeducedTemplateArgument InnerArg(P); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2179 | InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound()); |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2180 | assert(InnerArg.getKind() != TemplateArgument::Pack && |
| 2181 | "deduced nested pack"); |
| 2182 | if (ConvertArg(InnerArg, PackedArgsBuilder.size())) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2183 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2184 | |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2185 | // Move the converted template argument into our argument pack. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 2186 | PackedArgsBuilder.push_back(Output.pop_back_val()); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2187 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2188 | |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2189 | // If the pack is empty, we still need to substitute into the parameter |
| 2190 | // itself, in case that substitution fails. For non-type parameters, we did |
| 2191 | // this above. For type parameters, no substitution is ever required. |
| 2192 | auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param); |
| 2193 | if (TTP && PackedArgsBuilder.empty()) { |
| 2194 | // Set up a template instantiation context. |
| 2195 | LocalInstantiationScope Scope(S); |
| 2196 | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
| 2197 | TTP, Output, |
| 2198 | Template->getSourceRange()); |
| 2199 | if (Inst.isInvalid()) |
| 2200 | return true; |
| 2201 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2202 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Output); |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2203 | if (!S.SubstDecl(TTP, S.CurContext, |
| 2204 | MultiLevelTemplateArgumentList(TemplateArgs))) |
| 2205 | return true; |
| 2206 | } |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2207 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2208 | // Create the resulting argument pack. |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 2209 | Output.push_back( |
| 2210 | TemplateArgument::CreatePackCopy(S.Context, PackedArgsBuilder)); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2211 | return false; |
| 2212 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2213 | |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2214 | return ConvertArg(Arg, 0); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame^] | 2217 | // FIXME: This should not be a template, but |
| 2218 | // ClassTemplatePartialSpecializationDecl sadly does not derive from |
| 2219 | // TemplateDecl. |
| 2220 | template<typename TemplateDeclT> |
| 2221 | static Sema::TemplateDeductionResult ConvertDeducedTemplateArguments( |
| 2222 | Sema &S, TemplateDeclT *Template, |
| 2223 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2224 | TemplateDeductionInfo &Info, SmallVectorImpl<TemplateArgument> &Builder, |
| 2225 | LocalInstantiationScope *CurrentInstantiationScope = nullptr, |
| 2226 | unsigned NumAlreadyConverted = 0, bool PartialOverloading = false) { |
| 2227 | TemplateParameterList *TemplateParams = Template->getTemplateParameters(); |
| 2228 | |
| 2229 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 2230 | NamedDecl *Param = TemplateParams->getParam(I); |
| 2231 | |
| 2232 | if (!Deduced[I].isNull()) { |
| 2233 | if (I < NumAlreadyConverted) { |
| 2234 | // We have already fully type-checked and converted this |
| 2235 | // argument, because it was explicitly-specified. Just record the |
| 2236 | // presence of this argument. |
| 2237 | Builder.push_back(Deduced[I]); |
| 2238 | // We may have had explicitly-specified template arguments for a |
| 2239 | // template parameter pack (that may or may not have been extended |
| 2240 | // via additional deduced arguments). |
| 2241 | if (Param->isParameterPack() && CurrentInstantiationScope) { |
| 2242 | if (CurrentInstantiationScope->getPartiallySubstitutedPack() == |
| 2243 | Param) { |
| 2244 | // Forget the partially-substituted pack; its substitution is now |
| 2245 | // complete. |
| 2246 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2247 | } |
| 2248 | } |
| 2249 | continue; |
| 2250 | } |
| 2251 | |
| 2252 | // We have deduced this argument, so it still needs to be |
| 2253 | // checked and converted. |
| 2254 | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Template, Info, |
| 2255 | isa<FunctionTemplateDecl>(Template), |
| 2256 | Builder)) { |
| 2257 | Info.Param = makeTemplateParameter(Param); |
| 2258 | // FIXME: These template arguments are temporary. Free them! |
| 2259 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2260 | return Sema::TDK_SubstitutionFailure; |
| 2261 | } |
| 2262 | |
| 2263 | continue; |
| 2264 | } |
| 2265 | |
| 2266 | // C++0x [temp.arg.explicit]p3: |
| 2267 | // A trailing template parameter pack (14.5.3) not otherwise deduced will |
| 2268 | // be deduced to an empty sequence of template arguments. |
| 2269 | // FIXME: Where did the word "trailing" come from? |
| 2270 | if (Param->isTemplateParameterPack()) { |
| 2271 | // We may have had explicitly-specified template arguments for this |
| 2272 | // template parameter pack. If so, our empty deduction extends the |
| 2273 | // explicitly-specified set (C++0x [temp.arg.explicit]p9). |
| 2274 | const TemplateArgument *ExplicitArgs; |
| 2275 | unsigned NumExplicitArgs; |
| 2276 | if (CurrentInstantiationScope && |
| 2277 | CurrentInstantiationScope->getPartiallySubstitutedPack( |
| 2278 | &ExplicitArgs, &NumExplicitArgs) == Param) { |
| 2279 | Builder.push_back(TemplateArgument( |
| 2280 | llvm::makeArrayRef(ExplicitArgs, NumExplicitArgs))); |
| 2281 | |
| 2282 | // Forget the partially-substituted pack; its substitution is now |
| 2283 | // complete. |
| 2284 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2285 | } else { |
| 2286 | // Go through the motions of checking the empty argument pack against |
| 2287 | // the parameter pack. |
| 2288 | DeducedTemplateArgument DeducedPack(TemplateArgument::getEmptyPack()); |
| 2289 | if (ConvertDeducedTemplateArgument( |
| 2290 | S, Param, DeducedPack, Template, Info, |
| 2291 | isa<FunctionTemplateDecl>(Template), Builder)) { |
| 2292 | Info.Param = makeTemplateParameter(Param); |
| 2293 | // FIXME: These template arguments are temporary. Free them! |
| 2294 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2295 | return Sema::TDK_SubstitutionFailure; |
| 2296 | } |
| 2297 | } |
| 2298 | continue; |
| 2299 | } |
| 2300 | |
| 2301 | // Substitute into the default template argument, if available. |
| 2302 | bool HasDefaultArg = false; |
| 2303 | TemplateDecl *TD = dyn_cast<TemplateDecl>(Template); |
| 2304 | if (!TD) { |
| 2305 | assert(isa<ClassTemplatePartialSpecializationDecl>(Template)); |
| 2306 | return Sema::TDK_Incomplete; |
| 2307 | } |
| 2308 | |
| 2309 | TemplateArgumentLoc DefArg = S.SubstDefaultTemplateArgumentIfAvailable( |
| 2310 | TD, TD->getLocation(), TD->getSourceRange().getEnd(), Param, Builder, |
| 2311 | HasDefaultArg); |
| 2312 | |
| 2313 | // If there was no default argument, deduction is incomplete. |
| 2314 | if (DefArg.getArgument().isNull()) { |
| 2315 | Info.Param = makeTemplateParameter( |
| 2316 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
| 2317 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2318 | if (PartialOverloading) break; |
| 2319 | |
| 2320 | return HasDefaultArg ? Sema::TDK_SubstitutionFailure |
| 2321 | : Sema::TDK_Incomplete; |
| 2322 | } |
| 2323 | |
| 2324 | // Check whether we can actually use the default argument. |
| 2325 | if (S.CheckTemplateArgument(Param, DefArg, TD, TD->getLocation(), |
| 2326 | TD->getSourceRange().getEnd(), 0, Builder, |
| 2327 | Sema::CTAK_Specified)) { |
| 2328 | Info.Param = makeTemplateParameter( |
| 2329 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
| 2330 | // FIXME: These template arguments are temporary. Free them! |
| 2331 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
| 2332 | return Sema::TDK_SubstitutionFailure; |
| 2333 | } |
| 2334 | |
| 2335 | // If we get here, we successfully used the default template argument. |
| 2336 | } |
| 2337 | |
| 2338 | return Sema::TDK_Success; |
| 2339 | } |
| 2340 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2341 | /// Complete template argument deduction for a class template partial |
| 2342 | /// specialization. |
| 2343 | static Sema::TemplateDeductionResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2344 | FinishTemplateArgumentDeduction(Sema &S, |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2345 | ClassTemplatePartialSpecializationDecl *Partial, |
| 2346 | const TemplateArgumentList &TemplateArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2347 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2348 | TemplateDeductionInfo &Info) { |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2349 | // Unevaluated SFINAE context. |
| 2350 | EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2351 | Sema::SFINAETrap Trap(S); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2352 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2353 | Sema::ContextRAII SavedContext(S, Partial); |
| 2354 | |
| 2355 | // C++ [temp.deduct.type]p2: |
| 2356 | // [...] or if any template argument remains neither deduced nor |
| 2357 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2358 | SmallVector<TemplateArgument, 4> Builder; |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame^] | 2359 | if (auto Result = ConvertDeducedTemplateArguments(S, Partial, Deduced, |
| 2360 | Info, Builder)) |
| 2361 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2362 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2363 | // Form the template argument list from the deduced template arguments. |
| 2364 | TemplateArgumentList *DeducedArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2365 | = TemplateArgumentList::CreateCopy(S.Context, Builder); |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2366 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2367 | Info.reset(DeducedArgumentList); |
| 2368 | |
| 2369 | // Substitute the deduced template arguments into the template |
| 2370 | // arguments of the class template partial specialization, and |
| 2371 | // verify that the instantiated template arguments are both valid |
| 2372 | // and are equivalent to the template arguments originally provided |
| 2373 | // to the class template. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2374 | LocalInstantiationScope InstScope(S); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2375 | ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate(); |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2376 | const ASTTemplateArgumentListInfo *PartialTemplArgInfo |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2377 | = Partial->getTemplateArgsAsWritten(); |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2378 | const TemplateArgumentLoc *PartialTemplateArgs |
| 2379 | = PartialTemplArgInfo->getTemplateArgs(); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2380 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2381 | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, |
| 2382 | PartialTemplArgInfo->RAngleLoc); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2383 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2384 | if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs, |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2385 | InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { |
| 2386 | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; |
| 2387 | if (ParamIdx >= Partial->getTemplateParameters()->size()) |
| 2388 | ParamIdx = Partial->getTemplateParameters()->size() - 1; |
| 2389 | |
| 2390 | Decl *Param |
| 2391 | = const_cast<NamedDecl *>( |
| 2392 | Partial->getTemplateParameters()->getParam(ParamIdx)); |
| 2393 | Info.Param = makeTemplateParameter(Param); |
| 2394 | Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); |
| 2395 | return Sema::TDK_SubstitutionFailure; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2396 | } |
| 2397 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2398 | SmallVector<TemplateArgument, 4> ConvertedInstArgs; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2399 | if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(), |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2400 | InstArgs, false, ConvertedInstArgs)) |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2401 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2402 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2403 | TemplateParameterList *TemplateParams |
| 2404 | = ClassTemplate->getTemplateParameters(); |
| 2405 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2406 | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2407 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 2408 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2409 | Info.FirstArg = TemplateArgs[I]; |
| 2410 | Info.SecondArg = InstArg; |
| 2411 | return Sema::TDK_NonDeducedMismatch; |
| 2412 | } |
| 2413 | } |
| 2414 | |
| 2415 | if (Trap.hasErrorOccurred()) |
| 2416 | return Sema::TDK_SubstitutionFailure; |
| 2417 | |
| 2418 | return Sema::TDK_Success; |
| 2419 | } |
| 2420 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2421 | /// \brief Perform template argument deduction to determine whether |
Larisse Voufo | 833b05a | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 2422 | /// the given template arguments match the given class template |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2423 | /// partial specialization per C++ [temp.class.spec.match]. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2424 | Sema::TemplateDeductionResult |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2425 | Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2426 | const TemplateArgumentList &TemplateArgs, |
| 2427 | TemplateDeductionInfo &Info) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 2428 | if (Partial->isInvalidDecl()) |
| 2429 | return TDK_Invalid; |
| 2430 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2431 | // C++ [temp.class.spec.match]p2: |
| 2432 | // A partial specialization matches a given actual template |
| 2433 | // argument list if the template arguments of the partial |
| 2434 | // specialization can be deduced from the actual template argument |
| 2435 | // list (14.8.2). |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2436 | |
| 2437 | // Unevaluated SFINAE context. |
| 2438 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2439 | SFINAETrap Trap(*this); |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2440 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2441 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2442 | Deduced.resize(Partial->getTemplateParameters()->size()); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2443 | if (TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2444 | = ::DeduceTemplateArguments(*this, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2445 | Partial->getTemplateParameters(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2446 | Partial->getTemplateArgs(), |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2447 | TemplateArgs, Info, Deduced)) |
| 2448 | return Result; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 2449 | |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2450 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2451 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2452 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2453 | if (Inst.isInvalid()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2454 | return TDK_InstantiationDepth; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2455 | |
Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2456 | if (Trap.hasErrorOccurred()) |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2457 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2458 | |
| 2459 | return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs, |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2460 | Deduced, Info); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2461 | } |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2462 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2463 | /// Complete template argument deduction for a variable template partial |
| 2464 | /// specialization. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2465 | /// TODO: Unify with ClassTemplatePartialSpecializationDecl version? |
| 2466 | /// May require unifying ClassTemplate(Partial)SpecializationDecl and |
| 2467 | /// VarTemplate(Partial)SpecializationDecl with a new data |
| 2468 | /// structure Template(Partial)SpecializationDecl, and |
| 2469 | /// using Template(Partial)SpecializationDecl as input type. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2470 | static Sema::TemplateDeductionResult FinishTemplateArgumentDeduction( |
| 2471 | Sema &S, VarTemplatePartialSpecializationDecl *Partial, |
| 2472 | const TemplateArgumentList &TemplateArgs, |
| 2473 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2474 | TemplateDeductionInfo &Info) { |
| 2475 | // Unevaluated SFINAE context. |
| 2476 | EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated); |
| 2477 | Sema::SFINAETrap Trap(S); |
| 2478 | |
| 2479 | // C++ [temp.deduct.type]p2: |
| 2480 | // [...] or if any template argument remains neither deduced nor |
| 2481 | // explicitly specified, template argument deduction fails. |
| 2482 | SmallVector<TemplateArgument, 4> Builder; |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame^] | 2483 | if (auto Result = ConvertDeducedTemplateArguments(S, Partial, Deduced, |
| 2484 | Info, Builder)) |
| 2485 | return Result; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2486 | |
| 2487 | // Form the template argument list from the deduced template arguments. |
| 2488 | TemplateArgumentList *DeducedArgumentList = TemplateArgumentList::CreateCopy( |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2489 | S.Context, Builder); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2490 | |
| 2491 | Info.reset(DeducedArgumentList); |
| 2492 | |
| 2493 | // Substitute the deduced template arguments into the template |
| 2494 | // arguments of the class template partial specialization, and |
| 2495 | // verify that the instantiated template arguments are both valid |
| 2496 | // and are equivalent to the template arguments originally provided |
| 2497 | // to the class template. |
| 2498 | LocalInstantiationScope InstScope(S); |
| 2499 | VarTemplateDecl *VarTemplate = Partial->getSpecializedTemplate(); |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2500 | const ASTTemplateArgumentListInfo *PartialTemplArgInfo |
| 2501 | = Partial->getTemplateArgsAsWritten(); |
| 2502 | const TemplateArgumentLoc *PartialTemplateArgs |
| 2503 | = PartialTemplArgInfo->getTemplateArgs(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2504 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2505 | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, |
| 2506 | PartialTemplArgInfo->RAngleLoc); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2507 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2508 | if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2509 | InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { |
| 2510 | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; |
| 2511 | if (ParamIdx >= Partial->getTemplateParameters()->size()) |
| 2512 | ParamIdx = Partial->getTemplateParameters()->size() - 1; |
| 2513 | |
| 2514 | Decl *Param = const_cast<NamedDecl *>( |
| 2515 | Partial->getTemplateParameters()->getParam(ParamIdx)); |
| 2516 | Info.Param = makeTemplateParameter(Param); |
| 2517 | Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); |
| 2518 | return Sema::TDK_SubstitutionFailure; |
| 2519 | } |
| 2520 | SmallVector<TemplateArgument, 4> ConvertedInstArgs; |
| 2521 | if (S.CheckTemplateArgumentList(VarTemplate, Partial->getLocation(), InstArgs, |
| 2522 | false, ConvertedInstArgs)) |
| 2523 | return Sema::TDK_SubstitutionFailure; |
| 2524 | |
| 2525 | TemplateParameterList *TemplateParams = VarTemplate->getTemplateParameters(); |
| 2526 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
| 2527 | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; |
| 2528 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { |
| 2529 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
| 2530 | Info.FirstArg = TemplateArgs[I]; |
| 2531 | Info.SecondArg = InstArg; |
| 2532 | return Sema::TDK_NonDeducedMismatch; |
| 2533 | } |
| 2534 | } |
| 2535 | |
| 2536 | if (Trap.hasErrorOccurred()) |
| 2537 | return Sema::TDK_SubstitutionFailure; |
| 2538 | |
| 2539 | return Sema::TDK_Success; |
| 2540 | } |
| 2541 | |
| 2542 | /// \brief Perform template argument deduction to determine whether |
| 2543 | /// the given template arguments match the given variable template |
| 2544 | /// partial specialization per C++ [temp.class.spec.match]. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2545 | /// TODO: Unify with ClassTemplatePartialSpecializationDecl version? |
| 2546 | /// May require unifying ClassTemplate(Partial)SpecializationDecl and |
| 2547 | /// VarTemplate(Partial)SpecializationDecl with a new data |
| 2548 | /// structure Template(Partial)SpecializationDecl, and |
| 2549 | /// using Template(Partial)SpecializationDecl as input type. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2550 | Sema::TemplateDeductionResult |
| 2551 | Sema::DeduceTemplateArguments(VarTemplatePartialSpecializationDecl *Partial, |
| 2552 | const TemplateArgumentList &TemplateArgs, |
| 2553 | TemplateDeductionInfo &Info) { |
| 2554 | if (Partial->isInvalidDecl()) |
| 2555 | return TDK_Invalid; |
| 2556 | |
| 2557 | // C++ [temp.class.spec.match]p2: |
| 2558 | // A partial specialization matches a given actual template |
| 2559 | // argument list if the template arguments of the partial |
| 2560 | // specialization can be deduced from the actual template argument |
| 2561 | // list (14.8.2). |
| 2562 | |
| 2563 | // Unevaluated SFINAE context. |
| 2564 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
| 2565 | SFINAETrap Trap(*this); |
| 2566 | |
| 2567 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 2568 | Deduced.resize(Partial->getTemplateParameters()->size()); |
| 2569 | if (TemplateDeductionResult Result = ::DeduceTemplateArguments( |
| 2570 | *this, Partial->getTemplateParameters(), Partial->getTemplateArgs(), |
| 2571 | TemplateArgs, Info, Deduced)) |
| 2572 | return Result; |
| 2573 | |
| 2574 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2575 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2576 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2577 | if (Inst.isInvalid()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2578 | return TDK_InstantiationDepth; |
| 2579 | |
| 2580 | if (Trap.hasErrorOccurred()) |
| 2581 | return Sema::TDK_SubstitutionFailure; |
| 2582 | |
| 2583 | return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs, |
| 2584 | Deduced, Info); |
| 2585 | } |
| 2586 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2587 | /// \brief Determine whether the given type T is a simple-template-id type. |
| 2588 | static bool isSimpleTemplateIdType(QualType T) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2589 | if (const TemplateSpecializationType *Spec |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2590 | = T->getAs<TemplateSpecializationType>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2591 | return Spec->getTemplateName().getAsTemplateDecl() != nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2592 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2593 | return false; |
| 2594 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2595 | |
| 2596 | /// \brief Substitute the explicitly-provided template arguments into the |
| 2597 | /// given function template according to C++ [temp.arg.explicit]. |
| 2598 | /// |
| 2599 | /// \param FunctionTemplate the function template into which the explicit |
| 2600 | /// template arguments will be substituted. |
| 2601 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2602 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2603 | /// arguments. |
| 2604 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2605 | /// \param Deduced the deduced template arguments, which will be populated |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2606 | /// with the converted and checked explicit template arguments. |
| 2607 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2608 | /// \param ParamTypes will be populated with the instantiated function |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2609 | /// parameters. |
| 2610 | /// |
| 2611 | /// \param FunctionType if non-NULL, the result type of the function template |
| 2612 | /// will also be instantiated and the pointed-to value will be updated with |
| 2613 | /// the instantiated function type. |
| 2614 | /// |
| 2615 | /// \param Info if substitution fails for any reason, this object will be |
| 2616 | /// populated with more information about the failure. |
| 2617 | /// |
| 2618 | /// \returns TDK_Success if substitution was successful, or some failure |
| 2619 | /// condition. |
| 2620 | Sema::TemplateDeductionResult |
| 2621 | Sema::SubstituteExplicitTemplateArguments( |
| 2622 | FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2623 | TemplateArgumentListInfo &ExplicitTemplateArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2624 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2625 | SmallVectorImpl<QualType> &ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2626 | QualType *FunctionType, |
| 2627 | TemplateDeductionInfo &Info) { |
| 2628 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 2629 | TemplateParameterList *TemplateParams |
| 2630 | = FunctionTemplate->getTemplateParameters(); |
| 2631 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2632 | if (ExplicitTemplateArgs.size() == 0) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2633 | // No arguments to substitute; just copy over the parameter types and |
| 2634 | // fill in the function type. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2635 | for (auto P : Function->parameters()) |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 2636 | ParamTypes.push_back(P->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2637 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2638 | if (FunctionType) |
| 2639 | *FunctionType = Function->getType(); |
| 2640 | return TDK_Success; |
| 2641 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2642 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2643 | // Unevaluated SFINAE context. |
| 2644 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2645 | SFINAETrap Trap(*this); |
| 2646 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2647 | // C++ [temp.arg.explicit]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2648 | // Template arguments that are present shall be specified in the |
| 2649 | // declaration order of their corresponding template-parameters. The |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2650 | // template argument list shall not specify more template-arguments than |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2651 | // there are corresponding template-parameters. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2652 | SmallVector<TemplateArgument, 4> Builder; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2653 | |
| 2654 | // Enter a new template instantiation context where we check the |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2655 | // explicitly-specified template arguments against this function template, |
| 2656 | // and then substitute them into the function parameter types. |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2657 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2658 | InstantiatingTemplate Inst(*this, Info.getLocation(), FunctionTemplate, |
| 2659 | DeducedArgs, |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2660 | ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution, |
| 2661 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2662 | if (Inst.isInvalid()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2663 | return TDK_InstantiationDepth; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2664 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2665 | if (CheckTemplateArgumentList(FunctionTemplate, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2666 | SourceLocation(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2667 | ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2668 | true, |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 2669 | Builder) || Trap.hasErrorOccurred()) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2670 | unsigned Index = Builder.size(); |
Douglas Gregor | 62c281a | 2010-05-09 01:26:06 +0000 | [diff] [blame] | 2671 | if (Index >= TemplateParams->size()) |
| 2672 | Index = TemplateParams->size() - 1; |
| 2673 | Info.Param = makeTemplateParameter(TemplateParams->getParam(Index)); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2674 | return TDK_InvalidExplicitArguments; |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 2675 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2676 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2677 | // Form the template argument list from the explicitly-specified |
| 2678 | // template arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2679 | TemplateArgumentList *ExplicitArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2680 | = TemplateArgumentList::CreateCopy(Context, Builder); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2681 | Info.reset(ExplicitArgumentList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2682 | |
John McCall | 036855a | 2010-10-12 19:40:14 +0000 | [diff] [blame] | 2683 | // Template argument deduction and the final substitution should be |
| 2684 | // done in the context of the templated declaration. Explicit |
| 2685 | // argument substitution, on the other hand, needs to happen in the |
| 2686 | // calling context. |
| 2687 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
| 2688 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2689 | // If we deduced template arguments for a template parameter pack, |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2690 | // note that the template argument pack is partially substituted and record |
| 2691 | // the explicit template arguments. They'll be used as part of deduction |
| 2692 | // for this template parameter pack. |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2693 | for (unsigned I = 0, N = Builder.size(); I != N; ++I) { |
| 2694 | const TemplateArgument &Arg = Builder[I]; |
| 2695 | if (Arg.getKind() == TemplateArgument::Pack) { |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2696 | CurrentInstantiationScope->SetPartiallySubstitutedPack( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2697 | TemplateParams->getParam(I), |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2698 | Arg.pack_begin(), |
| 2699 | Arg.pack_size()); |
| 2700 | break; |
| 2701 | } |
| 2702 | } |
| 2703 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2704 | const FunctionProtoType *Proto |
| 2705 | = Function->getType()->getAs<FunctionProtoType>(); |
| 2706 | assert(Proto && "Function template does not have a prototype?"); |
| 2707 | |
Richard Smith | 70b1304 | 2015-01-09 01:19:56 +0000 | [diff] [blame] | 2708 | // Isolate our substituted parameters from our caller. |
| 2709 | LocalInstantiationScope InstScope(*this, /*MergeWithOuterScope*/true); |
| 2710 | |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2711 | ExtParameterInfoBuilder ExtParamInfos; |
| 2712 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2713 | // Instantiate the types of each of the function parameters given the |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2714 | // explicitly-specified template arguments. If the function has a trailing |
| 2715 | // return type, substitute it after the arguments to ensure we substitute |
| 2716 | // in lexical order. |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2717 | if (Proto->hasTrailingReturn()) { |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2718 | if (SubstParmTypes(Function->getLocation(), Function->parameters(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2719 | Proto->getExtParameterInfosOrNull(), |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2720 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2721 | ParamTypes, /*params*/ nullptr, ExtParamInfos)) |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2722 | return TDK_SubstitutionFailure; |
| 2723 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2724 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2725 | // Instantiate the return type. |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2726 | QualType ResultType; |
| 2727 | { |
| 2728 | // C++11 [expr.prim.general]p3: |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2729 | // If a declaration declares a member function or member function |
| 2730 | // 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] | 2731 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2732 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2733 | // declarator. |
| 2734 | unsigned ThisTypeQuals = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2735 | CXXRecordDecl *ThisContext = nullptr; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2736 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
| 2737 | ThisContext = Method->getParent(); |
| 2738 | ThisTypeQuals = Method->getTypeQualifiers(); |
| 2739 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2740 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2741 | CXXThisScopeRAII ThisScope(*this, ThisContext, ThisTypeQuals, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2742 | getLangOpts().CPlusPlus11); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2743 | |
| 2744 | ResultType = |
| 2745 | SubstType(Proto->getReturnType(), |
| 2746 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 2747 | Function->getTypeSpecStartLoc(), Function->getDeclName()); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2748 | if (ResultType.isNull() || Trap.hasErrorOccurred()) |
| 2749 | return TDK_SubstitutionFailure; |
| 2750 | } |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2751 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2752 | // Instantiate the types of each of the function parameters given the |
| 2753 | // explicitly-specified template arguments if we didn't do so earlier. |
| 2754 | if (!Proto->hasTrailingReturn() && |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2755 | SubstParmTypes(Function->getLocation(), Function->parameters(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2756 | Proto->getExtParameterInfosOrNull(), |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2757 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2758 | ParamTypes, /*params*/ nullptr, ExtParamInfos)) |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2759 | return TDK_SubstitutionFailure; |
| 2760 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2761 | if (FunctionType) { |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2762 | auto EPI = Proto->getExtProtoInfo(); |
| 2763 | EPI.ExtParameterInfos = ExtParamInfos.getPointerOrNull(ParamTypes.size()); |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2764 | *FunctionType = BuildFunctionType(ResultType, ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2765 | Function->getLocation(), |
Eli Friedman | d8725a9 | 2010-08-05 02:54:05 +0000 | [diff] [blame] | 2766 | Function->getDeclName(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2767 | EPI); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2768 | if (FunctionType->isNull() || Trap.hasErrorOccurred()) |
| 2769 | return TDK_SubstitutionFailure; |
| 2770 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2771 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2772 | // C++ [temp.arg.explicit]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2773 | // Trailing template arguments that can be deduced (14.8.2) may be |
| 2774 | // omitted from the list of explicit template-arguments. If all of the |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2775 | // template arguments can be deduced, they may all be omitted; in this |
| 2776 | // case, the empty template argument list <> itself may also be omitted. |
| 2777 | // |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2778 | // Take all of the explicitly-specified arguments and put them into |
| 2779 | // the set of deduced template arguments. Explicitly-specified |
| 2780 | // parameter packs, however, will be set to NULL since the deduction |
| 2781 | // mechanisms handle explicitly-specified argument packs directly. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2782 | Deduced.reserve(TemplateParams->size()); |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2783 | for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) { |
| 2784 | const TemplateArgument &Arg = ExplicitArgumentList->get(I); |
| 2785 | if (Arg.getKind() == TemplateArgument::Pack) |
| 2786 | Deduced.push_back(DeducedTemplateArgument()); |
| 2787 | else |
| 2788 | Deduced.push_back(Arg); |
| 2789 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2790 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2791 | return TDK_Success; |
| 2792 | } |
| 2793 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2794 | /// \brief Check whether the deduced argument type for a call to a function |
| 2795 | /// template matches the actual argument type per C++ [temp.deduct.call]p4. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2796 | static bool |
| 2797 | CheckOriginalCallArgDeduction(Sema &S, Sema::OriginalCallArg OriginalArg, |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2798 | QualType DeducedA) { |
| 2799 | ASTContext &Context = S.Context; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2800 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2801 | QualType A = OriginalArg.OriginalArgType; |
| 2802 | QualType OriginalParamType = OriginalArg.OriginalParamType; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2803 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2804 | // Check for type equality (top-level cv-qualifiers are ignored). |
| 2805 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| 2806 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2807 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2808 | // Strip off references on the argument types; they aren't needed for |
| 2809 | // the following checks. |
| 2810 | if (const ReferenceType *DeducedARef = DeducedA->getAs<ReferenceType>()) |
| 2811 | DeducedA = DeducedARef->getPointeeType(); |
| 2812 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
| 2813 | A = ARef->getPointeeType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2814 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2815 | // C++ [temp.deduct.call]p4: |
| 2816 | // [...] However, there are three cases that allow a difference: |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2817 | // - If the original P is a reference type, the deduced A (i.e., the |
| 2818 | // type referred to by the reference) can be more cv-qualified than |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2819 | // the transformed A. |
| 2820 | if (const ReferenceType *OriginalParamRef |
| 2821 | = OriginalParamType->getAs<ReferenceType>()) { |
| 2822 | // We don't want to keep the reference around any more. |
| 2823 | OriginalParamType = OriginalParamRef->getPointeeType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2824 | |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 2825 | // FIXME: Resolve core issue (no number yet): if the original P is a |
| 2826 | // reference type and the transformed A is function type "noexcept F", |
| 2827 | // the deduced A can be F. |
| 2828 | QualType Tmp; |
| 2829 | if (A->isFunctionType() && S.IsFunctionConversion(A, DeducedA, Tmp)) |
| 2830 | return false; |
| 2831 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2832 | Qualifiers AQuals = A.getQualifiers(); |
| 2833 | Qualifiers DeducedAQuals = DeducedA.getQualifiers(); |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2834 | |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2835 | // Under Objective-C++ ARC, the deduced type may have implicitly |
| 2836 | // been given strong or (when dealing with a const reference) |
| 2837 | // unsafe_unretained lifetime. If so, update the original |
| 2838 | // qualifiers to include this lifetime. |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2839 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2840 | ((DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_Strong && |
| 2841 | AQuals.getObjCLifetime() == Qualifiers::OCL_None) || |
| 2842 | (DeducedAQuals.hasConst() && |
| 2843 | DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone))) { |
| 2844 | AQuals.setObjCLifetime(DeducedAQuals.getObjCLifetime()); |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2845 | } |
| 2846 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2847 | if (AQuals == DeducedAQuals) { |
| 2848 | // Qualifiers match; there's nothing to do. |
| 2849 | } else if (!DeducedAQuals.compatiblyIncludes(AQuals)) { |
Douglas Gregor | ddaae52 | 2011-06-17 14:36:00 +0000 | [diff] [blame] | 2850 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2851 | } else { |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2852 | // Qualifiers are compatible, so have the argument type adopt the |
| 2853 | // deduced argument type's qualifiers as if we had performed the |
| 2854 | // qualification conversion. |
| 2855 | A = Context.getQualifiedType(A.getUnqualifiedType(), DeducedAQuals); |
| 2856 | } |
| 2857 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2858 | |
| 2859 | // - The transformed A can be another pointer or pointer to member |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 2860 | // type that can be converted to the deduced A via a function pointer |
| 2861 | // conversion and/or a qualification conversion. |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2862 | // |
Richard Smith | 1be59c5 | 2016-10-22 01:32:19 +0000 | [diff] [blame] | 2863 | // Also allow conversions which merely strip __attribute__((noreturn)) from |
| 2864 | // function types (recursively). |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2865 | bool ObjCLifetimeConversion = false; |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2866 | QualType ResultTy; |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2867 | if ((A->isAnyPointerType() || A->isMemberPointerType()) && |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2868 | (S.IsQualificationConversion(A, DeducedA, false, |
| 2869 | ObjCLifetimeConversion) || |
Richard Smith | 3c4f8d2 | 2016-10-16 17:54:23 +0000 | [diff] [blame] | 2870 | S.IsFunctionConversion(A, DeducedA, ResultTy))) |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2871 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2872 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2873 | // - 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] | 2874 | // transformed A can be a derived class of the deduced A. [...] |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2875 | // [...] Likewise, if P is a pointer to a class of the form |
| 2876 | // simple-template-id, the transformed A can be a pointer to a |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2877 | // derived class pointed to by the deduced A. |
| 2878 | if (const PointerType *OriginalParamPtr |
| 2879 | = OriginalParamType->getAs<PointerType>()) { |
| 2880 | if (const PointerType *DeducedAPtr = DeducedA->getAs<PointerType>()) { |
| 2881 | if (const PointerType *APtr = A->getAs<PointerType>()) { |
| 2882 | if (A->getPointeeType()->isRecordType()) { |
| 2883 | OriginalParamType = OriginalParamPtr->getPointeeType(); |
| 2884 | DeducedA = DeducedAPtr->getPointeeType(); |
| 2885 | A = APtr->getPointeeType(); |
| 2886 | } |
| 2887 | } |
| 2888 | } |
| 2889 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2890 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2891 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| 2892 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2893 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2894 | if (A->isRecordType() && isSimpleTemplateIdType(OriginalParamType) && |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 2895 | S.IsDerivedFrom(SourceLocation(), A, DeducedA)) |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2896 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2897 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2898 | return true; |
| 2899 | } |
| 2900 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2901 | /// \brief Finish template argument deduction for a function template, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2902 | /// checking the deduced template arguments for completeness and forming |
| 2903 | /// the function template specialization. |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2904 | /// |
| 2905 | /// \param OriginalCallArgs If non-NULL, the original call arguments against |
| 2906 | /// which the deduced argument types should be compared. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2907 | Sema::TemplateDeductionResult |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2908 | Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2909 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2910 | unsigned NumExplicitlySpecified, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2911 | FunctionDecl *&Specialization, |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2912 | TemplateDeductionInfo &Info, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 2913 | SmallVectorImpl<OriginalCallArg> const *OriginalCallArgs, |
| 2914 | bool PartialOverloading) { |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2915 | // Unevaluated SFINAE context. |
| 2916 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2917 | SFINAETrap Trap(*this); |
| 2918 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2919 | // Enter a new template instantiation context while we instantiate the |
| 2920 | // actual function declaration. |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2921 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2922 | InstantiatingTemplate Inst(*this, Info.getLocation(), FunctionTemplate, |
| 2923 | DeducedArgs, |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2924 | ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution, |
| 2925 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2926 | if (Inst.isInvalid()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2927 | return TDK_InstantiationDepth; |
| 2928 | |
John McCall | e23b871 | 2010-04-29 01:18:58 +0000 | [diff] [blame] | 2929 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
John McCall | 80e58cd | 2010-04-29 00:35:03 +0000 | [diff] [blame] | 2930 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2931 | // C++ [temp.deduct.type]p2: |
| 2932 | // [...] or if any template argument remains neither deduced nor |
| 2933 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2934 | SmallVector<TemplateArgument, 4> Builder; |
Richard Smith | 1f5be4d | 2016-12-21 01:10:31 +0000 | [diff] [blame^] | 2935 | if (auto Result = ConvertDeducedTemplateArguments( |
| 2936 | *this, FunctionTemplate, Deduced, Info, Builder, |
| 2937 | CurrentInstantiationScope, NumExplicitlySpecified, |
| 2938 | PartialOverloading)) |
| 2939 | return Result; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2940 | |
| 2941 | // Form the template argument list from the deduced template arguments. |
| 2942 | TemplateArgumentList *DeducedArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2943 | = TemplateArgumentList::CreateCopy(Context, Builder); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2944 | Info.reset(DeducedArgumentList); |
| 2945 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2946 | // Substitute the deduced template arguments into the function template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2947 | // declaration to produce the function template specialization. |
Douglas Gregor | 1614237 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 2948 | DeclContext *Owner = FunctionTemplate->getDeclContext(); |
| 2949 | if (FunctionTemplate->getFriendObjectKind()) |
| 2950 | Owner = FunctionTemplate->getLexicalDeclContext(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2951 | Specialization = cast_or_null<FunctionDecl>( |
Douglas Gregor | 1614237 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 2952 | SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner, |
Douglas Gregor | 39cacdb | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 2953 | MultiLevelTemplateArgumentList(*DeducedArgumentList))); |
Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 2954 | if (!Specialization || Specialization->isInvalidDecl()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2955 | return TDK_SubstitutionFailure; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2956 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2957 | assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == |
Douglas Gregor | 31fae89 | 2009-09-15 18:26:13 +0000 | [diff] [blame] | 2958 | FunctionTemplate->getCanonicalDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2959 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2960 | // If the template argument list is owned by the function template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2961 | // specialization, release it. |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 2962 | if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList && |
| 2963 | !Trap.hasErrorOccurred()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2964 | Info.take(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2965 | |
Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 2966 | // There may have been an error that did not prevent us from constructing a |
| 2967 | // declaration. Mark the declaration invalid and return with a substitution |
| 2968 | // failure. |
| 2969 | if (Trap.hasErrorOccurred()) { |
| 2970 | Specialization->setInvalidDecl(true); |
| 2971 | return TDK_SubstitutionFailure; |
| 2972 | } |
| 2973 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2974 | if (OriginalCallArgs) { |
| 2975 | // C++ [temp.deduct.call]p4: |
| 2976 | // In general, the deduction process attempts to find template argument |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2977 | // 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] | 2978 | // is transformed as described above). [...] |
| 2979 | for (unsigned I = 0, N = OriginalCallArgs->size(); I != N; ++I) { |
| 2980 | OriginalCallArg OriginalArg = (*OriginalCallArgs)[I]; |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2981 | unsigned ParamIdx = OriginalArg.ArgIdx; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2982 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2983 | if (ParamIdx >= Specialization->getNumParams()) |
| 2984 | continue; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2985 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2986 | QualType DeducedA = Specialization->getParamDecl(ParamIdx)->getType(); |
Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 2987 | if (CheckOriginalCallArgDeduction(*this, OriginalArg, DeducedA)) { |
| 2988 | Info.FirstArg = TemplateArgument(DeducedA); |
| 2989 | Info.SecondArg = TemplateArgument(OriginalArg.OriginalArgType); |
| 2990 | Info.CallArgIndex = OriginalArg.ArgIdx; |
| 2991 | return TDK_DeducedMismatch; |
| 2992 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2993 | } |
| 2994 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2995 | |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2996 | // If we suppressed any diagnostics while performing template argument |
| 2997 | // deduction, and if we haven't already instantiated this declaration, |
| 2998 | // keep track of these diagnostics. They'll be emitted if this specialization |
| 2999 | // is actually used. |
| 3000 | if (Info.diag_begin() != Info.diag_end()) { |
Craig Topper | 79be4cd | 2013-07-05 04:33:53 +0000 | [diff] [blame] | 3001 | SuppressedDiagnosticsMap::iterator |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3002 | Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl()); |
| 3003 | if (Pos == SuppressedDiagnostics.end()) |
| 3004 | SuppressedDiagnostics[Specialization->getCanonicalDecl()] |
| 3005 | .append(Info.diag_begin(), Info.diag_end()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3006 | } |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 3007 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3008 | return TDK_Success; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3009 | } |
| 3010 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3011 | /// Gets the type of a function for template-argument-deducton |
| 3012 | /// purposes when it's considered as part of an overload set. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3013 | static QualType GetTypeOfFunction(Sema &S, const OverloadExpr::FindResult &R, |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3014 | FunctionDecl *Fn) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3015 | // We may need to deduce the return type of the function now. |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3016 | if (S.getLangOpts().CPlusPlus14 && Fn->getReturnType()->isUndeducedType() && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3017 | S.DeduceReturnType(Fn, R.Expression->getExprLoc(), /*Diagnose*/ false)) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3018 | return QualType(); |
| 3019 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3020 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3021 | if (Method->isInstance()) { |
| 3022 | // An instance method that's referenced in a form that doesn't |
| 3023 | // look like a member pointer is just invalid. |
| 3024 | if (!R.HasFormOfMemberPointer) return QualType(); |
| 3025 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3026 | return S.Context.getMemberPointerType(Fn->getType(), |
| 3027 | S.Context.getTypeDeclType(Method->getParent()).getTypePtr()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
| 3030 | if (!R.IsAddressOfOperand) return Fn->getType(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3031 | return S.Context.getPointerType(Fn->getType()); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3032 | } |
| 3033 | |
| 3034 | /// Apply the deduction rules for overload sets. |
| 3035 | /// |
| 3036 | /// \return the null type if this argument should be treated as an |
| 3037 | /// undeduced context |
| 3038 | static QualType |
| 3039 | ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams, |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3040 | Expr *Arg, QualType ParamType, |
| 3041 | bool ParamWasReference) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3042 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3043 | OverloadExpr::FindResult R = OverloadExpr::find(Arg); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3044 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3045 | OverloadExpr *Ovl = R.Expression; |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3046 | |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3047 | // C++0x [temp.deduct.call]p4 |
| 3048 | unsigned TDF = 0; |
| 3049 | if (ParamWasReference) |
| 3050 | TDF |= TDF_ParamWithReferenceType; |
| 3051 | if (R.IsAddressOfOperand) |
| 3052 | TDF |= TDF_IgnoreQualifiers; |
| 3053 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3054 | // C++0x [temp.deduct.call]p6: |
| 3055 | // When P is a function type, pointer to function type, or pointer |
| 3056 | // to member function type: |
| 3057 | |
| 3058 | if (!ParamType->isFunctionType() && |
| 3059 | !ParamType->isFunctionPointerType() && |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3060 | !ParamType->isMemberFunctionPointerType()) { |
| 3061 | if (Ovl->hasExplicitTemplateArgs()) { |
| 3062 | // But we can still look for an explicit specialization. |
| 3063 | if (FunctionDecl *ExplicitSpec |
| 3064 | = S.ResolveSingleFunctionTemplateSpecialization(Ovl)) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3065 | return GetTypeOfFunction(S, R, ExplicitSpec); |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3066 | } |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3067 | |
George Burgess IV | cc2f355 | 2016-03-19 21:51:45 +0000 | [diff] [blame] | 3068 | DeclAccessPair DAP; |
| 3069 | if (FunctionDecl *Viable = |
| 3070 | S.resolveAddressOfOnlyViableOverloadCandidate(Arg, DAP)) |
| 3071 | return GetTypeOfFunction(S, R, Viable); |
| 3072 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3073 | return QualType(); |
| 3074 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3075 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3076 | // Gather the explicit template arguments, if any. |
| 3077 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 3078 | if (Ovl->hasExplicitTemplateArgs()) |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 3079 | Ovl->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3080 | QualType Match; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 3081 | for (UnresolvedSetIterator I = Ovl->decls_begin(), |
| 3082 | E = Ovl->decls_end(); I != E; ++I) { |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3083 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 3084 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3085 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) { |
| 3086 | // - If the argument is an overload set containing one or more |
| 3087 | // function templates, the parameter is treated as a |
| 3088 | // non-deduced context. |
| 3089 | if (!Ovl->hasExplicitTemplateArgs()) |
| 3090 | return QualType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3091 | |
| 3092 | // Otherwise, see if we can resolve a function type |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3093 | FunctionDecl *Specialization = nullptr; |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3094 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3095 | if (S.DeduceTemplateArguments(FunTmpl, &ExplicitTemplateArgs, |
| 3096 | Specialization, Info)) |
| 3097 | continue; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3098 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3099 | D = Specialization; |
| 3100 | } |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3101 | |
| 3102 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3103 | QualType ArgType = GetTypeOfFunction(S, R, Fn); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3104 | if (ArgType.isNull()) continue; |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3105 | |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3106 | // Function-to-pointer conversion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3107 | if (!ParamWasReference && ParamType->isPointerType() && |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3108 | ArgType->isFunctionType()) |
| 3109 | ArgType = S.Context.getPointerType(ArgType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3110 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3111 | // - If the argument is an overload set (not containing function |
| 3112 | // templates), trial argument deduction is attempted using each |
| 3113 | // of the members of the set. If deduction succeeds for only one |
| 3114 | // of the overload set members, that member is used as the |
| 3115 | // argument value for the deduction. If deduction succeeds for |
| 3116 | // more than one member of the overload set the parameter is |
| 3117 | // treated as a non-deduced context. |
| 3118 | |
| 3119 | // We do all of this in a fresh context per C++0x [temp.deduct.type]p2: |
| 3120 | // Type deduction is done independently for each P/A pair, and |
| 3121 | // the deduced template argument values are then combined. |
| 3122 | // So we do not reject deductions which were made elsewhere. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3123 | SmallVector<DeducedTemplateArgument, 8> |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3124 | Deduced(TemplateParams->size()); |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3125 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3126 | Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3127 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
| 3128 | ArgType, Info, Deduced, TDF); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3129 | if (Result) continue; |
| 3130 | if (!Match.isNull()) return QualType(); |
| 3131 | Match = ArgType; |
| 3132 | } |
| 3133 | |
| 3134 | return Match; |
| 3135 | } |
| 3136 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3137 | /// \brief Perform the adjustments to the parameter and argument types |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3138 | /// described in C++ [temp.deduct.call]. |
| 3139 | /// |
| 3140 | /// \returns true if the caller should not attempt to perform any template |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3141 | /// argument deduction based on this P/A pair because the argument is an |
| 3142 | /// overloaded function set that could not be resolved. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3143 | static bool AdjustFunctionParmAndArgTypesForDeduction(Sema &S, |
| 3144 | TemplateParameterList *TemplateParams, |
| 3145 | QualType &ParamType, |
| 3146 | QualType &ArgType, |
| 3147 | Expr *Arg, |
| 3148 | unsigned &TDF) { |
| 3149 | // C++0x [temp.deduct.call]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3150 | // 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] | 3151 | // are ignored for type deduction. |
Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3152 | if (ParamType.hasQualifiers()) |
| 3153 | ParamType = ParamType.getUnqualifiedType(); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3154 | |
| 3155 | // [...] If P is a reference type, the type referred to by P is |
| 3156 | // used for type deduction. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3157 | const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>(); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3158 | if (ParamRefType) |
| 3159 | ParamType = ParamRefType->getPointeeType(); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3160 | |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3161 | // Overload sets usually make this parameter an undeduced context, |
| 3162 | // but there are sometimes special circumstances. Typically |
| 3163 | // involving a template-id-expr. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3164 | if (ArgType == S.Context.OverloadTy) { |
| 3165 | ArgType = ResolveOverloadForDeduction(S, TemplateParams, |
| 3166 | Arg, ParamType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3167 | ParamRefType != nullptr); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3168 | if (ArgType.isNull()) |
| 3169 | return true; |
| 3170 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3171 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3172 | if (ParamRefType) { |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3173 | // If the argument has incomplete array type, try to complete its type. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3174 | if (ArgType->isIncompleteArrayType()) { |
| 3175 | S.completeExprArrayBound(Arg); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3176 | ArgType = Arg->getType(); |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3177 | } |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3178 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3179 | // C++0x [temp.deduct.call]p3: |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3180 | // If P is an rvalue reference to a cv-unqualified template |
| 3181 | // parameter and the argument is an lvalue, the type "lvalue |
| 3182 | // reference to A" is used in place of A for type deduction. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3183 | if (ParamRefType->isRValueReferenceType() && |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3184 | !ParamType.getQualifiers() && |
| 3185 | isa<TemplateTypeParmType>(ParamType) && |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3186 | Arg->isLValue()) |
| 3187 | ArgType = S.Context.getLValueReferenceType(ArgType); |
| 3188 | } else { |
| 3189 | // C++ [temp.deduct.call]p2: |
| 3190 | // If P is not a reference type: |
| 3191 | // - If A is an array type, the pointer type produced by the |
| 3192 | // array-to-pointer standard conversion (4.2) is used in place of |
| 3193 | // A for type deduction; otherwise, |
| 3194 | if (ArgType->isArrayType()) |
| 3195 | ArgType = S.Context.getArrayDecayedType(ArgType); |
| 3196 | // - If A is a function type, the pointer type produced by the |
| 3197 | // function-to-pointer standard conversion (4.3) is used in place |
| 3198 | // of A for type deduction; otherwise, |
| 3199 | else if (ArgType->isFunctionType()) |
| 3200 | ArgType = S.Context.getPointerType(ArgType); |
| 3201 | else { |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3202 | // - 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] | 3203 | // type are ignored for type deduction. |
Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3204 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3205 | } |
| 3206 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3207 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3208 | // C++0x [temp.deduct.call]p4: |
| 3209 | // In general, the deduction process attempts to find template argument |
| 3210 | // values that will make the deduced A identical to A (after the type A |
| 3211 | // is transformed as described above). [...] |
| 3212 | TDF = TDF_SkipNonDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3213 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3214 | // - If the original P is a reference type, the deduced A (i.e., the |
| 3215 | // type referred to by the reference) can be more cv-qualified than |
| 3216 | // the transformed A. |
| 3217 | if (ParamRefType) |
| 3218 | TDF |= TDF_ParamWithReferenceType; |
| 3219 | // - The transformed A can be another pointer or pointer to member |
| 3220 | // type that can be converted to the deduced A via a qualification |
| 3221 | // conversion (4.4). |
| 3222 | if (ArgType->isPointerType() || ArgType->isMemberPointerType() || |
| 3223 | ArgType->isObjCObjectPointerType()) |
| 3224 | TDF |= TDF_IgnoreQualifiers; |
| 3225 | // - If P is a class and P has the form simple-template-id, then the |
| 3226 | // transformed A can be a derived class of the deduced A. Likewise, |
| 3227 | // if P is a pointer to a class of the form simple-template-id, the |
| 3228 | // transformed A can be a pointer to a derived class pointed to by |
| 3229 | // the deduced A. |
| 3230 | if (isSimpleTemplateIdType(ParamType) || |
| 3231 | (isa<PointerType>(ParamType) && |
| 3232 | isSimpleTemplateIdType( |
| 3233 | ParamType->getAs<PointerType>()->getPointeeType()))) |
| 3234 | TDF |= TDF_DerivedClass; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3235 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3236 | return false; |
| 3237 | } |
| 3238 | |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3239 | static bool |
| 3240 | hasDeducibleTemplateParameters(Sema &S, FunctionTemplateDecl *FunctionTemplate, |
| 3241 | QualType T); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3242 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3243 | static Sema::TemplateDeductionResult DeduceTemplateArgumentByListElement( |
| 3244 | Sema &S, TemplateParameterList *TemplateParams, QualType ParamType, |
| 3245 | Expr *Arg, TemplateDeductionInfo &Info, |
| 3246 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, unsigned TDF); |
| 3247 | |
| 3248 | /// \brief Attempt template argument deduction from an initializer list |
| 3249 | /// deemed to be an argument in a function call. |
| 3250 | static bool |
| 3251 | DeduceFromInitializerList(Sema &S, TemplateParameterList *TemplateParams, |
| 3252 | QualType AdjustedParamType, InitListExpr *ILE, |
| 3253 | TemplateDeductionInfo &Info, |
| 3254 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3255 | unsigned TDF, Sema::TemplateDeductionResult &Result) { |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3256 | |
| 3257 | // [temp.deduct.call] p1 (post CWG-1591) |
| 3258 | // If removing references and cv-qualifiers from P gives |
| 3259 | // std::initializer_list<P0> or P0[N] for some P0 and N and the argument is a |
| 3260 | // non-empty initializer list (8.5.4), then deduction is performed instead for |
| 3261 | // each element of the initializer list, taking P0 as a function template |
| 3262 | // parameter type and the initializer element as its argument, and in the |
| 3263 | // P0[N] case, if N is a non-type template parameter, N is deduced from the |
| 3264 | // length of the initializer list. Otherwise, an initializer list argument |
| 3265 | // causes the parameter to be considered a non-deduced context |
| 3266 | |
| 3267 | const bool IsConstSizedArray = AdjustedParamType->isConstantArrayType(); |
| 3268 | |
| 3269 | const bool IsDependentSizedArray = |
| 3270 | !IsConstSizedArray && AdjustedParamType->isDependentSizedArrayType(); |
| 3271 | |
Faisal Vali | dd76cc1 | 2015-12-10 12:29:11 +0000 | [diff] [blame] | 3272 | QualType ElTy; // The element type of the std::initializer_list or the array. |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3273 | |
| 3274 | const bool IsSTDList = !IsConstSizedArray && !IsDependentSizedArray && |
| 3275 | S.isStdInitializerList(AdjustedParamType, &ElTy); |
| 3276 | |
| 3277 | if (!IsConstSizedArray && !IsDependentSizedArray && !IsSTDList) |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3278 | return false; |
| 3279 | |
| 3280 | Result = Sema::TDK_Success; |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3281 | // If we are not deducing against the 'T' in a std::initializer_list<T> then |
| 3282 | // deduce against the 'T' in T[N]. |
| 3283 | if (ElTy.isNull()) { |
| 3284 | assert(!IsSTDList); |
| 3285 | ElTy = S.Context.getAsArrayType(AdjustedParamType)->getElementType(); |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3286 | } |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3287 | // Deduction only needs to be done for dependent types. |
| 3288 | if (ElTy->isDependentType()) { |
| 3289 | for (Expr *E : ILE->inits()) { |
Craig Topper | 0852953 | 2015-12-10 08:49:55 +0000 | [diff] [blame] | 3290 | if ((Result = DeduceTemplateArgumentByListElement(S, TemplateParams, ElTy, |
| 3291 | E, Info, Deduced, TDF))) |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3292 | return true; |
| 3293 | } |
| 3294 | } |
| 3295 | if (IsDependentSizedArray) { |
| 3296 | const DependentSizedArrayType *ArrTy = |
| 3297 | S.Context.getAsDependentSizedArrayType(AdjustedParamType); |
| 3298 | // Determine the array bound is something we can deduce. |
| 3299 | if (NonTypeTemplateParmDecl *NTTP = |
| 3300 | getDeducedParameterFromExpr(ArrTy->getSizeExpr())) { |
| 3301 | // We can perform template argument deduction for the given non-type |
| 3302 | // template parameter. |
| 3303 | assert(NTTP->getDepth() == 0 && |
| 3304 | "Cannot deduce non-type template argument at depth > 0"); |
| 3305 | llvm::APInt Size(S.Context.getIntWidth(NTTP->getType()), |
| 3306 | ILE->getNumInits()); |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3307 | |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3308 | Result = DeduceNonTypeTemplateArgument( |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 3309 | S, TemplateParams, NTTP, llvm::APSInt(Size), NTTP->getType(), |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3310 | /*ArrayBound=*/true, Info, Deduced); |
| 3311 | } |
| 3312 | } |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3313 | return true; |
| 3314 | } |
| 3315 | |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3316 | /// \brief Perform template argument deduction by matching a parameter type |
| 3317 | /// against a single expression, where the expression is an element of |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3318 | /// an initializer list that was originally matched against a parameter |
| 3319 | /// of type \c initializer_list\<ParamType\>. |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3320 | static Sema::TemplateDeductionResult |
| 3321 | DeduceTemplateArgumentByListElement(Sema &S, |
| 3322 | TemplateParameterList *TemplateParams, |
| 3323 | QualType ParamType, Expr *Arg, |
| 3324 | TemplateDeductionInfo &Info, |
| 3325 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3326 | unsigned TDF) { |
| 3327 | // Handle the case where an init list contains another init list as the |
| 3328 | // element. |
| 3329 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3330 | Sema::TemplateDeductionResult Result; |
| 3331 | if (!DeduceFromInitializerList(S, TemplateParams, |
| 3332 | ParamType.getNonReferenceType(), ILE, Info, |
| 3333 | Deduced, TDF, Result)) |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3334 | return Sema::TDK_Success; // Just ignore this expression. |
| 3335 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3336 | return Result; |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3337 | } |
| 3338 | |
| 3339 | // For all other cases, just match by type. |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3340 | QualType ArgType = Arg->getType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3341 | if (AdjustFunctionParmAndArgTypesForDeduction(S, TemplateParams, ParamType, |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3342 | ArgType, Arg, TDF)) { |
| 3343 | Info.Expression = Arg; |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3344 | return Sema::TDK_FailedOverloadResolution; |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3345 | } |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3346 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3347 | ArgType, Info, Deduced, TDF); |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3348 | } |
| 3349 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3350 | /// \brief Perform template argument deduction from a function call |
| 3351 | /// (C++ [temp.deduct.call]). |
| 3352 | /// |
| 3353 | /// \param FunctionTemplate the function template for which we are performing |
| 3354 | /// template argument deduction. |
| 3355 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3356 | /// \param ExplicitTemplateArgs the explicit template arguments provided |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 3357 | /// for this call. |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3358 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3359 | /// \param Args the function call arguments |
| 3360 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3361 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3362 | /// this will be set to the function template specialization produced by |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3363 | /// template argument deduction. |
| 3364 | /// |
| 3365 | /// \param Info the argument will be updated to provide additional information |
| 3366 | /// about template argument deduction. |
| 3367 | /// |
| 3368 | /// \returns the result of template argument deduction. |
Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 3369 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3370 | FunctionTemplateDecl *FunctionTemplate, |
| 3371 | TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3372 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 3373 | bool PartialOverloading) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3374 | if (FunctionTemplate->isInvalidDecl()) |
| 3375 | return TDK_Invalid; |
| 3376 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3377 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3378 | unsigned NumParams = Function->getNumParams(); |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3379 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3380 | // C++ [temp.deduct.call]p1: |
| 3381 | // Template argument deduction is done by comparing each function template |
| 3382 | // parameter type (call it P) with the type of the corresponding argument |
| 3383 | // of the call (call it A) as described below. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3384 | unsigned CheckArgs = Args.size(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3385 | if (Args.size() < Function->getMinRequiredArguments() && !PartialOverloading) |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3386 | return TDK_TooFewArguments; |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3387 | else if (TooManyArguments(NumParams, Args.size(), PartialOverloading)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3388 | const FunctionProtoType *Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3389 | = Function->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3390 | if (Proto->isTemplateVariadic()) |
| 3391 | /* Do nothing */; |
| 3392 | else if (Proto->isVariadic()) |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3393 | CheckArgs = NumParams; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3394 | else |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3395 | return TDK_TooManyArguments; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3396 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3397 | |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3398 | // The types of the parameters from which we will perform template argument |
| 3399 | // deduction. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3400 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3401 | TemplateParameterList *TemplateParams |
| 3402 | = FunctionTemplate->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3403 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 3404 | SmallVector<QualType, 4> ParamTypes; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3405 | unsigned NumExplicitlySpecified = 0; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3406 | if (ExplicitTemplateArgs) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3407 | TemplateDeductionResult Result = |
| 3408 | SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3409 | *ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3410 | Deduced, |
| 3411 | ParamTypes, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3412 | nullptr, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3413 | Info); |
| 3414 | if (Result) |
| 3415 | return Result; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3416 | |
| 3417 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3418 | } else { |
| 3419 | // Just fill in the parameter types from the function declaration. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3420 | for (unsigned I = 0; I != NumParams; ++I) |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3421 | ParamTypes.push_back(Function->getParamDecl(I)->getType()); |
| 3422 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3423 | |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3424 | // Deduce template arguments from the function parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3425 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3426 | unsigned ArgIdx = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3427 | SmallVector<OriginalCallArg, 4> OriginalCallArgs; |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3428 | for (unsigned ParamIdx = 0, NumParamTypes = ParamTypes.size(); |
| 3429 | ParamIdx != NumParamTypes; ++ParamIdx) { |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3430 | QualType OrigParamType = ParamTypes[ParamIdx]; |
| 3431 | QualType ParamType = OrigParamType; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3432 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3433 | const PackExpansionType *ParamExpansion |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3434 | = dyn_cast<PackExpansionType>(ParamType); |
| 3435 | if (!ParamExpansion) { |
| 3436 | // Simple case: matching a function parameter to a function argument. |
| 3437 | if (ArgIdx >= CheckArgs) |
| 3438 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3439 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3440 | Expr *Arg = Args[ArgIdx++]; |
| 3441 | QualType ArgType = Arg->getType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3442 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3443 | unsigned TDF = 0; |
| 3444 | if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams, |
| 3445 | ParamType, ArgType, Arg, |
| 3446 | TDF)) |
| 3447 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3448 | |
Douglas Gregor | 0c83c81 | 2011-10-09 22:06:46 +0000 | [diff] [blame] | 3449 | // If we have nothing to deduce, we're done. |
| 3450 | if (!hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
| 3451 | continue; |
| 3452 | |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3453 | // If the argument is an initializer list ... |
| 3454 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3455 | TemplateDeductionResult Result; |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3456 | // Removing references was already done. |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3457 | if (!DeduceFromInitializerList(*this, TemplateParams, ParamType, ILE, |
| 3458 | Info, Deduced, TDF, Result)) |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3459 | continue; |
| 3460 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3461 | if (Result) |
| 3462 | return Result; |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3463 | // Don't track the argument type, since an initializer list has none. |
| 3464 | continue; |
| 3465 | } |
| 3466 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3467 | // Keep track of the argument type and corresponding parameter index, |
| 3468 | // so we can check for compatibility between the deduced A and A. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3469 | OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx-1, |
Douglas Gregor | 0c83c81 | 2011-10-09 22:06:46 +0000 | [diff] [blame] | 3470 | ArgType)); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3471 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3472 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3473 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3474 | ParamType, ArgType, |
| 3475 | Info, Deduced, TDF)) |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3476 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3477 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3478 | continue; |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3479 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3480 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3481 | // C++0x [temp.deduct.call]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3482 | // For a function parameter pack that occurs at the end of the |
| 3483 | // parameter-declaration-list, the type A of each remaining argument of |
| 3484 | // the call is compared with the type P of the declarator-id of the |
| 3485 | // function parameter pack. Each comparison deduces template arguments |
| 3486 | // for subsequent positions in the template parameter packs expanded by |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3487 | // the function parameter pack. For a function parameter pack that does |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3488 | // not occur at the end of the parameter-declaration-list, the type of |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3489 | // the parameter pack is a non-deduced context. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3490 | if (ParamIdx + 1 < NumParamTypes) |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3491 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3492 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3493 | QualType ParamPattern = ParamExpansion->getPattern(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3494 | PackDeductionScope PackScope(*this, TemplateParams, Deduced, Info, |
| 3495 | ParamPattern); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3496 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3497 | bool HasAnyArguments = false; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3498 | for (; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3499 | HasAnyArguments = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3500 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3501 | QualType OrigParamType = ParamPattern; |
| 3502 | ParamType = OrigParamType; |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3503 | Expr *Arg = Args[ArgIdx]; |
| 3504 | QualType ArgType = Arg->getType(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3505 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3506 | unsigned TDF = 0; |
| 3507 | if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams, |
| 3508 | ParamType, ArgType, Arg, |
| 3509 | TDF)) { |
| 3510 | // We can't actually perform any deduction for this argument, so stop |
| 3511 | // deduction at this point. |
| 3512 | ++ArgIdx; |
| 3513 | break; |
| 3514 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3515 | |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3516 | // As above, initializer lists need special handling. |
| 3517 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3518 | TemplateDeductionResult Result; |
| 3519 | if (!DeduceFromInitializerList(*this, TemplateParams, ParamType, ILE, |
| 3520 | Info, Deduced, TDF, Result)) { |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3521 | ++ArgIdx; |
| 3522 | break; |
| 3523 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3524 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3525 | if (Result) |
| 3526 | return Result; |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3527 | } else { |
| 3528 | |
| 3529 | // Keep track of the argument type and corresponding argument index, |
| 3530 | // so we can check for compatibility between the deduced A and A. |
| 3531 | if (hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3532 | OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx, |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3533 | ArgType)); |
| 3534 | |
| 3535 | if (TemplateDeductionResult Result |
| 3536 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3537 | ParamType, ArgType, Info, |
| 3538 | Deduced, TDF)) |
| 3539 | return Result; |
| 3540 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3541 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3542 | PackScope.nextPackElement(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3543 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3544 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3545 | // Build argument packs for each of the parameter packs expanded by this |
| 3546 | // pack expansion. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3547 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3548 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3549 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3550 | // After we've matching against a parameter pack, we're done. |
| 3551 | break; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3552 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3553 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3554 | return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3555 | NumExplicitlySpecified, Specialization, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3556 | Info, &OriginalCallArgs, |
| 3557 | PartialOverloading); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3560 | QualType Sema::adjustCCAndNoReturn(QualType ArgFunctionType, |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3561 | QualType FunctionType, |
| 3562 | bool AdjustExceptionSpec) { |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3563 | if (ArgFunctionType.isNull()) |
| 3564 | return ArgFunctionType; |
| 3565 | |
| 3566 | const FunctionProtoType *FunctionTypeP = |
| 3567 | FunctionType->castAs<FunctionProtoType>(); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3568 | const FunctionProtoType *ArgFunctionTypeP = |
| 3569 | ArgFunctionType->getAs<FunctionProtoType>(); |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3570 | |
| 3571 | FunctionProtoType::ExtProtoInfo EPI = ArgFunctionTypeP->getExtProtoInfo(); |
| 3572 | bool Rebuild = false; |
| 3573 | |
| 3574 | CallingConv CC = FunctionTypeP->getCallConv(); |
| 3575 | if (EPI.ExtInfo.getCC() != CC) { |
| 3576 | EPI.ExtInfo = EPI.ExtInfo.withCallingConv(CC); |
| 3577 | Rebuild = true; |
| 3578 | } |
| 3579 | |
| 3580 | bool NoReturn = FunctionTypeP->getNoReturnAttr(); |
| 3581 | if (EPI.ExtInfo.getNoReturn() != NoReturn) { |
| 3582 | EPI.ExtInfo = EPI.ExtInfo.withNoReturn(NoReturn); |
| 3583 | Rebuild = true; |
| 3584 | } |
| 3585 | |
| 3586 | if (AdjustExceptionSpec && (FunctionTypeP->hasExceptionSpec() || |
| 3587 | ArgFunctionTypeP->hasExceptionSpec())) { |
| 3588 | EPI.ExceptionSpec = FunctionTypeP->getExtProtoInfo().ExceptionSpec; |
| 3589 | Rebuild = true; |
| 3590 | } |
| 3591 | |
| 3592 | if (!Rebuild) |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3593 | return ArgFunctionType; |
| 3594 | |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3595 | return Context.getFunctionType(ArgFunctionTypeP->getReturnType(), |
| 3596 | ArgFunctionTypeP->getParamTypes(), EPI); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3597 | } |
| 3598 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3599 | /// \brief Deduce template arguments when taking the address of a function |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3600 | /// template (C++ [temp.deduct.funcaddr]) or matching a specialization to |
| 3601 | /// a template. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3602 | /// |
| 3603 | /// \param FunctionTemplate the function template for which we are performing |
| 3604 | /// template argument deduction. |
| 3605 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3606 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3607 | /// arguments. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3608 | /// |
| 3609 | /// \param ArgFunctionType the function type that will be used as the |
| 3610 | /// "argument" type (A) when performing template argument deduction from the |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3611 | /// function template's function type. This type may be NULL, if there is no |
| 3612 | /// argument type to compare against, in C++0x [temp.arg.explicit]p3. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3613 | /// |
| 3614 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3615 | /// this will be set to the function template specialization produced by |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3616 | /// template argument deduction. |
| 3617 | /// |
| 3618 | /// \param Info the argument will be updated to provide additional information |
| 3619 | /// about template argument deduction. |
| 3620 | /// |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3621 | /// \param IsAddressOfFunction If \c true, we are deducing as part of taking |
| 3622 | /// the address of a function template per [temp.deduct.funcaddr] and |
| 3623 | /// [over.over]. If \c false, we are looking up a function template |
| 3624 | /// specialization based on its signature, per [temp.deduct.decl]. |
| 3625 | /// |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3626 | /// \returns the result of template argument deduction. |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3627 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3628 | FunctionTemplateDecl *FunctionTemplate, |
| 3629 | TemplateArgumentListInfo *ExplicitTemplateArgs, QualType ArgFunctionType, |
| 3630 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 3631 | bool IsAddressOfFunction) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3632 | if (FunctionTemplate->isInvalidDecl()) |
| 3633 | return TDK_Invalid; |
| 3634 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3635 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 3636 | TemplateParameterList *TemplateParams |
| 3637 | = FunctionTemplate->getTemplateParameters(); |
| 3638 | QualType FunctionType = Function->getType(); |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3639 | |
| 3640 | // When taking the address of a function, we require convertibility of |
| 3641 | // the resulting function type. Otherwise, we allow arbitrary mismatches |
| 3642 | // of calling convention, noreturn, and noexcept. |
| 3643 | if (!IsAddressOfFunction) |
| 3644 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, FunctionType, |
| 3645 | /*AdjustExceptionSpec*/true); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3646 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3647 | // Substitute any explicit template arguments. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3648 | LocalInstantiationScope InstScope(*this); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3649 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3650 | unsigned NumExplicitlySpecified = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3651 | SmallVector<QualType, 4> ParamTypes; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3652 | if (ExplicitTemplateArgs) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3653 | if (TemplateDeductionResult Result |
| 3654 | = SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3655 | *ExplicitTemplateArgs, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3656 | Deduced, ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3657 | &FunctionType, Info)) |
| 3658 | return Result; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3659 | |
| 3660 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3661 | } |
| 3662 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3663 | // Unevaluated SFINAE context. |
| 3664 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3665 | SFINAETrap Trap(*this); |
| 3666 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3667 | Deduced.resize(TemplateParams->size()); |
| 3668 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3669 | // 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] | 3670 | // type so that we treat it as a non-deduced context in what follows. If we |
| 3671 | // are looking up by signature, the signature type should also have a deduced |
| 3672 | // return type, which we instead expect to exactly match. |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3673 | bool HasDeducedReturnType = false; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3674 | if (getLangOpts().CPlusPlus14 && IsAddressOfFunction && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3675 | Function->getReturnType()->getContainedAutoType()) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3676 | FunctionType = SubstAutoType(FunctionType, Context.DependentTy); |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3677 | HasDeducedReturnType = true; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3678 | } |
| 3679 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3680 | if (!ArgFunctionType.isNull()) { |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3681 | unsigned TDF = TDF_TopLevelParameterTypeList; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3682 | if (IsAddressOfFunction) |
| 3683 | TDF |= TDF_InOverloadResolution; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3684 | // Deduce template arguments from the function type. |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3685 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3686 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3687 | FunctionType, ArgFunctionType, |
| 3688 | Info, Deduced, TDF)) |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3689 | return Result; |
| 3690 | } |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3691 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3692 | if (TemplateDeductionResult Result |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3693 | = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
| 3694 | NumExplicitlySpecified, |
| 3695 | Specialization, Info)) |
| 3696 | return Result; |
| 3697 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3698 | // If the function has a deduced return type, deduce it now, so we can check |
| 3699 | // that the deduced function type matches the requested type. |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3700 | if (HasDeducedReturnType && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3701 | Specialization->getReturnType()->isUndeducedType() && |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3702 | DeduceReturnType(Specialization, Info.getLocation(), false)) |
| 3703 | return TDK_MiscellaneousDeductionFailure; |
| 3704 | |
Richard Smith | 9095e5b | 2016-11-01 01:31:23 +0000 | [diff] [blame] | 3705 | // If the function has a dependent exception specification, resolve it now, |
| 3706 | // so we can check that the exception specification matches. |
| 3707 | auto *SpecializationFPT = |
| 3708 | Specialization->getType()->castAs<FunctionProtoType>(); |
| 3709 | if (getLangOpts().CPlusPlus1z && |
| 3710 | isUnresolvedExceptionSpec(SpecializationFPT->getExceptionSpecType()) && |
| 3711 | !ResolveExceptionSpec(Info.getLocation(), SpecializationFPT)) |
| 3712 | return TDK_MiscellaneousDeductionFailure; |
| 3713 | |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3714 | // Adjust the exception specification of the argument again to match the |
| 3715 | // substituted and resolved type we just formed. (Calling convention and |
| 3716 | // noreturn can't be dependent, so we don't actually need this for them |
| 3717 | // right now.) |
| 3718 | QualType SpecializationType = Specialization->getType(); |
| 3719 | if (!IsAddressOfFunction) |
| 3720 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, SpecializationType, |
| 3721 | /*AdjustExceptionSpec*/true); |
| 3722 | |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3723 | // 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] | 3724 | // specialization with respect to arguments of compatible pointer to function |
| 3725 | // types, template argument deduction fails. |
| 3726 | if (!ArgFunctionType.isNull()) { |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3727 | if (IsAddressOfFunction && |
| 3728 | !isSameOrCompatibleFunctionType( |
| 3729 | Context.getCanonicalType(SpecializationType), |
| 3730 | Context.getCanonicalType(ArgFunctionType))) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3731 | return TDK_MiscellaneousDeductionFailure; |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 3732 | |
| 3733 | if (!IsAddressOfFunction && |
| 3734 | !Context.hasSameType(SpecializationType, ArgFunctionType)) |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3735 | return TDK_MiscellaneousDeductionFailure; |
| 3736 | } |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3737 | |
| 3738 | return TDK_Success; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3739 | } |
| 3740 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3741 | /// \brief Given a function declaration (e.g. a generic lambda conversion |
| 3742 | /// function) that contains an 'auto' in its result type, substitute it |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3743 | /// with TypeToReplaceAutoWith. Be careful to pass in the type you want |
| 3744 | /// to replace 'auto' with and not the actual result type you want |
| 3745 | /// to set the function to. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3746 | static inline void |
| 3747 | SubstAutoWithinFunctionReturnType(FunctionDecl *F, |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3748 | QualType TypeToReplaceAutoWith, Sema &S) { |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3749 | assert(!TypeToReplaceAutoWith->getContainedAutoType()); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3750 | QualType AutoResultType = F->getReturnType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3751 | assert(AutoResultType->getContainedAutoType()); |
| 3752 | QualType DeducedResultType = S.SubstAutoType(AutoResultType, |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3753 | TypeToReplaceAutoWith); |
| 3754 | S.Context.adjustDeducedFunctionResultType(F, DeducedResultType); |
| 3755 | } |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3756 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3757 | /// \brief Given a specialized conversion operator of a generic lambda |
| 3758 | /// create the corresponding specializations of the call operator and |
| 3759 | /// the static-invoker. If the return type of the call operator is auto, |
| 3760 | /// deduce its return type and check if that matches the |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3761 | /// return type of the destination function ptr. |
| 3762 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3763 | static inline Sema::TemplateDeductionResult |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3764 | SpecializeCorrespondingLambdaCallOperatorAndInvoker( |
| 3765 | CXXConversionDecl *ConversionSpecialized, |
| 3766 | SmallVectorImpl<DeducedTemplateArgument> &DeducedArguments, |
| 3767 | QualType ReturnTypeOfDestFunctionPtr, |
| 3768 | TemplateDeductionInfo &TDInfo, |
| 3769 | Sema &S) { |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3770 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3771 | CXXRecordDecl *LambdaClass = ConversionSpecialized->getParent(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3772 | assert(LambdaClass && LambdaClass->isGenericLambda()); |
| 3773 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3774 | CXXMethodDecl *CallOpGeneric = LambdaClass->getLambdaCallOperator(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3775 | QualType CallOpResultType = CallOpGeneric->getReturnType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3776 | const bool GenericLambdaCallOperatorHasDeducedReturnType = |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3777 | CallOpResultType->getContainedAutoType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3778 | |
| 3779 | FunctionTemplateDecl *CallOpTemplate = |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3780 | CallOpGeneric->getDescribedFunctionTemplate(); |
| 3781 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3782 | FunctionDecl *CallOpSpecialized = nullptr; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3783 | // Use the deduced arguments of the conversion function, to specialize our |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3784 | // generic lambda's call operator. |
| 3785 | if (Sema::TemplateDeductionResult Result |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3786 | = S.FinishTemplateArgumentDeduction(CallOpTemplate, |
| 3787 | DeducedArguments, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3788 | 0, CallOpSpecialized, TDInfo)) |
| 3789 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3790 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3791 | // If we need to deduce the return type, do so (instantiates the callop). |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3792 | if (GenericLambdaCallOperatorHasDeducedReturnType && |
| 3793 | CallOpSpecialized->getReturnType()->isUndeducedType()) |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3794 | S.DeduceReturnType(CallOpSpecialized, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3795 | CallOpSpecialized->getPointOfInstantiation(), |
| 3796 | /*Diagnose*/ true); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3797 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3798 | // Check to see if the return type of the destination ptr-to-function |
| 3799 | // matches the return type of the call operator. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3800 | if (!S.Context.hasSameType(CallOpSpecialized->getReturnType(), |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3801 | ReturnTypeOfDestFunctionPtr)) |
| 3802 | return Sema::TDK_NonDeducedMismatch; |
| 3803 | // Since we have succeeded in matching the source and destination |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3804 | // ptr-to-functions (now including return type), and have successfully |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3805 | // specialized our corresponding call operator, we are ready to |
| 3806 | // specialize the static invoker with the deduced arguments of our |
| 3807 | // ptr-to-function. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3808 | FunctionDecl *InvokerSpecialized = nullptr; |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3809 | FunctionTemplateDecl *InvokerTemplate = LambdaClass-> |
| 3810 | getLambdaStaticInvoker()->getDescribedFunctionTemplate(); |
| 3811 | |
Yaron Keren | f428fcf | 2015-05-13 17:56:46 +0000 | [diff] [blame] | 3812 | #ifndef NDEBUG |
| 3813 | Sema::TemplateDeductionResult LLVM_ATTRIBUTE_UNUSED Result = |
| 3814 | #endif |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3815 | S.FinishTemplateArgumentDeduction(InvokerTemplate, DeducedArguments, 0, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3816 | InvokerSpecialized, TDInfo); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3817 | assert(Result == Sema::TDK_Success && |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3818 | "If the call operator succeeded so should the invoker!"); |
| 3819 | // Set the result type to match the corresponding call operator |
| 3820 | // specialization's result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3821 | if (GenericLambdaCallOperatorHasDeducedReturnType && |
| 3822 | InvokerSpecialized->getReturnType()->isUndeducedType()) { |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3823 | // Be sure to get the type to replace 'auto' with and not |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3824 | // the full result type of the call op specialization |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3825 | // to substitute into the 'auto' of the invoker and conversion |
| 3826 | // function. |
| 3827 | // For e.g. |
| 3828 | // int* (*fp)(int*) = [](auto* a) -> auto* { return a; }; |
| 3829 | // We don't want to subst 'int*' into 'auto' to get int**. |
| 3830 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3831 | QualType TypeToReplaceAutoWith = CallOpSpecialized->getReturnType() |
| 3832 | ->getContainedAutoType() |
| 3833 | ->getDeducedType(); |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3834 | SubstAutoWithinFunctionReturnType(InvokerSpecialized, |
| 3835 | TypeToReplaceAutoWith, S); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3836 | SubstAutoWithinFunctionReturnType(ConversionSpecialized, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3837 | TypeToReplaceAutoWith, S); |
| 3838 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3839 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3840 | // Ensure that static invoker doesn't have a const qualifier. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3841 | // FIXME: When creating the InvokerTemplate in SemaLambda.cpp |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3842 | // do not use the CallOperator's TypeSourceInfo which allows |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3843 | // the const qualifier to leak through. |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3844 | const FunctionProtoType *InvokerFPT = InvokerSpecialized-> |
| 3845 | getType().getTypePtr()->castAs<FunctionProtoType>(); |
| 3846 | FunctionProtoType::ExtProtoInfo EPI = InvokerFPT->getExtProtoInfo(); |
| 3847 | EPI.TypeQuals = 0; |
| 3848 | InvokerSpecialized->setType(S.Context.getFunctionType( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3849 | InvokerFPT->getReturnType(), InvokerFPT->getParamTypes(), EPI)); |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3850 | return Sema::TDK_Success; |
| 3851 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3852 | /// \brief Deduce template arguments for a templated conversion |
| 3853 | /// function (C++ [temp.deduct.conv]) and, if successful, produce a |
| 3854 | /// conversion function template specialization. |
| 3855 | Sema::TemplateDeductionResult |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3856 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *ConversionTemplate, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3857 | QualType ToType, |
| 3858 | CXXConversionDecl *&Specialization, |
| 3859 | TemplateDeductionInfo &Info) { |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3860 | if (ConversionTemplate->isInvalidDecl()) |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3861 | return TDK_Invalid; |
| 3862 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3863 | CXXConversionDecl *ConversionGeneric |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3864 | = cast<CXXConversionDecl>(ConversionTemplate->getTemplatedDecl()); |
| 3865 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3866 | QualType FromType = ConversionGeneric->getConversionType(); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3867 | |
| 3868 | // Canonicalize the types for deduction. |
| 3869 | QualType P = Context.getCanonicalType(FromType); |
| 3870 | QualType A = Context.getCanonicalType(ToType); |
| 3871 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3872 | // C++0x [temp.deduct.conv]p2: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3873 | // If P is a reference type, the type referred to by P is used for |
| 3874 | // type deduction. |
| 3875 | if (const ReferenceType *PRef = P->getAs<ReferenceType>()) |
| 3876 | P = PRef->getPointeeType(); |
| 3877 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3878 | // C++0x [temp.deduct.conv]p4: |
| 3879 | // [...] 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] | 3880 | // for type deduction. |
| 3881 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3882 | A = ARef->getPointeeType().getUnqualifiedType(); |
| 3883 | // C++ [temp.deduct.conv]p3: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3884 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3885 | // If A is not a reference type: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3886 | else { |
| 3887 | assert(!A->isReferenceType() && "Reference types were handled above"); |
| 3888 | |
| 3889 | // - If P is an array type, the pointer type produced by the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3890 | // array-to-pointer standard conversion (4.2) is used in place |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3891 | // of P for type deduction; otherwise, |
| 3892 | if (P->isArrayType()) |
| 3893 | P = Context.getArrayDecayedType(P); |
| 3894 | // - If P is a function type, the pointer type produced by the |
| 3895 | // function-to-pointer standard conversion (4.3) is used in |
| 3896 | // place of P for type deduction; otherwise, |
| 3897 | else if (P->isFunctionType()) |
| 3898 | P = Context.getPointerType(P); |
| 3899 | // - 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] | 3900 | // P's type are ignored for type deduction. |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3901 | else |
| 3902 | P = P.getUnqualifiedType(); |
| 3903 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3904 | // C++0x [temp.deduct.conv]p4: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3905 | // 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] | 3906 | // 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] | 3907 | // referred to by A is used for type deduction. |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3908 | A = A.getUnqualifiedType(); |
| 3909 | } |
| 3910 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3911 | // Unevaluated SFINAE context. |
| 3912 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3913 | SFINAETrap Trap(*this); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3914 | |
| 3915 | // C++ [temp.deduct.conv]p1: |
| 3916 | // Template argument deduction is done by comparing the return |
| 3917 | // type of the template conversion function (call it P) with the |
| 3918 | // type that is required as the result of the conversion (call it |
| 3919 | // A) as described in 14.8.2.4. |
| 3920 | TemplateParameterList *TemplateParams |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3921 | = ConversionTemplate->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3922 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3923 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3924 | |
| 3925 | // C++0x [temp.deduct.conv]p4: |
| 3926 | // In general, the deduction process attempts to find template |
| 3927 | // argument values that will make the deduced A identical to |
| 3928 | // A. However, there are two cases that allow a difference: |
| 3929 | unsigned TDF = 0; |
| 3930 | // - If the original A is a reference type, A can be more |
| 3931 | // cv-qualified than the deduced A (i.e., the type referred to |
| 3932 | // by the reference) |
| 3933 | if (ToType->isReferenceType()) |
| 3934 | TDF |= TDF_ParamWithReferenceType; |
| 3935 | // - The deduced A can be another pointer or pointer to member |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3936 | // type that can be converted to A via a qualification |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3937 | // conversion. |
| 3938 | // |
| 3939 | // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when |
| 3940 | // both P and A are pointers or member pointers. In this case, we |
| 3941 | // just ignore cv-qualifiers completely). |
| 3942 | if ((P->isPointerType() && A->isPointerType()) || |
Douglas Gregor | 9f05ed5 | 2011-08-30 00:37:54 +0000 | [diff] [blame] | 3943 | (P->isMemberPointerType() && A->isMemberPointerType())) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3944 | TDF |= TDF_IgnoreQualifiers; |
| 3945 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3946 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3947 | P, A, Info, Deduced, TDF)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3948 | return Result; |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3949 | |
| 3950 | // Create an Instantiation Scope for finalizing the operator. |
| 3951 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3952 | // Finish template argument deduction. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3953 | FunctionDecl *ConversionSpecialized = nullptr; |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3954 | TemplateDeductionResult Result |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3955 | = FinishTemplateArgumentDeduction(ConversionTemplate, Deduced, 0, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3956 | ConversionSpecialized, Info); |
| 3957 | Specialization = cast_or_null<CXXConversionDecl>(ConversionSpecialized); |
| 3958 | |
| 3959 | // If the conversion operator is being invoked on a lambda closure to convert |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3960 | // to a ptr-to-function, use the deduced arguments from the conversion |
| 3961 | // function to specialize the corresponding call operator. |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3962 | // e.g., int (*fp)(int) = [](auto a) { return a; }; |
| 3963 | if (Result == TDK_Success && isLambdaConversionOperator(ConversionGeneric)) { |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3964 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3965 | // Get the return type of the destination ptr-to-function we are converting |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3966 | // to. This is necessary for matching the lambda call operator's return |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3967 | // type to that of the destination ptr-to-function's return type. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3968 | assert(A->isPointerType() && |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3969 | "Can only convert from lambda to ptr-to-function"); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3970 | const FunctionType *ToFunType = |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3971 | A->getPointeeType().getTypePtr()->getAs<FunctionType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3972 | const QualType DestFunctionPtrReturnType = ToFunType->getReturnType(); |
| 3973 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3974 | // Create the corresponding specializations of the call operator and |
| 3975 | // the static-invoker; and if the return type is auto, |
| 3976 | // deduce the return type and check if it matches the |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3977 | // DestFunctionPtrReturnType. |
| 3978 | // For instance: |
| 3979 | // auto L = [](auto a) { return f(a); }; |
| 3980 | // int (*fp)(int) = L; |
| 3981 | // char (*fp2)(int) = L; <-- Not OK. |
| 3982 | |
| 3983 | Result = SpecializeCorrespondingLambdaCallOperatorAndInvoker( |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3984 | Specialization, Deduced, DestFunctionPtrReturnType, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3985 | Info, *this); |
| 3986 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3987 | return Result; |
| 3988 | } |
| 3989 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3990 | /// \brief Deduce template arguments for a function template when there is |
| 3991 | /// nothing to deduce against (C++0x [temp.arg.explicit]p3). |
| 3992 | /// |
| 3993 | /// \param FunctionTemplate the function template for which we are performing |
| 3994 | /// template argument deduction. |
| 3995 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3996 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3997 | /// arguments. |
| 3998 | /// |
| 3999 | /// \param Specialization if template argument deduction was successful, |
| 4000 | /// this will be set to the function template specialization produced by |
| 4001 | /// template argument deduction. |
| 4002 | /// |
| 4003 | /// \param Info the argument will be updated to provide additional information |
| 4004 | /// about template argument deduction. |
| 4005 | /// |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4006 | /// \param IsAddressOfFunction If \c true, we are deducing as part of taking |
| 4007 | /// the address of a function template in a context where we do not have a |
| 4008 | /// target type, per [over.over]. If \c false, we are looking up a function |
| 4009 | /// template specialization based on its signature, which only happens when |
| 4010 | /// deducing a function parameter type from an argument that is a template-id |
| 4011 | /// naming a function template specialization. |
| 4012 | /// |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4013 | /// \returns the result of template argument deduction. |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4014 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 4015 | FunctionTemplateDecl *FunctionTemplate, |
| 4016 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 4017 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 4018 | bool IsAddressOfFunction) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4019 | return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 4020 | QualType(), Specialization, Info, |
Richard Smith | baa4783 | 2016-12-01 02:11:49 +0000 | [diff] [blame] | 4021 | IsAddressOfFunction); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 4022 | } |
| 4023 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4024 | namespace { |
| 4025 | /// Substitute the 'auto' type specifier within a type for a given replacement |
| 4026 | /// type. |
| 4027 | class SubstituteAutoTransform : |
| 4028 | public TreeTransform<SubstituteAutoTransform> { |
| 4029 | QualType Replacement; |
| 4030 | public: |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 4031 | SubstituteAutoTransform(Sema &SemaRef, QualType Replacement) |
| 4032 | : TreeTransform<SubstituteAutoTransform>(SemaRef), |
| 4033 | Replacement(Replacement) {} |
| 4034 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4035 | QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) { |
| 4036 | // If we're building the type pattern to deduce against, don't wrap the |
| 4037 | // substituted type in an AutoType. Certain template deduction rules |
| 4038 | // apply only when a template type parameter appears directly (and not if |
| 4039 | // the parameter is found through desugaring). For instance: |
| 4040 | // auto &&lref = lvalue; |
| 4041 | // must transform into "rvalue reference to T" not "rvalue reference to |
| 4042 | // auto type deduced as T" in order for [temp.deduct.call]p3 to apply. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4043 | if (!Replacement.isNull() && isa<TemplateTypeParmType>(Replacement)) { |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4044 | QualType Result = Replacement; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4045 | TemplateTypeParmTypeLoc NewTL = |
| 4046 | TLB.push<TemplateTypeParmTypeLoc>(Result); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4047 | NewTL.setNameLoc(TL.getNameLoc()); |
| 4048 | return Result; |
| 4049 | } else { |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 4050 | bool Dependent = |
| 4051 | !Replacement.isNull() && Replacement->isDependentType(); |
| 4052 | QualType Result = |
| 4053 | SemaRef.Context.getAutoType(Dependent ? QualType() : Replacement, |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4054 | TL.getTypePtr()->getKeyword(), |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 4055 | Dependent); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4056 | AutoTypeLoc NewTL = TLB.push<AutoTypeLoc>(Result); |
| 4057 | NewTL.setNameLoc(TL.getNameLoc()); |
| 4058 | return Result; |
| 4059 | } |
| 4060 | } |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 4061 | |
| 4062 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 4063 | // Lambdas never need to be transformed. |
| 4064 | return E; |
| 4065 | } |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4066 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4067 | QualType Apply(TypeLoc TL) { |
| 4068 | // Create some scratch storage for the transformed type locations. |
| 4069 | // FIXME: We're just going to throw this information away. Don't build it. |
| 4070 | TypeLocBuilder TLB; |
| 4071 | TLB.reserve(TL.getFullDataSize()); |
| 4072 | return TransformType(TLB, TL); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4073 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4074 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 4075 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4076 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4077 | Sema::DeduceAutoResult |
| 4078 | Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *&Init, QualType &Result) { |
| 4079 | return DeduceAutoType(Type->getTypeLoc(), Init, Result); |
| 4080 | } |
| 4081 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4082 | /// \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] | 4083 | /// |
| 4084 | /// \param Type the type pattern using the auto type-specifier. |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4085 | /// \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] | 4086 | /// \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] | 4087 | /// deduced type. |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4088 | Sema::DeduceAutoResult |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4089 | Sema::DeduceAutoType(TypeLoc Type, Expr *&Init, QualType &Result) { |
John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4090 | if (Init->getType()->isNonOverloadPlaceholderType()) { |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4091 | ExprResult NonPlaceholder = CheckPlaceholderExpr(Init); |
| 4092 | if (NonPlaceholder.isInvalid()) |
| 4093 | return DAR_FailedAlreadyDiagnosed; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4094 | Init = NonPlaceholder.get(); |
John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4097 | if (Init->isTypeDependent() || Type.getType()->isDependentType()) { |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4098 | Result = SubstituteAutoTransform(*this, Context.DependentTy).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4099 | assert(!Result.isNull() && "substituting DependentTy can't fail"); |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4100 | return DAR_Succeeded; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4101 | } |
| 4102 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4103 | // If this is a 'decltype(auto)' specifier, do the decltype dance. |
| 4104 | // Since 'decltype(auto)' can only occur at the top of the type, we |
| 4105 | // don't need to go digging for it. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4106 | if (const AutoType *AT = Type.getType()->getAs<AutoType>()) { |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4107 | if (AT->isDecltypeAuto()) { |
| 4108 | if (isa<InitListExpr>(Init)) { |
| 4109 | Diag(Init->getLocStart(), diag::err_decltype_auto_initializer_list); |
| 4110 | return DAR_FailedAlreadyDiagnosed; |
| 4111 | } |
| 4112 | |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 4113 | QualType Deduced = BuildDecltypeType(Init, Init->getLocStart(), false); |
David Majnemer | 3c20ab2 | 2015-07-01 00:29:28 +0000 | [diff] [blame] | 4114 | if (Deduced.isNull()) |
| 4115 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4116 | // FIXME: Support a non-canonical deduced type for 'auto'. |
| 4117 | Deduced = Context.getCanonicalType(Deduced); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4118 | Result = SubstituteAutoTransform(*this, Deduced).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4119 | if (Result.isNull()) |
| 4120 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4121 | return DAR_Succeeded; |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4122 | } else if (!getLangOpts().CPlusPlus) { |
| 4123 | if (isa<InitListExpr>(Init)) { |
| 4124 | Diag(Init->getLocStart(), diag::err_auto_init_list_from_c); |
| 4125 | return DAR_FailedAlreadyDiagnosed; |
| 4126 | } |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4127 | } |
| 4128 | } |
| 4129 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4130 | SourceLocation Loc = Init->getExprLoc(); |
| 4131 | |
| 4132 | LocalInstantiationScope InstScope(*this); |
| 4133 | |
| 4134 | // Build template<class TemplParam> void Func(FuncParam); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4135 | TemplateTypeParmDecl *TemplParam = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4136 | TemplateTypeParmDecl::Create(Context, nullptr, SourceLocation(), Loc, 0, 0, |
| 4137 | nullptr, false, false); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4138 | QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0); |
| 4139 | NamedDecl *TemplParamPtr = TemplParam; |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 4140 | FixedSizeTemplateParameterListStorage<1, false> TemplateParamsSt( |
| 4141 | Loc, Loc, TemplParamPtr, Loc, nullptr); |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 4142 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4143 | QualType FuncParam = SubstituteAutoTransform(*this, TemplArg).Apply(Type); |
| 4144 | assert(!FuncParam.isNull() && |
| 4145 | "substituting template parameter for 'auto' failed"); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4146 | |
| 4147 | // Deduce type of TemplParam in Func(Init) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4148 | SmallVector<DeducedTemplateArgument, 1> Deduced; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4149 | Deduced.resize(1); |
| 4150 | QualType InitType = Init->getType(); |
| 4151 | unsigned TDF = 0; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4152 | |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4153 | TemplateDeductionInfo Info(Loc); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4154 | |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 4155 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4156 | if (InitList) { |
| 4157 | for (unsigned i = 0, e = InitList->getNumInits(); i < e; ++i) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4158 | if (DeduceTemplateArgumentByListElement(*this, TemplateParamsSt.get(), |
| 4159 | TemplArg, InitList->getInit(i), |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 4160 | Info, Deduced, TDF)) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4161 | return DAR_Failed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4162 | } |
| 4163 | } else { |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4164 | if (!getLangOpts().CPlusPlus && Init->refersToBitField()) { |
| 4165 | Diag(Loc, diag::err_auto_bitfield); |
| 4166 | return DAR_FailedAlreadyDiagnosed; |
| 4167 | } |
| 4168 | |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4169 | if (AdjustFunctionParmAndArgTypesForDeduction( |
| 4170 | *this, TemplateParamsSt.get(), FuncParam, InitType, Init, TDF)) |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 4171 | return DAR_Failed; |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 4172 | |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4173 | if (DeduceTemplateArgumentsByTypeMatch(*this, TemplateParamsSt.get(), |
| 4174 | FuncParam, InitType, Info, Deduced, |
| 4175 | TDF)) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4176 | return DAR_Failed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4177 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4178 | |
Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4179 | if (Deduced[0].getKind() != TemplateArgument::Type) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4180 | return DAR_Failed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4181 | |
Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4182 | QualType DeducedType = Deduced[0].getAsType(); |
| 4183 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4184 | if (InitList) { |
| 4185 | DeducedType = BuildStdInitializerList(DeducedType, Loc); |
| 4186 | if (DeducedType.isNull()) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4187 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4188 | } |
| 4189 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4190 | Result = SubstituteAutoTransform(*this, DeducedType).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4191 | if (Result.isNull()) |
| 4192 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4193 | |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4194 | // Check that the deduced argument type is compatible with the original |
| 4195 | // argument type per C++ [temp.deduct.call]p4. |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4196 | if (!InitList && !Result.isNull() && |
| 4197 | CheckOriginalCallArgDeduction(*this, |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4198 | Sema::OriginalCallArg(FuncParam,0,InitType), |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4199 | Result)) { |
| 4200 | Result = QualType(); |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4201 | return DAR_Failed; |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4202 | } |
| 4203 | |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4204 | return DAR_Succeeded; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4205 | } |
| 4206 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 4207 | QualType Sema::SubstAutoType(QualType TypeWithAuto, |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4208 | QualType TypeToReplaceAuto) { |
| 4209 | return SubstituteAutoTransform(*this, TypeToReplaceAuto). |
| 4210 | TransformType(TypeWithAuto); |
| 4211 | } |
| 4212 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 4213 | TypeSourceInfo* Sema::SubstAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto, |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4214 | QualType TypeToReplaceAuto) { |
| 4215 | return SubstituteAutoTransform(*this, TypeToReplaceAuto). |
| 4216 | TransformType(TypeWithAuto); |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 4217 | } |
| 4218 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4219 | void Sema::DiagnoseAutoDeductionFailure(VarDecl *VDecl, Expr *Init) { |
| 4220 | if (isa<InitListExpr>(Init)) |
| 4221 | Diag(VDecl->getLocation(), |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4222 | VDecl->isInitCapture() |
| 4223 | ? diag::err_init_capture_deduction_failure_from_init_list |
| 4224 | : diag::err_auto_var_deduction_failure_from_init_list) |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4225 | << VDecl->getDeclName() << VDecl->getType() << Init->getSourceRange(); |
| 4226 | else |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4227 | Diag(VDecl->getLocation(), |
| 4228 | VDecl->isInitCapture() ? diag::err_init_capture_deduction_failure |
| 4229 | : diag::err_auto_var_deduction_failure) |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4230 | << VDecl->getDeclName() << VDecl->getType() << Init->getType() |
| 4231 | << Init->getSourceRange(); |
| 4232 | } |
| 4233 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4234 | bool Sema::DeduceReturnType(FunctionDecl *FD, SourceLocation Loc, |
| 4235 | bool Diagnose) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4236 | assert(FD->getReturnType()->isUndeducedType()); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4237 | |
| 4238 | if (FD->getTemplateInstantiationPattern()) |
| 4239 | InstantiateFunctionDefinition(Loc, FD); |
| 4240 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4241 | bool StillUndeduced = FD->getReturnType()->isUndeducedType(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4242 | if (StillUndeduced && Diagnose && !FD->isInvalidDecl()) { |
| 4243 | Diag(Loc, diag::err_auto_fn_used_before_defined) << FD; |
| 4244 | Diag(FD->getLocation(), diag::note_callee_decl) << FD; |
| 4245 | } |
| 4246 | |
| 4247 | return StillUndeduced; |
| 4248 | } |
| 4249 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4250 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4251 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4252 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4253 | unsigned Level, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4254 | llvm::SmallBitVector &Deduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4255 | |
| 4256 | /// \brief If this is a non-static member function, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 4257 | static void |
| 4258 | AddImplicitObjectParameterType(ASTContext &Context, |
| 4259 | CXXMethodDecl *Method, |
| 4260 | SmallVectorImpl<QualType> &ArgTypes) { |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4261 | // C++11 [temp.func.order]p3: |
| 4262 | // [...] The new parameter is of type "reference to cv A," where cv are |
| 4263 | // the cv-qualifiers of the function template (if any) and A is |
| 4264 | // the class of which the function template is a member. |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4265 | // |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4266 | // The standard doesn't say explicitly, but we pick the appropriate kind of |
| 4267 | // reference type based on [over.match.funcs]p4. |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4268 | QualType ArgTy = Context.getTypeDeclType(Method->getParent()); |
| 4269 | ArgTy = Context.getQualifiedType(ArgTy, |
| 4270 | Qualifiers::fromCVRMask(Method->getTypeQualifiers())); |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4271 | if (Method->getRefQualifier() == RQ_RValue) |
| 4272 | ArgTy = Context.getRValueReferenceType(ArgTy); |
| 4273 | else |
| 4274 | ArgTy = Context.getLValueReferenceType(ArgTy); |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4275 | ArgTypes.push_back(ArgTy); |
| 4276 | } |
| 4277 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4278 | /// \brief Determine whether the function template \p FT1 is at least as |
| 4279 | /// specialized as \p FT2. |
| 4280 | static bool isAtLeastAsSpecializedAs(Sema &S, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4281 | SourceLocation Loc, |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4282 | FunctionTemplateDecl *FT1, |
| 4283 | FunctionTemplateDecl *FT2, |
| 4284 | TemplatePartialOrderingContext TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4285 | unsigned NumCallArguments1) { |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4286 | FunctionDecl *FD1 = FT1->getTemplatedDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4287 | FunctionDecl *FD2 = FT2->getTemplatedDecl(); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4288 | const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); |
| 4289 | const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4290 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4291 | assert(Proto1 && Proto2 && "Function templates must have prototypes"); |
| 4292 | TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4293 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4294 | Deduced.resize(TemplateParams->size()); |
| 4295 | |
| 4296 | // C++0x [temp.deduct.partial]p3: |
| 4297 | // The types used to determine the ordering depend on the context in which |
| 4298 | // the partial ordering is done: |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4299 | TemplateDeductionInfo Info(Loc); |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4300 | SmallVector<QualType, 4> Args2; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4301 | switch (TPOC) { |
| 4302 | case TPOC_Call: { |
| 4303 | // - In the context of a function call, the function parameter types are |
| 4304 | // used. |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4305 | CXXMethodDecl *Method1 = dyn_cast<CXXMethodDecl>(FD1); |
| 4306 | CXXMethodDecl *Method2 = dyn_cast<CXXMethodDecl>(FD2); |
Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4307 | |
Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4308 | // C++11 [temp.func.order]p3: |
Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4309 | // [...] If only one of the function templates is a non-static |
| 4310 | // member, that function template is considered to have a new |
| 4311 | // first parameter inserted in its function parameter list. The |
| 4312 | // new parameter is of type "reference to cv A," where cv are |
| 4313 | // the cv-qualifiers of the function template (if any) and A is |
| 4314 | // the class of which the function template is a member. |
| 4315 | // |
Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4316 | // Note that we interpret this to mean "if one of the function |
| 4317 | // templates is a non-static member and the other is a non-member"; |
| 4318 | // otherwise, the ordering rules for static functions against non-static |
| 4319 | // functions don't make any sense. |
| 4320 | // |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4321 | // C++98/03 doesn't have this provision but we've extended DR532 to cover |
| 4322 | // it as wording was broken prior to it. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4323 | SmallVector<QualType, 4> Args1; |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4324 | |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4325 | unsigned NumComparedArguments = NumCallArguments1; |
| 4326 | |
| 4327 | if (!Method2 && Method1 && !Method1->isStatic()) { |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4328 | // Compare 'this' from Method1 against first parameter from Method2. |
| 4329 | AddImplicitObjectParameterType(S.Context, Method1, Args1); |
| 4330 | ++NumComparedArguments; |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4331 | } else if (!Method1 && Method2 && !Method2->isStatic()) { |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4332 | // Compare 'this' from Method2 against first parameter from Method1. |
| 4333 | AddImplicitObjectParameterType(S.Context, Method2, Args2); |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4334 | } |
| 4335 | |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4336 | Args1.insert(Args1.end(), Proto1->param_type_begin(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4337 | Proto1->param_type_end()); |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4338 | Args2.insert(Args2.end(), Proto2->param_type_begin(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4339 | Proto2->param_type_end()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4340 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4341 | // C++ [temp.func.order]p5: |
| 4342 | // The presence of unused ellipsis and default arguments has no effect on |
| 4343 | // the partial ordering of function templates. |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4344 | if (Args1.size() > NumComparedArguments) |
| 4345 | Args1.resize(NumComparedArguments); |
| 4346 | if (Args2.size() > NumComparedArguments) |
| 4347 | Args2.resize(NumComparedArguments); |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4348 | if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(), |
| 4349 | Args1.data(), Args1.size(), Info, Deduced, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4350 | TDF_None, /*PartialOrdering=*/true)) |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 4351 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4352 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4353 | break; |
| 4354 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4355 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4356 | case TPOC_Conversion: |
| 4357 | // - In the context of a call to a conversion operator, the return types |
| 4358 | // of the conversion function templates are used. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4359 | if (DeduceTemplateArgumentsByTypeMatch( |
| 4360 | S, TemplateParams, Proto2->getReturnType(), Proto1->getReturnType(), |
| 4361 | Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4362 | /*PartialOrdering=*/true)) |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4363 | return false; |
| 4364 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4365 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4366 | case TPOC_Other: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4367 | // - 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] | 4368 | // is used. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4369 | if (DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 4370 | FD2->getType(), FD1->getType(), |
| 4371 | Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4372 | /*PartialOrdering=*/true)) |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4373 | return false; |
| 4374 | break; |
| 4375 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4376 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4377 | // C++0x [temp.deduct.partial]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4378 | // In most cases, all template parameters must have values in order for |
| 4379 | // deduction to succeed, but for partial ordering purposes a template |
| 4380 | // 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] | 4381 | // types being used for partial ordering. [ Note: a template parameter used |
| 4382 | // in a non-deduced context is considered used. -end note] |
| 4383 | unsigned ArgIdx = 0, NumArgs = Deduced.size(); |
| 4384 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4385 | if (Deduced[ArgIdx].isNull()) |
| 4386 | break; |
| 4387 | |
| 4388 | if (ArgIdx == NumArgs) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4389 | // All template arguments were deduced. FT1 is at least as specialized |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4390 | // as FT2. |
| 4391 | return true; |
| 4392 | } |
| 4393 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4394 | // Figure out which template parameters were used. |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4395 | llvm::SmallBitVector UsedParameters(TemplateParams->size()); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4396 | switch (TPOC) { |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4397 | case TPOC_Call: |
| 4398 | for (unsigned I = 0, N = Args2.size(); I != N; ++I) |
| 4399 | ::MarkUsedTemplateParameters(S.Context, Args2[I], false, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4400 | TemplateParams->getDepth(), |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4401 | UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4402 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4403 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4404 | case TPOC_Conversion: |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4405 | ::MarkUsedTemplateParameters(S.Context, Proto2->getReturnType(), false, |
| 4406 | TemplateParams->getDepth(), UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4407 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4408 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4409 | case TPOC_Other: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4410 | ::MarkUsedTemplateParameters(S.Context, FD2->getType(), false, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4411 | TemplateParams->getDepth(), |
| 4412 | UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4413 | break; |
| 4414 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4415 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4416 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4417 | // If this argument had no value deduced but was used in one of the types |
| 4418 | // used for partial ordering, then deduction fails. |
| 4419 | if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) |
| 4420 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4421 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4422 | return true; |
| 4423 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4424 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4425 | /// \brief Determine whether this a function template whose parameter-type-list |
| 4426 | /// ends with a function parameter pack. |
| 4427 | static bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) { |
| 4428 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
| 4429 | unsigned NumParams = Function->getNumParams(); |
| 4430 | if (NumParams == 0) |
| 4431 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4432 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4433 | ParmVarDecl *Last = Function->getParamDecl(NumParams - 1); |
| 4434 | if (!Last->isParameterPack()) |
| 4435 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4436 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4437 | // Make sure that no previous parameter is a parameter pack. |
| 4438 | while (--NumParams > 0) { |
| 4439 | if (Function->getParamDecl(NumParams - 1)->isParameterPack()) |
| 4440 | return false; |
| 4441 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4442 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4443 | return true; |
| 4444 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4445 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4446 | /// \brief Returns the more specialized function template according |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4447 | /// to the rules of function template partial ordering (C++ [temp.func.order]). |
| 4448 | /// |
| 4449 | /// \param FT1 the first function template |
| 4450 | /// |
| 4451 | /// \param FT2 the second function template |
| 4452 | /// |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4453 | /// \param TPOC the context in which we are performing partial ordering of |
| 4454 | /// function templates. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4455 | /// |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4456 | /// \param NumCallArguments1 The number of arguments in the call to FT1, used |
| 4457 | /// only when \c TPOC is \c TPOC_Call. |
| 4458 | /// |
| 4459 | /// \param NumCallArguments2 The number of arguments in the call to FT2, used |
| 4460 | /// only when \c TPOC is \c TPOC_Call. |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4461 | /// |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4462 | /// \returns the more specialized function template. If neither |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4463 | /// template is more specialized, returns NULL. |
| 4464 | FunctionTemplateDecl * |
| 4465 | Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, |
| 4466 | FunctionTemplateDecl *FT2, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4467 | SourceLocation Loc, |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4468 | TemplatePartialOrderingContext TPOC, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4469 | unsigned NumCallArguments1, |
| 4470 | unsigned NumCallArguments2) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4471 | bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4472 | NumCallArguments1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4473 | bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4474 | NumCallArguments2); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4475 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4476 | if (Better1 != Better2) // We have a clear winner |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4477 | return Better1 ? FT1 : FT2; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4478 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4479 | if (!Better1 && !Better2) // Neither is better than the other |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4480 | return nullptr; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4481 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4482 | // FIXME: This mimics what GCC implements, but doesn't match up with the |
| 4483 | // proposed resolution for core issue 692. This area needs to be sorted out, |
| 4484 | // but for now we attempt to maintain compatibility. |
| 4485 | bool Variadic1 = isVariadicFunctionTemplate(FT1); |
| 4486 | bool Variadic2 = isVariadicFunctionTemplate(FT2); |
| 4487 | if (Variadic1 != Variadic2) |
| 4488 | return Variadic1? FT2 : FT1; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4489 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4490 | return nullptr; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4491 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4492 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4493 | /// \brief Determine if the two templates are equivalent. |
| 4494 | static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) { |
| 4495 | if (T1 == T2) |
| 4496 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4497 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4498 | if (!T1 || !T2) |
| 4499 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4500 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4501 | return T1->getCanonicalDecl() == T2->getCanonicalDecl(); |
| 4502 | } |
| 4503 | |
| 4504 | /// \brief Retrieve the most specialized of the given function template |
| 4505 | /// specializations. |
| 4506 | /// |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4507 | /// \param SpecBegin the start iterator of the function template |
| 4508 | /// specializations that we will be comparing. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4509 | /// |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4510 | /// \param SpecEnd the end iterator of the function template |
| 4511 | /// specializations, paired with \p SpecBegin. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4512 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4513 | /// \param Loc the location where the ambiguity or no-specializations |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4514 | /// diagnostic should occur. |
| 4515 | /// |
| 4516 | /// \param NoneDiag partial diagnostic used to diagnose cases where there are |
| 4517 | /// no matching candidates. |
| 4518 | /// |
| 4519 | /// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one |
| 4520 | /// occurs. |
| 4521 | /// |
| 4522 | /// \param CandidateDiag partial diagnostic used for each function template |
| 4523 | /// specialization that is a candidate in the ambiguous ordering. One parameter |
| 4524 | /// in this diagnostic should be unbound, which will correspond to the string |
| 4525 | /// describing the template arguments for the function template specialization. |
| 4526 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4527 | /// \returns the most specialized function template specialization, if |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4528 | /// found. Otherwise, returns SpecEnd. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4529 | UnresolvedSetIterator Sema::getMostSpecialized( |
| 4530 | UnresolvedSetIterator SpecBegin, UnresolvedSetIterator SpecEnd, |
| 4531 | TemplateSpecCandidateSet &FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4532 | SourceLocation Loc, const PartialDiagnostic &NoneDiag, |
| 4533 | const PartialDiagnostic &AmbigDiag, const PartialDiagnostic &CandidateDiag, |
| 4534 | bool Complain, QualType TargetType) { |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4535 | if (SpecBegin == SpecEnd) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4536 | if (Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4537 | Diag(Loc, NoneDiag); |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4538 | FailedCandidates.NoteCandidates(*this, Loc); |
| 4539 | } |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4540 | return SpecEnd; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4541 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4542 | |
| 4543 | if (SpecBegin + 1 == SpecEnd) |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4544 | return SpecBegin; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4545 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4546 | // Find the function template that is better than all of the templates it |
| 4547 | // has been compared to. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4548 | UnresolvedSetIterator Best = SpecBegin; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4549 | FunctionTemplateDecl *BestTemplate |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4550 | = cast<FunctionDecl>(*Best)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4551 | assert(BestTemplate && "Not a function template specialization?"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4552 | for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) { |
| 4553 | FunctionTemplateDecl *Challenger |
| 4554 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4555 | assert(Challenger && "Not a function template specialization?"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4556 | if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4557 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4558 | Challenger)) { |
| 4559 | Best = I; |
| 4560 | BestTemplate = Challenger; |
| 4561 | } |
| 4562 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4563 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4564 | // Make sure that the "best" function template is more specialized than all |
| 4565 | // of the others. |
| 4566 | bool Ambiguous = false; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4567 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4568 | FunctionTemplateDecl *Challenger |
| 4569 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4570 | if (I != Best && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4571 | !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4572 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4573 | BestTemplate)) { |
| 4574 | Ambiguous = true; |
| 4575 | break; |
| 4576 | } |
| 4577 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4578 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4579 | if (!Ambiguous) { |
| 4580 | // We found an answer. Return it. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4581 | return Best; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4582 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4583 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4584 | // Diagnose the ambiguity. |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4585 | if (Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4586 | Diag(Loc, AmbigDiag); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4587 | |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4588 | // FIXME: Can we order the candidates in some sane way? |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4589 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4590 | PartialDiagnostic PD = CandidateDiag; |
| 4591 | PD << getTemplateArgumentBindingsText( |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4592 | cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(), |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4593 | *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs()); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4594 | if (!TargetType.isNull()) |
| 4595 | HandleFunctionTypeMismatch(PD, cast<FunctionDecl>(*I)->getType(), |
| 4596 | TargetType); |
| 4597 | Diag((*I)->getLocation(), PD); |
| 4598 | } |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4599 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4600 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4601 | return SpecEnd; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4602 | } |
| 4603 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4604 | /// \brief Returns the more specialized class template partial specialization |
| 4605 | /// according to the rules of partial ordering of class template partial |
| 4606 | /// specializations (C++ [temp.class.order]). |
| 4607 | /// |
| 4608 | /// \param PS1 the first class template partial specialization |
| 4609 | /// |
| 4610 | /// \param PS2 the second class template partial specialization |
| 4611 | /// |
| 4612 | /// \returns the more specialized class template partial specialization. If |
| 4613 | /// neither partial specialization is more specialized, returns NULL. |
| 4614 | ClassTemplatePartialSpecializationDecl * |
| 4615 | Sema::getMoreSpecializedPartialSpecialization( |
| 4616 | ClassTemplatePartialSpecializationDecl *PS1, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4617 | ClassTemplatePartialSpecializationDecl *PS2, |
| 4618 | SourceLocation Loc) { |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4619 | // C++ [temp.class.order]p1: |
| 4620 | // For two class template partial specializations, the first is at least as |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4621 | // specialized as the second if, given the following rewrite to two |
| 4622 | // function templates, the first function template is at least as |
| 4623 | // specialized as the second according to the ordering rules for function |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4624 | // templates (14.6.6.2): |
| 4625 | // - the first function template has the same template parameters as the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4626 | // first partial specialization and has a single function parameter |
| 4627 | // whose type is a class template specialization with the template |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4628 | // arguments of the first partial specialization, and |
| 4629 | // - the second function template has the same template parameters as the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4630 | // second partial specialization and has a single function parameter |
| 4631 | // whose type is a class template specialization with the template |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4632 | // arguments of the second partial specialization. |
| 4633 | // |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 4634 | // Rather than synthesize function templates, we merely perform the |
| 4635 | // equivalent partial ordering by performing deduction directly on |
| 4636 | // the template arguments of the class template partial |
| 4637 | // specializations. This computation is slightly simpler than the |
| 4638 | // general problem of function template partial ordering, because |
| 4639 | // class template partial specializations are more constrained. We |
| 4640 | // know that every template parameter is deducible from the class |
| 4641 | // template partial specialization's template arguments, for |
| 4642 | // example. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4643 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4644 | TemplateDeductionInfo Info(Loc); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4645 | |
| 4646 | QualType PT1 = PS1->getInjectedSpecializationType(); |
| 4647 | QualType PT2 = PS2->getInjectedSpecializationType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4648 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4649 | // Determine whether PS1 is at least as specialized as PS2 |
| 4650 | Deduced.resize(PS2->getTemplateParameters()->size()); |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4651 | bool Better1 = !DeduceTemplateArgumentsByTypeMatch(*this, |
| 4652 | PS2->getTemplateParameters(), |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4653 | PT2, PT1, Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4654 | /*PartialOrdering=*/true); |
Argyrios Kyrtzidis | 7d39155 | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4655 | if (Better1) { |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 4656 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(),Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 4657 | InstantiatingTemplate Inst(*this, Loc, PS2, DeducedArgs, Info); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4658 | Better1 = !::FinishTemplateArgumentDeduction( |
| 4659 | *this, PS2, PS1->getTemplateArgs(), Deduced, Info); |
| 4660 | } |
| 4661 | |
| 4662 | // Determine whether PS2 is at least as specialized as PS1 |
| 4663 | Deduced.clear(); |
| 4664 | Deduced.resize(PS1->getTemplateParameters()->size()); |
| 4665 | bool Better2 = !DeduceTemplateArgumentsByTypeMatch( |
| 4666 | *this, PS1->getTemplateParameters(), PT1, PT2, Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4667 | /*PartialOrdering=*/true); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4668 | if (Better2) { |
| 4669 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), |
| 4670 | Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 4671 | InstantiatingTemplate Inst(*this, Loc, PS1, DeducedArgs, Info); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4672 | Better2 = !::FinishTemplateArgumentDeduction( |
| 4673 | *this, PS1, PS2->getTemplateArgs(), Deduced, Info); |
| 4674 | } |
| 4675 | |
| 4676 | if (Better1 == Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4677 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4678 | |
| 4679 | return Better1 ? PS1 : PS2; |
| 4680 | } |
| 4681 | |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 4682 | /// TODO: Unify with ClassTemplatePartialSpecializationDecl version? |
| 4683 | /// May require unifying ClassTemplate(Partial)SpecializationDecl and |
| 4684 | /// VarTemplate(Partial)SpecializationDecl with a new data |
| 4685 | /// structure Template(Partial)SpecializationDecl, and |
| 4686 | /// using Template(Partial)SpecializationDecl as input type. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4687 | VarTemplatePartialSpecializationDecl * |
| 4688 | Sema::getMoreSpecializedPartialSpecialization( |
| 4689 | VarTemplatePartialSpecializationDecl *PS1, |
| 4690 | VarTemplatePartialSpecializationDecl *PS2, SourceLocation Loc) { |
| 4691 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 4692 | TemplateDeductionInfo Info(Loc); |
| 4693 | |
Richard Smith | f04fd0b | 2013-12-12 23:14:16 +0000 | [diff] [blame] | 4694 | assert(PS1->getSpecializedTemplate() == PS2->getSpecializedTemplate() && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4695 | "the partial specializations being compared should specialize" |
| 4696 | " the same template."); |
| 4697 | TemplateName Name(PS1->getSpecializedTemplate()); |
| 4698 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4699 | QualType PT1 = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4700 | CanonTemplate, PS1->getTemplateArgs().asArray()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4701 | QualType PT2 = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4702 | CanonTemplate, PS2->getTemplateArgs().asArray()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4703 | |
| 4704 | // Determine whether PS1 is at least as specialized as PS2 |
| 4705 | Deduced.resize(PS2->getTemplateParameters()->size()); |
| 4706 | bool Better1 = !DeduceTemplateArgumentsByTypeMatch( |
| 4707 | *this, PS2->getTemplateParameters(), PT2, PT1, Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4708 | /*PartialOrdering=*/true); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4709 | if (Better1) { |
| 4710 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), |
| 4711 | Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 4712 | InstantiatingTemplate Inst(*this, Loc, PS2, DeducedArgs, Info); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4713 | Better1 = !::FinishTemplateArgumentDeduction(*this, PS2, |
| 4714 | PS1->getTemplateArgs(), |
Douglas Gregor | 9225b02 | 2010-04-29 06:31:36 +0000 | [diff] [blame] | 4715 | Deduced, Info); |
Argyrios Kyrtzidis | 7d39155 | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4716 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4717 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4718 | // Determine whether PS2 is at least as specialized as PS1 |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 4719 | Deduced.clear(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4720 | Deduced.resize(PS1->getTemplateParameters()->size()); |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4721 | bool Better2 = !DeduceTemplateArgumentsByTypeMatch(*this, |
| 4722 | PS1->getTemplateParameters(), |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4723 | PT1, PT2, Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4724 | /*PartialOrdering=*/true); |
Argyrios Kyrtzidis | 7d39155 | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4725 | if (Better2) { |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 4726 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(),Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 4727 | InstantiatingTemplate Inst(*this, Loc, PS1, DeducedArgs, Info); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4728 | Better2 = !::FinishTemplateArgumentDeduction(*this, PS1, |
| 4729 | PS2->getTemplateArgs(), |
Douglas Gregor | 9225b02 | 2010-04-29 06:31:36 +0000 | [diff] [blame] | 4730 | Deduced, Info); |
Argyrios Kyrtzidis | 7d39155 | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4731 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4732 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4733 | if (Better1 == Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4734 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4735 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4736 | return Better1? PS1 : PS2; |
| 4737 | } |
| 4738 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4739 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4740 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4741 | const TemplateArgument &TemplateArg, |
| 4742 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4743 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4744 | llvm::SmallBitVector &Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4745 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4746 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4747 | /// expression. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4748 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4749 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4750 | const Expr *E, |
| 4751 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4752 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4753 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 4754 | // We can deduce from a pack expansion. |
| 4755 | if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E)) |
| 4756 | E = Expansion->getPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4757 | |
Richard Smith | 3434900 | 2012-07-09 03:07:20 +0000 | [diff] [blame] | 4758 | // Skip through any implicit casts we added while type-checking, and any |
| 4759 | // substitutions performed by template alias expansion. |
| 4760 | while (1) { |
| 4761 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 4762 | E = ICE->getSubExpr(); |
| 4763 | else if (const SubstNonTypeTemplateParmExpr *Subst = |
| 4764 | dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 4765 | E = Subst->getReplacement(); |
| 4766 | else |
| 4767 | break; |
| 4768 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4769 | |
| 4770 | // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4771 | // find other occurrences of template parameters. |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4772 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
Douglas Gregor | 04b1152 | 2010-01-14 18:13:22 +0000 | [diff] [blame] | 4773 | if (!DRE) |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4774 | return; |
| 4775 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4776 | const NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4777 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 4778 | if (!NTTP) |
| 4779 | return; |
| 4780 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4781 | if (NTTP->getDepth() == Depth) |
| 4782 | Used[NTTP->getIndex()] = true; |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 4783 | |
| 4784 | // In C++1z mode, additional arguments may be deduced from the type of a |
| 4785 | // non-type argument. |
| 4786 | if (Ctx.getLangOpts().CPlusPlus1z) |
| 4787 | MarkUsedTemplateParameters(Ctx, NTTP->getType(), OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4788 | } |
| 4789 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4790 | /// \brief Mark the template parameters that are used by the given |
| 4791 | /// nested name specifier. |
| 4792 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4793 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4794 | NestedNameSpecifier *NNS, |
| 4795 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4796 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4797 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4798 | if (!NNS) |
| 4799 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4800 | |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4801 | MarkUsedTemplateParameters(Ctx, NNS->getPrefix(), OnlyDeduced, Depth, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4802 | Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4803 | MarkUsedTemplateParameters(Ctx, QualType(NNS->getAsType(), 0), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4804 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4805 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4806 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4807 | /// \brief Mark the template parameters that are used by the given |
| 4808 | /// template name. |
| 4809 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4810 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4811 | TemplateName Name, |
| 4812 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4813 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4814 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4815 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 4816 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4817 | = dyn_cast<TemplateTemplateParmDecl>(Template)) { |
| 4818 | if (TTP->getDepth() == Depth) |
| 4819 | Used[TTP->getIndex()] = true; |
| 4820 | } |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4821 | return; |
| 4822 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4823 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4824 | if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4825 | MarkUsedTemplateParameters(Ctx, QTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4826 | Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4827 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4828 | MarkUsedTemplateParameters(Ctx, DTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4829 | Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4830 | } |
| 4831 | |
| 4832 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4833 | /// type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4834 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4835 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4836 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4837 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4838 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4839 | if (T.isNull()) |
| 4840 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4841 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4842 | // Non-dependent types have nothing deducible |
| 4843 | if (!T->isDependentType()) |
| 4844 | return; |
| 4845 | |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4846 | T = Ctx.getCanonicalType(T); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4847 | switch (T->getTypeClass()) { |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4848 | case Type::Pointer: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4849 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4850 | cast<PointerType>(T)->getPointeeType(), |
| 4851 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4852 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4853 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4854 | break; |
| 4855 | |
| 4856 | case Type::BlockPointer: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4857 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4858 | cast<BlockPointerType>(T)->getPointeeType(), |
| 4859 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4860 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4861 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4862 | break; |
| 4863 | |
| 4864 | case Type::LValueReference: |
| 4865 | case Type::RValueReference: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4866 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4867 | cast<ReferenceType>(T)->getPointeeType(), |
| 4868 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4869 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4870 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4871 | break; |
| 4872 | |
| 4873 | case Type::MemberPointer: { |
| 4874 | const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4875 | MarkUsedTemplateParameters(Ctx, MemPtr->getPointeeType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4876 | Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4877 | MarkUsedTemplateParameters(Ctx, QualType(MemPtr->getClass(), 0), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4878 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4879 | break; |
| 4880 | } |
| 4881 | |
| 4882 | case Type::DependentSizedArray: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4883 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4884 | cast<DependentSizedArrayType>(T)->getSizeExpr(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4885 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4886 | // Fall through to check the element type |
| 4887 | |
| 4888 | case Type::ConstantArray: |
| 4889 | case Type::IncompleteArray: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4890 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4891 | cast<ArrayType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4892 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4893 | break; |
| 4894 | |
| 4895 | case Type::Vector: |
| 4896 | case Type::ExtVector: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4897 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4898 | cast<VectorType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4899 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4900 | break; |
| 4901 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4902 | case Type::DependentSizedExtVector: { |
| 4903 | const DependentSizedExtVectorType *VecType |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4904 | = cast<DependentSizedExtVectorType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4905 | MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4906 | Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4907 | MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4908 | Depth, Used); |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4909 | break; |
| 4910 | } |
| 4911 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4912 | case Type::FunctionProto: { |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4913 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4914 | MarkUsedTemplateParameters(Ctx, Proto->getReturnType(), OnlyDeduced, Depth, |
| 4915 | Used); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4916 | for (unsigned I = 0, N = Proto->getNumParams(); I != N; ++I) |
| 4917 | MarkUsedTemplateParameters(Ctx, Proto->getParamType(I), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4918 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4919 | break; |
| 4920 | } |
| 4921 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4922 | case Type::TemplateTypeParm: { |
| 4923 | const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T); |
| 4924 | if (TTP->getDepth() == Depth) |
| 4925 | Used[TTP->getIndex()] = true; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4926 | break; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4927 | } |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4928 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4929 | case Type::SubstTemplateTypeParmPack: { |
| 4930 | const SubstTemplateTypeParmPackType *Subst |
| 4931 | = cast<SubstTemplateTypeParmPackType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4932 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4933 | QualType(Subst->getReplacedParameter(), 0), |
| 4934 | OnlyDeduced, Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4935 | MarkUsedTemplateParameters(Ctx, Subst->getArgumentPack(), |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4936 | OnlyDeduced, Depth, Used); |
| 4937 | break; |
| 4938 | } |
| 4939 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4940 | case Type::InjectedClassName: |
| 4941 | T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); |
| 4942 | // fall through |
| 4943 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4944 | case Type::TemplateSpecialization: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4945 | const TemplateSpecializationType *Spec |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4946 | = cast<TemplateSpecializationType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4947 | MarkUsedTemplateParameters(Ctx, Spec->getTemplateName(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4948 | Depth, Used); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4949 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4950 | // C++0x [temp.deduct.type]p9: |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 4951 | // If the template argument list of P contains a pack expansion that is |
| 4952 | // not the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4953 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4954 | if (OnlyDeduced && |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4955 | hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs())) |
| 4956 | break; |
| 4957 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4958 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4959 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4960 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4961 | break; |
| 4962 | } |
| 4963 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4964 | case Type::Complex: |
| 4965 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4966 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4967 | cast<ComplexType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4968 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4969 | break; |
| 4970 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4971 | case Type::Atomic: |
| 4972 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4973 | MarkUsedTemplateParameters(Ctx, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4974 | cast<AtomicType>(T)->getValueType(), |
| 4975 | OnlyDeduced, Depth, Used); |
| 4976 | break; |
| 4977 | |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4978 | case Type::DependentName: |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4979 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4980 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4981 | cast<DependentNameType>(T)->getQualifier(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4982 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4983 | break; |
| 4984 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4985 | case Type::DependentTemplateSpecialization: { |
Richard Smith | 50d5b97 | 2015-12-30 20:56:05 +0000 | [diff] [blame] | 4986 | // C++14 [temp.deduct.type]p5: |
| 4987 | // The non-deduced contexts are: |
| 4988 | // -- The nested-name-specifier of a type that was specified using a |
| 4989 | // qualified-id |
| 4990 | // |
| 4991 | // C++14 [temp.deduct.type]p6: |
| 4992 | // When a type name is specified in a way that includes a non-deduced |
| 4993 | // context, all of the types that comprise that type name are also |
| 4994 | // non-deduced. |
| 4995 | if (OnlyDeduced) |
| 4996 | break; |
| 4997 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4998 | const DependentTemplateSpecializationType *Spec |
| 4999 | = cast<DependentTemplateSpecializationType>(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5000 | |
Richard Smith | 50d5b97 | 2015-12-30 20:56:05 +0000 | [diff] [blame] | 5001 | MarkUsedTemplateParameters(Ctx, Spec->getQualifier(), |
| 5002 | OnlyDeduced, Depth, Used); |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5003 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5004 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5005 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 5006 | Used); |
| 5007 | break; |
| 5008 | } |
| 5009 | |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5010 | case Type::TypeOf: |
| 5011 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5012 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5013 | cast<TypeOfType>(T)->getUnderlyingType(), |
| 5014 | OnlyDeduced, Depth, Used); |
| 5015 | break; |
| 5016 | |
| 5017 | case Type::TypeOfExpr: |
| 5018 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5019 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5020 | cast<TypeOfExprType>(T)->getUnderlyingExpr(), |
| 5021 | OnlyDeduced, Depth, Used); |
| 5022 | break; |
| 5023 | |
| 5024 | case Type::Decltype: |
| 5025 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5026 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 5027 | cast<DecltypeType>(T)->getUnderlyingExpr(), |
| 5028 | OnlyDeduced, Depth, Used); |
| 5029 | break; |
| 5030 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 5031 | case Type::UnaryTransform: |
| 5032 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5033 | MarkUsedTemplateParameters(Ctx, |
Richard Smith | 5f27438 | 2016-09-28 23:55:27 +0000 | [diff] [blame] | 5034 | cast<UnaryTransformType>(T)->getUnderlyingType(), |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 5035 | OnlyDeduced, Depth, Used); |
| 5036 | break; |
| 5037 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 5038 | case Type::PackExpansion: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5039 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 5040 | cast<PackExpansionType>(T)->getPattern(), |
| 5041 | OnlyDeduced, Depth, Used); |
| 5042 | break; |
| 5043 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5044 | case Type::Auto: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5045 | MarkUsedTemplateParameters(Ctx, |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 5046 | cast<AutoType>(T)->getDeducedType(), |
| 5047 | OnlyDeduced, Depth, Used); |
| 5048 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5049 | // None of these types have any template parameters in them. |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5050 | case Type::Builtin: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5051 | case Type::VariableArray: |
| 5052 | case Type::FunctionNoProto: |
| 5053 | case Type::Record: |
| 5054 | case Type::Enum: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5055 | case Type::ObjCInterface: |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5056 | case Type::ObjCObject: |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 5057 | case Type::ObjCObjectPointer: |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 5058 | case Type::UnresolvedUsing: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 5059 | case Type::Pipe: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5060 | #define TYPE(Class, Base) |
| 5061 | #define ABSTRACT_TYPE(Class, Base) |
| 5062 | #define DEPENDENT_TYPE(Class, Base) |
| 5063 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 5064 | #include "clang/AST/TypeNodes.def" |
| 5065 | break; |
| 5066 | } |
| 5067 | } |
| 5068 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5069 | /// \brief Mark the template parameters that are used by this |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5070 | /// template argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5071 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5072 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5073 | const TemplateArgument &TemplateArg, |
| 5074 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5075 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5076 | llvm::SmallBitVector &Used) { |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5077 | switch (TemplateArg.getKind()) { |
| 5078 | case TemplateArgument::Null: |
| 5079 | case TemplateArgument::Integral: |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 5080 | case TemplateArgument::Declaration: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5081 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5082 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 5083 | case TemplateArgument::NullPtr: |
| 5084 | MarkUsedTemplateParameters(Ctx, TemplateArg.getNullPtrType(), OnlyDeduced, |
| 5085 | Depth, Used); |
| 5086 | break; |
| 5087 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5088 | case TemplateArgument::Type: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5089 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5090 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5091 | break; |
| 5092 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5093 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 5094 | case TemplateArgument::TemplateExpansion: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5095 | MarkUsedTemplateParameters(Ctx, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5096 | TemplateArg.getAsTemplateOrTemplatePattern(), |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5097 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5098 | break; |
| 5099 | |
| 5100 | case TemplateArgument::Expression: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5101 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsExpr(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5102 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5103 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5104 | |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5105 | case TemplateArgument::Pack: |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 5106 | for (const auto &P : TemplateArg.pack_elements()) |
| 5107 | MarkUsedTemplateParameters(Ctx, P, OnlyDeduced, Depth, Used); |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5108 | break; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5109 | } |
| 5110 | } |
| 5111 | |
James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 5112 | /// \brief Mark which template parameters can be deduced from a given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5113 | /// template argument list. |
| 5114 | /// |
| 5115 | /// \param TemplateArgs the template argument list from which template |
| 5116 | /// parameters will be deduced. |
| 5117 | /// |
James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 5118 | /// \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] | 5119 | /// to indicate when the corresponding template parameter will be |
| 5120 | /// deduced. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5121 | void |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5122 | Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5123 | bool OnlyDeduced, unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5124 | llvm::SmallBitVector &Used) { |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5125 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5126 | // If the template argument list of P contains a pack expansion that is not |
| 5127 | // the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5128 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5129 | if (OnlyDeduced && |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5130 | hasPackExpansionBeforeEnd(TemplateArgs.data(), TemplateArgs.size())) |
| 5131 | return; |
| 5132 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5133 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5134 | ::MarkUsedTemplateParameters(Context, TemplateArgs[I], OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5135 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5136 | } |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5137 | |
| 5138 | /// \brief Marks all of the template parameters that will be deduced by a |
| 5139 | /// call to the given function template. |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 5140 | void Sema::MarkDeducedTemplateParameters( |
| 5141 | ASTContext &Ctx, const FunctionTemplateDecl *FunctionTemplate, |
| 5142 | llvm::SmallBitVector &Deduced) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5143 | TemplateParameterList *TemplateParams |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5144 | = FunctionTemplate->getTemplateParameters(); |
| 5145 | Deduced.clear(); |
| 5146 | Deduced.resize(TemplateParams->size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5147 | |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5148 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 5149 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5150 | ::MarkUsedTemplateParameters(Ctx, Function->getParamDecl(I)->getType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5151 | true, TemplateParams->getDepth(), Deduced); |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5152 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5153 | |
| 5154 | bool hasDeducibleTemplateParameters(Sema &S, |
| 5155 | FunctionTemplateDecl *FunctionTemplate, |
| 5156 | QualType T) { |
| 5157 | if (!T->isDependentType()) |
| 5158 | return false; |
| 5159 | |
| 5160 | TemplateParameterList *TemplateParams |
| 5161 | = FunctionTemplate->getTemplateParameters(); |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5162 | llvm::SmallBitVector Deduced(TemplateParams->size()); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 5163 | ::MarkUsedTemplateParameters(S.Context, T, true, TemplateParams->getDepth(), |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5164 | Deduced); |
| 5165 | |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5166 | return Deduced.any(); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5167 | } |