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 | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 103 | bool PartialOrdering = false); |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 104 | |
| 105 | static Sema::TemplateDeductionResult |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 106 | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 107 | const TemplateArgument *Params, unsigned NumParams, |
| 108 | const TemplateArgument *Args, unsigned NumArgs, |
| 109 | TemplateDeductionInfo &Info, |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 110 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 111 | bool NumberOfArgumentsMustMatch); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 112 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 113 | /// \brief If the given expression is of a form that permits the deduction |
| 114 | /// of a non-type template parameter, return the declaration of that |
| 115 | /// non-type template parameter. |
| 116 | static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) { |
Richard Smith | 7ebb07c | 2012-07-08 04:37:51 +0000 | [diff] [blame] | 117 | // If we are within an alias template, the expression may have undergone |
| 118 | // any number of parameter substitutions already. |
| 119 | while (1) { |
| 120 | if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E)) |
| 121 | E = IC->getSubExpr(); |
| 122 | else if (SubstNonTypeTemplateParmExpr *Subst = |
| 123 | dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 124 | E = Subst->getReplacement(); |
| 125 | else |
| 126 | break; |
| 127 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 129 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 130 | return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 132 | return nullptr; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 135 | /// \brief Determine whether two declaration pointers refer to the same |
| 136 | /// declaration. |
| 137 | static bool isSameDeclaration(Decl *X, Decl *Y) { |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 138 | if (NamedDecl *NX = dyn_cast<NamedDecl>(X)) |
| 139 | X = NX->getUnderlyingDecl(); |
| 140 | if (NamedDecl *NY = dyn_cast<NamedDecl>(Y)) |
| 141 | Y = NY->getUnderlyingDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 142 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 143 | return X->getCanonicalDecl() == Y->getCanonicalDecl(); |
| 144 | } |
| 145 | |
| 146 | /// \brief Verify that the given, deduced template arguments are compatible. |
| 147 | /// |
| 148 | /// \returns The deduced template argument, or a NULL template argument if |
| 149 | /// the deduced template arguments were incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 150 | static DeducedTemplateArgument |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 151 | checkDeducedTemplateArguments(ASTContext &Context, |
| 152 | const DeducedTemplateArgument &X, |
| 153 | const DeducedTemplateArgument &Y) { |
| 154 | // We have no deduction for one or both of the arguments; they're compatible. |
| 155 | if (X.isNull()) |
| 156 | return Y; |
| 157 | if (Y.isNull()) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 158 | return X; |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 159 | |
| 160 | switch (X.getKind()) { |
| 161 | case TemplateArgument::Null: |
| 162 | llvm_unreachable("Non-deduced template arguments handled above"); |
| 163 | |
| 164 | case TemplateArgument::Type: |
| 165 | // If two template type arguments have the same type, they're compatible. |
| 166 | if (Y.getKind() == TemplateArgument::Type && |
| 167 | Context.hasSameType(X.getAsType(), Y.getAsType())) |
| 168 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 169 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 170 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 171 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 172 | case TemplateArgument::Integral: |
| 173 | // If we deduced a constant in one case and either a dependent expression or |
| 174 | // declaration in another case, keep the integral constant. |
| 175 | // If both are integral constants with the same value, keep that value. |
| 176 | if (Y.getKind() == TemplateArgument::Expression || |
| 177 | Y.getKind() == TemplateArgument::Declaration || |
| 178 | (Y.getKind() == TemplateArgument::Integral && |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 179 | hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()))) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 180 | return DeducedTemplateArgument(X, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 181 | X.wasDeducedFromArrayBound() && |
| 182 | Y.wasDeducedFromArrayBound()); |
| 183 | |
| 184 | // All other combinations are incompatible. |
| 185 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 186 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 187 | case TemplateArgument::Template: |
| 188 | if (Y.getKind() == TemplateArgument::Template && |
| 189 | Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate())) |
| 190 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 191 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 192 | // All other combinations are incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 193 | return DeducedTemplateArgument(); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 194 | |
| 195 | case TemplateArgument::TemplateExpansion: |
| 196 | if (Y.getKind() == TemplateArgument::TemplateExpansion && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 197 | Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(), |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 198 | Y.getAsTemplateOrTemplatePattern())) |
| 199 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 200 | |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 201 | // All other combinations are incompatible. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 202 | return DeducedTemplateArgument(); |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 203 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 204 | case TemplateArgument::Expression: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 205 | // If we deduced a dependent expression in one case and either an integral |
| 206 | // constant or a declaration in another case, keep the integral constant |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 207 | // or declaration. |
| 208 | if (Y.getKind() == TemplateArgument::Integral || |
| 209 | Y.getKind() == TemplateArgument::Declaration) |
| 210 | return DeducedTemplateArgument(Y, X.wasDeducedFromArrayBound() && |
| 211 | Y.wasDeducedFromArrayBound()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 212 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 213 | if (Y.getKind() == TemplateArgument::Expression) { |
| 214 | // Compare the expressions for equality |
| 215 | llvm::FoldingSetNodeID ID1, ID2; |
| 216 | X.getAsExpr()->Profile(ID1, Context, true); |
| 217 | Y.getAsExpr()->Profile(ID2, Context, true); |
| 218 | if (ID1 == ID2) |
| 219 | return X; |
| 220 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 221 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 222 | // All other combinations are incompatible. |
| 223 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 224 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 225 | case TemplateArgument::Declaration: |
| 226 | // If we deduced a declaration and a dependent expression, keep the |
| 227 | // declaration. |
| 228 | if (Y.getKind() == TemplateArgument::Expression) |
| 229 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 230 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 231 | // If we deduced a declaration and an integral constant, keep the |
| 232 | // integral constant. |
| 233 | if (Y.getKind() == TemplateArgument::Integral) |
| 234 | return Y; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 235 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 236 | // If we deduced two declarations, make sure they they refer to the |
| 237 | // same declaration. |
| 238 | if (Y.getKind() == TemplateArgument::Declaration && |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 239 | isSameDeclaration(X.getAsDecl(), Y.getAsDecl())) |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 240 | return X; |
| 241 | |
| 242 | // All other combinations are incompatible. |
| 243 | return DeducedTemplateArgument(); |
| 244 | |
| 245 | case TemplateArgument::NullPtr: |
| 246 | // If we deduced a null pointer and a dependent expression, keep the |
| 247 | // null pointer. |
| 248 | if (Y.getKind() == TemplateArgument::Expression) |
| 249 | return X; |
| 250 | |
| 251 | // If we deduced a null pointer and an integral constant, keep the |
| 252 | // integral constant. |
| 253 | if (Y.getKind() == TemplateArgument::Integral) |
| 254 | return Y; |
| 255 | |
| 256 | // If we deduced two null pointers, make sure they have the same type. |
| 257 | if (Y.getKind() == TemplateArgument::NullPtr && |
| 258 | Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType())) |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 259 | return X; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 260 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 261 | // All other combinations are incompatible. |
| 262 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 263 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 264 | case TemplateArgument::Pack: |
| 265 | if (Y.getKind() != TemplateArgument::Pack || |
| 266 | X.pack_size() != Y.pack_size()) |
| 267 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 268 | |
| 269 | for (TemplateArgument::pack_iterator XA = X.pack_begin(), |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 270 | XAEnd = X.pack_end(), |
| 271 | YA = Y.pack_begin(); |
| 272 | XA != XAEnd; ++XA, ++YA) { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 273 | // FIXME: Do we need to merge the results together here? |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 274 | if (checkDeducedTemplateArguments(Context, |
| 275 | DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()), |
Douglas Gregor | f491ee2 | 2011-01-05 21:00:53 +0000 | [diff] [blame] | 276 | DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound())) |
| 277 | .isNull()) |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 278 | return DeducedTemplateArgument(); |
| 279 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 280 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 281 | return X; |
| 282 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 283 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 284 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | /// \brief Deduce the value of the given non-type template parameter |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame^] | 288 | /// from the given integral constant. |
Benjamin Kramer | 7320b99 | 2016-06-15 14:20:56 +0000 | [diff] [blame] | 289 | static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( |
| 290 | Sema &S, NonTypeTemplateParmDecl *NTTP, const llvm::APSInt &Value, |
| 291 | QualType ValueType, bool DeducedFromArrayBound, TemplateDeductionInfo &Info, |
| 292 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 294 | "Cannot deduce non-type template argument with depth > 0"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 295 | |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 296 | DeducedTemplateArgument NewDeduced(S.Context, Value, ValueType, |
| 297 | DeducedFromArrayBound); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 298 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 299 | Deduced[NTTP->getIndex()], |
| 300 | NewDeduced); |
| 301 | if (Result.isNull()) { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 302 | Info.Param = NTTP; |
| 303 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 304 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 305 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 306 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 307 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 308 | Deduced[NTTP->getIndex()] = Result; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 309 | return Sema::TDK_Success; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | /// \brief Deduce the value of the given non-type template parameter |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame^] | 313 | /// from the given null pointer template argument type. |
| 314 | static Sema::TemplateDeductionResult DeduceNullPtrTemplateArgument( |
| 315 | Sema &S, NonTypeTemplateParmDecl *NTTP, QualType NullPtrType, |
| 316 | TemplateDeductionInfo &Info, |
| 317 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
| 318 | Expr *Value = |
| 319 | S.ImpCastExprToType(new (S.Context) CXXNullPtrLiteralExpr( |
| 320 | S.Context.NullPtrTy, NTTP->getLocation()), |
| 321 | NullPtrType, CK_NullToPointer) |
| 322 | .get(); |
| 323 | DeducedTemplateArgument NewDeduced(Value); |
| 324 | DeducedTemplateArgument Result = checkDeducedTemplateArguments( |
| 325 | S.Context, Deduced[NTTP->getIndex()], NewDeduced); |
| 326 | |
| 327 | if (Result.isNull()) { |
| 328 | Info.Param = NTTP; |
| 329 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
| 330 | Info.SecondArg = NewDeduced; |
| 331 | return Sema::TDK_Inconsistent; |
| 332 | } |
| 333 | |
| 334 | Deduced[NTTP->getIndex()] = Result; |
| 335 | return Sema::TDK_Success; |
| 336 | } |
| 337 | |
| 338 | /// \brief Deduce the value of the given non-type template parameter |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 339 | /// from the given type- or value-dependent expression. |
| 340 | /// |
| 341 | /// \returns true if deduction succeeded, false otherwise. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 342 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 343 | DeduceNonTypeTemplateArgument(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 344 | NonTypeTemplateParmDecl *NTTP, |
| 345 | Expr *Value, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 346 | TemplateDeductionInfo &Info, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 347 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 349 | "Cannot deduce non-type template argument with depth > 0"); |
| 350 | assert((Value->isTypeDependent() || Value->isValueDependent()) && |
| 351 | "Expression template argument must be type- or value-dependent."); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 353 | DeducedTemplateArgument NewDeduced(Value); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 354 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
| 355 | Deduced[NTTP->getIndex()], |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 356 | NewDeduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 357 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 358 | if (Result.isNull()) { |
| 359 | Info.Param = NTTP; |
| 360 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
| 361 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 362 | return Sema::TDK_Inconsistent; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 363 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 364 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 365 | Deduced[NTTP->getIndex()] = Result; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 366 | return Sema::TDK_Success; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 369 | /// \brief Deduce the value of the given non-type template parameter |
| 370 | /// from the given declaration. |
| 371 | /// |
| 372 | /// \returns true if deduction succeeded, false otherwise. |
| 373 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 374 | DeduceNonTypeTemplateArgument(Sema &S, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 375 | NonTypeTemplateParmDecl *NTTP, |
| 376 | ValueDecl *D, |
| 377 | TemplateDeductionInfo &Info, |
| 378 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 379 | assert(NTTP->getDepth() == 0 && |
| 380 | "Cannot deduce non-type template argument with depth > 0"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 381 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 382 | D = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 383 | TemplateArgument New(D, NTTP->getType()); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 384 | DeducedTemplateArgument NewDeduced(New); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 385 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 386 | Deduced[NTTP->getIndex()], |
| 387 | NewDeduced); |
| 388 | if (Result.isNull()) { |
| 389 | Info.Param = NTTP; |
| 390 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
| 391 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 392 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 393 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 394 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 395 | Deduced[NTTP->getIndex()] = Result; |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 396 | return Sema::TDK_Success; |
| 397 | } |
| 398 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 399 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 400 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 401 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 402 | TemplateName Param, |
| 403 | TemplateName Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 404 | TemplateDeductionInfo &Info, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 405 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 406 | TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 407 | if (!ParamDecl) { |
| 408 | // The parameter type is dependent and is not a template template parameter, |
| 409 | // so there is nothing that we can deduce. |
| 410 | return Sema::TDK_Success; |
| 411 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 412 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 413 | if (TemplateTemplateParmDecl *TempParam |
| 414 | = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) { |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 415 | DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 416 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 417 | Deduced[TempParam->getIndex()], |
| 418 | NewDeduced); |
| 419 | if (Result.isNull()) { |
| 420 | Info.Param = TempParam; |
| 421 | Info.FirstArg = Deduced[TempParam->getIndex()]; |
| 422 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 423 | return Sema::TDK_Inconsistent; |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 424 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 425 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 426 | Deduced[TempParam->getIndex()] = Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 427 | return Sema::TDK_Success; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 428 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 429 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 430 | // Verify that the two template names are equivalent. |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 431 | if (S.Context.hasSameTemplateName(Param, Arg)) |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 432 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 433 | |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 434 | // Mismatch of non-dependent template parameter to argument. |
| 435 | Info.FirstArg = TemplateArgument(Param); |
| 436 | Info.SecondArg = TemplateArgument(Arg); |
| 437 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 440 | /// \brief Deduce the template arguments by comparing the template parameter |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 441 | /// type (which is a template-id) with the template argument type. |
| 442 | /// |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 443 | /// \param S the Sema |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 444 | /// |
| 445 | /// \param TemplateParams the template parameters that we are deducing |
| 446 | /// |
| 447 | /// \param Param the parameter type |
| 448 | /// |
| 449 | /// \param Arg the argument type |
| 450 | /// |
| 451 | /// \param Info information about the template argument deduction itself |
| 452 | /// |
| 453 | /// \param Deduced the deduced template arguments |
| 454 | /// |
| 455 | /// \returns the result of template argument deduction so far. Note that a |
| 456 | /// "success" result means that template argument deduction has not yet failed, |
| 457 | /// but it may still fail, later, for other reasons. |
| 458 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 459 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 460 | TemplateParameterList *TemplateParams, |
| 461 | const TemplateSpecializationType *Param, |
| 462 | QualType Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 463 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 464 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
John McCall | b692a09 | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 465 | assert(Arg.isCanonical() && "Argument type must be canonical"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 466 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 467 | // Check whether the template argument is a dependent template-id. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 468 | if (const TemplateSpecializationType *SpecArg |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 469 | = dyn_cast<TemplateSpecializationType>(Arg)) { |
| 470 | // Perform template argument deduction for the template name. |
| 471 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 472 | = DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 473 | Param->getTemplateName(), |
| 474 | SpecArg->getTemplateName(), |
| 475 | Info, Deduced)) |
| 476 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 477 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 479 | // Perform template argument deduction on each template |
Douglas Gregor | d80ea20 | 2010-12-22 18:55:49 +0000 | [diff] [blame] | 480 | // argument. Ignore any missing/extra arguments, since they could be |
| 481 | // filled in by default arguments. |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 482 | return DeduceTemplateArguments(S, TemplateParams, Param->getArgs(), |
| 483 | Param->getNumArgs(), SpecArg->getArgs(), |
| 484 | SpecArg->getNumArgs(), Info, Deduced, |
| 485 | /*NumberOfArgumentsMustMatch=*/false); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 486 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 487 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 488 | // If the argument type is a class template specialization, we |
| 489 | // perform template argument deduction using its template |
| 490 | // arguments. |
| 491 | const RecordType *RecordArg = dyn_cast<RecordType>(Arg); |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 492 | if (!RecordArg) { |
| 493 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 494 | Info.SecondArg = TemplateArgument(Arg); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 495 | return Sema::TDK_NonDeducedMismatch; |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 496 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 497 | |
| 498 | ClassTemplateSpecializationDecl *SpecArg |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 499 | = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 500 | if (!SpecArg) { |
| 501 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 502 | Info.SecondArg = TemplateArgument(Arg); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 503 | return Sema::TDK_NonDeducedMismatch; |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 504 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 505 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 506 | // Perform template argument deduction for the template name. |
| 507 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 508 | = DeduceTemplateArguments(S, |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 509 | TemplateParams, |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 510 | Param->getTemplateName(), |
| 511 | TemplateName(SpecArg->getSpecializedTemplate()), |
| 512 | Info, Deduced)) |
| 513 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 514 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 515 | // Perform template argument deduction for the template arguments. |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 516 | return DeduceTemplateArguments( |
| 517 | S, TemplateParams, Param->getArgs(), Param->getNumArgs(), |
| 518 | SpecArg->getTemplateArgs().data(), SpecArg->getTemplateArgs().size(), |
| 519 | Info, Deduced, /*NumberOfArgumentsMustMatch=*/true); |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 520 | } |
| 521 | |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 522 | /// \brief Determines whether the given type is an opaque type that |
| 523 | /// might be more qualified when instantiated. |
| 524 | static bool IsPossiblyOpaquelyQualifiedType(QualType T) { |
| 525 | switch (T->getTypeClass()) { |
| 526 | case Type::TypeOfExpr: |
| 527 | case Type::TypeOf: |
| 528 | case Type::DependentName: |
| 529 | case Type::Decltype: |
| 530 | case Type::UnresolvedUsing: |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 531 | case Type::TemplateTypeParm: |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 532 | return true; |
| 533 | |
| 534 | case Type::ConstantArray: |
| 535 | case Type::IncompleteArray: |
| 536 | case Type::VariableArray: |
| 537 | case Type::DependentSizedArray: |
| 538 | return IsPossiblyOpaquelyQualifiedType( |
| 539 | cast<ArrayType>(T)->getElementType()); |
| 540 | |
| 541 | default: |
| 542 | return false; |
| 543 | } |
| 544 | } |
| 545 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 546 | /// \brief Retrieve the depth and index of a template parameter. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 547 | static std::pair<unsigned, unsigned> |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 548 | getDepthAndIndex(NamedDecl *ND) { |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 549 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND)) |
| 550 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 551 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 552 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND)) |
| 553 | return std::make_pair(NTTP->getDepth(), NTTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 554 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 555 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND); |
| 556 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| 557 | } |
| 558 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 559 | /// \brief Retrieve the depth and index of an unexpanded parameter pack. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 560 | static std::pair<unsigned, unsigned> |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 561 | getDepthAndIndex(UnexpandedParameterPack UPP) { |
| 562 | if (const TemplateTypeParmType *TTP |
| 563 | = UPP.first.dyn_cast<const TemplateTypeParmType *>()) |
| 564 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 565 | |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 566 | return getDepthAndIndex(UPP.first.get<NamedDecl *>()); |
| 567 | } |
| 568 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 569 | /// \brief Helper function to build a TemplateParameter when we don't |
| 570 | /// know its type statically. |
| 571 | static TemplateParameter makeTemplateParameter(Decl *D) { |
| 572 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) |
| 573 | return TemplateParameter(TTP); |
Craig Topper | 4b482ee | 2013-07-08 04:24:47 +0000 | [diff] [blame] | 574 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 575 | return TemplateParameter(NTTP); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 576 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 577 | return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); |
| 578 | } |
| 579 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 580 | /// A pack that we're currently deducing. |
| 581 | struct clang::DeducedPack { |
| 582 | DeducedPack(unsigned Index) : Index(Index), Outer(nullptr) {} |
Craig Topper | 0a4e1f5 | 2013-07-08 04:44:01 +0000 | [diff] [blame] | 583 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 584 | // The index of the pack. |
| 585 | unsigned Index; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 586 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 587 | // The old value of the pack before we started deducing it. |
| 588 | DeducedTemplateArgument Saved; |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 589 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 590 | // A deferred value of this pack from an inner deduction, that couldn't be |
| 591 | // deduced because this deduction hadn't happened yet. |
| 592 | DeducedTemplateArgument DeferredDeduction; |
| 593 | |
| 594 | // The new value of the pack. |
| 595 | SmallVector<DeducedTemplateArgument, 4> New; |
| 596 | |
| 597 | // The outer deduction for this pack, if any. |
| 598 | DeducedPack *Outer; |
| 599 | }; |
| 600 | |
Benjamin Kramer | d5748c7 | 2015-03-23 12:31:05 +0000 | [diff] [blame] | 601 | namespace { |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 602 | /// A scope in which we're performing pack deduction. |
| 603 | class PackDeductionScope { |
| 604 | public: |
| 605 | PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, |
| 606 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 607 | TemplateDeductionInfo &Info, TemplateArgument Pattern) |
| 608 | : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { |
| 609 | // Compute the set of template parameter indices that correspond to |
| 610 | // parameter packs expanded by the pack expansion. |
| 611 | { |
| 612 | llvm::SmallBitVector SawIndices(TemplateParams->size()); |
| 613 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 614 | S.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
| 615 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 616 | unsigned Depth, Index; |
| 617 | std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); |
| 618 | if (Depth == 0 && !SawIndices[Index]) { |
| 619 | SawIndices[Index] = true; |
| 620 | |
| 621 | // Save the deduced template argument for the parameter pack expanded |
| 622 | // by this pack expansion, then clear out the deduction. |
| 623 | DeducedPack Pack(Index); |
| 624 | Pack.Saved = Deduced[Index]; |
| 625 | Deduced[Index] = TemplateArgument(); |
| 626 | |
| 627 | Packs.push_back(Pack); |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | assert(!Packs.empty() && "Pack expansion without unexpanded packs?"); |
| 632 | |
| 633 | for (auto &Pack : Packs) { |
| 634 | if (Info.PendingDeducedPacks.size() > Pack.Index) |
| 635 | Pack.Outer = Info.PendingDeducedPacks[Pack.Index]; |
| 636 | else |
| 637 | Info.PendingDeducedPacks.resize(Pack.Index + 1); |
| 638 | Info.PendingDeducedPacks[Pack.Index] = &Pack; |
| 639 | |
| 640 | if (S.CurrentInstantiationScope) { |
| 641 | // If the template argument pack was explicitly specified, add that to |
| 642 | // the set of deduced arguments. |
| 643 | const TemplateArgument *ExplicitArgs; |
| 644 | unsigned NumExplicitArgs; |
| 645 | NamedDecl *PartiallySubstitutedPack = |
| 646 | S.CurrentInstantiationScope->getPartiallySubstitutedPack( |
| 647 | &ExplicitArgs, &NumExplicitArgs); |
| 648 | if (PartiallySubstitutedPack && |
| 649 | getDepthAndIndex(PartiallySubstitutedPack).second == Pack.Index) |
| 650 | Pack.New.append(ExplicitArgs, ExplicitArgs + NumExplicitArgs); |
| 651 | } |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 652 | } |
| 653 | } |
Douglas Gregor | a8bd0d9 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 654 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 655 | ~PackDeductionScope() { |
| 656 | for (auto &Pack : Packs) |
| 657 | Info.PendingDeducedPacks[Pack.Index] = Pack.Outer; |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 658 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 659 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 660 | /// Move to deducing the next element in each pack that is being deduced. |
| 661 | void nextPackElement() { |
| 662 | // Capture the deduced template arguments for each parameter pack expanded |
| 663 | // by this pack expansion, add them to the list of arguments we've deduced |
| 664 | // for that pack, then clear out the deduced argument. |
| 665 | for (auto &Pack : Packs) { |
| 666 | DeducedTemplateArgument &DeducedArg = Deduced[Pack.Index]; |
| 667 | if (!DeducedArg.isNull()) { |
| 668 | Pack.New.push_back(DeducedArg); |
| 669 | DeducedArg = DeducedTemplateArgument(); |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | /// \brief Finish template argument deduction for a set of argument packs, |
| 675 | /// producing the argument packs and checking for consistency with prior |
| 676 | /// deductions. |
| 677 | Sema::TemplateDeductionResult finish(bool HasAnyArguments) { |
| 678 | // Build argument packs for each of the parameter packs expanded by this |
| 679 | // pack expansion. |
| 680 | for (auto &Pack : Packs) { |
| 681 | // Put back the old value for this pack. |
| 682 | Deduced[Pack.Index] = Pack.Saved; |
| 683 | |
| 684 | // Build or find a new value for this pack. |
| 685 | DeducedTemplateArgument NewPack; |
| 686 | if (HasAnyArguments && Pack.New.empty()) { |
| 687 | if (Pack.DeferredDeduction.isNull()) { |
| 688 | // We were not able to deduce anything for this parameter pack |
| 689 | // (because it only appeared in non-deduced contexts), so just |
| 690 | // restore the saved argument pack. |
| 691 | continue; |
| 692 | } |
| 693 | |
| 694 | NewPack = Pack.DeferredDeduction; |
| 695 | Pack.DeferredDeduction = TemplateArgument(); |
| 696 | } else if (Pack.New.empty()) { |
| 697 | // If we deduced an empty argument pack, create it now. |
| 698 | NewPack = DeducedTemplateArgument(TemplateArgument::getEmptyPack()); |
| 699 | } else { |
| 700 | TemplateArgument *ArgumentPack = |
| 701 | new (S.Context) TemplateArgument[Pack.New.size()]; |
| 702 | std::copy(Pack.New.begin(), Pack.New.end(), ArgumentPack); |
| 703 | NewPack = DeducedTemplateArgument( |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 704 | TemplateArgument(llvm::makeArrayRef(ArgumentPack, Pack.New.size())), |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 705 | Pack.New[0].wasDeducedFromArrayBound()); |
| 706 | } |
| 707 | |
| 708 | // Pick where we're going to put the merged pack. |
| 709 | DeducedTemplateArgument *Loc; |
| 710 | if (Pack.Outer) { |
| 711 | if (Pack.Outer->DeferredDeduction.isNull()) { |
| 712 | // Defer checking this pack until we have a complete pack to compare |
| 713 | // it against. |
| 714 | Pack.Outer->DeferredDeduction = NewPack; |
| 715 | continue; |
| 716 | } |
| 717 | Loc = &Pack.Outer->DeferredDeduction; |
| 718 | } else { |
| 719 | Loc = &Deduced[Pack.Index]; |
| 720 | } |
| 721 | |
| 722 | // Check the new pack matches any previous value. |
| 723 | DeducedTemplateArgument OldPack = *Loc; |
| 724 | DeducedTemplateArgument Result = |
| 725 | checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 726 | |
| 727 | // If we deferred a deduction of this pack, check that one now too. |
| 728 | if (!Result.isNull() && !Pack.DeferredDeduction.isNull()) { |
| 729 | OldPack = Result; |
| 730 | NewPack = Pack.DeferredDeduction; |
| 731 | Result = checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 732 | } |
| 733 | |
| 734 | if (Result.isNull()) { |
| 735 | Info.Param = |
| 736 | makeTemplateParameter(TemplateParams->getParam(Pack.Index)); |
| 737 | Info.FirstArg = OldPack; |
| 738 | Info.SecondArg = NewPack; |
| 739 | return Sema::TDK_Inconsistent; |
| 740 | } |
| 741 | |
| 742 | *Loc = Result; |
| 743 | } |
| 744 | |
| 745 | return Sema::TDK_Success; |
| 746 | } |
| 747 | |
| 748 | private: |
| 749 | Sema &S; |
| 750 | TemplateParameterList *TemplateParams; |
| 751 | SmallVectorImpl<DeducedTemplateArgument> &Deduced; |
| 752 | TemplateDeductionInfo &Info; |
| 753 | |
| 754 | SmallVector<DeducedPack, 2> Packs; |
| 755 | }; |
Benjamin Kramer | d5748c7 | 2015-03-23 12:31:05 +0000 | [diff] [blame] | 756 | } // namespace |
Douglas Gregor | b94a617 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 757 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 758 | /// \brief Deduce the template arguments by comparing the list of parameter |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 759 | /// types to the list of argument types, as in the parameter-type-lists of |
| 760 | /// function types (C++ [temp.deduct.type]p10). |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 761 | /// |
| 762 | /// \param S The semantic analysis object within which we are deducing |
| 763 | /// |
| 764 | /// \param TemplateParams The template parameters that we are deducing |
| 765 | /// |
| 766 | /// \param Params The list of parameter types |
| 767 | /// |
| 768 | /// \param NumParams The number of types in \c Params |
| 769 | /// |
| 770 | /// \param Args The list of argument types |
| 771 | /// |
| 772 | /// \param NumArgs The number of types in \c Args |
| 773 | /// |
| 774 | /// \param Info information about the template argument deduction itself |
| 775 | /// |
| 776 | /// \param Deduced the deduced template arguments |
| 777 | /// |
| 778 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
| 779 | /// how template argument deduction is performed. |
| 780 | /// |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 781 | /// \param PartialOrdering If true, we are performing template argument |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 782 | /// deduction for during partial ordering for a call |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 783 | /// (C++0x [temp.deduct.partial]). |
| 784 | /// |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 785 | /// \returns the result of template argument deduction so far. Note that a |
| 786 | /// "success" result means that template argument deduction has not yet failed, |
| 787 | /// but it may still fail, later, for other reasons. |
| 788 | static Sema::TemplateDeductionResult |
| 789 | DeduceTemplateArguments(Sema &S, |
| 790 | TemplateParameterList *TemplateParams, |
| 791 | const QualType *Params, unsigned NumParams, |
| 792 | const QualType *Args, unsigned NumArgs, |
| 793 | TemplateDeductionInfo &Info, |
Craig Topper | c1bbe8d | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 794 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 795 | unsigned TDF, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 796 | bool PartialOrdering = false) { |
Douglas Gregor | 86bea35 | 2011-01-05 23:23:17 +0000 | [diff] [blame] | 797 | // Fast-path check to see if we have too many/too few arguments. |
| 798 | if (NumParams != NumArgs && |
| 799 | !(NumParams && isa<PackExpansionType>(Params[NumParams - 1])) && |
| 800 | !(NumArgs && isa<PackExpansionType>(Args[NumArgs - 1]))) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 801 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 802 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 803 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 804 | // Similarly, if P has a form that contains (T), then each parameter type |
| 805 | // Pi of the respective parameter-type- list of P is compared with the |
| 806 | // corresponding parameter type Ai of the corresponding parameter-type-list |
| 807 | // of A. [...] |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 808 | unsigned ArgIdx = 0, ParamIdx = 0; |
| 809 | for (; ParamIdx != NumParams; ++ParamIdx) { |
| 810 | // Check argument types. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 811 | const PackExpansionType *Expansion |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 812 | = dyn_cast<PackExpansionType>(Params[ParamIdx]); |
| 813 | if (!Expansion) { |
| 814 | // Simple case: compare the parameter and argument types at this point. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 815 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 816 | // Make sure we have an argument. |
| 817 | if (ArgIdx >= NumArgs) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 818 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 819 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 820 | if (isa<PackExpansionType>(Args[ArgIdx])) { |
| 821 | // C++0x [temp.deduct.type]p22: |
| 822 | // If the original function parameter associated with A is a function |
| 823 | // parameter pack and the function parameter associated with P is not |
| 824 | // a function parameter pack, then template argument deduction fails. |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 825 | return Sema::TDK_MiscellaneousDeductionFailure; |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 826 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 827 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 828 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 829 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 830 | Params[ParamIdx], Args[ArgIdx], |
| 831 | Info, Deduced, TDF, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 832 | PartialOrdering)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 833 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 834 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 835 | ++ArgIdx; |
| 836 | continue; |
| 837 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 838 | |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 839 | // C++0x [temp.deduct.type]p5: |
| 840 | // The non-deduced contexts are: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 841 | // - 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] | 842 | // parameter-declaration-clause. |
| 843 | if (ParamIdx + 1 < NumParams) |
| 844 | return Sema::TDK_Success; |
| 845 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 846 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 847 | // If the parameter-declaration corresponding to Pi is a function |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 848 | // parameter pack, then the type of its declarator- id is compared with |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 849 | // each remaining parameter type in the parameter-type-list of A. Each |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 850 | // comparison deduces template arguments for subsequent positions in the |
| 851 | // template parameter packs expanded by the function parameter pack. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 852 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 853 | QualType Pattern = Expansion->getPattern(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 854 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 855 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 856 | bool HasAnyArguments = false; |
| 857 | for (; ArgIdx < NumArgs; ++ArgIdx) { |
| 858 | HasAnyArguments = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 859 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 860 | // Deduce template arguments from the pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 861 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 862 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, Pattern, |
| 863 | Args[ArgIdx], Info, Deduced, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 864 | TDF, PartialOrdering)) |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 865 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 866 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 867 | PackScope.nextPackElement(); |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 868 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 869 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 870 | // Build argument packs for each of the parameter packs expanded by this |
| 871 | // pack expansion. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 872 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 873 | return Result; |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 874 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 875 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 876 | // Make sure we don't have any extra arguments. |
| 877 | if (ArgIdx < NumArgs) |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 878 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 879 | |
Douglas Gregor | 5499af4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 880 | return Sema::TDK_Success; |
| 881 | } |
| 882 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 883 | /// \brief Determine whether the parameter has qualifiers that are either |
| 884 | /// inconsistent with or a superset of the argument's qualifiers. |
| 885 | static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType, |
| 886 | QualType ArgType) { |
| 887 | Qualifiers ParamQs = ParamType.getQualifiers(); |
| 888 | Qualifiers ArgQs = ArgType.getQualifiers(); |
| 889 | |
| 890 | if (ParamQs == ArgQs) |
| 891 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 892 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 893 | // Mismatched (but not missing) Objective-C GC attributes. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 894 | if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() && |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 895 | ParamQs.hasObjCGCAttr()) |
| 896 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 897 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 898 | // Mismatched (but not missing) address spaces. |
| 899 | if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() && |
| 900 | ParamQs.hasAddressSpace()) |
| 901 | return true; |
| 902 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 903 | // Mismatched (but not missing) Objective-C lifetime qualifiers. |
| 904 | if (ParamQs.getObjCLifetime() != ArgQs.getObjCLifetime() && |
| 905 | ParamQs.hasObjCLifetime()) |
| 906 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 907 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 908 | // CVR qualifier superset. |
| 909 | return (ParamQs.getCVRQualifiers() != ArgQs.getCVRQualifiers()) && |
| 910 | ((ParamQs.getCVRQualifiers() | ArgQs.getCVRQualifiers()) |
| 911 | == ParamQs.getCVRQualifiers()); |
| 912 | } |
| 913 | |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 914 | /// \brief Compare types for equality with respect to possibly compatible |
| 915 | /// function types (noreturn adjustment, implicit calling conventions). If any |
| 916 | /// of parameter and argument is not a function, just perform type comparison. |
| 917 | /// |
| 918 | /// \param Param the template parameter type. |
| 919 | /// |
| 920 | /// \param Arg the argument type. |
| 921 | bool Sema::isSameOrCompatibleFunctionType(CanQualType Param, |
| 922 | CanQualType Arg) { |
| 923 | const FunctionType *ParamFunction = Param->getAs<FunctionType>(), |
| 924 | *ArgFunction = Arg->getAs<FunctionType>(); |
| 925 | |
| 926 | // Just compare if not functions. |
| 927 | if (!ParamFunction || !ArgFunction) |
| 928 | return Param == Arg; |
| 929 | |
| 930 | // Noreturn adjustment. |
| 931 | QualType AdjustedParam; |
| 932 | if (IsNoReturnConversion(Param, Arg, AdjustedParam)) |
| 933 | return Arg == Context.getCanonicalType(AdjustedParam); |
| 934 | |
| 935 | // FIXME: Compatible calling conventions. |
| 936 | |
| 937 | return Param == Arg; |
| 938 | } |
| 939 | |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 940 | /// \brief Deduce the template arguments by comparing the parameter type and |
| 941 | /// the argument type (C++ [temp.deduct.type]). |
| 942 | /// |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 943 | /// \param S the semantic analysis object within which we are deducing |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 944 | /// |
| 945 | /// \param TemplateParams the template parameters that we are deducing |
| 946 | /// |
| 947 | /// \param ParamIn the parameter type |
| 948 | /// |
| 949 | /// \param ArgIn the argument type |
| 950 | /// |
| 951 | /// \param Info information about the template argument deduction itself |
| 952 | /// |
| 953 | /// \param Deduced the deduced template arguments |
| 954 | /// |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 955 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 956 | /// how template argument deduction is performed. |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 957 | /// |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 958 | /// \param PartialOrdering Whether we're performing template argument deduction |
| 959 | /// in the context of partial ordering (C++0x [temp.deduct.partial]). |
| 960 | /// |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 961 | /// \returns the result of template argument deduction so far. Note that a |
| 962 | /// "success" result means that template argument deduction has not yet failed, |
| 963 | /// but it may still fail, later, for other reasons. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 964 | static Sema::TemplateDeductionResult |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 965 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 966 | TemplateParameterList *TemplateParams, |
| 967 | QualType ParamIn, QualType ArgIn, |
| 968 | TemplateDeductionInfo &Info, |
| 969 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 970 | unsigned TDF, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 971 | bool PartialOrdering) { |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 972 | // We only want to look at the canonical types, since typedefs and |
| 973 | // sugar are not part of template argument deduction. |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 974 | QualType Param = S.Context.getCanonicalType(ParamIn); |
| 975 | QualType Arg = S.Context.getCanonicalType(ArgIn); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 976 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 977 | // If the argument type is a pack expansion, look at its pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 978 | // This isn't explicitly called out |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 979 | if (const PackExpansionType *ArgExpansion |
| 980 | = dyn_cast<PackExpansionType>(Arg)) |
| 981 | Arg = ArgExpansion->getPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 982 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 983 | if (PartialOrdering) { |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 984 | // C++11 [temp.deduct.partial]p5: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 985 | // Before the partial ordering is done, certain transformations are |
| 986 | // performed on the types used for partial ordering: |
| 987 | // - 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] | 988 | const ReferenceType *ParamRef = Param->getAs<ReferenceType>(); |
| 989 | if (ParamRef) |
| 990 | Param = ParamRef->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 991 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 992 | // - If A is a reference type, A is replaced by the type referred to. |
| 993 | const ReferenceType *ArgRef = Arg->getAs<ReferenceType>(); |
| 994 | if (ArgRef) |
| 995 | Arg = ArgRef->getPointeeType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 996 | |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 997 | if (ParamRef && ArgRef && S.Context.hasSameUnqualifiedType(Param, Arg)) { |
| 998 | // C++11 [temp.deduct.partial]p9: |
| 999 | // If, for a given type, deduction succeeds in both directions (i.e., |
| 1000 | // the types are identical after the transformations above) and both |
| 1001 | // P and A were reference types [...]: |
| 1002 | // - if [one type] was an lvalue reference and [the other type] was |
| 1003 | // not, [the other type] is not considered to be at least as |
| 1004 | // specialized as [the first type] |
| 1005 | // - if [one type] is more cv-qualified than [the other type], |
| 1006 | // [the other type] is not considered to be at least as specialized |
| 1007 | // as [the first type] |
| 1008 | // Objective-C ARC adds: |
| 1009 | // - [one type] has non-trivial lifetime, [the other type] has |
| 1010 | // __unsafe_unretained lifetime, and the types are otherwise |
| 1011 | // identical |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1012 | // |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1013 | // A is "considered to be at least as specialized" as P iff deduction |
| 1014 | // succeeds, so we model this as a deduction failure. Note that |
| 1015 | // [the first type] is P and [the other type] is A here; the standard |
| 1016 | // gets this backwards. |
Douglas Gregor | 85894a8 | 2011-04-30 17:07:52 +0000 | [diff] [blame] | 1017 | Qualifiers ParamQuals = Param.getQualifiers(); |
| 1018 | Qualifiers ArgQuals = Arg.getQualifiers(); |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1019 | if ((ParamRef->isLValueReferenceType() && |
| 1020 | !ArgRef->isLValueReferenceType()) || |
| 1021 | ParamQuals.isStrictSupersetOf(ArgQuals) || |
| 1022 | (ParamQuals.hasNonTrivialObjCLifetime() && |
| 1023 | ArgQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone && |
| 1024 | ParamQuals.withoutObjCLifetime() == |
| 1025 | ArgQuals.withoutObjCLifetime())) { |
| 1026 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1027 | Info.SecondArg = TemplateArgument(ArgIn); |
| 1028 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 6beabee | 2014-01-02 19:42:02 +0000 | [diff] [blame] | 1029 | } |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1030 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1031 | |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 1032 | // C++11 [temp.deduct.partial]p7: |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1033 | // Remove any top-level cv-qualifiers: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1034 | // - 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] | 1035 | // version of P. |
| 1036 | Param = Param.getUnqualifiedType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1037 | // - 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] | 1038 | // version of A. |
| 1039 | Arg = Arg.getUnqualifiedType(); |
| 1040 | } else { |
| 1041 | // C++0x [temp.deduct.call]p4 bullet 1: |
| 1042 | // - If the original P is a reference type, the deduced A (i.e., the type |
| 1043 | // referred to by the reference) can be more cv-qualified than the |
| 1044 | // transformed A. |
| 1045 | if (TDF & TDF_ParamWithReferenceType) { |
| 1046 | Qualifiers Quals; |
| 1047 | QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals); |
| 1048 | Quals.setCVRQualifiers(Quals.getCVRQualifiers() & |
John McCall | 6c9dd52 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 1049 | Arg.getCVRQualifiers()); |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1050 | Param = S.Context.getQualifiedType(UnqualParam, Quals); |
| 1051 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1052 | |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1053 | if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) { |
| 1054 | // C++0x [temp.deduct.type]p10: |
| 1055 | // If P and A are function types that originated from deduction when |
| 1056 | // taking the address of a function template (14.8.2.2) or when deducing |
| 1057 | // 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] | 1058 | // Ai are parameters of the top-level parameter-type-list of P and A, |
| 1059 | // respectively, Pi is adjusted if it is an rvalue reference to a |
| 1060 | // cv-unqualified template parameter and Ai is an lvalue reference, in |
| 1061 | // 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] | 1062 | // type (i.e., T&& is changed to simply T). [ Note: As a result, when |
| 1063 | // 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] | 1064 | // deduced as X&. - end note ] |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1065 | TDF &= ~TDF_TopLevelParameterTypeList; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1066 | |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1067 | if (const RValueReferenceType *ParamRef |
| 1068 | = Param->getAs<RValueReferenceType>()) { |
| 1069 | if (isa<TemplateTypeParmType>(ParamRef->getPointeeType()) && |
| 1070 | !ParamRef->getPointeeType().getQualifiers()) |
| 1071 | if (Arg->isLValueReferenceType()) |
| 1072 | Param = ParamRef->getPointeeType(); |
| 1073 | } |
| 1074 | } |
Douglas Gregor | cceb975 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1075 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1076 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1077 | // C++ [temp.deduct.type]p9: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1078 | // A template type argument T, a template template argument TT or a |
| 1079 | // 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] | 1080 | // the following forms: |
| 1081 | // |
| 1082 | // T |
| 1083 | // cv-list T |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1084 | if (const TemplateTypeParmType *TemplateTypeParm |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1085 | = Param->getAs<TemplateTypeParmType>()) { |
Douglas Gregor | 4ea5dec | 2011-09-22 15:57:07 +0000 | [diff] [blame] | 1086 | // Just skip any attempts to deduce from a placeholder type. |
| 1087 | if (Arg->isPlaceholderType()) |
| 1088 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1089 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1090 | unsigned Index = TemplateTypeParm->getIndex(); |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1091 | bool RecanonicalizeArg = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | |
Douglas Gregor | 6045482 | 2009-07-22 20:02:25 +0000 | [diff] [blame] | 1093 | // If the argument type is an array type, move the qualifiers up to the |
| 1094 | // 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] | 1095 | if (isa<ArrayType>(Arg)) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1096 | Qualifiers Quals; |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1097 | Arg = S.Context.getUnqualifiedArrayType(Arg, Quals); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1098 | if (Quals) { |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1099 | Arg = S.Context.getQualifiedType(Arg, Quals); |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1100 | RecanonicalizeArg = true; |
| 1101 | } |
| 1102 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1103 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1104 | // The argument type can not be less qualified than the parameter |
| 1105 | // type. |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1106 | if (!(TDF & TDF_IgnoreQualifiers) && |
| 1107 | hasInconsistentOrSupersetQualifiersOf(Param, Arg)) { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1108 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1109 | Info.FirstArg = TemplateArgument(Param); |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1110 | Info.SecondArg = TemplateArgument(Arg); |
John McCall | 42d7d19 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1111 | return Sema::TDK_Underqualified; |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1112 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1113 | |
| 1114 | assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0"); |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1115 | assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function"); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1116 | QualType DeducedType = Arg; |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1117 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1118 | // Remove any qualifiers on the parameter from the deduced type. |
| 1119 | // We checked the qualifiers for consistency above. |
| 1120 | Qualifiers DeducedQs = DeducedType.getQualifiers(); |
| 1121 | Qualifiers ParamQs = Param.getQualifiers(); |
| 1122 | DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers()); |
| 1123 | if (ParamQs.hasObjCGCAttr()) |
| 1124 | DeducedQs.removeObjCGCAttr(); |
| 1125 | if (ParamQs.hasAddressSpace()) |
| 1126 | DeducedQs.removeAddressSpace(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1127 | if (ParamQs.hasObjCLifetime()) |
| 1128 | DeducedQs.removeObjCLifetime(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1129 | |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1130 | // Objective-C ARC: |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1131 | // If template deduction would produce a lifetime qualifier on a type |
| 1132 | // that is not a lifetime type, template argument deduction fails. |
| 1133 | if (ParamQs.hasObjCLifetime() && !DeducedType->isObjCLifetimeType() && |
| 1134 | !DeducedType->isDependentType()) { |
| 1135 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1136 | Info.FirstArg = TemplateArgument(Param); |
| 1137 | Info.SecondArg = TemplateArgument(Arg); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1138 | return Sema::TDK_Underqualified; |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1139 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1140 | |
Douglas Gregor | a4f2b43 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1141 | // Objective-C ARC: |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1142 | // If template deduction would produce an argument type with lifetime type |
| 1143 | // but no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1144 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e46db90 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1145 | DeducedType->isObjCLifetimeType() && |
| 1146 | !DeducedQs.hasObjCLifetime()) |
| 1147 | DeducedQs.setObjCLifetime(Qualifiers::OCL_Strong); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1148 | |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1149 | DeducedType = S.Context.getQualifiedType(DeducedType.getUnqualifiedType(), |
| 1150 | DeducedQs); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1151 | |
Douglas Gregor | d6605db | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1152 | if (RecanonicalizeArg) |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1153 | DeducedType = S.Context.getCanonicalType(DeducedType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1154 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1155 | DeducedTemplateArgument NewDeduced(DeducedType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1156 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1157 | Deduced[Index], |
| 1158 | NewDeduced); |
| 1159 | if (Result.isNull()) { |
| 1160 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1161 | Info.FirstArg = Deduced[Index]; |
| 1162 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1163 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1164 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1165 | |
Douglas Gregor | 7f8e768 | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1166 | Deduced[Index] = Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1167 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1170 | // Set up the template argument deduction information for a failure. |
John McCall | 0ad1666 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1171 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1172 | Info.SecondArg = TemplateArgument(ArgIn); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1173 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 1174 | // If the parameter is an already-substituted template parameter |
| 1175 | // pack, do nothing: we don't know which of its arguments to look |
| 1176 | // at, so we have to wait until all of the parameter packs in this |
| 1177 | // expansion have arguments. |
| 1178 | if (isa<SubstTemplateTypeParmPackType>(Param)) |
| 1179 | return Sema::TDK_Success; |
| 1180 | |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1181 | // Check the cv-qualifiers on the parameter and argument types. |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1182 | CanQualType CanParam = S.Context.getCanonicalType(Param); |
| 1183 | CanQualType CanArg = S.Context.getCanonicalType(Arg); |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1184 | if (!(TDF & TDF_IgnoreQualifiers)) { |
| 1185 | if (TDF & TDF_ParamWithReferenceType) { |
Douglas Gregor | 1d684c2 | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1186 | if (hasInconsistentOrSupersetQualifiersOf(Param, Arg)) |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1187 | return Sema::TDK_NonDeducedMismatch; |
John McCall | 0856906 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 1188 | } else if (!IsPossiblyOpaquelyQualifiedType(Param)) { |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1189 | if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1190 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1191 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1192 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1193 | // If the parameter type is not dependent, there is nothing to deduce. |
| 1194 | if (!Param->isDependentType()) { |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1195 | if (!(TDF & TDF_SkipNonDependent)) { |
| 1196 | bool NonDeduced = (TDF & TDF_InOverloadResolution)? |
| 1197 | !S.isSameOrCompatibleFunctionType(CanParam, CanArg) : |
| 1198 | Param != Arg; |
| 1199 | if (NonDeduced) { |
| 1200 | return Sema::TDK_NonDeducedMismatch; |
| 1201 | } |
| 1202 | } |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1203 | return Sema::TDK_Success; |
| 1204 | } |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1205 | } else if (!Param->isDependentType()) { |
| 1206 | CanQualType ParamUnqualType = CanParam.getUnqualifiedType(), |
| 1207 | ArgUnqualType = CanArg.getUnqualifiedType(); |
| 1208 | bool Success = (TDF & TDF_InOverloadResolution)? |
| 1209 | S.isSameOrCompatibleFunctionType(ParamUnqualType, |
| 1210 | ArgUnqualType) : |
| 1211 | ParamUnqualType == ArgUnqualType; |
| 1212 | if (Success) |
| 1213 | return Sema::TDK_Success; |
Douglas Gregor | cf0b47d | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1214 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1215 | |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1216 | switch (Param->getTypeClass()) { |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1217 | // Non-canonical types cannot appear here. |
| 1218 | #define NON_CANONICAL_TYPE(Class, Base) \ |
| 1219 | case Type::Class: llvm_unreachable("deducing non-canonical type: " #Class); |
| 1220 | #define TYPE(Class, Base) |
| 1221 | #include "clang/AST/TypeNodes.def" |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1222 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1223 | case Type::TemplateTypeParm: |
| 1224 | case Type::SubstTemplateTypeParmPack: |
| 1225 | llvm_unreachable("Type nodes handled above"); |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1226 | |
| 1227 | // These types cannot be dependent, so simply check whether the types are |
| 1228 | // the same. |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1229 | case Type::Builtin: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1230 | case Type::VariableArray: |
| 1231 | case Type::Vector: |
| 1232 | case Type::FunctionNoProto: |
| 1233 | case Type::Record: |
| 1234 | case Type::Enum: |
| 1235 | case Type::ObjCObject: |
| 1236 | case Type::ObjCInterface: |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1237 | case Type::ObjCObjectPointer: { |
| 1238 | if (TDF & TDF_SkipNonDependent) |
| 1239 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1240 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1241 | if (TDF & TDF_IgnoreQualifiers) { |
| 1242 | Param = Param.getUnqualifiedType(); |
| 1243 | Arg = Arg.getUnqualifiedType(); |
| 1244 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1245 | |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1246 | return Param == Arg? Sema::TDK_Success : Sema::TDK_NonDeducedMismatch; |
| 1247 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1248 | |
| 1249 | // _Complex T [placeholder extension] |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1250 | case Type::Complex: |
| 1251 | if (const ComplexType *ComplexArg = Arg->getAs<ComplexType>()) |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1252 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1253 | cast<ComplexType>(Param)->getElementType(), |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1254 | ComplexArg->getElementType(), |
| 1255 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1256 | |
| 1257 | return Sema::TDK_NonDeducedMismatch; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1258 | |
| 1259 | // _Atomic T [extension] |
| 1260 | case Type::Atomic: |
| 1261 | if (const AtomicType *AtomicArg = Arg->getAs<AtomicType>()) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1262 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1263 | cast<AtomicType>(Param)->getValueType(), |
| 1264 | AtomicArg->getValueType(), |
| 1265 | Info, Deduced, TDF); |
| 1266 | |
| 1267 | return Sema::TDK_NonDeducedMismatch; |
| 1268 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1269 | // T * |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1270 | case Type::Pointer: { |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1271 | QualType PointeeType; |
| 1272 | if (const PointerType *PointerArg = Arg->getAs<PointerType>()) { |
| 1273 | PointeeType = PointerArg->getPointeeType(); |
| 1274 | } else if (const ObjCObjectPointerType *PointerArg |
| 1275 | = Arg->getAs<ObjCObjectPointerType>()) { |
| 1276 | PointeeType = PointerArg->getPointeeType(); |
| 1277 | } else { |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1278 | return Sema::TDK_NonDeducedMismatch; |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1279 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1280 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1281 | unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1282 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1283 | cast<PointerType>(Param)->getPointeeType(), |
John McCall | bb4ea81 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1284 | PointeeType, |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1285 | Info, Deduced, SubTDF); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1286 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1287 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1288 | // T & |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1289 | case Type::LValueReference: { |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 1290 | const LValueReferenceType *ReferenceArg = |
| 1291 | Arg->getAs<LValueReferenceType>(); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1292 | if (!ReferenceArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1293 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1294 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1295 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1296 | cast<LValueReferenceType>(Param)->getPointeeType(), |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1297 | ReferenceArg->getPointeeType(), Info, Deduced, 0); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1298 | } |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1299 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1300 | // T && [C++0x] |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1301 | case Type::RValueReference: { |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 1302 | const RValueReferenceType *ReferenceArg = |
| 1303 | Arg->getAs<RValueReferenceType>(); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1304 | if (!ReferenceArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1305 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1306 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1307 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1308 | cast<RValueReferenceType>(Param)->getPointeeType(), |
| 1309 | ReferenceArg->getPointeeType(), |
| 1310 | Info, Deduced, 0); |
Douglas Gregor | 5cdac0a | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1311 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1312 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1313 | // T [] (implied, but not stated explicitly) |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1314 | case Type::IncompleteArray: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1315 | const IncompleteArrayType *IncompleteArrayArg = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1316 | S.Context.getAsIncompleteArrayType(Arg); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1317 | if (!IncompleteArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1318 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1319 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1320 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1321 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1322 | S.Context.getAsIncompleteArrayType(Param)->getElementType(), |
| 1323 | IncompleteArrayArg->getElementType(), |
| 1324 | Info, Deduced, SubTDF); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1325 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1326 | |
| 1327 | // T [integer-constant] |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1328 | case Type::ConstantArray: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1329 | const ConstantArrayType *ConstantArrayArg = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1330 | S.Context.getAsConstantArrayType(Arg); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1331 | if (!ConstantArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1332 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1333 | |
| 1334 | const ConstantArrayType *ConstantArrayParm = |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1335 | S.Context.getAsConstantArrayType(Param); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1336 | if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1337 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1338 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1339 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1340 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1341 | ConstantArrayParm->getElementType(), |
| 1342 | ConstantArrayArg->getElementType(), |
| 1343 | Info, Deduced, SubTDF); |
Anders Carlsson | 35533d1 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1346 | // type [i] |
| 1347 | case Type::DependentSizedArray: { |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1348 | const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1349 | if (!ArrayArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1350 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1351 | |
John McCall | f733268 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1352 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
| 1353 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1354 | // Check the element type of the arrays |
| 1355 | const DependentSizedArrayType *DependentArrayParm |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1356 | = S.Context.getAsDependentSizedArrayType(Param); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1357 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1358 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1359 | DependentArrayParm->getElementType(), |
| 1360 | ArrayArg->getElementType(), |
| 1361 | Info, Deduced, SubTDF)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1362 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1363 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1364 | // Determine the array bound is something we can deduce. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1365 | NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1366 | = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr()); |
| 1367 | if (!NTTP) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1368 | return Sema::TDK_Success; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1369 | |
| 1370 | // We can perform template argument deduction for the given non-type |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1371 | // template parameter. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1372 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1373 | "Cannot deduce non-type template argument at depth > 0"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1374 | if (const ConstantArrayType *ConstantArrayArg |
Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1375 | = dyn_cast<ConstantArrayType>(ArrayArg)) { |
| 1376 | llvm::APSInt Size(ConstantArrayArg->getSize()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1377 | return DeduceNonTypeTemplateArgument(S, NTTP, Size, |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1378 | S.Context.getSizeType(), |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1379 | /*ArrayBound=*/true, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1380 | Info, Deduced); |
Anders Carlsson | 3a106e0 | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1381 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1382 | if (const DependentSizedArrayType *DependentArrayArg |
| 1383 | = dyn_cast<DependentSizedArrayType>(ArrayArg)) |
Douglas Gregor | 7a49ead | 2010-12-22 23:15:38 +0000 | [diff] [blame] | 1384 | if (DependentArrayArg->getSizeExpr()) |
| 1385 | return DeduceNonTypeTemplateArgument(S, NTTP, |
| 1386 | DependentArrayArg->getSizeExpr(), |
| 1387 | Info, Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1388 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1389 | // Incomplete type does not match a dependently-sized array type |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1390 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1391 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1392 | |
| 1393 | // type(*)(T) |
| 1394 | // T(*)() |
| 1395 | // T(*)(T) |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1396 | case Type::FunctionProto: { |
Douglas Gregor | 85f240c | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1397 | unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1398 | const FunctionProtoType *FunctionProtoArg = |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1399 | dyn_cast<FunctionProtoType>(Arg); |
| 1400 | if (!FunctionProtoArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1401 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1402 | |
| 1403 | const FunctionProtoType *FunctionProtoParam = |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1404 | cast<FunctionProtoType>(Param); |
Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1405 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1406 | if (FunctionProtoParam->getTypeQuals() |
Douglas Gregor | 54e462a | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1407 | != FunctionProtoArg->getTypeQuals() || |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1408 | FunctionProtoParam->getRefQualifier() |
Douglas Gregor | 54e462a | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1409 | != FunctionProtoArg->getRefQualifier() || |
| 1410 | FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1411 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 096e6ee | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1412 | |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1413 | // Check return types. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1414 | if (Sema::TemplateDeductionResult Result = |
| 1415 | DeduceTemplateArgumentsByTypeMatch( |
| 1416 | S, TemplateParams, FunctionProtoParam->getReturnType(), |
| 1417 | FunctionProtoArg->getReturnType(), Info, Deduced, 0)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1418 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1419 | |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 1420 | return DeduceTemplateArguments( |
| 1421 | S, TemplateParams, FunctionProtoParam->param_type_begin(), |
| 1422 | FunctionProtoParam->getNumParams(), |
| 1423 | FunctionProtoArg->param_type_begin(), |
| 1424 | FunctionProtoArg->getNumParams(), Info, Deduced, SubTDF); |
Anders Carlsson | 2128ec7 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1425 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1426 | |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1427 | case Type::InjectedClassName: { |
| 1428 | // Treat a template's injected-class-name as if the template |
| 1429 | // specialization type had been used. |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1430 | Param = cast<InjectedClassNameType>(Param) |
| 1431 | ->getInjectedSpecializationType(); |
John McCall | e78aac4 | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1432 | assert(isa<TemplateSpecializationType>(Param) && |
| 1433 | "injected class name is not a template specialization type"); |
| 1434 | // fall through |
| 1435 | } |
| 1436 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1437 | // template-name<T> (where template-name refers to a class template) |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1438 | // template-name<i> |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1439 | // TT<T> |
| 1440 | // TT<i> |
| 1441 | // TT<> |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1442 | case Type::TemplateSpecialization: { |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1443 | const TemplateSpecializationType *SpecParam = |
| 1444 | cast<TemplateSpecializationType>(Param); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1445 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1446 | // When Arg cannot be a derived class, we can just try to deduce template |
| 1447 | // arguments from the template-id. |
| 1448 | const RecordType *RecordT = Arg->getAs<RecordType>(); |
| 1449 | if (!(TDF & TDF_DerivedClass) || !RecordT) |
| 1450 | return DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg, Info, |
| 1451 | Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1452 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1453 | SmallVector<DeducedTemplateArgument, 8> DeducedOrig(Deduced.begin(), |
| 1454 | Deduced.end()); |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1455 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1456 | Sema::TemplateDeductionResult Result = DeduceTemplateArguments( |
| 1457 | S, TemplateParams, SpecParam, Arg, Info, Deduced); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1459 | if (Result == Sema::TDK_Success) |
| 1460 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1461 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1462 | // We cannot inspect base classes as part of deduction when the type |
| 1463 | // is incomplete, so either instantiate any templates necessary to |
| 1464 | // complete the type, or skip over it if it cannot be completed. |
| 1465 | if (!S.isCompleteType(Info.getLocation(), Arg)) |
| 1466 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1467 | |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1468 | // C++14 [temp.deduct.call] p4b3: |
| 1469 | // If P is a class and P has the form simple-template-id, then the |
| 1470 | // transformed A can be a derived class of the deduced A. Likewise if |
| 1471 | // P is a pointer to a class of the form simple-template-id, the |
| 1472 | // transformed A can be a pointer to a derived class pointed to by the |
| 1473 | // deduced A. |
| 1474 | // |
| 1475 | // These alternatives are considered only if type deduction would |
| 1476 | // otherwise fail. If they yield more than one possible deduced A, the |
| 1477 | // type deduction fails. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1478 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1479 | // Reset the incorrectly deduced argument from above. |
| 1480 | Deduced = DeducedOrig; |
| 1481 | |
| 1482 | // Use data recursion to crawl through the list of base classes. |
| 1483 | // Visited contains the set of nodes we have already visited, while |
| 1484 | // ToVisit is our stack of records that we still need to visit. |
| 1485 | llvm::SmallPtrSet<const RecordType *, 8> Visited; |
| 1486 | SmallVector<const RecordType *, 8> ToVisit; |
| 1487 | ToVisit.push_back(RecordT); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1488 | bool Successful = false; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1489 | SmallVector<DeducedTemplateArgument, 8> SuccessfulDeduced; |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1490 | while (!ToVisit.empty()) { |
| 1491 | // Retrieve the next class in the inheritance hierarchy. |
| 1492 | const RecordType *NextT = ToVisit.pop_back_val(); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1493 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1494 | // If we have already seen this type, skip it. |
| 1495 | if (!Visited.insert(NextT).second) |
| 1496 | continue; |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1497 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1498 | // If this is a base class, try to perform template argument |
| 1499 | // deduction from it. |
| 1500 | if (NextT != RecordT) { |
| 1501 | TemplateDeductionInfo BaseInfo(Info.getLocation()); |
| 1502 | Sema::TemplateDeductionResult BaseResult = |
| 1503 | DeduceTemplateArguments(S, TemplateParams, SpecParam, |
| 1504 | QualType(NextT, 0), BaseInfo, Deduced); |
| 1505 | |
| 1506 | // If template argument deduction for this base was successful, |
| 1507 | // note that we had some success. Otherwise, ignore any deductions |
| 1508 | // from this base class. |
| 1509 | if (BaseResult == Sema::TDK_Success) { |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1510 | // If we've already seen some success, then deduction fails due to |
| 1511 | // an ambiguity (temp.deduct.call p5). |
| 1512 | if (Successful) |
| 1513 | return Sema::TDK_MiscellaneousDeductionFailure; |
| 1514 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1515 | Successful = true; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1516 | std::swap(SuccessfulDeduced, Deduced); |
| 1517 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1518 | Info.Param = BaseInfo.Param; |
| 1519 | Info.FirstArg = BaseInfo.FirstArg; |
| 1520 | Info.SecondArg = BaseInfo.SecondArg; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | Deduced = DeducedOrig; |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1524 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1525 | |
Faisal Vali | 683b074 | 2016-05-19 02:28:21 +0000 | [diff] [blame] | 1526 | // Visit base classes |
| 1527 | CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl()); |
| 1528 | for (const auto &Base : Next->bases()) { |
| 1529 | assert(Base.getType()->isRecordType() && |
| 1530 | "Base class that isn't a record?"); |
| 1531 | ToVisit.push_back(Base.getType()->getAs<RecordType>()); |
| 1532 | } |
| 1533 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1534 | |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1535 | if (Successful) { |
| 1536 | std::swap(SuccessfulDeduced, Deduced); |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1537 | return Sema::TDK_Success; |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1538 | } |
Richard Smith | 9b296e3 | 2016-04-25 19:09:05 +0000 | [diff] [blame] | 1539 | |
Douglas Gregor | e81f3e7 | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1540 | return Result; |
Douglas Gregor | 4fbe3e3 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1543 | // T type::* |
| 1544 | // T T::* |
| 1545 | // T (type::*)() |
| 1546 | // type (T::*)() |
| 1547 | // type (type::*)(T) |
| 1548 | // type (T::*)(T) |
| 1549 | // T (type::*)(T) |
| 1550 | // T (T::*)() |
| 1551 | // T (T::*)(T) |
| 1552 | case Type::MemberPointer: { |
| 1553 | const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); |
| 1554 | const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); |
| 1555 | if (!MemPtrArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1556 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1557 | |
David Majnemer | a381cda | 2015-11-30 20:34:28 +0000 | [diff] [blame] | 1558 | QualType ParamPointeeType = MemPtrParam->getPointeeType(); |
| 1559 | if (ParamPointeeType->isFunctionType()) |
| 1560 | S.adjustMemberFunctionCC(ParamPointeeType, /*IsStatic=*/true, |
| 1561 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
| 1562 | QualType ArgPointeeType = MemPtrArg->getPointeeType(); |
| 1563 | if (ArgPointeeType->isFunctionType()) |
| 1564 | S.adjustMemberFunctionCC(ArgPointeeType, /*IsStatic=*/true, |
| 1565 | /*IsCtorOrDtor=*/false, Info.getLocation()); |
| 1566 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1567 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1568 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
David Majnemer | a381cda | 2015-11-30 20:34:28 +0000 | [diff] [blame] | 1569 | ParamPointeeType, |
| 1570 | ArgPointeeType, |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1571 | Info, Deduced, |
| 1572 | TDF & TDF_IgnoreQualifiers)) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1573 | return Result; |
| 1574 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1575 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1576 | QualType(MemPtrParam->getClass(), 0), |
| 1577 | QualType(MemPtrArg->getClass(), 0), |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1578 | Info, Deduced, |
Douglas Gregor | 194ea69 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1579 | TDF & TDF_IgnoreQualifiers); |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
Anders Carlsson | 15f1dd1 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1582 | // (clang extension) |
| 1583 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1584 | // type(^)(T) |
| 1585 | // T(^)() |
| 1586 | // T(^)(T) |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1587 | case Type::BlockPointer: { |
| 1588 | const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); |
| 1589 | const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1590 | |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1591 | if (!BlockPtrArg) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1592 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1593 | |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1594 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1595 | BlockPtrParam->getPointeeType(), |
| 1596 | BlockPtrArg->getPointeeType(), |
| 1597 | Info, Deduced, 0); |
Anders Carlsson | a767eee | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1600 | // (clang extension) |
| 1601 | // |
| 1602 | // T __attribute__(((ext_vector_type(<integral constant>)))) |
| 1603 | case Type::ExtVector: { |
| 1604 | const ExtVectorType *VectorParam = cast<ExtVectorType>(Param); |
| 1605 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1606 | // Make sure that the vectors have the same number of elements. |
| 1607 | if (VectorParam->getNumElements() != VectorArg->getNumElements()) |
| 1608 | return Sema::TDK_NonDeducedMismatch; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1609 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1610 | // Perform deduction on the element types. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1611 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1612 | VectorParam->getElementType(), |
| 1613 | VectorArg->getElementType(), |
| 1614 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1615 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1616 | |
| 1617 | if (const DependentSizedExtVectorType *VectorArg |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1618 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1619 | // We can't check the number of elements, since the argument has a |
| 1620 | // dependent number of elements. This can only occur during partial |
| 1621 | // ordering. |
| 1622 | |
| 1623 | // Perform deduction on the element types. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1624 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1625 | VectorParam->getElementType(), |
| 1626 | VectorArg->getElementType(), |
| 1627 | Info, Deduced, TDF); |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1628 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1629 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1630 | return Sema::TDK_NonDeducedMismatch; |
| 1631 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1632 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1633 | // (clang extension) |
| 1634 | // |
| 1635 | // T __attribute__(((ext_vector_type(N)))) |
| 1636 | case Type::DependentSizedExtVector: { |
| 1637 | const DependentSizedExtVectorType *VectorParam |
| 1638 | = cast<DependentSizedExtVectorType>(Param); |
| 1639 | |
| 1640 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1641 | // Perform deduction on the element types. |
| 1642 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1643 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1644 | VectorParam->getElementType(), |
| 1645 | VectorArg->getElementType(), |
| 1646 | Info, Deduced, TDF)) |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1647 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1648 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1649 | // Perform deduction on the vector size, if we can. |
| 1650 | NonTypeTemplateParmDecl *NTTP |
| 1651 | = getDeducedParameterFromExpr(VectorParam->getSizeExpr()); |
| 1652 | if (!NTTP) |
| 1653 | return Sema::TDK_Success; |
| 1654 | |
| 1655 | llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); |
| 1656 | ArgSize = VectorArg->getNumElements(); |
| 1657 | return DeduceNonTypeTemplateArgument(S, NTTP, ArgSize, S.Context.IntTy, |
| 1658 | false, Info, Deduced); |
| 1659 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1660 | |
| 1661 | if (const DependentSizedExtVectorType *VectorArg |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1662 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1663 | // Perform deduction on the element types. |
| 1664 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1665 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1666 | VectorParam->getElementType(), |
| 1667 | VectorArg->getElementType(), |
| 1668 | Info, Deduced, TDF)) |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1669 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1670 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1671 | // Perform deduction on the vector size, if we can. |
| 1672 | NonTypeTemplateParmDecl *NTTP |
| 1673 | = getDeducedParameterFromExpr(VectorParam->getSizeExpr()); |
| 1674 | if (!NTTP) |
| 1675 | return Sema::TDK_Success; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1676 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1677 | return DeduceNonTypeTemplateArgument(S, NTTP, VectorArg->getSizeExpr(), |
| 1678 | Info, Deduced); |
| 1679 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1680 | |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1681 | return Sema::TDK_NonDeducedMismatch; |
| 1682 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 1683 | |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1684 | case Type::TypeOfExpr: |
| 1685 | case Type::TypeOf: |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 1686 | case Type::DependentName: |
Douglas Gregor | 39c0272 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1687 | case Type::UnresolvedUsing: |
| 1688 | case Type::Decltype: |
| 1689 | case Type::UnaryTransform: |
| 1690 | case Type::Auto: |
| 1691 | case Type::DependentTemplateSpecialization: |
| 1692 | case Type::PackExpansion: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 1693 | case Type::Pipe: |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1694 | // No template argument deduction for these types |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1695 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1698 | llvm_unreachable("Invalid Type Class!"); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1701 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1702 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1703 | TemplateParameterList *TemplateParams, |
| 1704 | const TemplateArgument &Param, |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1705 | TemplateArgument Arg, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1706 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 1707 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1708 | // If the template argument is a pack expansion, perform template argument |
| 1709 | // deduction against the pattern of that expansion. This only occurs during |
| 1710 | // partial ordering. |
| 1711 | if (Arg.isPackExpansion()) |
| 1712 | Arg = Arg.getPackExpansionPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1713 | |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1714 | switch (Param.getKind()) { |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1715 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1716 | llvm_unreachable("Null template argument in parameter list"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1717 | |
| 1718 | case TemplateArgument::Type: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1719 | if (Arg.getKind() == TemplateArgument::Type) |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1720 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1721 | Param.getAsType(), |
| 1722 | Arg.getAsType(), |
| 1723 | Info, Deduced, 0); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1724 | Info.FirstArg = Param; |
| 1725 | Info.SecondArg = Arg; |
| 1726 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1727 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1728 | case TemplateArgument::Template: |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1729 | if (Arg.getKind() == TemplateArgument::Template) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1730 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1731 | Param.getAsTemplate(), |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1732 | Arg.getAsTemplate(), Info, Deduced); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1733 | Info.FirstArg = Param; |
| 1734 | Info.SecondArg = Arg; |
| 1735 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1736 | |
| 1737 | case TemplateArgument::TemplateExpansion: |
| 1738 | llvm_unreachable("caller should handle pack expansions"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1739 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1740 | case TemplateArgument::Declaration: |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1741 | if (Arg.getKind() == TemplateArgument::Declaration && |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 1742 | isSameDeclaration(Param.getAsDecl(), Arg.getAsDecl())) |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 1743 | return Sema::TDK_Success; |
| 1744 | |
| 1745 | Info.FirstArg = Param; |
| 1746 | Info.SecondArg = Arg; |
| 1747 | return Sema::TDK_NonDeducedMismatch; |
| 1748 | |
| 1749 | case TemplateArgument::NullPtr: |
| 1750 | if (Arg.getKind() == TemplateArgument::NullPtr && |
| 1751 | S.Context.hasSameType(Param.getNullPtrType(), Arg.getNullPtrType())) |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1752 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1753 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1754 | Info.FirstArg = Param; |
| 1755 | Info.SecondArg = Arg; |
| 1756 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1757 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1758 | case TemplateArgument::Integral: |
| 1759 | if (Arg.getKind() == TemplateArgument::Integral) { |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1760 | if (hasSameExtendedValue(Param.getAsIntegral(), Arg.getAsIntegral())) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1761 | return Sema::TDK_Success; |
| 1762 | |
| 1763 | Info.FirstArg = Param; |
| 1764 | Info.SecondArg = Arg; |
| 1765 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1766 | } |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1767 | |
| 1768 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 1769 | Info.FirstArg = Param; |
| 1770 | Info.SecondArg = Arg; |
| 1771 | return Sema::TDK_NonDeducedMismatch; |
| 1772 | } |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1773 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1774 | Info.FirstArg = Param; |
| 1775 | Info.SecondArg = Arg; |
| 1776 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1777 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1778 | case TemplateArgument::Expression: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1779 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1780 | = getDeducedParameterFromExpr(Param.getAsExpr())) { |
| 1781 | if (Arg.getKind() == TemplateArgument::Integral) |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1782 | return DeduceNonTypeTemplateArgument(S, NTTP, |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1783 | Arg.getAsIntegral(), |
Douglas Gregor | 0a29a05 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1784 | Arg.getIntegralType(), |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1785 | /*ArrayBound=*/false, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1786 | Info, Deduced); |
Richard Smith | 38175a2 | 2016-09-28 22:08:38 +0000 | [diff] [blame^] | 1787 | if (Arg.getKind() == TemplateArgument::NullPtr) |
| 1788 | return DeduceNullPtrTemplateArgument(S, NTTP, Arg.getNullPtrType(), |
| 1789 | Info, Deduced); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1790 | if (Arg.getKind() == TemplateArgument::Expression) |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1791 | return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(), |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1792 | Info, Deduced); |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 1793 | if (Arg.getKind() == TemplateArgument::Declaration) |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1794 | return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(), |
Douglas Gregor | 2bb756a | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 1795 | Info, Deduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1796 | |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1797 | Info.FirstArg = Param; |
| 1798 | Info.SecondArg = Arg; |
| 1799 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1800 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1801 | |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1802 | // Can't deduce anything, but that's okay. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1803 | return Sema::TDK_Success; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1804 | } |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 1805 | case TemplateArgument::Pack: |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1806 | llvm_unreachable("Argument packs should be expanded by the caller!"); |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1807 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1808 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1809 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1812 | /// \brief Determine whether there is a template argument to be used for |
| 1813 | /// deduction. |
| 1814 | /// |
| 1815 | /// This routine "expands" argument packs in-place, overriding its input |
| 1816 | /// parameters so that \c Args[ArgIdx] will be the available template argument. |
| 1817 | /// |
| 1818 | /// \returns true if there is another template argument (which will be at |
| 1819 | /// \c Args[ArgIdx]), false otherwise. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1820 | static bool hasTemplateArgumentForDeduction(const TemplateArgument *&Args, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1821 | unsigned &ArgIdx, |
| 1822 | unsigned &NumArgs) { |
| 1823 | if (ArgIdx == NumArgs) |
| 1824 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1825 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1826 | const TemplateArgument &Arg = Args[ArgIdx]; |
| 1827 | if (Arg.getKind() != TemplateArgument::Pack) |
| 1828 | return true; |
| 1829 | |
| 1830 | assert(ArgIdx == NumArgs - 1 && "Pack not at the end of argument list?"); |
| 1831 | Args = Arg.pack_begin(); |
| 1832 | NumArgs = Arg.pack_size(); |
| 1833 | ArgIdx = 0; |
| 1834 | return ArgIdx < NumArgs; |
| 1835 | } |
| 1836 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1837 | /// \brief Determine whether the given set of template arguments has a pack |
| 1838 | /// expansion that is not the last template argument. |
| 1839 | static bool hasPackExpansionBeforeEnd(const TemplateArgument *Args, |
| 1840 | unsigned NumArgs) { |
| 1841 | unsigned ArgIdx = 0; |
| 1842 | while (ArgIdx < NumArgs) { |
| 1843 | const TemplateArgument &Arg = Args[ArgIdx]; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1844 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1845 | // Unwrap argument packs. |
| 1846 | if (Args[ArgIdx].getKind() == TemplateArgument::Pack) { |
| 1847 | Args = Arg.pack_begin(); |
| 1848 | NumArgs = Arg.pack_size(); |
| 1849 | ArgIdx = 0; |
| 1850 | continue; |
| 1851 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1852 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1853 | ++ArgIdx; |
| 1854 | if (ArgIdx == NumArgs) |
| 1855 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1856 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1857 | if (Arg.isPackExpansion()) |
| 1858 | return true; |
| 1859 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1860 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1861 | return false; |
| 1862 | } |
| 1863 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1864 | static Sema::TemplateDeductionResult |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1865 | DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1866 | const TemplateArgument *Params, unsigned NumParams, |
| 1867 | const TemplateArgument *Args, unsigned NumArgs, |
| 1868 | TemplateDeductionInfo &Info, |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1869 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 1870 | bool NumberOfArgumentsMustMatch) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1871 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1872 | // If the template argument list of P contains a pack expansion that is not |
| 1873 | // the last template argument, the entire template argument list is a |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1874 | // non-deduced context. |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1875 | if (hasPackExpansionBeforeEnd(Params, NumParams)) |
| 1876 | return Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1877 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1878 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1879 | // If P has a form that contains <T> or <i>, then each argument Pi of the |
| 1880 | // respective template argument list P is compared with the corresponding |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1881 | // argument Ai of the corresponding template argument list of A. |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1882 | unsigned ArgIdx = 0, ParamIdx = 0; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1883 | for (; hasTemplateArgumentForDeduction(Params, ParamIdx, NumParams); |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1884 | ++ParamIdx) { |
| 1885 | if (!Params[ParamIdx].isPackExpansion()) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1886 | // The simple case: deduce template arguments by matching Pi and Ai. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1887 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1888 | // Check whether we have enough arguments. |
| 1889 | if (!hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs)) |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1890 | return NumberOfArgumentsMustMatch ? Sema::TDK_TooFewArguments |
| 1891 | : Sema::TDK_Success; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1892 | |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1893 | if (Args[ArgIdx].isPackExpansion()) { |
| 1894 | // FIXME: We follow the logic of C++0x [temp.deduct.type]p22 here, |
| 1895 | // but applied to pack expansions that are template arguments. |
Richard Smith | 44ecdbd | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 1896 | return Sema::TDK_MiscellaneousDeductionFailure; |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1897 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1898 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1899 | // Perform deduction for this Pi/Ai pair. |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1900 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | 2fcb863 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1901 | = DeduceTemplateArguments(S, TemplateParams, |
| 1902 | Params[ParamIdx], Args[ArgIdx], |
| 1903 | Info, Deduced)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1904 | return Result; |
| 1905 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1906 | // Move to the next argument. |
| 1907 | ++ArgIdx; |
| 1908 | continue; |
| 1909 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1910 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1911 | // The parameter is a pack expansion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1912 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1913 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1914 | // If Pi is a pack expansion, then the pattern of Pi is compared with |
| 1915 | // each remaining argument in the template argument list of A. Each |
| 1916 | // comparison deduces template arguments for subsequent positions in the |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1917 | // template parameter packs expanded by Pi. |
| 1918 | TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1919 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1920 | // FIXME: If there are no remaining arguments, we can bail out early |
| 1921 | // and set any deduced parameter packs to an empty argument pack. |
| 1922 | // The latter part of this is a (minor) correctness issue. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1923 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1924 | // Prepare to deduce the packs within the pattern. |
| 1925 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1926 | |
| 1927 | // Keep track of the deduced template arguments for each parameter pack |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1928 | // expanded by this pack expansion (the outer index) and for each |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1929 | // template argument (the inner SmallVectors). |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1930 | bool HasAnyArguments = false; |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1931 | for (; hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs); ++ArgIdx) { |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1932 | HasAnyArguments = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1933 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1934 | // Deduce template arguments from the pattern. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1935 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1936 | = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx], |
| 1937 | Info, Deduced)) |
| 1938 | return Result; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1939 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1940 | PackScope.nextPackElement(); |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1941 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1942 | |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1943 | // Build argument packs for each of the parameter packs expanded by this |
| 1944 | // pack expansion. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 1945 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1946 | return Result; |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1947 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1948 | |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1949 | return Sema::TDK_Success; |
| 1950 | } |
| 1951 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1952 | static Sema::TemplateDeductionResult |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1953 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1954 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1955 | const TemplateArgumentList &ParamList, |
| 1956 | const TemplateArgumentList &ArgList, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1957 | TemplateDeductionInfo &Info, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 1958 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1959 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | 7baabef | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1960 | ParamList.data(), ParamList.size(), |
| 1961 | ArgList.data(), ArgList.size(), |
Erik Pilkington | 6a16ac0 | 2016-06-28 23:05:09 +0000 | [diff] [blame] | 1962 | Info, Deduced, false); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1963 | } |
| 1964 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1965 | /// \brief Determine whether two template arguments are the same. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1966 | static bool isSameTemplateArg(ASTContext &Context, |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1967 | const TemplateArgument &X, |
| 1968 | const TemplateArgument &Y) { |
| 1969 | if (X.getKind() != Y.getKind()) |
| 1970 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1971 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1972 | switch (X.getKind()) { |
| 1973 | case TemplateArgument::Null: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1974 | llvm_unreachable("Comparing NULL template argument"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1975 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1976 | case TemplateArgument::Type: |
| 1977 | return Context.getCanonicalType(X.getAsType()) == |
| 1978 | Context.getCanonicalType(Y.getAsType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1979 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1980 | case TemplateArgument::Declaration: |
David Blaikie | 0f62c8d | 2014-10-16 04:21:25 +0000 | [diff] [blame] | 1981 | return isSameDeclaration(X.getAsDecl(), Y.getAsDecl()); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 1982 | |
| 1983 | case TemplateArgument::NullPtr: |
| 1984 | return Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1985 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1986 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1987 | case TemplateArgument::TemplateExpansion: |
| 1988 | return Context.getCanonicalTemplateName( |
| 1989 | X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() == |
| 1990 | Context.getCanonicalTemplateName( |
| 1991 | Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1992 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1993 | case TemplateArgument::Integral: |
Benjamin Kramer | 6003ad5 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1994 | return X.getAsIntegral() == Y.getAsIntegral(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1995 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1996 | case TemplateArgument::Expression: { |
| 1997 | llvm::FoldingSetNodeID XID, YID; |
| 1998 | X.getAsExpr()->Profile(XID, Context, true); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1999 | Y.getAsExpr()->Profile(YID, Context, true); |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2000 | return XID == YID; |
| 2001 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2002 | |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2003 | case TemplateArgument::Pack: |
| 2004 | if (X.pack_size() != Y.pack_size()) |
| 2005 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2006 | |
| 2007 | for (TemplateArgument::pack_iterator XP = X.pack_begin(), |
| 2008 | XPEnd = X.pack_end(), |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2009 | YP = Y.pack_begin(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2010 | XP != XPEnd; ++XP, ++YP) |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2011 | if (!isSameTemplateArg(Context, *XP, *YP)) |
| 2012 | return false; |
| 2013 | |
| 2014 | return true; |
| 2015 | } |
| 2016 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2017 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 705c900 | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 2018 | } |
| 2019 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2020 | /// \brief Allocate a TemplateArgumentLoc where all locations have |
| 2021 | /// been initialized to the given location. |
| 2022 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2023 | /// \param Arg The template argument we are producing template argument |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2024 | /// location information for. |
| 2025 | /// |
| 2026 | /// \param NTTPType For a declaration template argument, the type of |
| 2027 | /// the non-type template parameter that corresponds to this template |
| 2028 | /// argument. |
| 2029 | /// |
| 2030 | /// \param Loc The source location to use for the resulting template |
| 2031 | /// argument. |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2032 | TemplateArgumentLoc |
| 2033 | Sema::getTrivialTemplateArgumentLoc(const TemplateArgument &Arg, |
| 2034 | QualType NTTPType, SourceLocation Loc) { |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2035 | switch (Arg.getKind()) { |
| 2036 | case TemplateArgument::Null: |
| 2037 | llvm_unreachable("Can't get a NULL template argument here"); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2038 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2039 | case TemplateArgument::Type: |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2040 | return TemplateArgumentLoc( |
| 2041 | Arg, Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc)); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2042 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2043 | case TemplateArgument::Declaration: { |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2044 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
| 2045 | .getAs<Expr>(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2046 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2047 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2048 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2049 | case TemplateArgument::NullPtr: { |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2050 | Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
| 2051 | .getAs<Expr>(); |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2052 | return TemplateArgumentLoc(TemplateArgument(NTTPType, /*isNullPtr*/true), |
| 2053 | E); |
| 2054 | } |
| 2055 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2056 | case TemplateArgument::Integral: { |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2057 | Expr *E = |
| 2058 | BuildExpressionFromIntegralTemplateArgument(Arg, Loc).getAs<Expr>(); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2059 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2060 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2061 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2062 | case TemplateArgument::Template: |
| 2063 | case TemplateArgument::TemplateExpansion: { |
| 2064 | NestedNameSpecifierLocBuilder Builder; |
| 2065 | TemplateName Template = Arg.getAsTemplate(); |
| 2066 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2067 | Builder.MakeTrivial(Context, DTN->getQualifier(), Loc); |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 2068 | else if (QualifiedTemplateName *QTN = |
| 2069 | Template.getAsQualifiedTemplateName()) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2070 | Builder.MakeTrivial(Context, QTN->getQualifier(), Loc); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2071 | |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2072 | if (Arg.getKind() == TemplateArgument::Template) |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2073 | return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(Context), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2074 | Loc); |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2075 | |
| 2076 | return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(Context), |
Douglas Gregor | 9d80212 | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2077 | Loc, Loc); |
| 2078 | } |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2079 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2080 | case TemplateArgument::Expression: |
| 2081 | return TemplateArgumentLoc(Arg, Arg.getAsExpr()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2082 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2083 | case TemplateArgument::Pack: |
| 2084 | return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo()); |
| 2085 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2086 | |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2087 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | |
| 2091 | /// \brief Convert the given deduced template argument and add it to the set of |
| 2092 | /// fully-converted template arguments. |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2093 | static bool |
| 2094 | ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param, |
| 2095 | DeducedTemplateArgument Arg, |
| 2096 | NamedDecl *Template, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2097 | TemplateDeductionInfo &Info, |
| 2098 | bool InFunctionTemplate, |
| 2099 | SmallVectorImpl<TemplateArgument> &Output) { |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2100 | // First, for a non-type template parameter type that is |
| 2101 | // initialized by a declaration, we need the type of the |
| 2102 | // corresponding non-type template parameter. |
| 2103 | QualType NTTPType; |
| 2104 | if (NonTypeTemplateParmDecl *NTTP = |
| 2105 | dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2106 | NTTPType = NTTP->getType(); |
| 2107 | if (NTTPType->isDependentType()) { |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2108 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Output); |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2109 | NTTPType = S.SubstType(NTTPType, |
| 2110 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2111 | NTTP->getLocation(), |
| 2112 | NTTP->getDeclName()); |
| 2113 | if (NTTPType.isNull()) |
| 2114 | return true; |
| 2115 | } |
| 2116 | } |
| 2117 | |
| 2118 | auto ConvertArg = [&](DeducedTemplateArgument Arg, |
| 2119 | unsigned ArgumentPackIndex) { |
| 2120 | // Convert the deduced template argument into a template |
| 2121 | // argument that we can check, almost as if the user had written |
| 2122 | // the template argument explicitly. |
| 2123 | TemplateArgumentLoc ArgLoc = |
Richard Smith | 7873de0 | 2016-08-11 22:25:46 +0000 | [diff] [blame] | 2124 | S.getTrivialTemplateArgumentLoc(Arg, NTTPType, Info.getLocation()); |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2125 | |
| 2126 | // Check the template argument, converting it as necessary. |
| 2127 | return S.CheckTemplateArgument( |
| 2128 | Param, ArgLoc, Template, Template->getLocation(), |
| 2129 | Template->getSourceRange().getEnd(), ArgumentPackIndex, Output, |
| 2130 | InFunctionTemplate |
| 2131 | ? (Arg.wasDeducedFromArrayBound() ? Sema::CTAK_DeducedFromArrayBound |
| 2132 | : Sema::CTAK_Deduced) |
| 2133 | : Sema::CTAK_Specified); |
| 2134 | }; |
| 2135 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2136 | if (Arg.getKind() == TemplateArgument::Pack) { |
| 2137 | // This is a template argument pack, so check each of its arguments against |
| 2138 | // the template parameter. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2139 | SmallVector<TemplateArgument, 2> PackedArgsBuilder; |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 2140 | for (const auto &P : Arg.pack_elements()) { |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2141 | // When converting the deduced template argument, append it to the |
| 2142 | // general output list. We need to do this so that the template argument |
| 2143 | // checking logic has all of the prior template arguments available. |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 2144 | DeducedTemplateArgument InnerArg(P); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2145 | InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound()); |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2146 | assert(InnerArg.getKind() != TemplateArgument::Pack && |
| 2147 | "deduced nested pack"); |
| 2148 | if (ConvertArg(InnerArg, PackedArgsBuilder.size())) |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2149 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2150 | |
Douglas Gregor | 51bc571 | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2151 | // Move the converted template argument into our argument pack. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 2152 | PackedArgsBuilder.push_back(Output.pop_back_val()); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2153 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2154 | |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2155 | // If the pack is empty, we still need to substitute into the parameter |
| 2156 | // itself, in case that substitution fails. For non-type parameters, we did |
| 2157 | // this above. For type parameters, no substitution is ever required. |
| 2158 | auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param); |
| 2159 | if (TTP && PackedArgsBuilder.empty()) { |
| 2160 | // Set up a template instantiation context. |
| 2161 | LocalInstantiationScope Scope(S); |
| 2162 | Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, |
| 2163 | TTP, Output, |
| 2164 | Template->getSourceRange()); |
| 2165 | if (Inst.isInvalid()) |
| 2166 | return true; |
| 2167 | |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2168 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Output); |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2169 | if (!S.SubstDecl(TTP, S.CurContext, |
| 2170 | MultiLevelTemplateArgumentList(TemplateArgs))) |
| 2171 | return true; |
| 2172 | } |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2173 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2174 | // Create the resulting argument pack. |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 2175 | Output.push_back( |
| 2176 | TemplateArgument::CreatePackCopy(S.Context, PackedArgsBuilder)); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2177 | return false; |
| 2178 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2179 | |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2180 | return ConvertArg(Arg, 0); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2181 | } |
| 2182 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2183 | /// Complete template argument deduction for a class template partial |
| 2184 | /// specialization. |
| 2185 | static Sema::TemplateDeductionResult |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2186 | FinishTemplateArgumentDeduction(Sema &S, |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2187 | ClassTemplatePartialSpecializationDecl *Partial, |
| 2188 | const TemplateArgumentList &TemplateArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2189 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2190 | TemplateDeductionInfo &Info) { |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2191 | // Unevaluated SFINAE context. |
| 2192 | EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2193 | Sema::SFINAETrap Trap(S); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2194 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2195 | Sema::ContextRAII SavedContext(S, Partial); |
| 2196 | |
| 2197 | // C++ [temp.deduct.type]p2: |
| 2198 | // [...] or if any template argument remains neither deduced nor |
| 2199 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2200 | SmallVector<TemplateArgument, 4> Builder; |
Douglas Gregor | aef93f2 | 2011-01-04 22:23:38 +0000 | [diff] [blame] | 2201 | TemplateParameterList *PartialParams = Partial->getTemplateParameters(); |
| 2202 | for (unsigned I = 0, N = PartialParams->size(); I != N; ++I) { |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2203 | NamedDecl *Param = PartialParams->getParam(I); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2204 | if (Deduced[I].isNull()) { |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2205 | Info.Param = makeTemplateParameter(Param); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2206 | return Sema::TDK_Incomplete; |
| 2207 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2208 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2209 | // We have deduced this argument, so it still needs to be |
| 2210 | // checked and converted. |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2211 | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2212 | Partial, Info, false, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2213 | Builder)) { |
| 2214 | Info.Param = makeTemplateParameter(Param); |
| 2215 | // FIXME: These template arguments are temporary. Free them! |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2216 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2217 | return Sema::TDK_SubstitutionFailure; |
| 2218 | } |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2219 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2220 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2221 | // Form the template argument list from the deduced template arguments. |
| 2222 | TemplateArgumentList *DeducedArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2223 | = TemplateArgumentList::CreateCopy(S.Context, Builder); |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2224 | |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2225 | Info.reset(DeducedArgumentList); |
| 2226 | |
| 2227 | // Substitute the deduced template arguments into the template |
| 2228 | // arguments of the class template partial specialization, and |
| 2229 | // verify that the instantiated template arguments are both valid |
| 2230 | // and are equivalent to the template arguments originally provided |
| 2231 | // to the class template. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2232 | LocalInstantiationScope InstScope(S); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2233 | ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate(); |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2234 | const ASTTemplateArgumentListInfo *PartialTemplArgInfo |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2235 | = Partial->getTemplateArgsAsWritten(); |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2236 | const TemplateArgumentLoc *PartialTemplateArgs |
| 2237 | = PartialTemplArgInfo->getTemplateArgs(); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2238 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2239 | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, |
| 2240 | PartialTemplArgInfo->RAngleLoc); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2241 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2242 | if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs, |
Douglas Gregor | 0f3feb4 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2243 | InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { |
| 2244 | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; |
| 2245 | if (ParamIdx >= Partial->getTemplateParameters()->size()) |
| 2246 | ParamIdx = Partial->getTemplateParameters()->size() - 1; |
| 2247 | |
| 2248 | Decl *Param |
| 2249 | = const_cast<NamedDecl *>( |
| 2250 | Partial->getTemplateParameters()->getParam(ParamIdx)); |
| 2251 | Info.Param = makeTemplateParameter(Param); |
| 2252 | Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); |
| 2253 | return Sema::TDK_SubstitutionFailure; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2256 | SmallVector<TemplateArgument, 4> ConvertedInstArgs; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2257 | if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(), |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2258 | InstArgs, false, ConvertedInstArgs)) |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2259 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2260 | |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2261 | TemplateParameterList *TemplateParams |
| 2262 | = ClassTemplate->getTemplateParameters(); |
| 2263 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2264 | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2265 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { |
Douglas Gregor | 6699003 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 2266 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2267 | Info.FirstArg = TemplateArgs[I]; |
| 2268 | Info.SecondArg = InstArg; |
| 2269 | return Sema::TDK_NonDeducedMismatch; |
| 2270 | } |
| 2271 | } |
| 2272 | |
| 2273 | if (Trap.hasErrorOccurred()) |
| 2274 | return Sema::TDK_SubstitutionFailure; |
| 2275 | |
| 2276 | return Sema::TDK_Success; |
| 2277 | } |
| 2278 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2279 | /// \brief Perform template argument deduction to determine whether |
Larisse Voufo | 833b05a | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 2280 | /// the given template arguments match the given class template |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2281 | /// partial specialization per C++ [temp.class.spec.match]. |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2282 | Sema::TemplateDeductionResult |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2283 | Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2284 | const TemplateArgumentList &TemplateArgs, |
| 2285 | TemplateDeductionInfo &Info) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 2286 | if (Partial->isInvalidDecl()) |
| 2287 | return TDK_Invalid; |
| 2288 | |
Douglas Gregor | 170bc42 | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2289 | // C++ [temp.class.spec.match]p2: |
| 2290 | // A partial specialization matches a given actual template |
| 2291 | // argument list if the template arguments of the partial |
| 2292 | // specialization can be deduced from the actual template argument |
| 2293 | // list (14.8.2). |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2294 | |
| 2295 | // Unevaluated SFINAE context. |
| 2296 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2297 | SFINAETrap Trap(*this); |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2298 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2299 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2300 | Deduced.resize(Partial->getTemplateParameters()->size()); |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2301 | if (TemplateDeductionResult Result |
Chandler Carruth | c126311 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2302 | = ::DeduceTemplateArguments(*this, |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2303 | Partial->getTemplateParameters(), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2304 | Partial->getTemplateArgs(), |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2305 | TemplateArgs, Info, Deduced)) |
| 2306 | return Result; |
Douglas Gregor | 637d998 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 2307 | |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2308 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2309 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2310 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2311 | if (Inst.isInvalid()) |
Douglas Gregor | 181aa4a | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2312 | return TDK_InstantiationDepth; |
Douglas Gregor | b7ae10f | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2313 | |
Douglas Gregor | e141633 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2314 | if (Trap.hasErrorOccurred()) |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2315 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2316 | |
| 2317 | return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs, |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2318 | Deduced, Info); |
Douglas Gregor | 55ca8f6 | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2319 | } |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2320 | |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2321 | /// Complete template argument deduction for a variable template partial |
| 2322 | /// specialization. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2323 | /// TODO: Unify with ClassTemplatePartialSpecializationDecl version? |
| 2324 | /// May require unifying ClassTemplate(Partial)SpecializationDecl and |
| 2325 | /// VarTemplate(Partial)SpecializationDecl with a new data |
| 2326 | /// structure Template(Partial)SpecializationDecl, and |
| 2327 | /// using Template(Partial)SpecializationDecl as input type. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2328 | static Sema::TemplateDeductionResult FinishTemplateArgumentDeduction( |
| 2329 | Sema &S, VarTemplatePartialSpecializationDecl *Partial, |
| 2330 | const TemplateArgumentList &TemplateArgs, |
| 2331 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2332 | TemplateDeductionInfo &Info) { |
| 2333 | // Unevaluated SFINAE context. |
| 2334 | EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated); |
| 2335 | Sema::SFINAETrap Trap(S); |
| 2336 | |
| 2337 | // C++ [temp.deduct.type]p2: |
| 2338 | // [...] or if any template argument remains neither deduced nor |
| 2339 | // explicitly specified, template argument deduction fails. |
| 2340 | SmallVector<TemplateArgument, 4> Builder; |
| 2341 | TemplateParameterList *PartialParams = Partial->getTemplateParameters(); |
| 2342 | for (unsigned I = 0, N = PartialParams->size(); I != N; ++I) { |
| 2343 | NamedDecl *Param = PartialParams->getParam(I); |
| 2344 | if (Deduced[I].isNull()) { |
| 2345 | Info.Param = makeTemplateParameter(Param); |
| 2346 | return Sema::TDK_Incomplete; |
| 2347 | } |
| 2348 | |
| 2349 | // We have deduced this argument, so it still needs to be |
| 2350 | // checked and converted. |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2351 | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Partial, |
| 2352 | Info, false, Builder)) { |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2353 | Info.Param = makeTemplateParameter(Param); |
| 2354 | // FIXME: These template arguments are temporary. Free them! |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2355 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2356 | return Sema::TDK_SubstitutionFailure; |
| 2357 | } |
| 2358 | } |
| 2359 | |
| 2360 | // Form the template argument list from the deduced template arguments. |
| 2361 | TemplateArgumentList *DeducedArgumentList = TemplateArgumentList::CreateCopy( |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2362 | S.Context, Builder); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2363 | |
| 2364 | Info.reset(DeducedArgumentList); |
| 2365 | |
| 2366 | // Substitute the deduced template arguments into the template |
| 2367 | // arguments of the class template partial specialization, and |
| 2368 | // verify that the instantiated template arguments are both valid |
| 2369 | // and are equivalent to the template arguments originally provided |
| 2370 | // to the class template. |
| 2371 | LocalInstantiationScope InstScope(S); |
| 2372 | VarTemplateDecl *VarTemplate = Partial->getSpecializedTemplate(); |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2373 | const ASTTemplateArgumentListInfo *PartialTemplArgInfo |
| 2374 | = Partial->getTemplateArgsAsWritten(); |
| 2375 | const TemplateArgumentLoc *PartialTemplateArgs |
| 2376 | = PartialTemplArgInfo->getTemplateArgs(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2377 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2378 | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, |
| 2379 | PartialTemplArgInfo->RAngleLoc); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2380 | |
Enea Zaffanella | 6dbe187 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2381 | if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs, |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2382 | InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { |
| 2383 | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; |
| 2384 | if (ParamIdx >= Partial->getTemplateParameters()->size()) |
| 2385 | ParamIdx = Partial->getTemplateParameters()->size() - 1; |
| 2386 | |
| 2387 | Decl *Param = const_cast<NamedDecl *>( |
| 2388 | Partial->getTemplateParameters()->getParam(ParamIdx)); |
| 2389 | Info.Param = makeTemplateParameter(Param); |
| 2390 | Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); |
| 2391 | return Sema::TDK_SubstitutionFailure; |
| 2392 | } |
| 2393 | SmallVector<TemplateArgument, 4> ConvertedInstArgs; |
| 2394 | if (S.CheckTemplateArgumentList(VarTemplate, Partial->getLocation(), InstArgs, |
| 2395 | false, ConvertedInstArgs)) |
| 2396 | return Sema::TDK_SubstitutionFailure; |
| 2397 | |
| 2398 | TemplateParameterList *TemplateParams = VarTemplate->getTemplateParameters(); |
| 2399 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
| 2400 | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; |
| 2401 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { |
| 2402 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
| 2403 | Info.FirstArg = TemplateArgs[I]; |
| 2404 | Info.SecondArg = InstArg; |
| 2405 | return Sema::TDK_NonDeducedMismatch; |
| 2406 | } |
| 2407 | } |
| 2408 | |
| 2409 | if (Trap.hasErrorOccurred()) |
| 2410 | return Sema::TDK_SubstitutionFailure; |
| 2411 | |
| 2412 | return Sema::TDK_Success; |
| 2413 | } |
| 2414 | |
| 2415 | /// \brief Perform template argument deduction to determine whether |
| 2416 | /// the given template arguments match the given variable template |
| 2417 | /// partial specialization per C++ [temp.class.spec.match]. |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2418 | /// TODO: Unify with ClassTemplatePartialSpecializationDecl version? |
| 2419 | /// May require unifying ClassTemplate(Partial)SpecializationDecl and |
| 2420 | /// VarTemplate(Partial)SpecializationDecl with a new data |
| 2421 | /// structure Template(Partial)SpecializationDecl, and |
| 2422 | /// using Template(Partial)SpecializationDecl as input type. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2423 | Sema::TemplateDeductionResult |
| 2424 | Sema::DeduceTemplateArguments(VarTemplatePartialSpecializationDecl *Partial, |
| 2425 | const TemplateArgumentList &TemplateArgs, |
| 2426 | TemplateDeductionInfo &Info) { |
| 2427 | if (Partial->isInvalidDecl()) |
| 2428 | return TDK_Invalid; |
| 2429 | |
| 2430 | // C++ [temp.class.spec.match]p2: |
| 2431 | // A partial specialization matches a given actual template |
| 2432 | // argument list if the template arguments of the partial |
| 2433 | // specialization can be deduced from the actual template argument |
| 2434 | // list (14.8.2). |
| 2435 | |
| 2436 | // Unevaluated SFINAE context. |
| 2437 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
| 2438 | SFINAETrap Trap(*this); |
| 2439 | |
| 2440 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 2441 | Deduced.resize(Partial->getTemplateParameters()->size()); |
| 2442 | if (TemplateDeductionResult Result = ::DeduceTemplateArguments( |
| 2443 | *this, Partial->getTemplateParameters(), Partial->getTemplateArgs(), |
| 2444 | TemplateArgs, Info, Deduced)) |
| 2445 | return Result; |
| 2446 | |
| 2447 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2448 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2449 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2450 | if (Inst.isInvalid()) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2451 | return TDK_InstantiationDepth; |
| 2452 | |
| 2453 | if (Trap.hasErrorOccurred()) |
| 2454 | return Sema::TDK_SubstitutionFailure; |
| 2455 | |
| 2456 | return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs, |
| 2457 | Deduced, Info); |
| 2458 | } |
| 2459 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2460 | /// \brief Determine whether the given type T is a simple-template-id type. |
| 2461 | static bool isSimpleTemplateIdType(QualType T) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2462 | if (const TemplateSpecializationType *Spec |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2463 | = T->getAs<TemplateSpecializationType>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2464 | return Spec->getTemplateName().getAsTemplateDecl() != nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2465 | |
Douglas Gregor | fc516c9 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2466 | return false; |
| 2467 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2468 | |
| 2469 | /// \brief Substitute the explicitly-provided template arguments into the |
| 2470 | /// given function template according to C++ [temp.arg.explicit]. |
| 2471 | /// |
| 2472 | /// \param FunctionTemplate the function template into which the explicit |
| 2473 | /// template arguments will be substituted. |
| 2474 | /// |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2475 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2476 | /// arguments. |
| 2477 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2478 | /// \param Deduced the deduced template arguments, which will be populated |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2479 | /// with the converted and checked explicit template arguments. |
| 2480 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2481 | /// \param ParamTypes will be populated with the instantiated function |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2482 | /// parameters. |
| 2483 | /// |
| 2484 | /// \param FunctionType if non-NULL, the result type of the function template |
| 2485 | /// will also be instantiated and the pointed-to value will be updated with |
| 2486 | /// the instantiated function type. |
| 2487 | /// |
| 2488 | /// \param Info if substitution fails for any reason, this object will be |
| 2489 | /// populated with more information about the failure. |
| 2490 | /// |
| 2491 | /// \returns TDK_Success if substitution was successful, or some failure |
| 2492 | /// condition. |
| 2493 | Sema::TemplateDeductionResult |
| 2494 | Sema::SubstituteExplicitTemplateArguments( |
| 2495 | FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2496 | TemplateArgumentListInfo &ExplicitTemplateArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2497 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2498 | SmallVectorImpl<QualType> &ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2499 | QualType *FunctionType, |
| 2500 | TemplateDeductionInfo &Info) { |
| 2501 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 2502 | TemplateParameterList *TemplateParams |
| 2503 | = FunctionTemplate->getTemplateParameters(); |
| 2504 | |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2505 | if (ExplicitTemplateArgs.size() == 0) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2506 | // No arguments to substitute; just copy over the parameter types and |
| 2507 | // fill in the function type. |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2508 | for (auto P : Function->parameters()) |
Aaron Ballman | f6bf62e | 2014-03-07 15:12:56 +0000 | [diff] [blame] | 2509 | ParamTypes.push_back(P->getType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2510 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2511 | if (FunctionType) |
| 2512 | *FunctionType = Function->getType(); |
| 2513 | return TDK_Success; |
| 2514 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2515 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2516 | // Unevaluated SFINAE context. |
| 2517 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2518 | SFINAETrap Trap(*this); |
| 2519 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2520 | // C++ [temp.arg.explicit]p3: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2521 | // Template arguments that are present shall be specified in the |
| 2522 | // declaration order of their corresponding template-parameters. The |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2523 | // template argument list shall not specify more template-arguments than |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2524 | // there are corresponding template-parameters. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2525 | SmallVector<TemplateArgument, 4> Builder; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2526 | |
| 2527 | // Enter a new template instantiation context where we check the |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2528 | // explicitly-specified template arguments against this function template, |
| 2529 | // and then substitute them into the function parameter types. |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2530 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2531 | InstantiatingTemplate Inst(*this, Info.getLocation(), FunctionTemplate, |
| 2532 | DeducedArgs, |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2533 | ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution, |
| 2534 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2535 | if (Inst.isInvalid()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2536 | return TDK_InstantiationDepth; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2537 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2538 | if (CheckTemplateArgumentList(FunctionTemplate, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2539 | SourceLocation(), |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2540 | ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2541 | true, |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 2542 | Builder) || Trap.hasErrorOccurred()) { |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2543 | unsigned Index = Builder.size(); |
Douglas Gregor | 62c281a | 2010-05-09 01:26:06 +0000 | [diff] [blame] | 2544 | if (Index >= TemplateParams->size()) |
| 2545 | Index = TemplateParams->size() - 1; |
| 2546 | Info.Param = makeTemplateParameter(TemplateParams->getParam(Index)); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2547 | return TDK_InvalidExplicitArguments; |
Douglas Gregor | 1d72edd | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 2548 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2549 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2550 | // Form the template argument list from the explicitly-specified |
| 2551 | // template arguments. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2552 | TemplateArgumentList *ExplicitArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2553 | = TemplateArgumentList::CreateCopy(Context, Builder); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2554 | Info.reset(ExplicitArgumentList); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2555 | |
John McCall | 036855a | 2010-10-12 19:40:14 +0000 | [diff] [blame] | 2556 | // Template argument deduction and the final substitution should be |
| 2557 | // done in the context of the templated declaration. Explicit |
| 2558 | // argument substitution, on the other hand, needs to happen in the |
| 2559 | // calling context. |
| 2560 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
| 2561 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2562 | // If we deduced template arguments for a template parameter pack, |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2563 | // note that the template argument pack is partially substituted and record |
| 2564 | // the explicit template arguments. They'll be used as part of deduction |
| 2565 | // for this template parameter pack. |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2566 | for (unsigned I = 0, N = Builder.size(); I != N; ++I) { |
| 2567 | const TemplateArgument &Arg = Builder[I]; |
| 2568 | if (Arg.getKind() == TemplateArgument::Pack) { |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2569 | CurrentInstantiationScope->SetPartiallySubstitutedPack( |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2570 | TemplateParams->getParam(I), |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2571 | Arg.pack_begin(), |
| 2572 | Arg.pack_size()); |
| 2573 | break; |
| 2574 | } |
| 2575 | } |
| 2576 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2577 | const FunctionProtoType *Proto |
| 2578 | = Function->getType()->getAs<FunctionProtoType>(); |
| 2579 | assert(Proto && "Function template does not have a prototype?"); |
| 2580 | |
Richard Smith | 70b1304 | 2015-01-09 01:19:56 +0000 | [diff] [blame] | 2581 | // Isolate our substituted parameters from our caller. |
| 2582 | LocalInstantiationScope InstScope(*this, /*MergeWithOuterScope*/true); |
| 2583 | |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2584 | ExtParameterInfoBuilder ExtParamInfos; |
| 2585 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2586 | // Instantiate the types of each of the function parameters given the |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2587 | // explicitly-specified template arguments. If the function has a trailing |
| 2588 | // return type, substitute it after the arguments to ensure we substitute |
| 2589 | // in lexical order. |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2590 | if (Proto->hasTrailingReturn()) { |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2591 | if (SubstParmTypes(Function->getLocation(), Function->parameters(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2592 | Proto->getExtParameterInfosOrNull(), |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2593 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2594 | ParamTypes, /*params*/ nullptr, ExtParamInfos)) |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2595 | return TDK_SubstitutionFailure; |
| 2596 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2597 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2598 | // Instantiate the return type. |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2599 | QualType ResultType; |
| 2600 | { |
| 2601 | // C++11 [expr.prim.general]p3: |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2602 | // If a declaration declares a member function or member function |
| 2603 | // 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] | 2604 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2605 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2606 | // declarator. |
| 2607 | unsigned ThisTypeQuals = 0; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2608 | CXXRecordDecl *ThisContext = nullptr; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2609 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
| 2610 | ThisContext = Method->getParent(); |
| 2611 | ThisTypeQuals = Method->getTypeQualifiers(); |
| 2612 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2613 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2614 | CXXThisScopeRAII ThisScope(*this, ThisContext, ThisTypeQuals, |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2615 | getLangOpts().CPlusPlus11); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2616 | |
| 2617 | ResultType = |
| 2618 | SubstType(Proto->getReturnType(), |
| 2619 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 2620 | Function->getTypeSpecStartLoc(), Function->getDeclName()); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2621 | if (ResultType.isNull() || Trap.hasErrorOccurred()) |
| 2622 | return TDK_SubstitutionFailure; |
| 2623 | } |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2624 | |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2625 | // Instantiate the types of each of the function parameters given the |
| 2626 | // explicitly-specified template arguments if we didn't do so earlier. |
| 2627 | if (!Proto->hasTrailingReturn() && |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 2628 | SubstParmTypes(Function->getLocation(), Function->parameters(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2629 | Proto->getExtParameterInfosOrNull(), |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2630 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2631 | ParamTypes, /*params*/ nullptr, ExtParamInfos)) |
Richard Smith | 5e58029 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2632 | return TDK_SubstitutionFailure; |
| 2633 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2634 | if (FunctionType) { |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2635 | auto EPI = Proto->getExtProtoInfo(); |
| 2636 | EPI.ExtParameterInfos = ExtParamInfos.getPointerOrNull(ParamTypes.size()); |
Jordan Rose | 5c38272 | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2637 | *FunctionType = BuildFunctionType(ResultType, ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2638 | Function->getLocation(), |
Eli Friedman | d8725a9 | 2010-08-05 02:54:05 +0000 | [diff] [blame] | 2639 | Function->getDeclName(), |
John McCall | c8e321d | 2016-03-01 02:09:25 +0000 | [diff] [blame] | 2640 | EPI); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2641 | if (FunctionType->isNull() || Trap.hasErrorOccurred()) |
| 2642 | return TDK_SubstitutionFailure; |
| 2643 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2644 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2645 | // C++ [temp.arg.explicit]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2646 | // Trailing template arguments that can be deduced (14.8.2) may be |
| 2647 | // omitted from the list of explicit template-arguments. If all of the |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2648 | // template arguments can be deduced, they may all be omitted; in this |
| 2649 | // case, the empty template argument list <> itself may also be omitted. |
| 2650 | // |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2651 | // Take all of the explicitly-specified arguments and put them into |
| 2652 | // the set of deduced template arguments. Explicitly-specified |
| 2653 | // parameter packs, however, will be set to NULL since the deduction |
| 2654 | // mechanisms handle explicitly-specified argument packs directly. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2655 | Deduced.reserve(TemplateParams->size()); |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2656 | for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) { |
| 2657 | const TemplateArgument &Arg = ExplicitArgumentList->get(I); |
| 2658 | if (Arg.getKind() == TemplateArgument::Pack) |
| 2659 | Deduced.push_back(DeducedTemplateArgument()); |
| 2660 | else |
| 2661 | Deduced.push_back(Arg); |
| 2662 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2663 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2664 | return TDK_Success; |
| 2665 | } |
| 2666 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2667 | /// \brief Check whether the deduced argument type for a call to a function |
| 2668 | /// template matches the actual argument type per C++ [temp.deduct.call]p4. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2669 | static bool |
| 2670 | CheckOriginalCallArgDeduction(Sema &S, Sema::OriginalCallArg OriginalArg, |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2671 | QualType DeducedA) { |
| 2672 | ASTContext &Context = S.Context; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2673 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2674 | QualType A = OriginalArg.OriginalArgType; |
| 2675 | QualType OriginalParamType = OriginalArg.OriginalParamType; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2676 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2677 | // Check for type equality (top-level cv-qualifiers are ignored). |
| 2678 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| 2679 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2680 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2681 | // Strip off references on the argument types; they aren't needed for |
| 2682 | // the following checks. |
| 2683 | if (const ReferenceType *DeducedARef = DeducedA->getAs<ReferenceType>()) |
| 2684 | DeducedA = DeducedARef->getPointeeType(); |
| 2685 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
| 2686 | A = ARef->getPointeeType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2687 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2688 | // C++ [temp.deduct.call]p4: |
| 2689 | // [...] However, there are three cases that allow a difference: |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2690 | // - If the original P is a reference type, the deduced A (i.e., the |
| 2691 | // type referred to by the reference) can be more cv-qualified than |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2692 | // the transformed A. |
| 2693 | if (const ReferenceType *OriginalParamRef |
| 2694 | = OriginalParamType->getAs<ReferenceType>()) { |
| 2695 | // We don't want to keep the reference around any more. |
| 2696 | OriginalParamType = OriginalParamRef->getPointeeType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2697 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2698 | Qualifiers AQuals = A.getQualifiers(); |
| 2699 | Qualifiers DeducedAQuals = DeducedA.getQualifiers(); |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2700 | |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2701 | // Under Objective-C++ ARC, the deduced type may have implicitly |
| 2702 | // been given strong or (when dealing with a const reference) |
| 2703 | // unsafe_unretained lifetime. If so, update the original |
| 2704 | // qualifiers to include this lifetime. |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2705 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2706 | ((DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_Strong && |
| 2707 | AQuals.getObjCLifetime() == Qualifiers::OCL_None) || |
| 2708 | (DeducedAQuals.hasConst() && |
| 2709 | DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone))) { |
| 2710 | AQuals.setObjCLifetime(DeducedAQuals.getObjCLifetime()); |
Douglas Gregor | a906ad2 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2711 | } |
| 2712 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2713 | if (AQuals == DeducedAQuals) { |
| 2714 | // Qualifiers match; there's nothing to do. |
| 2715 | } else if (!DeducedAQuals.compatiblyIncludes(AQuals)) { |
Douglas Gregor | ddaae52 | 2011-06-17 14:36:00 +0000 | [diff] [blame] | 2716 | return true; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2717 | } else { |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2718 | // Qualifiers are compatible, so have the argument type adopt the |
| 2719 | // deduced argument type's qualifiers as if we had performed the |
| 2720 | // qualification conversion. |
| 2721 | A = Context.getQualifiedType(A.getUnqualifiedType(), DeducedAQuals); |
| 2722 | } |
| 2723 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2724 | |
| 2725 | // - The transformed A can be another pointer or pointer to member |
| 2726 | // type that can be converted to the deduced A via a qualification |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2727 | // conversion. |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2728 | // |
| 2729 | // Also allow conversions which merely strip [[noreturn]] from function types |
| 2730 | // (recursively) as an extension. |
Douglas Gregor | c9f019a | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2731 | // FIXME: Currently, this doesn't play nicely with qualification conversions. |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2732 | bool ObjCLifetimeConversion = false; |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2733 | QualType ResultTy; |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2734 | if ((A->isAnyPointerType() || A->isMemberPointerType()) && |
Chandler Carruth | 53e61b0 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2735 | (S.IsQualificationConversion(A, DeducedA, false, |
| 2736 | ObjCLifetimeConversion) || |
| 2737 | S.IsNoReturnConversion(A, DeducedA, ResultTy))) |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2738 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2739 | |
| 2740 | |
| 2741 | // - 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] | 2742 | // transformed A can be a derived class of the deduced A. [...] |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2743 | // [...] Likewise, if P is a pointer to a class of the form |
| 2744 | // simple-template-id, the transformed A can be a pointer to a |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2745 | // derived class pointed to by the deduced A. |
| 2746 | if (const PointerType *OriginalParamPtr |
| 2747 | = OriginalParamType->getAs<PointerType>()) { |
| 2748 | if (const PointerType *DeducedAPtr = DeducedA->getAs<PointerType>()) { |
| 2749 | if (const PointerType *APtr = A->getAs<PointerType>()) { |
| 2750 | if (A->getPointeeType()->isRecordType()) { |
| 2751 | OriginalParamType = OriginalParamPtr->getPointeeType(); |
| 2752 | DeducedA = DeducedAPtr->getPointeeType(); |
| 2753 | A = APtr->getPointeeType(); |
| 2754 | } |
| 2755 | } |
| 2756 | } |
| 2757 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2758 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2759 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| 2760 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2761 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2762 | if (A->isRecordType() && isSimpleTemplateIdType(OriginalParamType) && |
Richard Smith | 0f59cb3 | 2015-12-18 21:45:41 +0000 | [diff] [blame] | 2763 | S.IsDerivedFrom(SourceLocation(), A, DeducedA)) |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2764 | return false; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2765 | |
Douglas Gregor | 2ead4c4 | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2766 | return true; |
| 2767 | } |
| 2768 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2769 | /// \brief Finish template argument deduction for a function template, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2770 | /// checking the deduced template arguments for completeness and forming |
| 2771 | /// the function template specialization. |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2772 | /// |
| 2773 | /// \param OriginalCallArgs If non-NULL, the original call arguments against |
| 2774 | /// which the deduced argument types should be compared. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2775 | Sema::TemplateDeductionResult |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2776 | Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2777 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2778 | unsigned NumExplicitlySpecified, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2779 | FunctionDecl *&Specialization, |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2780 | TemplateDeductionInfo &Info, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 2781 | SmallVectorImpl<OriginalCallArg> const *OriginalCallArgs, |
| 2782 | bool PartialOverloading) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2783 | TemplateParameterList *TemplateParams |
| 2784 | = FunctionTemplate->getTemplateParameters(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2785 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2786 | // Unevaluated SFINAE context. |
| 2787 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2788 | SFINAETrap Trap(*this); |
| 2789 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2790 | // Enter a new template instantiation context while we instantiate the |
| 2791 | // actual function declaration. |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2792 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 2793 | InstantiatingTemplate Inst(*this, Info.getLocation(), FunctionTemplate, |
| 2794 | DeducedArgs, |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2795 | ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution, |
| 2796 | Info); |
Alp Toker | d4a72d5 | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2797 | if (Inst.isInvalid()) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2798 | return TDK_InstantiationDepth; |
| 2799 | |
John McCall | e23b871 | 2010-04-29 01:18:58 +0000 | [diff] [blame] | 2800 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
John McCall | 80e58cd | 2010-04-29 00:35:03 +0000 | [diff] [blame] | 2801 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2802 | // C++ [temp.deduct.type]p2: |
| 2803 | // [...] or if any template argument remains neither deduced nor |
| 2804 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2805 | SmallVector<TemplateArgument, 4> Builder; |
Douglas Gregor | aaa6a90 | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2806 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 2807 | NamedDecl *Param = TemplateParams->getParam(I); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2808 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2809 | if (!Deduced[I].isNull()) { |
Douglas Gregor | 6e9cf63 | 2010-10-12 18:51:08 +0000 | [diff] [blame] | 2810 | if (I < NumExplicitlySpecified) { |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2811 | // We have already fully type-checked and converted this |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2812 | // argument, because it was explicitly-specified. Just record the |
Douglas Gregor | 6e9cf63 | 2010-10-12 18:51:08 +0000 | [diff] [blame] | 2813 | // presence of this argument. |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2814 | Builder.push_back(Deduced[I]); |
Faisal Vali | 3628cb9 | 2014-06-01 16:11:54 +0000 | [diff] [blame] | 2815 | // We may have had explicitly-specified template arguments for a |
| 2816 | // template parameter pack (that may or may not have been extended |
| 2817 | // via additional deduced arguments). |
| 2818 | if (Param->isParameterPack() && CurrentInstantiationScope) { |
| 2819 | if (CurrentInstantiationScope->getPartiallySubstitutedPack() == |
| 2820 | Param) { |
| 2821 | // Forget the partially-substituted pack; its substitution is now |
| 2822 | // complete. |
| 2823 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2824 | } |
| 2825 | } |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2826 | continue; |
| 2827 | } |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2828 | |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2829 | // We have deduced this argument, so it still needs to be |
| 2830 | // checked and converted. |
Douglas Gregor | aaa6a90 | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2831 | if (ConvertDeducedTemplateArgument(*this, Param, Deduced[I], |
Richard Smith | 37acb79 | 2016-02-03 20:15:01 +0000 | [diff] [blame] | 2832 | FunctionTemplate, Info, |
Douglas Gregor | ca4686d | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2833 | true, Builder)) { |
Douglas Gregor | aaa6a90 | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2834 | Info.Param = makeTemplateParameter(Param); |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2835 | // FIXME: These template arguments are temporary. Free them! |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2836 | Info.reset(TemplateArgumentList::CreateCopy(Context, Builder)); |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2837 | return TDK_SubstitutionFailure; |
| 2838 | } |
| 2839 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2840 | continue; |
| 2841 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2842 | |
Douglas Gregor | ca4d91d | 2010-12-23 01:52:01 +0000 | [diff] [blame] | 2843 | // C++0x [temp.arg.explicit]p3: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2844 | // A trailing template parameter pack (14.5.3) not otherwise deduced will |
Douglas Gregor | ca4d91d | 2010-12-23 01:52:01 +0000 | [diff] [blame] | 2845 | // be deduced to an empty sequence of template arguments. |
| 2846 | // FIXME: Where did the word "trailing" come from? |
| 2847 | if (Param->isTemplateParameterPack()) { |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2848 | // We may have had explicitly-specified template arguments for this |
| 2849 | // template parameter pack. If so, our empty deduction extends the |
| 2850 | // explicitly-specified set (C++0x [temp.arg.explicit]p9). |
| 2851 | const TemplateArgument *ExplicitArgs; |
| 2852 | unsigned NumExplicitArgs; |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2853 | if (CurrentInstantiationScope && |
| 2854 | CurrentInstantiationScope->getPartiallySubstitutedPack(&ExplicitArgs, |
Douglas Gregor | a8bac7f | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2855 | &NumExplicitArgs) |
Douglas Gregor | caddba9 | 2013-01-18 22:27:09 +0000 | [diff] [blame] | 2856 | == Param) { |
Benjamin Kramer | cce6347 | 2015-08-05 09:40:22 +0000 | [diff] [blame] | 2857 | Builder.push_back(TemplateArgument( |
| 2858 | llvm::makeArrayRef(ExplicitArgs, NumExplicitArgs))); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2859 | |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2860 | // Forget the partially-substituted pack; its substitution is now |
Douglas Gregor | caddba9 | 2013-01-18 22:27:09 +0000 | [diff] [blame] | 2861 | // complete. |
| 2862 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2863 | } else { |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2864 | // Go through the motions of checking the empty argument pack against |
| 2865 | // the parameter pack. |
| 2866 | DeducedTemplateArgument DeducedPack(TemplateArgument::getEmptyPack()); |
| 2867 | if (ConvertDeducedTemplateArgument(*this, Param, DeducedPack, |
| 2868 | FunctionTemplate, Info, true, |
| 2869 | Builder)) { |
| 2870 | Info.Param = makeTemplateParameter(Param); |
| 2871 | // FIXME: These template arguments are temporary. Free them! |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2872 | Info.reset(TemplateArgumentList::CreateCopy(Context, Builder)); |
Richard Smith | df18ee9 | 2016-02-03 20:40:30 +0000 | [diff] [blame] | 2873 | return TDK_SubstitutionFailure; |
| 2874 | } |
Douglas Gregor | caddba9 | 2013-01-18 22:27:09 +0000 | [diff] [blame] | 2875 | } |
Douglas Gregor | ca4d91d | 2010-12-23 01:52:01 +0000 | [diff] [blame] | 2876 | continue; |
| 2877 | } |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2878 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2879 | // Substitute into the default template argument, if available. |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 2880 | bool HasDefaultArg = false; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2881 | TemplateArgumentLoc DefArg |
| 2882 | = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate, |
| 2883 | FunctionTemplate->getLocation(), |
| 2884 | FunctionTemplate->getSourceRange().getEnd(), |
| 2885 | Param, |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 2886 | Builder, HasDefaultArg); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2887 | |
| 2888 | // If there was no default argument, deduction is incomplete. |
| 2889 | if (DefArg.getArgument().isNull()) { |
| 2890 | Info.Param = makeTemplateParameter( |
| 2891 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2892 | Info.reset(TemplateArgumentList::CreateCopy(Context, Builder)); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 2893 | if (PartialOverloading) break; |
| 2894 | |
Richard Smith | c87b938 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 2895 | return HasDefaultArg ? TDK_SubstitutionFailure : TDK_Incomplete; |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2896 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2897 | |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2898 | // Check whether we can actually use the default argument. |
| 2899 | if (CheckTemplateArgument(Param, DefArg, |
| 2900 | FunctionTemplate, |
| 2901 | FunctionTemplate->getLocation(), |
| 2902 | FunctionTemplate->getSourceRange().getEnd(), |
Douglas Gregor | 0231d8d | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2903 | 0, Builder, |
Douglas Gregor | 2f157c9 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2904 | CTAK_Specified)) { |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2905 | Info.Param = makeTemplateParameter( |
| 2906 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2907 | // FIXME: These template arguments are temporary. Free them! |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2908 | Info.reset(TemplateArgumentList::CreateCopy(Context, Builder)); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2909 | return TDK_SubstitutionFailure; |
| 2910 | } |
| 2911 | |
| 2912 | // If we get here, we successfully used the default template argument. |
| 2913 | } |
| 2914 | |
| 2915 | // Form the template argument list from the deduced template arguments. |
| 2916 | TemplateArgumentList *DeducedArgumentList |
David Majnemer | 8b62269 | 2016-07-03 21:17:51 +0000 | [diff] [blame] | 2917 | = TemplateArgumentList::CreateCopy(Context, Builder); |
Douglas Gregor | 5c80a27b | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2918 | Info.reset(DeducedArgumentList); |
| 2919 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2920 | // Substitute the deduced template arguments into the function template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2921 | // declaration to produce the function template specialization. |
Douglas Gregor | 1614237 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 2922 | DeclContext *Owner = FunctionTemplate->getDeclContext(); |
| 2923 | if (FunctionTemplate->getFriendObjectKind()) |
| 2924 | Owner = FunctionTemplate->getLexicalDeclContext(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2925 | Specialization = cast_or_null<FunctionDecl>( |
Douglas Gregor | 1614237 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 2926 | SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner, |
Douglas Gregor | 39cacdb | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 2927 | MultiLevelTemplateArgumentList(*DeducedArgumentList))); |
Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 2928 | if (!Specialization || Specialization->isInvalidDecl()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2929 | return TDK_SubstitutionFailure; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2930 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2931 | assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == |
Douglas Gregor | 31fae89 | 2009-09-15 18:26:13 +0000 | [diff] [blame] | 2932 | FunctionTemplate->getCanonicalDecl()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2933 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2934 | // If the template argument list is owned by the function template |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2935 | // specialization, release it. |
Douglas Gregor | d09efd4 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 2936 | if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList && |
| 2937 | !Trap.hasErrorOccurred()) |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2938 | Info.take(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2939 | |
Douglas Gregor | ebcfbb5 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 2940 | // There may have been an error that did not prevent us from constructing a |
| 2941 | // declaration. Mark the declaration invalid and return with a substitution |
| 2942 | // failure. |
| 2943 | if (Trap.hasErrorOccurred()) { |
| 2944 | Specialization->setInvalidDecl(true); |
| 2945 | return TDK_SubstitutionFailure; |
| 2946 | } |
| 2947 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2948 | if (OriginalCallArgs) { |
| 2949 | // C++ [temp.deduct.call]p4: |
| 2950 | // In general, the deduction process attempts to find template argument |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2951 | // 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] | 2952 | // is transformed as described above). [...] |
| 2953 | for (unsigned I = 0, N = OriginalCallArgs->size(); I != N; ++I) { |
| 2954 | OriginalCallArg OriginalArg = (*OriginalCallArgs)[I]; |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2955 | unsigned ParamIdx = OriginalArg.ArgIdx; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2956 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2957 | if (ParamIdx >= Specialization->getNumParams()) |
| 2958 | continue; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2959 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2960 | QualType DeducedA = Specialization->getParamDecl(ParamIdx)->getType(); |
Richard Smith | 9b53454 | 2015-12-31 02:02:54 +0000 | [diff] [blame] | 2961 | if (CheckOriginalCallArgDeduction(*this, OriginalArg, DeducedA)) { |
| 2962 | Info.FirstArg = TemplateArgument(DeducedA); |
| 2963 | Info.SecondArg = TemplateArgument(OriginalArg.OriginalArgType); |
| 2964 | Info.CallArgIndex = OriginalArg.ArgIdx; |
| 2965 | return TDK_DeducedMismatch; |
| 2966 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2967 | } |
| 2968 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 2969 | |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2970 | // If we suppressed any diagnostics while performing template argument |
| 2971 | // deduction, and if we haven't already instantiated this declaration, |
| 2972 | // keep track of these diagnostics. They'll be emitted if this specialization |
| 2973 | // is actually used. |
| 2974 | if (Info.diag_begin() != Info.diag_end()) { |
Craig Topper | 79be4cd | 2013-07-05 04:33:53 +0000 | [diff] [blame] | 2975 | SuppressedDiagnosticsMap::iterator |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2976 | Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl()); |
| 2977 | if (Pos == SuppressedDiagnostics.end()) |
| 2978 | SuppressedDiagnostics[Specialization->getCanonicalDecl()] |
| 2979 | .append(Info.diag_begin(), Info.diag_end()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2980 | } |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2981 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2982 | return TDK_Success; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2985 | /// Gets the type of a function for template-argument-deducton |
| 2986 | /// purposes when it's considered as part of an overload set. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2987 | static QualType GetTypeOfFunction(Sema &S, const OverloadExpr::FindResult &R, |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2988 | FunctionDecl *Fn) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2989 | // We may need to deduce the return type of the function now. |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 2990 | if (S.getLangOpts().CPlusPlus14 && Fn->getReturnType()->isUndeducedType() && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2991 | S.DeduceReturnType(Fn, R.Expression->getExprLoc(), /*Diagnose*/ false)) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2992 | return QualType(); |
| 2993 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2994 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2995 | if (Method->isInstance()) { |
| 2996 | // An instance method that's referenced in a form that doesn't |
| 2997 | // look like a member pointer is just invalid. |
| 2998 | if (!R.HasFormOfMemberPointer) return QualType(); |
| 2999 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3000 | return S.Context.getMemberPointerType(Fn->getType(), |
| 3001 | S.Context.getTypeDeclType(Method->getParent()).getTypePtr()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | if (!R.IsAddressOfOperand) return Fn->getType(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3005 | return S.Context.getPointerType(Fn->getType()); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3006 | } |
| 3007 | |
| 3008 | /// Apply the deduction rules for overload sets. |
| 3009 | /// |
| 3010 | /// \return the null type if this argument should be treated as an |
| 3011 | /// undeduced context |
| 3012 | static QualType |
| 3013 | ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams, |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3014 | Expr *Arg, QualType ParamType, |
| 3015 | bool ParamWasReference) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3016 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3017 | OverloadExpr::FindResult R = OverloadExpr::find(Arg); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3018 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3019 | OverloadExpr *Ovl = R.Expression; |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3020 | |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3021 | // C++0x [temp.deduct.call]p4 |
| 3022 | unsigned TDF = 0; |
| 3023 | if (ParamWasReference) |
| 3024 | TDF |= TDF_ParamWithReferenceType; |
| 3025 | if (R.IsAddressOfOperand) |
| 3026 | TDF |= TDF_IgnoreQualifiers; |
| 3027 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3028 | // C++0x [temp.deduct.call]p6: |
| 3029 | // When P is a function type, pointer to function type, or pointer |
| 3030 | // to member function type: |
| 3031 | |
| 3032 | if (!ParamType->isFunctionType() && |
| 3033 | !ParamType->isFunctionPointerType() && |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3034 | !ParamType->isMemberFunctionPointerType()) { |
| 3035 | if (Ovl->hasExplicitTemplateArgs()) { |
| 3036 | // But we can still look for an explicit specialization. |
| 3037 | if (FunctionDecl *ExplicitSpec |
| 3038 | = S.ResolveSingleFunctionTemplateSpecialization(Ovl)) |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3039 | return GetTypeOfFunction(S, R, ExplicitSpec); |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3040 | } |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3041 | |
George Burgess IV | cc2f355 | 2016-03-19 21:51:45 +0000 | [diff] [blame] | 3042 | DeclAccessPair DAP; |
| 3043 | if (FunctionDecl *Viable = |
| 3044 | S.resolveAddressOfOnlyViableOverloadCandidate(Arg, DAP)) |
| 3045 | return GetTypeOfFunction(S, R, Viable); |
| 3046 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3047 | return QualType(); |
| 3048 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3049 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3050 | // Gather the explicit template arguments, if any. |
| 3051 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 3052 | if (Ovl->hasExplicitTemplateArgs()) |
James Y Knight | 04ec5bf | 2015-12-24 02:59:37 +0000 | [diff] [blame] | 3053 | Ovl->copyTemplateArgumentsInto(ExplicitTemplateArgs); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3054 | QualType Match; |
John McCall | 1acbbb5 | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 3055 | for (UnresolvedSetIterator I = Ovl->decls_begin(), |
| 3056 | E = Ovl->decls_end(); I != E; ++I) { |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3057 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 3058 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3059 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) { |
| 3060 | // - If the argument is an overload set containing one or more |
| 3061 | // function templates, the parameter is treated as a |
| 3062 | // non-deduced context. |
| 3063 | if (!Ovl->hasExplicitTemplateArgs()) |
| 3064 | return QualType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3065 | |
| 3066 | // Otherwise, see if we can resolve a function type |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3067 | FunctionDecl *Specialization = nullptr; |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3068 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3069 | if (S.DeduceTemplateArguments(FunTmpl, &ExplicitTemplateArgs, |
| 3070 | Specialization, Info)) |
| 3071 | continue; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3072 | |
Douglas Gregor | 8409ccd | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3073 | D = Specialization; |
| 3074 | } |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3075 | |
| 3076 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3077 | QualType ArgType = GetTypeOfFunction(S, R, Fn); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3078 | if (ArgType.isNull()) continue; |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3079 | |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3080 | // Function-to-pointer conversion. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3081 | if (!ParamWasReference && ParamType->isPointerType() && |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3082 | ArgType->isFunctionType()) |
| 3083 | ArgType = S.Context.getPointerType(ArgType); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3084 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3085 | // - If the argument is an overload set (not containing function |
| 3086 | // templates), trial argument deduction is attempted using each |
| 3087 | // of the members of the set. If deduction succeeds for only one |
| 3088 | // of the overload set members, that member is used as the |
| 3089 | // argument value for the deduction. If deduction succeeds for |
| 3090 | // more than one member of the overload set the parameter is |
| 3091 | // treated as a non-deduced context. |
| 3092 | |
| 3093 | // We do all of this in a fresh context per C++0x [temp.deduct.type]p2: |
| 3094 | // Type deduction is done independently for each P/A pair, and |
| 3095 | // the deduced template argument values are then combined. |
| 3096 | // So we do not reject deductions which were made elsewhere. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3097 | SmallVector<DeducedTemplateArgument, 8> |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3098 | Deduced(TemplateParams->size()); |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3099 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3100 | Sema::TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3101 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
| 3102 | ArgType, Info, Deduced, TDF); |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3103 | if (Result) continue; |
| 3104 | if (!Match.isNull()) return QualType(); |
| 3105 | Match = ArgType; |
| 3106 | } |
| 3107 | |
| 3108 | return Match; |
| 3109 | } |
| 3110 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3111 | /// \brief Perform the adjustments to the parameter and argument types |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3112 | /// described in C++ [temp.deduct.call]. |
| 3113 | /// |
| 3114 | /// \returns true if the caller should not attempt to perform any template |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3115 | /// argument deduction based on this P/A pair because the argument is an |
| 3116 | /// overloaded function set that could not be resolved. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3117 | static bool AdjustFunctionParmAndArgTypesForDeduction(Sema &S, |
| 3118 | TemplateParameterList *TemplateParams, |
| 3119 | QualType &ParamType, |
| 3120 | QualType &ArgType, |
| 3121 | Expr *Arg, |
| 3122 | unsigned &TDF) { |
| 3123 | // C++0x [temp.deduct.call]p3: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3124 | // 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] | 3125 | // are ignored for type deduction. |
Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3126 | if (ParamType.hasQualifiers()) |
| 3127 | ParamType = ParamType.getUnqualifiedType(); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3128 | |
| 3129 | // [...] If P is a reference type, the type referred to by P is |
| 3130 | // used for type deduction. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3131 | const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>(); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3132 | if (ParamRefType) |
| 3133 | ParamType = ParamRefType->getPointeeType(); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3134 | |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3135 | // Overload sets usually make this parameter an undeduced context, |
| 3136 | // but there are sometimes special circumstances. Typically |
| 3137 | // involving a template-id-expr. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3138 | if (ArgType == S.Context.OverloadTy) { |
| 3139 | ArgType = ResolveOverloadForDeduction(S, TemplateParams, |
| 3140 | Arg, ParamType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3141 | ParamRefType != nullptr); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3142 | if (ArgType.isNull()) |
| 3143 | return true; |
| 3144 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3145 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3146 | if (ParamRefType) { |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3147 | // If the argument has incomplete array type, try to complete its type. |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3148 | if (ArgType->isIncompleteArrayType()) { |
| 3149 | S.completeExprArrayBound(Arg); |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3150 | ArgType = Arg->getType(); |
Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 3151 | } |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3152 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3153 | // C++0x [temp.deduct.call]p3: |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3154 | // If P is an rvalue reference to a cv-unqualified template |
| 3155 | // parameter and the argument is an lvalue, the type "lvalue |
| 3156 | // reference to A" is used in place of A for type deduction. |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3157 | if (ParamRefType->isRValueReferenceType() && |
Nathan Sidwell | 9609002 | 2015-01-16 15:20:14 +0000 | [diff] [blame] | 3158 | !ParamType.getQualifiers() && |
| 3159 | isa<TemplateTypeParmType>(ParamType) && |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3160 | Arg->isLValue()) |
| 3161 | ArgType = S.Context.getLValueReferenceType(ArgType); |
| 3162 | } else { |
| 3163 | // C++ [temp.deduct.call]p2: |
| 3164 | // If P is not a reference type: |
| 3165 | // - If A is an array type, the pointer type produced by the |
| 3166 | // array-to-pointer standard conversion (4.2) is used in place of |
| 3167 | // A for type deduction; otherwise, |
| 3168 | if (ArgType->isArrayType()) |
| 3169 | ArgType = S.Context.getArrayDecayedType(ArgType); |
| 3170 | // - If A is a function type, the pointer type produced by the |
| 3171 | // function-to-pointer standard conversion (4.3) is used in place |
| 3172 | // of A for type deduction; otherwise, |
| 3173 | else if (ArgType->isFunctionType()) |
| 3174 | ArgType = S.Context.getPointerType(ArgType); |
| 3175 | else { |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3176 | // - 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] | 3177 | // type are ignored for type deduction. |
Douglas Gregor | 1784688 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3178 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3179 | } |
| 3180 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3181 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3182 | // C++0x [temp.deduct.call]p4: |
| 3183 | // In general, the deduction process attempts to find template argument |
| 3184 | // values that will make the deduced A identical to A (after the type A |
| 3185 | // is transformed as described above). [...] |
| 3186 | TDF = TDF_SkipNonDependent; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3187 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3188 | // - If the original P is a reference type, the deduced A (i.e., the |
| 3189 | // type referred to by the reference) can be more cv-qualified than |
| 3190 | // the transformed A. |
| 3191 | if (ParamRefType) |
| 3192 | TDF |= TDF_ParamWithReferenceType; |
| 3193 | // - The transformed A can be another pointer or pointer to member |
| 3194 | // type that can be converted to the deduced A via a qualification |
| 3195 | // conversion (4.4). |
| 3196 | if (ArgType->isPointerType() || ArgType->isMemberPointerType() || |
| 3197 | ArgType->isObjCObjectPointerType()) |
| 3198 | TDF |= TDF_IgnoreQualifiers; |
| 3199 | // - If P is a class and P has the form simple-template-id, then the |
| 3200 | // transformed A can be a derived class of the deduced A. Likewise, |
| 3201 | // if P is a pointer to a class of the form simple-template-id, the |
| 3202 | // transformed A can be a pointer to a derived class pointed to by |
| 3203 | // the deduced A. |
| 3204 | if (isSimpleTemplateIdType(ParamType) || |
| 3205 | (isa<PointerType>(ParamType) && |
| 3206 | isSimpleTemplateIdType( |
| 3207 | ParamType->getAs<PointerType>()->getPointeeType()))) |
| 3208 | TDF |= TDF_DerivedClass; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3209 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3210 | return false; |
| 3211 | } |
| 3212 | |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3213 | static bool |
| 3214 | hasDeducibleTemplateParameters(Sema &S, FunctionTemplateDecl *FunctionTemplate, |
| 3215 | QualType T); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3216 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3217 | static Sema::TemplateDeductionResult DeduceTemplateArgumentByListElement( |
| 3218 | Sema &S, TemplateParameterList *TemplateParams, QualType ParamType, |
| 3219 | Expr *Arg, TemplateDeductionInfo &Info, |
| 3220 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, unsigned TDF); |
| 3221 | |
| 3222 | /// \brief Attempt template argument deduction from an initializer list |
| 3223 | /// deemed to be an argument in a function call. |
| 3224 | static bool |
| 3225 | DeduceFromInitializerList(Sema &S, TemplateParameterList *TemplateParams, |
| 3226 | QualType AdjustedParamType, InitListExpr *ILE, |
| 3227 | TemplateDeductionInfo &Info, |
| 3228 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3229 | unsigned TDF, Sema::TemplateDeductionResult &Result) { |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3230 | |
| 3231 | // [temp.deduct.call] p1 (post CWG-1591) |
| 3232 | // If removing references and cv-qualifiers from P gives |
| 3233 | // std::initializer_list<P0> or P0[N] for some P0 and N and the argument is a |
| 3234 | // non-empty initializer list (8.5.4), then deduction is performed instead for |
| 3235 | // each element of the initializer list, taking P0 as a function template |
| 3236 | // parameter type and the initializer element as its argument, and in the |
| 3237 | // P0[N] case, if N is a non-type template parameter, N is deduced from the |
| 3238 | // length of the initializer list. Otherwise, an initializer list argument |
| 3239 | // causes the parameter to be considered a non-deduced context |
| 3240 | |
| 3241 | const bool IsConstSizedArray = AdjustedParamType->isConstantArrayType(); |
| 3242 | |
| 3243 | const bool IsDependentSizedArray = |
| 3244 | !IsConstSizedArray && AdjustedParamType->isDependentSizedArrayType(); |
| 3245 | |
Faisal Vali | dd76cc1 | 2015-12-10 12:29:11 +0000 | [diff] [blame] | 3246 | 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] | 3247 | |
| 3248 | const bool IsSTDList = !IsConstSizedArray && !IsDependentSizedArray && |
| 3249 | S.isStdInitializerList(AdjustedParamType, &ElTy); |
| 3250 | |
| 3251 | if (!IsConstSizedArray && !IsDependentSizedArray && !IsSTDList) |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3252 | return false; |
| 3253 | |
| 3254 | Result = Sema::TDK_Success; |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3255 | // If we are not deducing against the 'T' in a std::initializer_list<T> then |
| 3256 | // deduce against the 'T' in T[N]. |
| 3257 | if (ElTy.isNull()) { |
| 3258 | assert(!IsSTDList); |
| 3259 | ElTy = S.Context.getAsArrayType(AdjustedParamType)->getElementType(); |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3260 | } |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3261 | // Deduction only needs to be done for dependent types. |
| 3262 | if (ElTy->isDependentType()) { |
| 3263 | for (Expr *E : ILE->inits()) { |
Craig Topper | 0852953 | 2015-12-10 08:49:55 +0000 | [diff] [blame] | 3264 | if ((Result = DeduceTemplateArgumentByListElement(S, TemplateParams, ElTy, |
| 3265 | E, Info, Deduced, TDF))) |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3266 | return true; |
| 3267 | } |
| 3268 | } |
| 3269 | if (IsDependentSizedArray) { |
| 3270 | const DependentSizedArrayType *ArrTy = |
| 3271 | S.Context.getAsDependentSizedArrayType(AdjustedParamType); |
| 3272 | // Determine the array bound is something we can deduce. |
| 3273 | if (NonTypeTemplateParmDecl *NTTP = |
| 3274 | getDeducedParameterFromExpr(ArrTy->getSizeExpr())) { |
| 3275 | // We can perform template argument deduction for the given non-type |
| 3276 | // template parameter. |
| 3277 | assert(NTTP->getDepth() == 0 && |
| 3278 | "Cannot deduce non-type template argument at depth > 0"); |
| 3279 | llvm::APInt Size(S.Context.getIntWidth(NTTP->getType()), |
| 3280 | ILE->getNumInits()); |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3281 | |
Faisal Vali | f6dfdb3 | 2015-12-10 05:36:39 +0000 | [diff] [blame] | 3282 | Result = DeduceNonTypeTemplateArgument( |
| 3283 | S, NTTP, llvm::APSInt(Size), NTTP->getType(), |
| 3284 | /*ArrayBound=*/true, Info, Deduced); |
| 3285 | } |
| 3286 | } |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3287 | return true; |
| 3288 | } |
| 3289 | |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3290 | /// \brief Perform template argument deduction by matching a parameter type |
| 3291 | /// against a single expression, where the expression is an element of |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3292 | /// an initializer list that was originally matched against a parameter |
| 3293 | /// of type \c initializer_list\<ParamType\>. |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3294 | static Sema::TemplateDeductionResult |
| 3295 | DeduceTemplateArgumentByListElement(Sema &S, |
| 3296 | TemplateParameterList *TemplateParams, |
| 3297 | QualType ParamType, Expr *Arg, |
| 3298 | TemplateDeductionInfo &Info, |
| 3299 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3300 | unsigned TDF) { |
| 3301 | // Handle the case where an init list contains another init list as the |
| 3302 | // element. |
| 3303 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3304 | Sema::TemplateDeductionResult Result; |
| 3305 | if (!DeduceFromInitializerList(S, TemplateParams, |
| 3306 | ParamType.getNonReferenceType(), ILE, Info, |
| 3307 | Deduced, TDF, Result)) |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3308 | return Sema::TDK_Success; // Just ignore this expression. |
| 3309 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3310 | return Result; |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3311 | } |
| 3312 | |
| 3313 | // For all other cases, just match by type. |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3314 | QualType ArgType = Arg->getType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3315 | if (AdjustFunctionParmAndArgTypesForDeduction(S, TemplateParams, ParamType, |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3316 | ArgType, Arg, TDF)) { |
| 3317 | Info.Expression = Arg; |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3318 | return Sema::TDK_FailedOverloadResolution; |
Richard Smith | 8c6eeb9 | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3319 | } |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3320 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3321 | ArgType, Info, Deduced, TDF); |
Sebastian Redl | 1918166 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3322 | } |
| 3323 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3324 | /// \brief Perform template argument deduction from a function call |
| 3325 | /// (C++ [temp.deduct.call]). |
| 3326 | /// |
| 3327 | /// \param FunctionTemplate the function template for which we are performing |
| 3328 | /// template argument deduction. |
| 3329 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3330 | /// \param ExplicitTemplateArgs the explicit template arguments provided |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 3331 | /// for this call. |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3332 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3333 | /// \param Args the function call arguments |
| 3334 | /// |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3335 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3336 | /// this will be set to the function template specialization produced by |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3337 | /// template argument deduction. |
| 3338 | /// |
| 3339 | /// \param Info the argument will be updated to provide additional information |
| 3340 | /// about template argument deduction. |
| 3341 | /// |
| 3342 | /// \returns the result of template argument deduction. |
Robert Wilhelm | 16e94b9 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 3343 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3344 | FunctionTemplateDecl *FunctionTemplate, |
| 3345 | TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3346 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 3347 | bool PartialOverloading) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3348 | if (FunctionTemplate->isInvalidDecl()) |
| 3349 | return TDK_Invalid; |
| 3350 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3351 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3352 | unsigned NumParams = Function->getNumParams(); |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3353 | |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3354 | // C++ [temp.deduct.call]p1: |
| 3355 | // Template argument deduction is done by comparing each function template |
| 3356 | // parameter type (call it P) with the type of the corresponding argument |
| 3357 | // of the call (call it A) as described below. |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3358 | unsigned CheckArgs = Args.size(); |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3359 | if (Args.size() < Function->getMinRequiredArguments() && !PartialOverloading) |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3360 | return TDK_TooFewArguments; |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3361 | else if (TooManyArguments(NumParams, Args.size(), PartialOverloading)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3362 | const FunctionProtoType *Proto |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3363 | = Function->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3364 | if (Proto->isTemplateVariadic()) |
| 3365 | /* Do nothing */; |
| 3366 | else if (Proto->isVariadic()) |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3367 | CheckArgs = NumParams; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3368 | else |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3369 | return TDK_TooManyArguments; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3370 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3371 | |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3372 | // The types of the parameters from which we will perform template argument |
| 3373 | // deduction. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3374 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3375 | TemplateParameterList *TemplateParams |
| 3376 | = FunctionTemplate->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3377 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 3378 | SmallVector<QualType, 4> ParamTypes; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3379 | unsigned NumExplicitlySpecified = 0; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3380 | if (ExplicitTemplateArgs) { |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3381 | TemplateDeductionResult Result = |
| 3382 | SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3383 | *ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3384 | Deduced, |
| 3385 | ParamTypes, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3386 | nullptr, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3387 | Info); |
| 3388 | if (Result) |
| 3389 | return Result; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3390 | |
| 3391 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3392 | } else { |
| 3393 | // Just fill in the parameter types from the function declaration. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3394 | for (unsigned I = 0; I != NumParams; ++I) |
Douglas Gregor | 89026b5 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3395 | ParamTypes.push_back(Function->getParamDecl(I)->getType()); |
| 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 | // Deduce template arguments from the function parameters. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3399 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3400 | unsigned ArgIdx = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3401 | SmallVector<OriginalCallArg, 4> OriginalCallArgs; |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3402 | for (unsigned ParamIdx = 0, NumParamTypes = ParamTypes.size(); |
| 3403 | ParamIdx != NumParamTypes; ++ParamIdx) { |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3404 | QualType OrigParamType = ParamTypes[ParamIdx]; |
| 3405 | QualType ParamType = OrigParamType; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3406 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3407 | const PackExpansionType *ParamExpansion |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3408 | = dyn_cast<PackExpansionType>(ParamType); |
| 3409 | if (!ParamExpansion) { |
| 3410 | // Simple case: matching a function parameter to a function argument. |
| 3411 | if (ArgIdx >= CheckArgs) |
| 3412 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3413 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3414 | Expr *Arg = Args[ArgIdx++]; |
| 3415 | QualType ArgType = Arg->getType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3416 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3417 | unsigned TDF = 0; |
| 3418 | if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams, |
| 3419 | ParamType, ArgType, Arg, |
| 3420 | TDF)) |
| 3421 | continue; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3422 | |
Douglas Gregor | 0c83c81 | 2011-10-09 22:06:46 +0000 | [diff] [blame] | 3423 | // If we have nothing to deduce, we're done. |
| 3424 | if (!hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
| 3425 | continue; |
| 3426 | |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3427 | // If the argument is an initializer list ... |
| 3428 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3429 | TemplateDeductionResult Result; |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3430 | // Removing references was already done. |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3431 | if (!DeduceFromInitializerList(*this, TemplateParams, ParamType, ILE, |
| 3432 | Info, Deduced, TDF, Result)) |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3433 | continue; |
| 3434 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3435 | if (Result) |
| 3436 | return Result; |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3437 | // Don't track the argument type, since an initializer list has none. |
| 3438 | continue; |
| 3439 | } |
| 3440 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3441 | // Keep track of the argument type and corresponding parameter index, |
| 3442 | // so we can check for compatibility between the deduced A and A. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3443 | OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx-1, |
Douglas Gregor | 0c83c81 | 2011-10-09 22:06:46 +0000 | [diff] [blame] | 3444 | ArgType)); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3445 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3446 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3447 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3448 | ParamType, ArgType, |
| 3449 | Info, Deduced, TDF)) |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3450 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3451 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3452 | continue; |
Douglas Gregor | 66d2c8e | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3453 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3454 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3455 | // C++0x [temp.deduct.call]p1: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3456 | // For a function parameter pack that occurs at the end of the |
| 3457 | // parameter-declaration-list, the type A of each remaining argument of |
| 3458 | // the call is compared with the type P of the declarator-id of the |
| 3459 | // function parameter pack. Each comparison deduces template arguments |
| 3460 | // for subsequent positions in the template parameter packs expanded by |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3461 | // the function parameter pack. For a function parameter pack that does |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3462 | // 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] | 3463 | // the parameter pack is a non-deduced context. |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3464 | if (ParamIdx + 1 < NumParamTypes) |
Douglas Gregor | 0dd423e | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3465 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3466 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3467 | QualType ParamPattern = ParamExpansion->getPattern(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3468 | PackDeductionScope PackScope(*this, TemplateParams, Deduced, Info, |
| 3469 | ParamPattern); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3470 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3471 | bool HasAnyArguments = false; |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3472 | for (; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3473 | HasAnyArguments = true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3474 | |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3475 | QualType OrigParamType = ParamPattern; |
| 3476 | ParamType = OrigParamType; |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3477 | Expr *Arg = Args[ArgIdx]; |
| 3478 | QualType ArgType = Arg->getType(); |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3479 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3480 | unsigned TDF = 0; |
| 3481 | if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams, |
| 3482 | ParamType, ArgType, Arg, |
| 3483 | TDF)) { |
| 3484 | // We can't actually perform any deduction for this argument, so stop |
| 3485 | // deduction at this point. |
| 3486 | ++ArgIdx; |
| 3487 | break; |
| 3488 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3489 | |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3490 | // As above, initializer lists need special handling. |
| 3491 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3492 | TemplateDeductionResult Result; |
| 3493 | if (!DeduceFromInitializerList(*this, TemplateParams, ParamType, ILE, |
| 3494 | Info, Deduced, TDF, Result)) { |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3495 | ++ArgIdx; |
| 3496 | break; |
| 3497 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3498 | |
Hubert Tong | 3280b33 | 2015-06-25 00:25:49 +0000 | [diff] [blame] | 3499 | if (Result) |
| 3500 | return Result; |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3501 | } else { |
| 3502 | |
| 3503 | // Keep track of the argument type and corresponding argument index, |
| 3504 | // so we can check for compatibility between the deduced A and A. |
| 3505 | if (hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3506 | OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx, |
Sebastian Redl | 43144e7 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3507 | ArgType)); |
| 3508 | |
| 3509 | if (TemplateDeductionResult Result |
| 3510 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3511 | ParamType, ArgType, Info, |
| 3512 | Deduced, TDF)) |
| 3513 | return Result; |
| 3514 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3515 | |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3516 | PackScope.nextPackElement(); |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3517 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3518 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3519 | // Build argument packs for each of the parameter packs expanded by this |
| 3520 | // pack expansion. |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 3521 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3522 | return Result; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3523 | |
Douglas Gregor | 7825bf3 | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3524 | // After we've matching against a parameter pack, we're done. |
| 3525 | break; |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3526 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3527 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3528 | return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3529 | NumExplicitlySpecified, Specialization, |
Francisco Lopes da Silva | 975a9f6 | 2015-01-21 16:24:11 +0000 | [diff] [blame] | 3530 | Info, &OriginalCallArgs, |
| 3531 | PartialOverloading); |
Douglas Gregor | ad3f2fc | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3532 | } |
| 3533 | |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3534 | QualType Sema::adjustCCAndNoReturn(QualType ArgFunctionType, |
| 3535 | QualType FunctionType) { |
| 3536 | if (ArgFunctionType.isNull()) |
| 3537 | return ArgFunctionType; |
| 3538 | |
| 3539 | const FunctionProtoType *FunctionTypeP = |
| 3540 | FunctionType->castAs<FunctionProtoType>(); |
| 3541 | CallingConv CC = FunctionTypeP->getCallConv(); |
| 3542 | bool NoReturn = FunctionTypeP->getNoReturnAttr(); |
| 3543 | const FunctionProtoType *ArgFunctionTypeP = |
| 3544 | ArgFunctionType->getAs<FunctionProtoType>(); |
| 3545 | if (ArgFunctionTypeP->getCallConv() == CC && |
| 3546 | ArgFunctionTypeP->getNoReturnAttr() == NoReturn) |
| 3547 | return ArgFunctionType; |
| 3548 | |
| 3549 | FunctionType::ExtInfo EI = ArgFunctionTypeP->getExtInfo().withCallingConv(CC); |
| 3550 | EI = EI.withNoReturn(NoReturn); |
| 3551 | ArgFunctionTypeP = |
| 3552 | cast<FunctionProtoType>(Context.adjustFunctionType(ArgFunctionTypeP, EI)); |
| 3553 | return QualType(ArgFunctionTypeP, 0); |
| 3554 | } |
| 3555 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3556 | /// \brief Deduce template arguments when taking the address of a function |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3557 | /// template (C++ [temp.deduct.funcaddr]) or matching a specialization to |
| 3558 | /// a template. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3559 | /// |
| 3560 | /// \param FunctionTemplate the function template for which we are performing |
| 3561 | /// template argument deduction. |
| 3562 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3563 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3564 | /// arguments. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3565 | /// |
| 3566 | /// \param ArgFunctionType the function type that will be used as the |
| 3567 | /// "argument" type (A) when performing template argument deduction from the |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3568 | /// function template's function type. This type may be NULL, if there is no |
| 3569 | /// argument type to compare against, in C++0x [temp.arg.explicit]p3. |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3570 | /// |
| 3571 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3572 | /// this will be set to the function template specialization produced by |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3573 | /// template argument deduction. |
| 3574 | /// |
| 3575 | /// \param Info the argument will be updated to provide additional information |
| 3576 | /// about template argument deduction. |
| 3577 | /// |
| 3578 | /// \returns the result of template argument deduction. |
| 3579 | Sema::TemplateDeductionResult |
| 3580 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3581 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3582 | QualType ArgFunctionType, |
| 3583 | FunctionDecl *&Specialization, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3584 | TemplateDeductionInfo &Info, |
| 3585 | bool InOverloadResolution) { |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3586 | if (FunctionTemplate->isInvalidDecl()) |
| 3587 | return TDK_Invalid; |
| 3588 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3589 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 3590 | TemplateParameterList *TemplateParams |
| 3591 | = FunctionTemplate->getTemplateParameters(); |
| 3592 | QualType FunctionType = Function->getType(); |
Rafael Espindola | 6edca7d | 2013-12-01 16:54:29 +0000 | [diff] [blame] | 3593 | if (!InOverloadResolution) |
| 3594 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, FunctionType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3595 | |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3596 | // Substitute any explicit template arguments. |
John McCall | 19c1bfd | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3597 | LocalInstantiationScope InstScope(*this); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3598 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3599 | unsigned NumExplicitlySpecified = 0; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3600 | SmallVector<QualType, 4> ParamTypes; |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3601 | if (ExplicitTemplateArgs) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3602 | if (TemplateDeductionResult Result |
| 3603 | = SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | 6b51f28 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3604 | *ExplicitTemplateArgs, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3605 | Deduced, ParamTypes, |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3606 | &FunctionType, Info)) |
| 3607 | return Result; |
Douglas Gregor | d5cb1dd | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3608 | |
| 3609 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3610 | } |
| 3611 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3612 | // Unevaluated SFINAE context. |
| 3613 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3614 | SFINAETrap Trap(*this); |
| 3615 | |
John McCall | c1f6998 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3616 | Deduced.resize(TemplateParams->size()); |
| 3617 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3618 | // If the function has a deduced return type, substitute it for a dependent |
| 3619 | // type so that we treat it as a non-deduced context in what follows. |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3620 | bool HasDeducedReturnType = false; |
Aaron Ballman | dd69ef3 | 2014-08-19 15:55:55 +0000 | [diff] [blame] | 3621 | if (getLangOpts().CPlusPlus14 && InOverloadResolution && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3622 | Function->getReturnType()->getContainedAutoType()) { |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3623 | FunctionType = SubstAutoType(FunctionType, Context.DependentTy); |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3624 | HasDeducedReturnType = true; |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3625 | } |
| 3626 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3627 | if (!ArgFunctionType.isNull()) { |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3628 | unsigned TDF = TDF_TopLevelParameterTypeList; |
| 3629 | if (InOverloadResolution) TDF |= TDF_InOverloadResolution; |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3630 | // Deduce template arguments from the function type. |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3631 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3632 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3633 | FunctionType, ArgFunctionType, |
| 3634 | Info, Deduced, TDF)) |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3635 | return Result; |
| 3636 | } |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3637 | |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3638 | if (TemplateDeductionResult Result |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3639 | = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
| 3640 | NumExplicitlySpecified, |
| 3641 | Specialization, Info)) |
| 3642 | return Result; |
| 3643 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3644 | // If the function has a deduced return type, deduce it now, so we can check |
| 3645 | // that the deduced function type matches the requested type. |
Richard Smith | c58f38f | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3646 | if (HasDeducedReturnType && |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3647 | Specialization->getReturnType()->isUndeducedType() && |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3648 | DeduceReturnType(Specialization, Info.getLocation(), false)) |
| 3649 | return TDK_MiscellaneousDeductionFailure; |
| 3650 | |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3651 | // 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] | 3652 | // specialization with respect to arguments of compatible pointer to function |
| 3653 | // types, template argument deduction fails. |
| 3654 | if (!ArgFunctionType.isNull()) { |
| 3655 | if (InOverloadResolution && !isSameOrCompatibleFunctionType( |
| 3656 | Context.getCanonicalType(Specialization->getType()), |
| 3657 | Context.getCanonicalType(ArgFunctionType))) |
| 3658 | return TDK_MiscellaneousDeductionFailure; |
| 3659 | else if(!InOverloadResolution && |
| 3660 | !Context.hasSameType(Specialization->getType(), ArgFunctionType)) |
| 3661 | return TDK_MiscellaneousDeductionFailure; |
| 3662 | } |
Douglas Gregor | 4ed49f3 | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3663 | |
| 3664 | return TDK_Success; |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3665 | } |
| 3666 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3667 | /// \brief Given a function declaration (e.g. a generic lambda conversion |
| 3668 | /// function) that contains an 'auto' in its result type, substitute it |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3669 | /// with TypeToReplaceAutoWith. Be careful to pass in the type you want |
| 3670 | /// to replace 'auto' with and not the actual result type you want |
| 3671 | /// to set the function to. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3672 | static inline void |
| 3673 | SubstAutoWithinFunctionReturnType(FunctionDecl *F, |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3674 | QualType TypeToReplaceAutoWith, Sema &S) { |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3675 | assert(!TypeToReplaceAutoWith->getContainedAutoType()); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3676 | QualType AutoResultType = F->getReturnType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3677 | assert(AutoResultType->getContainedAutoType()); |
| 3678 | QualType DeducedResultType = S.SubstAutoType(AutoResultType, |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3679 | TypeToReplaceAutoWith); |
| 3680 | S.Context.adjustDeducedFunctionResultType(F, DeducedResultType); |
| 3681 | } |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3682 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3683 | /// \brief Given a specialized conversion operator of a generic lambda |
| 3684 | /// create the corresponding specializations of the call operator and |
| 3685 | /// the static-invoker. If the return type of the call operator is auto, |
| 3686 | /// deduce its return type and check if that matches the |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3687 | /// return type of the destination function ptr. |
| 3688 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3689 | static inline Sema::TemplateDeductionResult |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3690 | SpecializeCorrespondingLambdaCallOperatorAndInvoker( |
| 3691 | CXXConversionDecl *ConversionSpecialized, |
| 3692 | SmallVectorImpl<DeducedTemplateArgument> &DeducedArguments, |
| 3693 | QualType ReturnTypeOfDestFunctionPtr, |
| 3694 | TemplateDeductionInfo &TDInfo, |
| 3695 | Sema &S) { |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3696 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3697 | CXXRecordDecl *LambdaClass = ConversionSpecialized->getParent(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3698 | assert(LambdaClass && LambdaClass->isGenericLambda()); |
| 3699 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3700 | CXXMethodDecl *CallOpGeneric = LambdaClass->getLambdaCallOperator(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3701 | QualType CallOpResultType = CallOpGeneric->getReturnType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3702 | const bool GenericLambdaCallOperatorHasDeducedReturnType = |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3703 | CallOpResultType->getContainedAutoType(); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3704 | |
| 3705 | FunctionTemplateDecl *CallOpTemplate = |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3706 | CallOpGeneric->getDescribedFunctionTemplate(); |
| 3707 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3708 | FunctionDecl *CallOpSpecialized = nullptr; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3709 | // Use the deduced arguments of the conversion function, to specialize our |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3710 | // generic lambda's call operator. |
| 3711 | if (Sema::TemplateDeductionResult Result |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3712 | = S.FinishTemplateArgumentDeduction(CallOpTemplate, |
| 3713 | DeducedArguments, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3714 | 0, CallOpSpecialized, TDInfo)) |
| 3715 | return Result; |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3716 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3717 | // 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] | 3718 | if (GenericLambdaCallOperatorHasDeducedReturnType && |
| 3719 | CallOpSpecialized->getReturnType()->isUndeducedType()) |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3720 | S.DeduceReturnType(CallOpSpecialized, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3721 | CallOpSpecialized->getPointOfInstantiation(), |
| 3722 | /*Diagnose*/ true); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3723 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3724 | // Check to see if the return type of the destination ptr-to-function |
| 3725 | // matches the return type of the call operator. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3726 | if (!S.Context.hasSameType(CallOpSpecialized->getReturnType(), |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3727 | ReturnTypeOfDestFunctionPtr)) |
| 3728 | return Sema::TDK_NonDeducedMismatch; |
| 3729 | // Since we have succeeded in matching the source and destination |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3730 | // ptr-to-functions (now including return type), and have successfully |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3731 | // specialized our corresponding call operator, we are ready to |
| 3732 | // specialize the static invoker with the deduced arguments of our |
| 3733 | // ptr-to-function. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3734 | FunctionDecl *InvokerSpecialized = nullptr; |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3735 | FunctionTemplateDecl *InvokerTemplate = LambdaClass-> |
| 3736 | getLambdaStaticInvoker()->getDescribedFunctionTemplate(); |
| 3737 | |
Yaron Keren | f428fcf | 2015-05-13 17:56:46 +0000 | [diff] [blame] | 3738 | #ifndef NDEBUG |
| 3739 | Sema::TemplateDeductionResult LLVM_ATTRIBUTE_UNUSED Result = |
| 3740 | #endif |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3741 | S.FinishTemplateArgumentDeduction(InvokerTemplate, DeducedArguments, 0, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3742 | InvokerSpecialized, TDInfo); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3743 | assert(Result == Sema::TDK_Success && |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3744 | "If the call operator succeeded so should the invoker!"); |
| 3745 | // Set the result type to match the corresponding call operator |
| 3746 | // specialization's result type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3747 | if (GenericLambdaCallOperatorHasDeducedReturnType && |
| 3748 | InvokerSpecialized->getReturnType()->isUndeducedType()) { |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3749 | // Be sure to get the type to replace 'auto' with and not |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3750 | // the full result type of the call op specialization |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3751 | // to substitute into the 'auto' of the invoker and conversion |
| 3752 | // function. |
| 3753 | // For e.g. |
| 3754 | // int* (*fp)(int*) = [](auto* a) -> auto* { return a; }; |
| 3755 | // We don't want to subst 'int*' into 'auto' to get int**. |
| 3756 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3757 | QualType TypeToReplaceAutoWith = CallOpSpecialized->getReturnType() |
| 3758 | ->getContainedAutoType() |
| 3759 | ->getDeducedType(); |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3760 | SubstAutoWithinFunctionReturnType(InvokerSpecialized, |
| 3761 | TypeToReplaceAutoWith, S); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3762 | SubstAutoWithinFunctionReturnType(ConversionSpecialized, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3763 | TypeToReplaceAutoWith, S); |
| 3764 | } |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3765 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3766 | // Ensure that static invoker doesn't have a const qualifier. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3767 | // FIXME: When creating the InvokerTemplate in SemaLambda.cpp |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3768 | // do not use the CallOperator's TypeSourceInfo which allows |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3769 | // the const qualifier to leak through. |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3770 | const FunctionProtoType *InvokerFPT = InvokerSpecialized-> |
| 3771 | getType().getTypePtr()->castAs<FunctionProtoType>(); |
| 3772 | FunctionProtoType::ExtProtoInfo EPI = InvokerFPT->getExtProtoInfo(); |
| 3773 | EPI.TypeQuals = 0; |
| 3774 | InvokerSpecialized->setType(S.Context.getFunctionType( |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3775 | InvokerFPT->getReturnType(), InvokerFPT->getParamTypes(), EPI)); |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3776 | return Sema::TDK_Success; |
| 3777 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3778 | /// \brief Deduce template arguments for a templated conversion |
| 3779 | /// function (C++ [temp.deduct.conv]) and, if successful, produce a |
| 3780 | /// conversion function template specialization. |
| 3781 | Sema::TemplateDeductionResult |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3782 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *ConversionTemplate, |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3783 | QualType ToType, |
| 3784 | CXXConversionDecl *&Specialization, |
| 3785 | TemplateDeductionInfo &Info) { |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3786 | if (ConversionTemplate->isInvalidDecl()) |
Douglas Gregor | c5c01a6 | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3787 | return TDK_Invalid; |
| 3788 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3789 | CXXConversionDecl *ConversionGeneric |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3790 | = cast<CXXConversionDecl>(ConversionTemplate->getTemplatedDecl()); |
| 3791 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3792 | QualType FromType = ConversionGeneric->getConversionType(); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3793 | |
| 3794 | // Canonicalize the types for deduction. |
| 3795 | QualType P = Context.getCanonicalType(FromType); |
| 3796 | QualType A = Context.getCanonicalType(ToType); |
| 3797 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3798 | // C++0x [temp.deduct.conv]p2: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3799 | // If P is a reference type, the type referred to by P is used for |
| 3800 | // type deduction. |
| 3801 | if (const ReferenceType *PRef = P->getAs<ReferenceType>()) |
| 3802 | P = PRef->getPointeeType(); |
| 3803 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3804 | // C++0x [temp.deduct.conv]p4: |
| 3805 | // [...] 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] | 3806 | // for type deduction. |
| 3807 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3808 | A = ARef->getPointeeType().getUnqualifiedType(); |
| 3809 | // C++ [temp.deduct.conv]p3: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3810 | // |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3811 | // If A is not a reference type: |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3812 | else { |
| 3813 | assert(!A->isReferenceType() && "Reference types were handled above"); |
| 3814 | |
| 3815 | // - If P is an array type, the pointer type produced by the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3816 | // array-to-pointer standard conversion (4.2) is used in place |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3817 | // of P for type deduction; otherwise, |
| 3818 | if (P->isArrayType()) |
| 3819 | P = Context.getArrayDecayedType(P); |
| 3820 | // - If P is a function type, the pointer type produced by the |
| 3821 | // function-to-pointer standard conversion (4.3) is used in |
| 3822 | // place of P for type deduction; otherwise, |
| 3823 | else if (P->isFunctionType()) |
| 3824 | P = Context.getPointerType(P); |
| 3825 | // - 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] | 3826 | // P's type are ignored for type deduction. |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3827 | else |
| 3828 | P = P.getUnqualifiedType(); |
| 3829 | |
Douglas Gregor | d99609a | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3830 | // C++0x [temp.deduct.conv]p4: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3831 | // 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] | 3832 | // 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] | 3833 | // referred to by A is used for type deduction. |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3834 | A = A.getUnqualifiedType(); |
| 3835 | } |
| 3836 | |
Eli Friedman | 77dcc72 | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3837 | // Unevaluated SFINAE context. |
| 3838 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3839 | SFINAETrap Trap(*this); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3840 | |
| 3841 | // C++ [temp.deduct.conv]p1: |
| 3842 | // Template argument deduction is done by comparing the return |
| 3843 | // type of the template conversion function (call it P) with the |
| 3844 | // type that is required as the result of the conversion (call it |
| 3845 | // A) as described in 14.8.2.4. |
| 3846 | TemplateParameterList *TemplateParams |
Faisal Vali | 571df12 | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3847 | = ConversionTemplate->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3848 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3849 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3850 | |
| 3851 | // C++0x [temp.deduct.conv]p4: |
| 3852 | // In general, the deduction process attempts to find template |
| 3853 | // argument values that will make the deduced A identical to |
| 3854 | // A. However, there are two cases that allow a difference: |
| 3855 | unsigned TDF = 0; |
| 3856 | // - If the original A is a reference type, A can be more |
| 3857 | // cv-qualified than the deduced A (i.e., the type referred to |
| 3858 | // by the reference) |
| 3859 | if (ToType->isReferenceType()) |
| 3860 | TDF |= TDF_ParamWithReferenceType; |
| 3861 | // - The deduced A can be another pointer or pointer to member |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3862 | // type that can be converted to A via a qualification |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3863 | // conversion. |
| 3864 | // |
| 3865 | // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when |
| 3866 | // both P and A are pointers or member pointers. In this case, we |
| 3867 | // just ignore cv-qualifiers completely). |
| 3868 | if ((P->isPointerType() && A->isPointerType()) || |
Douglas Gregor | 9f05ed5 | 2011-08-30 00:37:54 +0000 | [diff] [blame] | 3869 | (P->isMemberPointerType() && A->isMemberPointerType())) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3870 | TDF |= TDF_IgnoreQualifiers; |
| 3871 | if (TemplateDeductionResult Result |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3872 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3873 | P, A, Info, Deduced, TDF)) |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3874 | return Result; |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3875 | |
| 3876 | // Create an Instantiation Scope for finalizing the operator. |
| 3877 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3878 | // Finish template argument deduction. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3879 | FunctionDecl *ConversionSpecialized = nullptr; |
Faisal Vali | 850da1a | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3880 | TemplateDeductionResult Result |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3881 | = FinishTemplateArgumentDeduction(ConversionTemplate, Deduced, 0, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3882 | ConversionSpecialized, Info); |
| 3883 | Specialization = cast_or_null<CXXConversionDecl>(ConversionSpecialized); |
| 3884 | |
| 3885 | // 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] | 3886 | // to a ptr-to-function, use the deduced arguments from the conversion |
| 3887 | // function to specialize the corresponding call operator. |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3888 | // e.g., int (*fp)(int) = [](auto a) { return a; }; |
| 3889 | if (Result == TDK_Success && isLambdaConversionOperator(ConversionGeneric)) { |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3890 | |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3891 | // 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] | 3892 | // to. This is necessary for matching the lambda call operator's return |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3893 | // type to that of the destination ptr-to-function's return type. |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3894 | assert(A->isPointerType() && |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3895 | "Can only convert from lambda to ptr-to-function"); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3896 | const FunctionType *ToFunType = |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3897 | A->getPointeeType().getTypePtr()->getAs<FunctionType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3898 | const QualType DestFunctionPtrReturnType = ToFunType->getReturnType(); |
| 3899 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3900 | // Create the corresponding specializations of the call operator and |
| 3901 | // the static-invoker; and if the return type is auto, |
| 3902 | // deduce the return type and check if it matches the |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3903 | // DestFunctionPtrReturnType. |
| 3904 | // For instance: |
| 3905 | // auto L = [](auto a) { return f(a); }; |
| 3906 | // int (*fp)(int) = L; |
| 3907 | // char (*fp2)(int) = L; <-- Not OK. |
| 3908 | |
| 3909 | Result = SpecializeCorrespondingLambdaCallOperatorAndInvoker( |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 3910 | Specialization, Deduced, DestFunctionPtrReturnType, |
Faisal Vali | 2b3a301 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3911 | Info, *this); |
| 3912 | } |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3913 | return Result; |
| 3914 | } |
| 3915 | |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3916 | /// \brief Deduce template arguments for a function template when there is |
| 3917 | /// nothing to deduce against (C++0x [temp.arg.explicit]p3). |
| 3918 | /// |
| 3919 | /// \param FunctionTemplate the function template for which we are performing |
| 3920 | /// template argument deduction. |
| 3921 | /// |
James Dennett | 18348b6 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3922 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3923 | /// arguments. |
| 3924 | /// |
| 3925 | /// \param Specialization if template argument deduction was successful, |
| 3926 | /// this will be set to the function template specialization produced by |
| 3927 | /// template argument deduction. |
| 3928 | /// |
| 3929 | /// \param Info the argument will be updated to provide additional information |
| 3930 | /// about template argument deduction. |
| 3931 | /// |
| 3932 | /// \returns the result of template argument deduction. |
| 3933 | Sema::TemplateDeductionResult |
| 3934 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 739b107a | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3935 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3936 | FunctionDecl *&Specialization, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3937 | TemplateDeductionInfo &Info, |
| 3938 | bool InOverloadResolution) { |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3939 | return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 19a41f1 | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3940 | QualType(), Specialization, Info, |
| 3941 | InOverloadResolution); |
Douglas Gregor | 8364e6b | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3942 | } |
| 3943 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3944 | namespace { |
| 3945 | /// Substitute the 'auto' type specifier within a type for a given replacement |
| 3946 | /// type. |
| 3947 | class SubstituteAutoTransform : |
| 3948 | public TreeTransform<SubstituteAutoTransform> { |
| 3949 | QualType Replacement; |
| 3950 | public: |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 3951 | SubstituteAutoTransform(Sema &SemaRef, QualType Replacement) |
| 3952 | : TreeTransform<SubstituteAutoTransform>(SemaRef), |
| 3953 | Replacement(Replacement) {} |
| 3954 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3955 | QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) { |
| 3956 | // If we're building the type pattern to deduce against, don't wrap the |
| 3957 | // substituted type in an AutoType. Certain template deduction rules |
| 3958 | // apply only when a template type parameter appears directly (and not if |
| 3959 | // the parameter is found through desugaring). For instance: |
| 3960 | // auto &&lref = lvalue; |
| 3961 | // must transform into "rvalue reference to T" not "rvalue reference to |
| 3962 | // 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] | 3963 | if (!Replacement.isNull() && isa<TemplateTypeParmType>(Replacement)) { |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3964 | QualType Result = Replacement; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 3965 | TemplateTypeParmTypeLoc NewTL = |
| 3966 | TLB.push<TemplateTypeParmTypeLoc>(Result); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3967 | NewTL.setNameLoc(TL.getNameLoc()); |
| 3968 | return Result; |
| 3969 | } else { |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 3970 | bool Dependent = |
| 3971 | !Replacement.isNull() && Replacement->isDependentType(); |
| 3972 | QualType Result = |
| 3973 | SemaRef.Context.getAutoType(Dependent ? QualType() : Replacement, |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 3974 | TL.getTypePtr()->getKeyword(), |
Manuel Klimek | 2fdbea2 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3975 | Dependent); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3976 | AutoTypeLoc NewTL = TLB.push<AutoTypeLoc>(Result); |
| 3977 | NewTL.setNameLoc(TL.getNameLoc()); |
| 3978 | return Result; |
| 3979 | } |
| 3980 | } |
Douglas Gregor | 0c46b2b | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 3981 | |
| 3982 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 3983 | // Lambdas never need to be transformed. |
| 3984 | return E; |
| 3985 | } |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3986 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3987 | QualType Apply(TypeLoc TL) { |
| 3988 | // Create some scratch storage for the transformed type locations. |
| 3989 | // FIXME: We're just going to throw this information away. Don't build it. |
| 3990 | TypeLocBuilder TLB; |
| 3991 | TLB.reserve(TL.getFullDataSize()); |
| 3992 | return TransformType(TLB, TL); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3993 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3994 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3995 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3996 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3997 | Sema::DeduceAutoResult |
| 3998 | Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *&Init, QualType &Result) { |
| 3999 | return DeduceAutoType(Type->getTypeLoc(), Init, Result); |
| 4000 | } |
| 4001 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4002 | /// \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] | 4003 | /// |
| 4004 | /// \param Type the type pattern using the auto type-specifier. |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4005 | /// \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] | 4006 | /// \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] | 4007 | /// deduced type. |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4008 | Sema::DeduceAutoResult |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4009 | Sema::DeduceAutoType(TypeLoc Type, Expr *&Init, QualType &Result) { |
John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4010 | if (Init->getType()->isNonOverloadPlaceholderType()) { |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4011 | ExprResult NonPlaceholder = CheckPlaceholderExpr(Init); |
| 4012 | if (NonPlaceholder.isInvalid()) |
| 4013 | return DAR_FailedAlreadyDiagnosed; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4014 | Init = NonPlaceholder.get(); |
John McCall | d5c98ae | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 4015 | } |
| 4016 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4017 | if (Init->isTypeDependent() || Type.getType()->isDependentType()) { |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4018 | Result = SubstituteAutoTransform(*this, Context.DependentTy).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4019 | assert(!Result.isNull() && "substituting DependentTy can't fail"); |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4020 | return DAR_Succeeded; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4021 | } |
| 4022 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4023 | // If this is a 'decltype(auto)' specifier, do the decltype dance. |
| 4024 | // Since 'decltype(auto)' can only occur at the top of the type, we |
| 4025 | // don't need to go digging for it. |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4026 | if (const AutoType *AT = Type.getType()->getAs<AutoType>()) { |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4027 | if (AT->isDecltypeAuto()) { |
| 4028 | if (isa<InitListExpr>(Init)) { |
| 4029 | Diag(Init->getLocStart(), diag::err_decltype_auto_initializer_list); |
| 4030 | return DAR_FailedAlreadyDiagnosed; |
| 4031 | } |
| 4032 | |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 4033 | QualType Deduced = BuildDecltypeType(Init, Init->getLocStart(), false); |
David Majnemer | 3c20ab2 | 2015-07-01 00:29:28 +0000 | [diff] [blame] | 4034 | if (Deduced.isNull()) |
| 4035 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4036 | // FIXME: Support a non-canonical deduced type for 'auto'. |
| 4037 | Deduced = Context.getCanonicalType(Deduced); |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4038 | Result = SubstituteAutoTransform(*this, Deduced).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4039 | if (Result.isNull()) |
| 4040 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4041 | return DAR_Succeeded; |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4042 | } else if (!getLangOpts().CPlusPlus) { |
| 4043 | if (isa<InitListExpr>(Init)) { |
| 4044 | Diag(Init->getLocStart(), diag::err_auto_init_list_from_c); |
| 4045 | return DAR_FailedAlreadyDiagnosed; |
| 4046 | } |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 4047 | } |
| 4048 | } |
| 4049 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4050 | SourceLocation Loc = Init->getExprLoc(); |
| 4051 | |
| 4052 | LocalInstantiationScope InstScope(*this); |
| 4053 | |
| 4054 | // Build template<class TemplParam> void Func(FuncParam); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4055 | TemplateTypeParmDecl *TemplParam = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4056 | TemplateTypeParmDecl::Create(Context, nullptr, SourceLocation(), Loc, 0, 0, |
| 4057 | nullptr, false, false); |
Chandler Carruth | 0883632 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 4058 | QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0); |
| 4059 | NamedDecl *TemplParamPtr = TemplParam; |
Hubert Tong | e4a0c0e | 2016-07-30 22:33:34 +0000 | [diff] [blame] | 4060 | FixedSizeTemplateParameterListStorage<1, false> TemplateParamsSt( |
| 4061 | Loc, Loc, TemplParamPtr, Loc, nullptr); |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 4062 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4063 | QualType FuncParam = SubstituteAutoTransform(*this, TemplArg).Apply(Type); |
| 4064 | assert(!FuncParam.isNull() && |
| 4065 | "substituting template parameter for 'auto' failed"); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4066 | |
| 4067 | // Deduce type of TemplParam in Func(Init) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4068 | SmallVector<DeducedTemplateArgument, 1> Deduced; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4069 | Deduced.resize(1); |
| 4070 | QualType InitType = Init->getType(); |
| 4071 | unsigned TDF = 0; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4072 | |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4073 | TemplateDeductionInfo Info(Loc); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4074 | |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 4075 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4076 | if (InitList) { |
| 4077 | for (unsigned i = 0, e = InitList->getNumInits(); i < e; ++i) { |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4078 | if (DeduceTemplateArgumentByListElement(*this, TemplateParamsSt.get(), |
| 4079 | TemplArg, InitList->getInit(i), |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 4080 | Info, Deduced, TDF)) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4081 | return DAR_Failed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4082 | } |
| 4083 | } else { |
Richard Smith | e301ba2 | 2015-11-11 02:02:15 +0000 | [diff] [blame] | 4084 | if (!getLangOpts().CPlusPlus && Init->refersToBitField()) { |
| 4085 | Diag(Loc, diag::err_auto_bitfield); |
| 4086 | return DAR_FailedAlreadyDiagnosed; |
| 4087 | } |
| 4088 | |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4089 | if (AdjustFunctionParmAndArgTypesForDeduction( |
| 4090 | *this, TemplateParamsSt.get(), FuncParam, InitType, Init, TDF)) |
Douglas Gregor | 0e60cd7 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 4091 | return DAR_Failed; |
Richard Smith | 74801c8 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 4092 | |
James Y Knight | 7a22b24 | 2015-08-06 20:26:32 +0000 | [diff] [blame] | 4093 | if (DeduceTemplateArgumentsByTypeMatch(*this, TemplateParamsSt.get(), |
| 4094 | FuncParam, InitType, Info, Deduced, |
| 4095 | TDF)) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4096 | return DAR_Failed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4097 | } |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4098 | |
Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4099 | if (Deduced[0].getKind() != TemplateArgument::Type) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4100 | return DAR_Failed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4101 | |
Eli Friedman | e431095 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4102 | QualType DeducedType = Deduced[0].getAsType(); |
| 4103 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4104 | if (InitList) { |
| 4105 | DeducedType = BuildStdInitializerList(DeducedType, Loc); |
| 4106 | if (DeducedType.isNull()) |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4107 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4108 | } |
| 4109 | |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4110 | Result = SubstituteAutoTransform(*this, DeducedType).Apply(Type); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4111 | if (Result.isNull()) |
| 4112 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4113 | |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4114 | // Check that the deduced argument type is compatible with the original |
| 4115 | // argument type per C++ [temp.deduct.call]p4. |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4116 | if (!InitList && !Result.isNull() && |
| 4117 | CheckOriginalCallArgDeduction(*this, |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4118 | Sema::OriginalCallArg(FuncParam,0,InitType), |
Richard Smith | 061f1e2 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4119 | Result)) { |
| 4120 | Result = QualType(); |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4121 | return DAR_Failed; |
Douglas Gregor | 518bc4c | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4122 | } |
| 4123 | |
Sebastian Redl | 09edce0 | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4124 | return DAR_Succeeded; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4125 | } |
| 4126 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 4127 | QualType Sema::SubstAutoType(QualType TypeWithAuto, |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4128 | QualType TypeToReplaceAuto) { |
| 4129 | return SubstituteAutoTransform(*this, TypeToReplaceAuto). |
| 4130 | TransformType(TypeWithAuto); |
| 4131 | } |
| 4132 | |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 4133 | TypeSourceInfo* Sema::SubstAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto, |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4134 | QualType TypeToReplaceAuto) { |
| 4135 | return SubstituteAutoTransform(*this, TypeToReplaceAuto). |
| 4136 | TransformType(TypeWithAuto); |
Richard Smith | 27d807c | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 4137 | } |
| 4138 | |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4139 | void Sema::DiagnoseAutoDeductionFailure(VarDecl *VDecl, Expr *Init) { |
| 4140 | if (isa<InitListExpr>(Init)) |
| 4141 | Diag(VDecl->getLocation(), |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4142 | VDecl->isInitCapture() |
| 4143 | ? diag::err_init_capture_deduction_failure_from_init_list |
| 4144 | : diag::err_auto_var_deduction_failure_from_init_list) |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4145 | << VDecl->getDeclName() << VDecl->getType() << Init->getSourceRange(); |
| 4146 | else |
Richard Smith | bb13c9a | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4147 | Diag(VDecl->getLocation(), |
| 4148 | VDecl->isInitCapture() ? diag::err_init_capture_deduction_failure |
| 4149 | : diag::err_auto_var_deduction_failure) |
Sebastian Redl | 42acd4a | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4150 | << VDecl->getDeclName() << VDecl->getType() << Init->getType() |
| 4151 | << Init->getSourceRange(); |
| 4152 | } |
| 4153 | |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4154 | bool Sema::DeduceReturnType(FunctionDecl *FD, SourceLocation Loc, |
| 4155 | bool Diagnose) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4156 | assert(FD->getReturnType()->isUndeducedType()); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4157 | |
| 4158 | if (FD->getTemplateInstantiationPattern()) |
| 4159 | InstantiateFunctionDefinition(Loc, FD); |
| 4160 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4161 | bool StillUndeduced = FD->getReturnType()->isUndeducedType(); |
Richard Smith | 2a7d481 | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4162 | if (StillUndeduced && Diagnose && !FD->isInvalidDecl()) { |
| 4163 | Diag(Loc, diag::err_auto_fn_used_before_defined) << FD; |
| 4164 | Diag(FD->getLocation(), diag::note_callee_decl) << FD; |
| 4165 | } |
| 4166 | |
| 4167 | return StillUndeduced; |
| 4168 | } |
| 4169 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4170 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4171 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4172 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4173 | unsigned Level, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4174 | llvm::SmallBitVector &Deduced); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4175 | |
| 4176 | /// \brief If this is a non-static member function, |
Craig Topper | 7965357 | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 4177 | static void |
| 4178 | AddImplicitObjectParameterType(ASTContext &Context, |
| 4179 | CXXMethodDecl *Method, |
| 4180 | SmallVectorImpl<QualType> &ArgTypes) { |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4181 | // C++11 [temp.func.order]p3: |
| 4182 | // [...] The new parameter is of type "reference to cv A," where cv are |
| 4183 | // the cv-qualifiers of the function template (if any) and A is |
| 4184 | // the class of which the function template is a member. |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4185 | // |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4186 | // The standard doesn't say explicitly, but we pick the appropriate kind of |
| 4187 | // reference type based on [over.match.funcs]p4. |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4188 | QualType ArgTy = Context.getTypeDeclType(Method->getParent()); |
| 4189 | ArgTy = Context.getQualifiedType(ArgTy, |
| 4190 | Qualifiers::fromCVRMask(Method->getTypeQualifiers())); |
Eli Friedman | ee2ff1c | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4191 | if (Method->getRefQualifier() == RQ_RValue) |
| 4192 | ArgTy = Context.getRValueReferenceType(ArgTy); |
| 4193 | else |
| 4194 | ArgTy = Context.getLValueReferenceType(ArgTy); |
Douglas Gregor | 52773dc | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4195 | ArgTypes.push_back(ArgTy); |
| 4196 | } |
| 4197 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4198 | /// \brief Determine whether the function template \p FT1 is at least as |
| 4199 | /// specialized as \p FT2. |
| 4200 | static bool isAtLeastAsSpecializedAs(Sema &S, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4201 | SourceLocation Loc, |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4202 | FunctionTemplateDecl *FT1, |
| 4203 | FunctionTemplateDecl *FT2, |
| 4204 | TemplatePartialOrderingContext TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4205 | unsigned NumCallArguments1) { |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4206 | FunctionDecl *FD1 = FT1->getTemplatedDecl(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4207 | FunctionDecl *FD2 = FT2->getTemplatedDecl(); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4208 | const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); |
| 4209 | const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4210 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4211 | assert(Proto1 && Proto2 && "Function templates must have prototypes"); |
| 4212 | TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4213 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4214 | Deduced.resize(TemplateParams->size()); |
| 4215 | |
| 4216 | // C++0x [temp.deduct.partial]p3: |
| 4217 | // The types used to determine the ordering depend on the context in which |
| 4218 | // the partial ordering is done: |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4219 | TemplateDeductionInfo Info(Loc); |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4220 | SmallVector<QualType, 4> Args2; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4221 | switch (TPOC) { |
| 4222 | case TPOC_Call: { |
| 4223 | // - In the context of a function call, the function parameter types are |
| 4224 | // used. |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4225 | CXXMethodDecl *Method1 = dyn_cast<CXXMethodDecl>(FD1); |
| 4226 | CXXMethodDecl *Method2 = dyn_cast<CXXMethodDecl>(FD2); |
Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4227 | |
Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4228 | // C++11 [temp.func.order]p3: |
Douglas Gregor | ee430a3 | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4229 | // [...] If only one of the function templates is a non-static |
| 4230 | // member, that function template is considered to have a new |
| 4231 | // first parameter inserted in its function parameter list. The |
| 4232 | // new parameter is of type "reference to cv A," where cv are |
| 4233 | // the cv-qualifiers of the function template (if any) and A is |
| 4234 | // the class of which the function template is a member. |
| 4235 | // |
Eli Friedman | 3b5774a | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4236 | // Note that we interpret this to mean "if one of the function |
| 4237 | // templates is a non-static member and the other is a non-member"; |
| 4238 | // otherwise, the ordering rules for static functions against non-static |
| 4239 | // functions don't make any sense. |
| 4240 | // |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4241 | // C++98/03 doesn't have this provision but we've extended DR532 to cover |
| 4242 | // it as wording was broken prior to it. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4243 | SmallVector<QualType, 4> Args1; |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4244 | |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4245 | unsigned NumComparedArguments = NumCallArguments1; |
| 4246 | |
| 4247 | if (!Method2 && Method1 && !Method1->isStatic()) { |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4248 | // Compare 'this' from Method1 against first parameter from Method2. |
| 4249 | AddImplicitObjectParameterType(S.Context, Method1, Args1); |
| 4250 | ++NumComparedArguments; |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4251 | } else if (!Method1 && Method2 && !Method2->isStatic()) { |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4252 | // Compare 'this' from Method2 against first parameter from Method1. |
| 4253 | AddImplicitObjectParameterType(S.Context, Method2, Args2); |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4254 | } |
| 4255 | |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4256 | Args1.insert(Args1.end(), Proto1->param_type_begin(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4257 | Proto1->param_type_end()); |
Nikola Smiljanic | 4461de2 | 2014-05-31 02:10:59 +0000 | [diff] [blame] | 4258 | Args2.insert(Args2.end(), Proto2->param_type_begin(), |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4259 | Proto2->param_type_end()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4260 | |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4261 | // C++ [temp.func.order]p5: |
| 4262 | // The presence of unused ellipsis and default arguments has no effect on |
| 4263 | // the partial ordering of function templates. |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4264 | if (Args1.size() > NumComparedArguments) |
| 4265 | Args1.resize(NumComparedArguments); |
| 4266 | if (Args2.size() > NumComparedArguments) |
| 4267 | Args2.resize(NumComparedArguments); |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4268 | if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(), |
| 4269 | Args1.data(), Args1.size(), Info, Deduced, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4270 | TDF_None, /*PartialOrdering=*/true)) |
Richard Smith | 0a80d57 | 2014-05-29 01:12:14 +0000 | [diff] [blame] | 4271 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4272 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4273 | break; |
| 4274 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4275 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4276 | case TPOC_Conversion: |
| 4277 | // - In the context of a call to a conversion operator, the return types |
| 4278 | // of the conversion function templates are used. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4279 | if (DeduceTemplateArgumentsByTypeMatch( |
| 4280 | S, TemplateParams, Proto2->getReturnType(), Proto1->getReturnType(), |
| 4281 | Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4282 | /*PartialOrdering=*/true)) |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4283 | return false; |
| 4284 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4285 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4286 | case TPOC_Other: |
NAKAMURA Takumi | 7c28886 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4287 | // - 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] | 4288 | // is used. |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4289 | if (DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 4290 | FD2->getType(), FD1->getType(), |
| 4291 | Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4292 | /*PartialOrdering=*/true)) |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4293 | return false; |
| 4294 | break; |
| 4295 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4296 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4297 | // C++0x [temp.deduct.partial]p11: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4298 | // In most cases, all template parameters must have values in order for |
| 4299 | // deduction to succeed, but for partial ordering purposes a template |
| 4300 | // 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] | 4301 | // types being used for partial ordering. [ Note: a template parameter used |
| 4302 | // in a non-deduced context is considered used. -end note] |
| 4303 | unsigned ArgIdx = 0, NumArgs = Deduced.size(); |
| 4304 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4305 | if (Deduced[ArgIdx].isNull()) |
| 4306 | break; |
| 4307 | |
| 4308 | if (ArgIdx == NumArgs) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4309 | // All template arguments were deduced. FT1 is at least as specialized |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4310 | // as FT2. |
| 4311 | return true; |
| 4312 | } |
| 4313 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4314 | // Figure out which template parameters were used. |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4315 | llvm::SmallBitVector UsedParameters(TemplateParams->size()); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4316 | switch (TPOC) { |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4317 | case TPOC_Call: |
| 4318 | for (unsigned I = 0, N = Args2.size(); I != N; ++I) |
| 4319 | ::MarkUsedTemplateParameters(S.Context, Args2[I], false, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4320 | TemplateParams->getDepth(), |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4321 | UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4322 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4323 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4324 | case TPOC_Conversion: |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4325 | ::MarkUsedTemplateParameters(S.Context, Proto2->getReturnType(), false, |
| 4326 | TemplateParams->getDepth(), UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4327 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4328 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4329 | case TPOC_Other: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4330 | ::MarkUsedTemplateParameters(S.Context, FD2->getType(), false, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4331 | TemplateParams->getDepth(), |
| 4332 | UsedParameters); |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4333 | break; |
| 4334 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4335 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4336 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4337 | // If this argument had no value deduced but was used in one of the types |
| 4338 | // used for partial ordering, then deduction fails. |
| 4339 | if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) |
| 4340 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4341 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4342 | return true; |
| 4343 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4344 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4345 | /// \brief Determine whether this a function template whose parameter-type-list |
| 4346 | /// ends with a function parameter pack. |
| 4347 | static bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) { |
| 4348 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
| 4349 | unsigned NumParams = Function->getNumParams(); |
| 4350 | if (NumParams == 0) |
| 4351 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4352 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4353 | ParmVarDecl *Last = Function->getParamDecl(NumParams - 1); |
| 4354 | if (!Last->isParameterPack()) |
| 4355 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4356 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4357 | // Make sure that no previous parameter is a parameter pack. |
| 4358 | while (--NumParams > 0) { |
| 4359 | if (Function->getParamDecl(NumParams - 1)->isParameterPack()) |
| 4360 | return false; |
| 4361 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4362 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4363 | return true; |
| 4364 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4365 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4366 | /// \brief Returns the more specialized function template according |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4367 | /// to the rules of function template partial ordering (C++ [temp.func.order]). |
| 4368 | /// |
| 4369 | /// \param FT1 the first function template |
| 4370 | /// |
| 4371 | /// \param FT2 the second function template |
| 4372 | /// |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4373 | /// \param TPOC the context in which we are performing partial ordering of |
| 4374 | /// function templates. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4375 | /// |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4376 | /// \param NumCallArguments1 The number of arguments in the call to FT1, used |
| 4377 | /// only when \c TPOC is \c TPOC_Call. |
| 4378 | /// |
| 4379 | /// \param NumCallArguments2 The number of arguments in the call to FT2, used |
| 4380 | /// only when \c TPOC is \c TPOC_Call. |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4381 | /// |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4382 | /// \returns the more specialized function template. If neither |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4383 | /// template is more specialized, returns NULL. |
| 4384 | FunctionTemplateDecl * |
| 4385 | Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, |
| 4386 | FunctionTemplateDecl *FT2, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4387 | SourceLocation Loc, |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4388 | TemplatePartialOrderingContext TPOC, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4389 | unsigned NumCallArguments1, |
| 4390 | unsigned NumCallArguments2) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4391 | bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4392 | NumCallArguments1); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4393 | bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4394 | NumCallArguments2); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4395 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4396 | if (Better1 != Better2) // We have a clear winner |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4397 | return Better1 ? FT1 : FT2; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4398 | |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4399 | if (!Better1 && !Better2) // Neither is better than the other |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4400 | return nullptr; |
Douglas Gregor | 0ff7d92 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4401 | |
Douglas Gregor | cef1a03 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4402 | // FIXME: This mimics what GCC implements, but doesn't match up with the |
| 4403 | // proposed resolution for core issue 692. This area needs to be sorted out, |
| 4404 | // but for now we attempt to maintain compatibility. |
| 4405 | bool Variadic1 = isVariadicFunctionTemplate(FT1); |
| 4406 | bool Variadic2 = isVariadicFunctionTemplate(FT2); |
| 4407 | if (Variadic1 != Variadic2) |
| 4408 | return Variadic1? FT2 : FT1; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4409 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4410 | return nullptr; |
Douglas Gregor | 05155d8 | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4411 | } |
Douglas Gregor | 9b14658 | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4412 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4413 | /// \brief Determine if the two templates are equivalent. |
| 4414 | static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) { |
| 4415 | if (T1 == T2) |
| 4416 | return true; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4417 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4418 | if (!T1 || !T2) |
| 4419 | return false; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4420 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4421 | return T1->getCanonicalDecl() == T2->getCanonicalDecl(); |
| 4422 | } |
| 4423 | |
| 4424 | /// \brief Retrieve the most specialized of the given function template |
| 4425 | /// specializations. |
| 4426 | /// |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4427 | /// \param SpecBegin the start iterator of the function template |
| 4428 | /// specializations that we will be comparing. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4429 | /// |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4430 | /// \param SpecEnd the end iterator of the function template |
| 4431 | /// specializations, paired with \p SpecBegin. |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4432 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4433 | /// \param Loc the location where the ambiguity or no-specializations |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4434 | /// diagnostic should occur. |
| 4435 | /// |
| 4436 | /// \param NoneDiag partial diagnostic used to diagnose cases where there are |
| 4437 | /// no matching candidates. |
| 4438 | /// |
| 4439 | /// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one |
| 4440 | /// occurs. |
| 4441 | /// |
| 4442 | /// \param CandidateDiag partial diagnostic used for each function template |
| 4443 | /// specialization that is a candidate in the ambiguous ordering. One parameter |
| 4444 | /// in this diagnostic should be unbound, which will correspond to the string |
| 4445 | /// describing the template arguments for the function template specialization. |
| 4446 | /// |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4447 | /// \returns the most specialized function template specialization, if |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4448 | /// found. Otherwise, returns SpecEnd. |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4449 | UnresolvedSetIterator Sema::getMostSpecialized( |
| 4450 | UnresolvedSetIterator SpecBegin, UnresolvedSetIterator SpecEnd, |
| 4451 | TemplateSpecCandidateSet &FailedCandidates, |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4452 | SourceLocation Loc, const PartialDiagnostic &NoneDiag, |
| 4453 | const PartialDiagnostic &AmbigDiag, const PartialDiagnostic &CandidateDiag, |
| 4454 | bool Complain, QualType TargetType) { |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4455 | if (SpecBegin == SpecEnd) { |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4456 | if (Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4457 | Diag(Loc, NoneDiag); |
Larisse Voufo | 98b20f1 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4458 | FailedCandidates.NoteCandidates(*this, Loc); |
| 4459 | } |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4460 | return SpecEnd; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4461 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4462 | |
| 4463 | if (SpecBegin + 1 == SpecEnd) |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4464 | return SpecBegin; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4465 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4466 | // Find the function template that is better than all of the templates it |
| 4467 | // has been compared to. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4468 | UnresolvedSetIterator Best = SpecBegin; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4469 | FunctionTemplateDecl *BestTemplate |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4470 | = cast<FunctionDecl>(*Best)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4471 | assert(BestTemplate && "Not a function template specialization?"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4472 | for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) { |
| 4473 | FunctionTemplateDecl *Challenger |
| 4474 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4475 | assert(Challenger && "Not a function template specialization?"); |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4476 | if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4477 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4478 | Challenger)) { |
| 4479 | Best = I; |
| 4480 | BestTemplate = Challenger; |
| 4481 | } |
| 4482 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4483 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4484 | // Make sure that the "best" function template is more specialized than all |
| 4485 | // of the others. |
| 4486 | bool Ambiguous = false; |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4487 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4488 | FunctionTemplateDecl *Challenger |
| 4489 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4490 | if (I != Best && |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4491 | !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | e5b5220 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4492 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4493 | BestTemplate)) { |
| 4494 | Ambiguous = true; |
| 4495 | break; |
| 4496 | } |
| 4497 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4498 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4499 | if (!Ambiguous) { |
| 4500 | // We found an answer. Return it. |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4501 | return Best; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4502 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4503 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4504 | // Diagnose the ambiguity. |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4505 | if (Complain) { |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4506 | Diag(Loc, AmbigDiag); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4507 | |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4508 | // FIXME: Can we order the candidates in some sane way? |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4509 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4510 | PartialDiagnostic PD = CandidateDiag; |
| 4511 | PD << getTemplateArgumentBindingsText( |
Douglas Gregor | b491ed3 | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4512 | cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(), |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4513 | *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs()); |
Richard Trieu | caff247 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4514 | if (!TargetType.isNull()) |
| 4515 | HandleFunctionTypeMismatch(PD, cast<FunctionDecl>(*I)->getType(), |
| 4516 | TargetType); |
| 4517 | Diag((*I)->getLocation(), PD); |
| 4518 | } |
Richard Smith | b875c43 | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4519 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4520 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4521 | return SpecEnd; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4522 | } |
| 4523 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4524 | /// \brief Returns the more specialized class template partial specialization |
| 4525 | /// according to the rules of partial ordering of class template partial |
| 4526 | /// specializations (C++ [temp.class.order]). |
| 4527 | /// |
| 4528 | /// \param PS1 the first class template partial specialization |
| 4529 | /// |
| 4530 | /// \param PS2 the second class template partial specialization |
| 4531 | /// |
| 4532 | /// \returns the more specialized class template partial specialization. If |
| 4533 | /// neither partial specialization is more specialized, returns NULL. |
| 4534 | ClassTemplatePartialSpecializationDecl * |
| 4535 | Sema::getMoreSpecializedPartialSpecialization( |
| 4536 | ClassTemplatePartialSpecializationDecl *PS1, |
John McCall | bc077cf | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4537 | ClassTemplatePartialSpecializationDecl *PS2, |
| 4538 | SourceLocation Loc) { |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4539 | // C++ [temp.class.order]p1: |
| 4540 | // For two class template partial specializations, the first is at least as |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4541 | // specialized as the second if, given the following rewrite to two |
| 4542 | // function templates, the first function template is at least as |
| 4543 | // specialized as the second according to the ordering rules for function |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4544 | // templates (14.6.6.2): |
| 4545 | // - the first function template has the same template parameters as the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4546 | // first partial specialization and has a single function parameter |
| 4547 | // whose type is a class template specialization with the template |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4548 | // arguments of the first partial specialization, and |
| 4549 | // - the second function template has the same template parameters as the |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4550 | // second partial specialization and has a single function parameter |
| 4551 | // whose type is a class template specialization with the template |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4552 | // arguments of the second partial specialization. |
| 4553 | // |
Douglas Gregor | 684268d | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 4554 | // Rather than synthesize function templates, we merely perform the |
| 4555 | // equivalent partial ordering by performing deduction directly on |
| 4556 | // the template arguments of the class template partial |
| 4557 | // specializations. This computation is slightly simpler than the |
| 4558 | // general problem of function template partial ordering, because |
| 4559 | // class template partial specializations are more constrained. We |
| 4560 | // know that every template parameter is deducible from the class |
| 4561 | // template partial specialization's template arguments, for |
| 4562 | // example. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4563 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Craig Topper | e6706e4 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4564 | TemplateDeductionInfo Info(Loc); |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4565 | |
| 4566 | QualType PT1 = PS1->getInjectedSpecializationType(); |
| 4567 | QualType PT2 = PS2->getInjectedSpecializationType(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4568 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4569 | // Determine whether PS1 is at least as specialized as PS2 |
| 4570 | Deduced.resize(PS2->getTemplateParameters()->size()); |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4571 | bool Better1 = !DeduceTemplateArgumentsByTypeMatch(*this, |
| 4572 | PS2->getTemplateParameters(), |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4573 | PT2, PT1, Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4574 | /*PartialOrdering=*/true); |
Argyrios Kyrtzidis | 7d39155 | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4575 | if (Better1) { |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 4576 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(),Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 4577 | InstantiatingTemplate Inst(*this, Loc, PS2, DeducedArgs, Info); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4578 | Better1 = !::FinishTemplateArgumentDeduction( |
| 4579 | *this, PS2, PS1->getTemplateArgs(), Deduced, Info); |
| 4580 | } |
| 4581 | |
| 4582 | // Determine whether PS2 is at least as specialized as PS1 |
| 4583 | Deduced.clear(); |
| 4584 | Deduced.resize(PS1->getTemplateParameters()->size()); |
| 4585 | bool Better2 = !DeduceTemplateArgumentsByTypeMatch( |
| 4586 | *this, PS1->getTemplateParameters(), PT1, PT2, Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4587 | /*PartialOrdering=*/true); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4588 | if (Better2) { |
| 4589 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), |
| 4590 | Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 4591 | InstantiatingTemplate Inst(*this, Loc, PS1, DeducedArgs, Info); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4592 | Better2 = !::FinishTemplateArgumentDeduction( |
| 4593 | *this, PS1, PS2->getTemplateArgs(), Deduced, Info); |
| 4594 | } |
| 4595 | |
| 4596 | if (Better1 == Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4597 | return nullptr; |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4598 | |
| 4599 | return Better1 ? PS1 : PS2; |
| 4600 | } |
| 4601 | |
Larisse Voufo | 3061638 | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 4602 | /// TODO: Unify with ClassTemplatePartialSpecializationDecl version? |
| 4603 | /// May require unifying ClassTemplate(Partial)SpecializationDecl and |
| 4604 | /// VarTemplate(Partial)SpecializationDecl with a new data |
| 4605 | /// structure Template(Partial)SpecializationDecl, and |
| 4606 | /// using Template(Partial)SpecializationDecl as input type. |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4607 | VarTemplatePartialSpecializationDecl * |
| 4608 | Sema::getMoreSpecializedPartialSpecialization( |
| 4609 | VarTemplatePartialSpecializationDecl *PS1, |
| 4610 | VarTemplatePartialSpecializationDecl *PS2, SourceLocation Loc) { |
| 4611 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 4612 | TemplateDeductionInfo Info(Loc); |
| 4613 | |
Richard Smith | f04fd0b | 2013-12-12 23:14:16 +0000 | [diff] [blame] | 4614 | assert(PS1->getSpecializedTemplate() == PS2->getSpecializedTemplate() && |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4615 | "the partial specializations being compared should specialize" |
| 4616 | " the same template."); |
| 4617 | TemplateName Name(PS1->getSpecializedTemplate()); |
| 4618 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4619 | QualType PT1 = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4620 | CanonTemplate, PS1->getTemplateArgs().asArray()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4621 | QualType PT2 = Context.getTemplateSpecializationType( |
David Majnemer | 6fbeee3 | 2016-07-07 04:43:07 +0000 | [diff] [blame] | 4622 | CanonTemplate, PS2->getTemplateArgs().asArray()); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4623 | |
| 4624 | // Determine whether PS1 is at least as specialized as PS2 |
| 4625 | Deduced.resize(PS2->getTemplateParameters()->size()); |
| 4626 | bool Better1 = !DeduceTemplateArgumentsByTypeMatch( |
| 4627 | *this, PS2->getTemplateParameters(), PT2, PT1, Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4628 | /*PartialOrdering=*/true); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4629 | if (Better1) { |
| 4630 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), |
| 4631 | Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 4632 | InstantiatingTemplate Inst(*this, Loc, PS2, DeducedArgs, Info); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4633 | Better1 = !::FinishTemplateArgumentDeduction(*this, PS2, |
| 4634 | PS1->getTemplateArgs(), |
Douglas Gregor | 9225b02 | 2010-04-29 06:31:36 +0000 | [diff] [blame] | 4635 | Deduced, Info); |
Argyrios Kyrtzidis | 7d39155 | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4636 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4637 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4638 | // Determine whether PS2 is at least as specialized as PS1 |
Douglas Gregor | adee3e3 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 4639 | Deduced.clear(); |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4640 | Deduced.resize(PS1->getTemplateParameters()->size()); |
Sebastian Redl | fb0b1f1 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4641 | bool Better2 = !DeduceTemplateArgumentsByTypeMatch(*this, |
| 4642 | PS1->getTemplateParameters(), |
Douglas Gregor | b837ea4 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4643 | PT1, PT2, Info, Deduced, TDF_None, |
Richard Smith | ed563c2 | 2015-02-20 04:45:22 +0000 | [diff] [blame] | 4644 | /*PartialOrdering=*/true); |
Argyrios Kyrtzidis | 7d39155 | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4645 | if (Better2) { |
Richard Smith | 8093465 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 4646 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(),Deduced.end()); |
Nick Lewycky | 5641233 | 2014-01-11 02:37:12 +0000 | [diff] [blame] | 4647 | InstantiatingTemplate Inst(*this, Loc, PS1, DeducedArgs, Info); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4648 | Better2 = !::FinishTemplateArgumentDeduction(*this, PS1, |
| 4649 | PS2->getTemplateArgs(), |
Douglas Gregor | 9225b02 | 2010-04-29 06:31:36 +0000 | [diff] [blame] | 4650 | Deduced, Info); |
Argyrios Kyrtzidis | 7d39155 | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4651 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4652 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4653 | if (Better1 == Better2) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4654 | return nullptr; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4655 | |
Douglas Gregor | be99939 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4656 | return Better1? PS1 : PS2; |
| 4657 | } |
| 4658 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4659 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4660 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4661 | const TemplateArgument &TemplateArg, |
| 4662 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4663 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4664 | llvm::SmallBitVector &Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4665 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4666 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4667 | /// expression. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4668 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4669 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4670 | const Expr *E, |
| 4671 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4672 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4673 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e8e9dd6 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 4674 | // We can deduce from a pack expansion. |
| 4675 | if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E)) |
| 4676 | E = Expansion->getPattern(); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4677 | |
Richard Smith | 3434900 | 2012-07-09 03:07:20 +0000 | [diff] [blame] | 4678 | // Skip through any implicit casts we added while type-checking, and any |
| 4679 | // substitutions performed by template alias expansion. |
| 4680 | while (1) { |
| 4681 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 4682 | E = ICE->getSubExpr(); |
| 4683 | else if (const SubstNonTypeTemplateParmExpr *Subst = |
| 4684 | dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 4685 | E = Subst->getReplacement(); |
| 4686 | else |
| 4687 | break; |
| 4688 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4689 | |
| 4690 | // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4691 | // find other occurrences of template parameters. |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4692 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
Douglas Gregor | 04b1152 | 2010-01-14 18:13:22 +0000 | [diff] [blame] | 4693 | if (!DRE) |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4694 | return; |
| 4695 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4696 | const NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4697 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 4698 | if (!NTTP) |
| 4699 | return; |
| 4700 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4701 | if (NTTP->getDepth() == Depth) |
| 4702 | Used[NTTP->getIndex()] = true; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4703 | } |
| 4704 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4705 | /// \brief Mark the template parameters that are used by the given |
| 4706 | /// nested name specifier. |
| 4707 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4708 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4709 | NestedNameSpecifier *NNS, |
| 4710 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4711 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4712 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4713 | if (!NNS) |
| 4714 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4715 | |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4716 | MarkUsedTemplateParameters(Ctx, NNS->getPrefix(), OnlyDeduced, Depth, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4717 | Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4718 | MarkUsedTemplateParameters(Ctx, QualType(NNS->getAsType(), 0), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4719 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4720 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4721 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4722 | /// \brief Mark the template parameters that are used by the given |
| 4723 | /// template name. |
| 4724 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4725 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4726 | TemplateName Name, |
| 4727 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4728 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4729 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4730 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 4731 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4732 | = dyn_cast<TemplateTemplateParmDecl>(Template)) { |
| 4733 | if (TTP->getDepth() == Depth) |
| 4734 | Used[TTP->getIndex()] = true; |
| 4735 | } |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4736 | return; |
| 4737 | } |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4738 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4739 | if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4740 | MarkUsedTemplateParameters(Ctx, QTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4741 | Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4742 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4743 | MarkUsedTemplateParameters(Ctx, DTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4744 | Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4745 | } |
| 4746 | |
| 4747 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4748 | /// type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4749 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4750 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 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 | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4754 | if (T.isNull()) |
| 4755 | return; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4756 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4757 | // Non-dependent types have nothing deducible |
| 4758 | if (!T->isDependentType()) |
| 4759 | return; |
| 4760 | |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4761 | T = Ctx.getCanonicalType(T); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4762 | switch (T->getTypeClass()) { |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4763 | case Type::Pointer: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4764 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4765 | cast<PointerType>(T)->getPointeeType(), |
| 4766 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4767 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4768 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4769 | break; |
| 4770 | |
| 4771 | case Type::BlockPointer: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4772 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4773 | cast<BlockPointerType>(T)->getPointeeType(), |
| 4774 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4775 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4776 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4777 | break; |
| 4778 | |
| 4779 | case Type::LValueReference: |
| 4780 | case Type::RValueReference: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4781 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4782 | cast<ReferenceType>(T)->getPointeeType(), |
| 4783 | OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4784 | Depth, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4785 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4786 | break; |
| 4787 | |
| 4788 | case Type::MemberPointer: { |
| 4789 | const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4790 | MarkUsedTemplateParameters(Ctx, MemPtr->getPointeeType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4791 | Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4792 | MarkUsedTemplateParameters(Ctx, QualType(MemPtr->getClass(), 0), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4793 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4794 | break; |
| 4795 | } |
| 4796 | |
| 4797 | case Type::DependentSizedArray: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4798 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4799 | cast<DependentSizedArrayType>(T)->getSizeExpr(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4800 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4801 | // Fall through to check the element type |
| 4802 | |
| 4803 | case Type::ConstantArray: |
| 4804 | case Type::IncompleteArray: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4805 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4806 | cast<ArrayType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4807 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4808 | break; |
| 4809 | |
| 4810 | case Type::Vector: |
| 4811 | case Type::ExtVector: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4812 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4813 | cast<VectorType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4814 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4815 | break; |
| 4816 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4817 | case Type::DependentSizedExtVector: { |
| 4818 | const DependentSizedExtVectorType *VecType |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4819 | = cast<DependentSizedExtVectorType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4820 | MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4821 | Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4822 | MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4823 | Depth, Used); |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4824 | break; |
| 4825 | } |
| 4826 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4827 | case Type::FunctionProto: { |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4828 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4829 | MarkUsedTemplateParameters(Ctx, Proto->getReturnType(), OnlyDeduced, Depth, |
| 4830 | Used); |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 4831 | for (unsigned I = 0, N = Proto->getNumParams(); I != N; ++I) |
| 4832 | MarkUsedTemplateParameters(Ctx, Proto->getParamType(I), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4833 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4834 | break; |
| 4835 | } |
| 4836 | |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4837 | case Type::TemplateTypeParm: { |
| 4838 | const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T); |
| 4839 | if (TTP->getDepth() == Depth) |
| 4840 | Used[TTP->getIndex()] = true; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4841 | break; |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4842 | } |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4843 | |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4844 | case Type::SubstTemplateTypeParmPack: { |
| 4845 | const SubstTemplateTypeParmPackType *Subst |
| 4846 | = cast<SubstTemplateTypeParmPackType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4847 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4848 | QualType(Subst->getReplacedParameter(), 0), |
| 4849 | OnlyDeduced, Depth, Used); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4850 | MarkUsedTemplateParameters(Ctx, Subst->getArgumentPack(), |
Douglas Gregor | fb322d8 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4851 | OnlyDeduced, Depth, Used); |
| 4852 | break; |
| 4853 | } |
| 4854 | |
John McCall | 2408e32 | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4855 | case Type::InjectedClassName: |
| 4856 | T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); |
| 4857 | // fall through |
| 4858 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4859 | case Type::TemplateSpecialization: { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4860 | const TemplateSpecializationType *Spec |
Douglas Gregor | 1e09bf83c | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4861 | = cast<TemplateSpecializationType>(T); |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4862 | MarkUsedTemplateParameters(Ctx, Spec->getTemplateName(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4863 | Depth, Used); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4864 | |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4865 | // C++0x [temp.deduct.type]p9: |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 4866 | // If the template argument list of P contains a pack expansion that is |
| 4867 | // not the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4868 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4869 | if (OnlyDeduced && |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4870 | hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs())) |
| 4871 | break; |
| 4872 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4873 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4874 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4875 | Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4876 | break; |
| 4877 | } |
| 4878 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4879 | case Type::Complex: |
| 4880 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4881 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4882 | cast<ComplexType>(T)->getElementType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4883 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4884 | break; |
| 4885 | |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4886 | case Type::Atomic: |
| 4887 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4888 | MarkUsedTemplateParameters(Ctx, |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4889 | cast<AtomicType>(T)->getValueType(), |
| 4890 | OnlyDeduced, Depth, Used); |
| 4891 | break; |
| 4892 | |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4893 | case Type::DependentName: |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4894 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4895 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | c1d2d8a | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4896 | cast<DependentNameType>(T)->getQualifier(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4897 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4898 | break; |
| 4899 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4900 | case Type::DependentTemplateSpecialization: { |
Richard Smith | 50d5b97 | 2015-12-30 20:56:05 +0000 | [diff] [blame] | 4901 | // C++14 [temp.deduct.type]p5: |
| 4902 | // The non-deduced contexts are: |
| 4903 | // -- The nested-name-specifier of a type that was specified using a |
| 4904 | // qualified-id |
| 4905 | // |
| 4906 | // C++14 [temp.deduct.type]p6: |
| 4907 | // When a type name is specified in a way that includes a non-deduced |
| 4908 | // context, all of the types that comprise that type name are also |
| 4909 | // non-deduced. |
| 4910 | if (OnlyDeduced) |
| 4911 | break; |
| 4912 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4913 | const DependentTemplateSpecializationType *Spec |
| 4914 | = cast<DependentTemplateSpecializationType>(T); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4915 | |
Richard Smith | 50d5b97 | 2015-12-30 20:56:05 +0000 | [diff] [blame] | 4916 | MarkUsedTemplateParameters(Ctx, Spec->getQualifier(), |
| 4917 | OnlyDeduced, Depth, Used); |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4918 | |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4919 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4920 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
John McCall | c392f37 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4921 | Used); |
| 4922 | break; |
| 4923 | } |
| 4924 | |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4925 | case Type::TypeOf: |
| 4926 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4927 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4928 | cast<TypeOfType>(T)->getUnderlyingType(), |
| 4929 | OnlyDeduced, Depth, Used); |
| 4930 | break; |
| 4931 | |
| 4932 | case Type::TypeOfExpr: |
| 4933 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4934 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4935 | cast<TypeOfExprType>(T)->getUnderlyingExpr(), |
| 4936 | OnlyDeduced, Depth, Used); |
| 4937 | break; |
| 4938 | |
| 4939 | case Type::Decltype: |
| 4940 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4941 | MarkUsedTemplateParameters(Ctx, |
John McCall | bd8d9bd | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4942 | cast<DecltypeType>(T)->getUnderlyingExpr(), |
| 4943 | OnlyDeduced, Depth, Used); |
| 4944 | break; |
| 4945 | |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4946 | case Type::UnaryTransform: |
| 4947 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4948 | MarkUsedTemplateParameters(Ctx, |
Alexis Hunt | e852b10 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4949 | cast<UnaryTransformType>(T)->getUnderlyingType(), |
| 4950 | OnlyDeduced, Depth, Used); |
| 4951 | break; |
| 4952 | |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4953 | case Type::PackExpansion: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4954 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | d2fa766 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4955 | cast<PackExpansionType>(T)->getPattern(), |
| 4956 | OnlyDeduced, Depth, Used); |
| 4957 | break; |
| 4958 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4959 | case Type::Auto: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4960 | MarkUsedTemplateParameters(Ctx, |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4961 | cast<AutoType>(T)->getDeducedType(), |
| 4962 | OnlyDeduced, Depth, Used); |
| 4963 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4964 | // None of these types have any template parameters in them. |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4965 | case Type::Builtin: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4966 | case Type::VariableArray: |
| 4967 | case Type::FunctionNoProto: |
| 4968 | case Type::Record: |
| 4969 | case Type::Enum: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4970 | case Type::ObjCInterface: |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4971 | case Type::ObjCObject: |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 4972 | case Type::ObjCObjectPointer: |
John McCall | b96ec56 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4973 | case Type::UnresolvedUsing: |
Xiuli Pan | 9c14e28 | 2016-01-09 12:53:17 +0000 | [diff] [blame] | 4974 | case Type::Pipe: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4975 | #define TYPE(Class, Base) |
| 4976 | #define ABSTRACT_TYPE(Class, Base) |
| 4977 | #define DEPENDENT_TYPE(Class, Base) |
| 4978 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 4979 | #include "clang/AST/TypeNodes.def" |
| 4980 | break; |
| 4981 | } |
| 4982 | } |
| 4983 | |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4984 | /// \brief Mark the template parameters that are used by this |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4985 | /// template argument. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4986 | static void |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4987 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4988 | const TemplateArgument &TemplateArg, |
| 4989 | bool OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4990 | unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4991 | llvm::SmallBitVector &Used) { |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4992 | switch (TemplateArg.getKind()) { |
| 4993 | case TemplateArgument::Null: |
| 4994 | case TemplateArgument::Integral: |
Douglas Gregor | 31f55dc | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4995 | case TemplateArgument::Declaration: |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4996 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4997 | |
Eli Friedman | b826a00 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4998 | case TemplateArgument::NullPtr: |
| 4999 | MarkUsedTemplateParameters(Ctx, TemplateArg.getNullPtrType(), OnlyDeduced, |
| 5000 | Depth, Used); |
| 5001 | break; |
| 5002 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5003 | case TemplateArgument::Type: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5004 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsType(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5005 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5006 | break; |
| 5007 | |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5008 | case TemplateArgument::Template: |
Douglas Gregor | e4ff4b5 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 5009 | case TemplateArgument::TemplateExpansion: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5010 | MarkUsedTemplateParameters(Ctx, |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5011 | TemplateArg.getAsTemplateOrTemplatePattern(), |
Douglas Gregor | 9167f8b | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 5012 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5013 | break; |
| 5014 | |
| 5015 | case TemplateArgument::Expression: |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5016 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsExpr(), OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5017 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5018 | break; |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5019 | |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5020 | case TemplateArgument::Pack: |
Aaron Ballman | 2a89e85 | 2014-07-15 21:32:31 +0000 | [diff] [blame] | 5021 | for (const auto &P : TemplateArg.pack_elements()) |
| 5022 | MarkUsedTemplateParameters(Ctx, P, OnlyDeduced, Depth, Used); |
Anders Carlsson | bc34391 | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 5023 | break; |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5024 | } |
| 5025 | } |
| 5026 | |
James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 5027 | /// \brief Mark which template parameters can be deduced from a given |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5028 | /// template argument list. |
| 5029 | /// |
| 5030 | /// \param TemplateArgs the template argument list from which template |
| 5031 | /// parameters will be deduced. |
| 5032 | /// |
James Dennett | 4172512 | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 5033 | /// \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] | 5034 | /// to indicate when the corresponding template parameter will be |
| 5035 | /// deduced. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5036 | void |
Douglas Gregor | e1d2ef3 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 5037 | Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5038 | bool OnlyDeduced, unsigned Depth, |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5039 | llvm::SmallBitVector &Used) { |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5040 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5041 | // If the template argument list of P contains a pack expansion that is not |
| 5042 | // the last template argument, the entire template argument list is a |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5043 | // non-deduced context. |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5044 | if (OnlyDeduced && |
Douglas Gregor | d0ad294 | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 5045 | hasPackExpansionBeforeEnd(TemplateArgs.data(), TemplateArgs.size())) |
| 5046 | return; |
| 5047 | |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5048 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5049 | ::MarkUsedTemplateParameters(Context, TemplateArgs[I], OnlyDeduced, |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5050 | Depth, Used); |
Douglas Gregor | 91772d1 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 5051 | } |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5052 | |
| 5053 | /// \brief Marks all of the template parameters that will be deduced by a |
| 5054 | /// call to the given function template. |
Nico Weber | c153d24 | 2014-07-28 00:02:09 +0000 | [diff] [blame] | 5055 | void Sema::MarkDeducedTemplateParameters( |
| 5056 | ASTContext &Ctx, const FunctionTemplateDecl *FunctionTemplate, |
| 5057 | llvm::SmallBitVector &Deduced) { |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5058 | TemplateParameterList *TemplateParams |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5059 | = FunctionTemplate->getTemplateParameters(); |
| 5060 | Deduced.clear(); |
| 5061 | Deduced.resize(TemplateParams->size()); |
NAKAMURA Takumi | f9cbcc4 | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5062 | |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5063 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 5064 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) |
Argyrios Kyrtzidis | f34950d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 5065 | ::MarkUsedTemplateParameters(Ctx, Function->getParamDecl(I)->getType(), |
Douglas Gregor | 2161038 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 5066 | true, TemplateParams->getDepth(), Deduced); |
Douglas Gregor | ce23bae | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 5067 | } |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5068 | |
| 5069 | bool hasDeducibleTemplateParameters(Sema &S, |
| 5070 | FunctionTemplateDecl *FunctionTemplate, |
| 5071 | QualType T) { |
| 5072 | if (!T->isDependentType()) |
| 5073 | return false; |
| 5074 | |
| 5075 | TemplateParameterList *TemplateParams |
| 5076 | = FunctionTemplate->getTemplateParameters(); |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5077 | llvm::SmallBitVector Deduced(TemplateParams->size()); |
Simon Pilgrim | 728134c | 2016-08-12 11:43:57 +0000 | [diff] [blame] | 5078 | ::MarkUsedTemplateParameters(S.Context, T, true, TemplateParams->getDepth(), |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5079 | Deduced); |
| 5080 | |
Benjamin Kramer | e0513cb | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 5081 | return Deduced.any(); |
Douglas Gregor | e65aacb | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 5082 | } |