Douglas Gregor | 0b9247f | 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 | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 13 | #include "clang/Sema/TemplateDeduction.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "TreeTransform.h" |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTLambda.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 19 | #include "clang/AST/Expr.h" |
| 20 | #include "clang/AST/ExprCXX.h" |
Chandler Carruth | 55fc873 | 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 | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallBitVector.h" |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 27 | |
| 28 | namespace clang { |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 29 | using namespace sema; |
Douglas Gregor | 508f1c8 | 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 | 4112877 | 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 | 1282029 | 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 | 73b3cf6 | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 54 | /// parameters and arguments in a top-level template argument |
Douglas Gregor | 092140a | 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 | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 60 | }; |
| 61 | } |
| 62 | |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 63 | using namespace clang; |
| 64 | |
Douglas Gregor | 9d0e441 | 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 | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 69 | X = X.extend(Y.getBitWidth()); |
Douglas Gregor | 9d0e441 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 70 | else if (Y.getBitWidth() < X.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 71 | Y = Y.extend(X.getBitWidth()); |
Douglas Gregor | 9d0e441 | 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 | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 86 | static Sema::TemplateDeductionResult |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 87 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 88 | TemplateParameterList *TemplateParams, |
| 89 | const TemplateArgument &Param, |
Douglas Gregor | 77d6bb9 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 90 | TemplateArgument Arg, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 91 | TemplateDeductionInfo &Info, |
Craig Topper | 1310aac | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 92 | SmallVectorImpl<DeducedTemplateArgument> &Deduced); |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 93 | |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 94 | static Sema::TemplateDeductionResult |
Sebastian Redl | bb95e51 | 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, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 103 | bool PartialOrdering = false); |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 104 | |
| 105 | static Sema::TemplateDeductionResult |
| 106 | DeduceTemplateArguments(Sema &S, |
| 107 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 108 | const TemplateArgument *Params, unsigned NumParams, |
| 109 | const TemplateArgument *Args, unsigned NumArgs, |
| 110 | TemplateDeductionInfo &Info, |
Richard Smith | 030a664 | 2012-12-06 06:44:44 +0000 | [diff] [blame] | 111 | SmallVectorImpl<DeducedTemplateArgument> &Deduced); |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 112 | |
Douglas Gregor | 199d991 | 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 | 5a343d7 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | |
Douglas Gregor | 199d991 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 131 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 132 | return nullptr; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Douglas Gregor | 0d80abc | 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 | 0d80abc | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 142 | |
Douglas Gregor | 0d80abc | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 150 | static DeducedTemplateArgument |
Douglas Gregor | 0d80abc | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 158 | return X; |
Douglas Gregor | 0d80abc | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 169 | |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 170 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 171 | |
Douglas Gregor | 0d80abc | 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 | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 179 | hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 180 | return DeducedTemplateArgument(X, |
Douglas Gregor | 0d80abc | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 186 | |
Douglas Gregor | 0d80abc | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 191 | |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 192 | // All other combinations are incompatible. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 193 | return DeducedTemplateArgument(); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 194 | |
| 195 | case TemplateArgument::TemplateExpansion: |
| 196 | if (Y.getKind() == TemplateArgument::TemplateExpansion && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 197 | Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(), |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 198 | Y.getAsTemplateOrTemplatePattern())) |
| 199 | return X; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 200 | |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 201 | // All other combinations are incompatible. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 202 | return DeducedTemplateArgument(); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 203 | |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 204 | case TemplateArgument::Expression: |
NAKAMURA Takumi | dfbb02a | 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 | 0d80abc | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 212 | |
Douglas Gregor | 0d80abc | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 221 | |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 222 | // All other combinations are incompatible. |
| 223 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 224 | |
Douglas Gregor | 0d80abc | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 230 | |
Douglas Gregor | 0d80abc | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 235 | |
Douglas Gregor | 0d80abc | 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 && |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 239 | isSameDeclaration(X.getAsDecl(), Y.getAsDecl())) |
Eli Friedman | d7a6b16 | 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 | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 259 | return X; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 260 | |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 261 | // All other combinations are incompatible. |
| 262 | return DeducedTemplateArgument(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 263 | |
Douglas Gregor | 0d80abc | 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 | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 268 | |
| 269 | for (TemplateArgument::pack_iterator XA = X.pack_begin(), |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 270 | XAEnd = X.pack_end(), |
| 271 | YA = Y.pack_begin(); |
| 272 | XA != XAEnd; ++XA, ++YA) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 273 | // FIXME: Do we need to merge the results together here? |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 274 | if (checkDeducedTemplateArguments(Context, |
| 275 | DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()), |
Douglas Gregor | 135ffa7 | 2011-01-05 21:00:53 +0000 | [diff] [blame] | 276 | DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound())) |
| 277 | .isNull()) |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 278 | return DeducedTemplateArgument(); |
| 279 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 280 | |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 281 | return X; |
| 282 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 283 | |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 284 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | /// \brief Deduce the value of the given non-type template parameter |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 288 | /// from the given constant. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 289 | static Sema::TemplateDeductionResult |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 290 | DeduceNonTypeTemplateArgument(Sema &S, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | NonTypeTemplateParmDecl *NTTP, |
Douglas Gregor | 9d0e441 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 292 | llvm::APSInt Value, QualType ValueType, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 293 | bool DeducedFromArrayBound, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 294 | TemplateDeductionInfo &Info, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 295 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 297 | "Cannot deduce non-type template argument with depth > 0"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 298 | |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 299 | DeducedTemplateArgument NewDeduced(S.Context, Value, ValueType, |
| 300 | DeducedFromArrayBound); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 301 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 302 | Deduced[NTTP->getIndex()], |
| 303 | NewDeduced); |
| 304 | if (Result.isNull()) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 305 | Info.Param = NTTP; |
| 306 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 307 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 308 | return Sema::TDK_Inconsistent; |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 309 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 310 | |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 311 | Deduced[NTTP->getIndex()] = Result; |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 312 | return Sema::TDK_Success; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | /// \brief Deduce the value of the given non-type template parameter |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 316 | /// from the given type- or value-dependent expression. |
| 317 | /// |
| 318 | /// \returns true if deduction succeeded, false otherwise. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 319 | static Sema::TemplateDeductionResult |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 320 | DeduceNonTypeTemplateArgument(Sema &S, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 321 | NonTypeTemplateParmDecl *NTTP, |
| 322 | Expr *Value, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 323 | TemplateDeductionInfo &Info, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 324 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 325 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 326 | "Cannot deduce non-type template argument with depth > 0"); |
| 327 | assert((Value->isTypeDependent() || Value->isValueDependent()) && |
| 328 | "Expression template argument must be type- or value-dependent."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 329 | |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 330 | DeducedTemplateArgument NewDeduced(Value); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 331 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
| 332 | Deduced[NTTP->getIndex()], |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 333 | NewDeduced); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 334 | |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 335 | if (Result.isNull()) { |
| 336 | Info.Param = NTTP; |
| 337 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
| 338 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 339 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 340 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 341 | |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 342 | Deduced[NTTP->getIndex()] = Result; |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 343 | return Sema::TDK_Success; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Douglas Gregor | 15755cb | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 346 | /// \brief Deduce the value of the given non-type template parameter |
| 347 | /// from the given declaration. |
| 348 | /// |
| 349 | /// \returns true if deduction succeeded, false otherwise. |
| 350 | static Sema::TemplateDeductionResult |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 351 | DeduceNonTypeTemplateArgument(Sema &S, |
Craig Topper | d82c091 | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 352 | NonTypeTemplateParmDecl *NTTP, |
| 353 | ValueDecl *D, |
| 354 | TemplateDeductionInfo &Info, |
| 355 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 15755cb | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 356 | assert(NTTP->getDepth() == 0 && |
| 357 | "Cannot deduce non-type template argument with depth > 0"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 358 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 359 | D = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 360 | TemplateArgument New(D, NTTP->getType()); |
Eli Friedman | d7a6b16 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 361 | DeducedTemplateArgument NewDeduced(New); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 362 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 363 | Deduced[NTTP->getIndex()], |
| 364 | NewDeduced); |
| 365 | if (Result.isNull()) { |
| 366 | Info.Param = NTTP; |
| 367 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
| 368 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 369 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 15755cb | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 370 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 371 | |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 372 | Deduced[NTTP->getIndex()] = Result; |
Douglas Gregor | 15755cb | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 373 | return Sema::TDK_Success; |
| 374 | } |
| 375 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 376 | static Sema::TemplateDeductionResult |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 377 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 378 | TemplateParameterList *TemplateParams, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 379 | TemplateName Param, |
| 380 | TemplateName Arg, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 381 | TemplateDeductionInfo &Info, |
Craig Topper | d82c091 | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 382 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 383 | TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 384 | if (!ParamDecl) { |
| 385 | // The parameter type is dependent and is not a template template parameter, |
| 386 | // so there is nothing that we can deduce. |
| 387 | return Sema::TDK_Success; |
| 388 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 389 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 390 | if (TemplateTemplateParmDecl *TempParam |
| 391 | = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) { |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 392 | DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 393 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 394 | Deduced[TempParam->getIndex()], |
| 395 | NewDeduced); |
| 396 | if (Result.isNull()) { |
| 397 | Info.Param = TempParam; |
| 398 | Info.FirstArg = Deduced[TempParam->getIndex()]; |
| 399 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 400 | return Sema::TDK_Inconsistent; |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 401 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 402 | |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 403 | Deduced[TempParam->getIndex()] = Result; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 404 | return Sema::TDK_Success; |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 405 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 406 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 407 | // Verify that the two template names are equivalent. |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 408 | if (S.Context.hasSameTemplateName(Param, Arg)) |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 409 | return Sema::TDK_Success; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 410 | |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 411 | // Mismatch of non-dependent template parameter to argument. |
| 412 | Info.FirstArg = TemplateArgument(Param); |
| 413 | Info.SecondArg = TemplateArgument(Arg); |
| 414 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | /// \brief Deduce the template arguments by comparing the template parameter |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 418 | /// type (which is a template-id) with the template argument type. |
| 419 | /// |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 420 | /// \param S the Sema |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 421 | /// |
| 422 | /// \param TemplateParams the template parameters that we are deducing |
| 423 | /// |
| 424 | /// \param Param the parameter type |
| 425 | /// |
| 426 | /// \param Arg the argument type |
| 427 | /// |
| 428 | /// \param Info information about the template argument deduction itself |
| 429 | /// |
| 430 | /// \param Deduced the deduced template arguments |
| 431 | /// |
| 432 | /// \returns the result of template argument deduction so far. Note that a |
| 433 | /// "success" result means that template argument deduction has not yet failed, |
| 434 | /// but it may still fail, later, for other reasons. |
| 435 | static Sema::TemplateDeductionResult |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 436 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 437 | TemplateParameterList *TemplateParams, |
| 438 | const TemplateSpecializationType *Param, |
| 439 | QualType Arg, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 440 | TemplateDeductionInfo &Info, |
Craig Topper | 1310aac | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 441 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 442 | assert(Arg.isCanonical() && "Argument type must be canonical"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 443 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 444 | // Check whether the template argument is a dependent template-id. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | if (const TemplateSpecializationType *SpecArg |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 446 | = dyn_cast<TemplateSpecializationType>(Arg)) { |
| 447 | // Perform template argument deduction for the template name. |
| 448 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 449 | = DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 450 | Param->getTemplateName(), |
| 451 | SpecArg->getTemplateName(), |
| 452 | Info, Deduced)) |
| 453 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 454 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 455 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 456 | // Perform template argument deduction on each template |
Douglas Gregor | 0972c86 | 2010-12-22 18:55:49 +0000 | [diff] [blame] | 457 | // argument. Ignore any missing/extra arguments, since they could be |
| 458 | // filled in by default arguments. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 459 | return DeduceTemplateArguments(S, TemplateParams, |
| 460 | Param->getArgs(), Param->getNumArgs(), |
Douglas Gregor | 0972c86 | 2010-12-22 18:55:49 +0000 | [diff] [blame] | 461 | SpecArg->getArgs(), SpecArg->getNumArgs(), |
Richard Smith | 030a664 | 2012-12-06 06:44:44 +0000 | [diff] [blame] | 462 | Info, Deduced); |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 463 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 464 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 465 | // If the argument type is a class template specialization, we |
| 466 | // perform template argument deduction using its template |
| 467 | // arguments. |
| 468 | const RecordType *RecordArg = dyn_cast<RecordType>(Arg); |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 469 | if (!RecordArg) { |
| 470 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 471 | Info.SecondArg = TemplateArgument(Arg); |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 472 | return Sema::TDK_NonDeducedMismatch; |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 473 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | |
| 475 | ClassTemplateSpecializationDecl *SpecArg |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 476 | = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 477 | if (!SpecArg) { |
| 478 | Info.FirstArg = TemplateArgument(QualType(Param, 0)); |
| 479 | Info.SecondArg = TemplateArgument(Arg); |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 480 | return Sema::TDK_NonDeducedMismatch; |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 481 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 483 | // Perform template argument deduction for the template name. |
| 484 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 485 | = DeduceTemplateArguments(S, |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 486 | TemplateParams, |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 487 | Param->getTemplateName(), |
| 488 | TemplateName(SpecArg->getSpecializedTemplate()), |
| 489 | Info, Deduced)) |
| 490 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 492 | // Perform template argument deduction for the template arguments. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 493 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 494 | Param->getArgs(), Param->getNumArgs(), |
| 495 | SpecArg->getTemplateArgs().data(), |
| 496 | SpecArg->getTemplateArgs().size(), |
| 497 | Info, Deduced); |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 498 | } |
| 499 | |
John McCall | cd05e81 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 500 | /// \brief Determines whether the given type is an opaque type that |
| 501 | /// might be more qualified when instantiated. |
| 502 | static bool IsPossiblyOpaquelyQualifiedType(QualType T) { |
| 503 | switch (T->getTypeClass()) { |
| 504 | case Type::TypeOfExpr: |
| 505 | case Type::TypeOf: |
| 506 | case Type::DependentName: |
| 507 | case Type::Decltype: |
| 508 | case Type::UnresolvedUsing: |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 509 | case Type::TemplateTypeParm: |
John McCall | cd05e81 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 510 | return true; |
| 511 | |
| 512 | case Type::ConstantArray: |
| 513 | case Type::IncompleteArray: |
| 514 | case Type::VariableArray: |
| 515 | case Type::DependentSizedArray: |
| 516 | return IsPossiblyOpaquelyQualifiedType( |
| 517 | cast<ArrayType>(T)->getElementType()); |
| 518 | |
| 519 | default: |
| 520 | return false; |
| 521 | } |
| 522 | } |
| 523 | |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 524 | /// \brief Retrieve the depth and index of a template parameter. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 525 | static std::pair<unsigned, unsigned> |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 526 | getDepthAndIndex(NamedDecl *ND) { |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 527 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND)) |
| 528 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 529 | |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 530 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND)) |
| 531 | return std::make_pair(NTTP->getDepth(), NTTP->getIndex()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 532 | |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 533 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND); |
| 534 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| 535 | } |
| 536 | |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 537 | /// \brief Retrieve the depth and index of an unexpanded parameter pack. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 538 | static std::pair<unsigned, unsigned> |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 539 | getDepthAndIndex(UnexpandedParameterPack UPP) { |
| 540 | if (const TemplateTypeParmType *TTP |
| 541 | = UPP.first.dyn_cast<const TemplateTypeParmType *>()) |
| 542 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 543 | |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 544 | return getDepthAndIndex(UPP.first.get<NamedDecl *>()); |
| 545 | } |
| 546 | |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 547 | /// \brief Helper function to build a TemplateParameter when we don't |
| 548 | /// know its type statically. |
| 549 | static TemplateParameter makeTemplateParameter(Decl *D) { |
| 550 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) |
| 551 | return TemplateParameter(TTP); |
Craig Topper | cb9186e | 2013-07-08 04:24:47 +0000 | [diff] [blame] | 552 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 553 | return TemplateParameter(NTTP); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 554 | |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 555 | return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); |
| 556 | } |
| 557 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 558 | /// A pack that we're currently deducing. |
| 559 | struct clang::DeducedPack { |
| 560 | DeducedPack(unsigned Index) : Index(Index), Outer(nullptr) {} |
Craig Topper | 70d214f | 2013-07-08 04:44:01 +0000 | [diff] [blame] | 561 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 562 | // The index of the pack. |
| 563 | unsigned Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 564 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 565 | // The old value of the pack before we started deducing it. |
| 566 | DeducedTemplateArgument Saved; |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 567 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 568 | // A deferred value of this pack from an inner deduction, that couldn't be |
| 569 | // deduced because this deduction hadn't happened yet. |
| 570 | DeducedTemplateArgument DeferredDeduction; |
| 571 | |
| 572 | // The new value of the pack. |
| 573 | SmallVector<DeducedTemplateArgument, 4> New; |
| 574 | |
| 575 | // The outer deduction for this pack, if any. |
| 576 | DeducedPack *Outer; |
| 577 | }; |
| 578 | |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 579 | namespace { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 580 | /// A scope in which we're performing pack deduction. |
| 581 | class PackDeductionScope { |
| 582 | public: |
| 583 | PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, |
| 584 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 585 | TemplateDeductionInfo &Info, TemplateArgument Pattern) |
| 586 | : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { |
| 587 | // Compute the set of template parameter indices that correspond to |
| 588 | // parameter packs expanded by the pack expansion. |
| 589 | { |
| 590 | llvm::SmallBitVector SawIndices(TemplateParams->size()); |
| 591 | SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 592 | S.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
| 593 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 594 | unsigned Depth, Index; |
| 595 | std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); |
| 596 | if (Depth == 0 && !SawIndices[Index]) { |
| 597 | SawIndices[Index] = true; |
| 598 | |
| 599 | // Save the deduced template argument for the parameter pack expanded |
| 600 | // by this pack expansion, then clear out the deduction. |
| 601 | DeducedPack Pack(Index); |
| 602 | Pack.Saved = Deduced[Index]; |
| 603 | Deduced[Index] = TemplateArgument(); |
| 604 | |
| 605 | Packs.push_back(Pack); |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | assert(!Packs.empty() && "Pack expansion without unexpanded packs?"); |
| 610 | |
| 611 | for (auto &Pack : Packs) { |
| 612 | if (Info.PendingDeducedPacks.size() > Pack.Index) |
| 613 | Pack.Outer = Info.PendingDeducedPacks[Pack.Index]; |
| 614 | else |
| 615 | Info.PendingDeducedPacks.resize(Pack.Index + 1); |
| 616 | Info.PendingDeducedPacks[Pack.Index] = &Pack; |
| 617 | |
| 618 | if (S.CurrentInstantiationScope) { |
| 619 | // If the template argument pack was explicitly specified, add that to |
| 620 | // the set of deduced arguments. |
| 621 | const TemplateArgument *ExplicitArgs; |
| 622 | unsigned NumExplicitArgs; |
| 623 | NamedDecl *PartiallySubstitutedPack = |
| 624 | S.CurrentInstantiationScope->getPartiallySubstitutedPack( |
| 625 | &ExplicitArgs, &NumExplicitArgs); |
| 626 | if (PartiallySubstitutedPack && |
| 627 | getDepthAndIndex(PartiallySubstitutedPack).second == Pack.Index) |
| 628 | Pack.New.append(ExplicitArgs, ExplicitArgs + NumExplicitArgs); |
| 629 | } |
Douglas Gregor | 5429385 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 630 | } |
| 631 | } |
Douglas Gregor | 5429385 | 2011-01-10 17:35:05 +0000 | [diff] [blame] | 632 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 633 | ~PackDeductionScope() { |
| 634 | for (auto &Pack : Packs) |
| 635 | Info.PendingDeducedPacks[Pack.Index] = Pack.Outer; |
Douglas Gregor | 0216f81 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 636 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 637 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 638 | /// Move to deducing the next element in each pack that is being deduced. |
| 639 | void nextPackElement() { |
| 640 | // Capture the deduced template arguments for each parameter pack expanded |
| 641 | // by this pack expansion, add them to the list of arguments we've deduced |
| 642 | // for that pack, then clear out the deduced argument. |
| 643 | for (auto &Pack : Packs) { |
| 644 | DeducedTemplateArgument &DeducedArg = Deduced[Pack.Index]; |
| 645 | if (!DeducedArg.isNull()) { |
| 646 | Pack.New.push_back(DeducedArg); |
| 647 | DeducedArg = DeducedTemplateArgument(); |
| 648 | } |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | /// \brief Finish template argument deduction for a set of argument packs, |
| 653 | /// producing the argument packs and checking for consistency with prior |
| 654 | /// deductions. |
| 655 | Sema::TemplateDeductionResult finish(bool HasAnyArguments) { |
| 656 | // Build argument packs for each of the parameter packs expanded by this |
| 657 | // pack expansion. |
| 658 | for (auto &Pack : Packs) { |
| 659 | // Put back the old value for this pack. |
| 660 | Deduced[Pack.Index] = Pack.Saved; |
| 661 | |
| 662 | // Build or find a new value for this pack. |
| 663 | DeducedTemplateArgument NewPack; |
| 664 | if (HasAnyArguments && Pack.New.empty()) { |
| 665 | if (Pack.DeferredDeduction.isNull()) { |
| 666 | // We were not able to deduce anything for this parameter pack |
| 667 | // (because it only appeared in non-deduced contexts), so just |
| 668 | // restore the saved argument pack. |
| 669 | continue; |
| 670 | } |
| 671 | |
| 672 | NewPack = Pack.DeferredDeduction; |
| 673 | Pack.DeferredDeduction = TemplateArgument(); |
| 674 | } else if (Pack.New.empty()) { |
| 675 | // If we deduced an empty argument pack, create it now. |
| 676 | NewPack = DeducedTemplateArgument(TemplateArgument::getEmptyPack()); |
| 677 | } else { |
| 678 | TemplateArgument *ArgumentPack = |
| 679 | new (S.Context) TemplateArgument[Pack.New.size()]; |
| 680 | std::copy(Pack.New.begin(), Pack.New.end(), ArgumentPack); |
| 681 | NewPack = DeducedTemplateArgument( |
| 682 | TemplateArgument(ArgumentPack, Pack.New.size()), |
| 683 | Pack.New[0].wasDeducedFromArrayBound()); |
| 684 | } |
| 685 | |
| 686 | // Pick where we're going to put the merged pack. |
| 687 | DeducedTemplateArgument *Loc; |
| 688 | if (Pack.Outer) { |
| 689 | if (Pack.Outer->DeferredDeduction.isNull()) { |
| 690 | // Defer checking this pack until we have a complete pack to compare |
| 691 | // it against. |
| 692 | Pack.Outer->DeferredDeduction = NewPack; |
| 693 | continue; |
| 694 | } |
| 695 | Loc = &Pack.Outer->DeferredDeduction; |
| 696 | } else { |
| 697 | Loc = &Deduced[Pack.Index]; |
| 698 | } |
| 699 | |
| 700 | // Check the new pack matches any previous value. |
| 701 | DeducedTemplateArgument OldPack = *Loc; |
| 702 | DeducedTemplateArgument Result = |
| 703 | checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 704 | |
| 705 | // If we deferred a deduction of this pack, check that one now too. |
| 706 | if (!Result.isNull() && !Pack.DeferredDeduction.isNull()) { |
| 707 | OldPack = Result; |
| 708 | NewPack = Pack.DeferredDeduction; |
| 709 | Result = checkDeducedTemplateArguments(S.Context, OldPack, NewPack); |
| 710 | } |
| 711 | |
| 712 | if (Result.isNull()) { |
| 713 | Info.Param = |
| 714 | makeTemplateParameter(TemplateParams->getParam(Pack.Index)); |
| 715 | Info.FirstArg = OldPack; |
| 716 | Info.SecondArg = NewPack; |
| 717 | return Sema::TDK_Inconsistent; |
| 718 | } |
| 719 | |
| 720 | *Loc = Result; |
| 721 | } |
| 722 | |
| 723 | return Sema::TDK_Success; |
| 724 | } |
| 725 | |
| 726 | private: |
| 727 | Sema &S; |
| 728 | TemplateParameterList *TemplateParams; |
| 729 | SmallVectorImpl<DeducedTemplateArgument> &Deduced; |
| 730 | TemplateDeductionInfo &Info; |
| 731 | |
| 732 | SmallVector<DeducedPack, 2> Packs; |
| 733 | }; |
Pirama Arumuga Nainar | 3ea9e33 | 2015-04-08 08:57:32 -0700 | [diff] [blame] | 734 | } // namespace |
Douglas Gregor | 0216f81 | 2011-01-10 17:53:52 +0000 | [diff] [blame] | 735 | |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 736 | /// \brief Deduce the template arguments by comparing the list of parameter |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 737 | /// types to the list of argument types, as in the parameter-type-lists of |
| 738 | /// function types (C++ [temp.deduct.type]p10). |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 739 | /// |
| 740 | /// \param S The semantic analysis object within which we are deducing |
| 741 | /// |
| 742 | /// \param TemplateParams The template parameters that we are deducing |
| 743 | /// |
| 744 | /// \param Params The list of parameter types |
| 745 | /// |
| 746 | /// \param NumParams The number of types in \c Params |
| 747 | /// |
| 748 | /// \param Args The list of argument types |
| 749 | /// |
| 750 | /// \param NumArgs The number of types in \c Args |
| 751 | /// |
| 752 | /// \param Info information about the template argument deduction itself |
| 753 | /// |
| 754 | /// \param Deduced the deduced template arguments |
| 755 | /// |
| 756 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
| 757 | /// how template argument deduction is performed. |
| 758 | /// |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 759 | /// \param PartialOrdering If true, we are performing template argument |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 760 | /// deduction for during partial ordering for a call |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 761 | /// (C++0x [temp.deduct.partial]). |
| 762 | /// |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 763 | /// \returns the result of template argument deduction so far. Note that a |
| 764 | /// "success" result means that template argument deduction has not yet failed, |
| 765 | /// but it may still fail, later, for other reasons. |
| 766 | static Sema::TemplateDeductionResult |
| 767 | DeduceTemplateArguments(Sema &S, |
| 768 | TemplateParameterList *TemplateParams, |
| 769 | const QualType *Params, unsigned NumParams, |
| 770 | const QualType *Args, unsigned NumArgs, |
| 771 | TemplateDeductionInfo &Info, |
Craig Topper | d82c091 | 2013-07-08 04:16:49 +0000 | [diff] [blame] | 772 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 773 | unsigned TDF, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 774 | bool PartialOrdering = false) { |
Douglas Gregor | 0bbacf8 | 2011-01-05 23:23:17 +0000 | [diff] [blame] | 775 | // Fast-path check to see if we have too many/too few arguments. |
| 776 | if (NumParams != NumArgs && |
| 777 | !(NumParams && isa<PackExpansionType>(Params[NumParams - 1])) && |
| 778 | !(NumArgs && isa<PackExpansionType>(Args[NumArgs - 1]))) |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 779 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 780 | |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 781 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 782 | // Similarly, if P has a form that contains (T), then each parameter type |
| 783 | // Pi of the respective parameter-type- list of P is compared with the |
| 784 | // corresponding parameter type Ai of the corresponding parameter-type-list |
| 785 | // of A. [...] |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 786 | unsigned ArgIdx = 0, ParamIdx = 0; |
| 787 | for (; ParamIdx != NumParams; ++ParamIdx) { |
| 788 | // Check argument types. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 789 | const PackExpansionType *Expansion |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 790 | = dyn_cast<PackExpansionType>(Params[ParamIdx]); |
| 791 | if (!Expansion) { |
| 792 | // Simple case: compare the parameter and argument types at this point. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 793 | |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 794 | // Make sure we have an argument. |
| 795 | if (ArgIdx >= NumArgs) |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 796 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 797 | |
Douglas Gregor | 77d6bb9 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 798 | if (isa<PackExpansionType>(Args[ArgIdx])) { |
| 799 | // C++0x [temp.deduct.type]p22: |
| 800 | // If the original function parameter associated with A is a function |
| 801 | // parameter pack and the function parameter associated with P is not |
| 802 | // a function parameter pack, then template argument deduction fails. |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 803 | return Sema::TDK_MiscellaneousDeductionFailure; |
Douglas Gregor | 77d6bb9 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 804 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 805 | |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 806 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 807 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 808 | Params[ParamIdx], Args[ArgIdx], |
| 809 | Info, Deduced, TDF, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 810 | PartialOrdering)) |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 811 | return Result; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 812 | |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 813 | ++ArgIdx; |
| 814 | continue; |
| 815 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 816 | |
Douglas Gregor | 7d5c0c1 | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 817 | // C++0x [temp.deduct.type]p5: |
| 818 | // The non-deduced contexts are: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 819 | // - A function parameter pack that does not occur at the end of the |
Douglas Gregor | 7d5c0c1 | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 820 | // parameter-declaration-clause. |
| 821 | if (ParamIdx + 1 < NumParams) |
| 822 | return Sema::TDK_Success; |
| 823 | |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 824 | // C++0x [temp.deduct.type]p10: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 825 | // If the parameter-declaration corresponding to Pi is a function |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 826 | // parameter pack, then the type of its declarator- id is compared with |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 827 | // each remaining parameter type in the parameter-type-list of A. Each |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 828 | // comparison deduces template arguments for subsequent positions in the |
| 829 | // template parameter packs expanded by the function parameter pack. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 830 | |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 831 | QualType Pattern = Expansion->getPattern(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 832 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 833 | |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 834 | bool HasAnyArguments = false; |
| 835 | for (; ArgIdx < NumArgs; ++ArgIdx) { |
| 836 | HasAnyArguments = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 837 | |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 838 | // Deduce template arguments from the pattern. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 839 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 840 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, Pattern, |
| 841 | Args[ArgIdx], Info, Deduced, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 842 | TDF, PartialOrdering)) |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 843 | return Result; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 844 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 845 | PackScope.nextPackElement(); |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 846 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 847 | |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 848 | // Build argument packs for each of the parameter packs expanded by this |
| 849 | // pack expansion. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 850 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 851 | return Result; |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 852 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 853 | |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 854 | // Make sure we don't have any extra arguments. |
| 855 | if (ArgIdx < NumArgs) |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 856 | return Sema::TDK_MiscellaneousDeductionFailure; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 857 | |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 858 | return Sema::TDK_Success; |
| 859 | } |
| 860 | |
Douglas Gregor | 61d0b6b | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 861 | /// \brief Determine whether the parameter has qualifiers that are either |
| 862 | /// inconsistent with or a superset of the argument's qualifiers. |
| 863 | static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType, |
| 864 | QualType ArgType) { |
| 865 | Qualifiers ParamQs = ParamType.getQualifiers(); |
| 866 | Qualifiers ArgQs = ArgType.getQualifiers(); |
| 867 | |
| 868 | if (ParamQs == ArgQs) |
| 869 | return false; |
| 870 | |
| 871 | // Mismatched (but not missing) Objective-C GC attributes. |
| 872 | if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() && |
| 873 | ParamQs.hasObjCGCAttr()) |
| 874 | return true; |
| 875 | |
| 876 | // Mismatched (but not missing) address spaces. |
| 877 | if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() && |
| 878 | ParamQs.hasAddressSpace()) |
| 879 | return true; |
| 880 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 881 | // Mismatched (but not missing) Objective-C lifetime qualifiers. |
| 882 | if (ParamQs.getObjCLifetime() != ArgQs.getObjCLifetime() && |
| 883 | ParamQs.hasObjCLifetime()) |
| 884 | return true; |
| 885 | |
Douglas Gregor | 61d0b6b | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 886 | // CVR qualifier superset. |
| 887 | return (ParamQs.getCVRQualifiers() != ArgQs.getCVRQualifiers()) && |
| 888 | ((ParamQs.getCVRQualifiers() | ArgQs.getCVRQualifiers()) |
| 889 | == ParamQs.getCVRQualifiers()); |
| 890 | } |
| 891 | |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 892 | /// \brief Compare types for equality with respect to possibly compatible |
| 893 | /// function types (noreturn adjustment, implicit calling conventions). If any |
| 894 | /// of parameter and argument is not a function, just perform type comparison. |
| 895 | /// |
| 896 | /// \param Param the template parameter type. |
| 897 | /// |
| 898 | /// \param Arg the argument type. |
| 899 | bool Sema::isSameOrCompatibleFunctionType(CanQualType Param, |
| 900 | CanQualType Arg) { |
| 901 | const FunctionType *ParamFunction = Param->getAs<FunctionType>(), |
| 902 | *ArgFunction = Arg->getAs<FunctionType>(); |
| 903 | |
| 904 | // Just compare if not functions. |
| 905 | if (!ParamFunction || !ArgFunction) |
| 906 | return Param == Arg; |
| 907 | |
| 908 | // Noreturn adjustment. |
| 909 | QualType AdjustedParam; |
| 910 | if (IsNoReturnConversion(Param, Arg, AdjustedParam)) |
| 911 | return Arg == Context.getCanonicalType(AdjustedParam); |
| 912 | |
| 913 | // FIXME: Compatible calling conventions. |
| 914 | |
| 915 | return Param == Arg; |
| 916 | } |
| 917 | |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 918 | /// \brief Deduce the template arguments by comparing the parameter type and |
| 919 | /// the argument type (C++ [temp.deduct.type]). |
| 920 | /// |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 921 | /// \param S the semantic analysis object within which we are deducing |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 922 | /// |
| 923 | /// \param TemplateParams the template parameters that we are deducing |
| 924 | /// |
| 925 | /// \param ParamIn the parameter type |
| 926 | /// |
| 927 | /// \param ArgIn the argument type |
| 928 | /// |
| 929 | /// \param Info information about the template argument deduction itself |
| 930 | /// |
| 931 | /// \param Deduced the deduced template arguments |
| 932 | /// |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 933 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 934 | /// how template argument deduction is performed. |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 935 | /// |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 936 | /// \param PartialOrdering Whether we're performing template argument deduction |
| 937 | /// in the context of partial ordering (C++0x [temp.deduct.partial]). |
| 938 | /// |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 939 | /// \returns the result of template argument deduction so far. Note that a |
| 940 | /// "success" result means that template argument deduction has not yet failed, |
| 941 | /// but it may still fail, later, for other reasons. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 942 | static Sema::TemplateDeductionResult |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 943 | DeduceTemplateArgumentsByTypeMatch(Sema &S, |
| 944 | TemplateParameterList *TemplateParams, |
| 945 | QualType ParamIn, QualType ArgIn, |
| 946 | TemplateDeductionInfo &Info, |
| 947 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 948 | unsigned TDF, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 949 | bool PartialOrdering) { |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 950 | // We only want to look at the canonical types, since typedefs and |
| 951 | // sugar are not part of template argument deduction. |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 952 | QualType Param = S.Context.getCanonicalType(ParamIn); |
| 953 | QualType Arg = S.Context.getCanonicalType(ArgIn); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 954 | |
Douglas Gregor | 77d6bb9 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 955 | // If the argument type is a pack expansion, look at its pattern. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 956 | // This isn't explicitly called out |
Douglas Gregor | 77d6bb9 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 957 | if (const PackExpansionType *ArgExpansion |
| 958 | = dyn_cast<PackExpansionType>(Arg)) |
| 959 | Arg = ArgExpansion->getPattern(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 960 | |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 961 | if (PartialOrdering) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 962 | // C++11 [temp.deduct.partial]p5: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 963 | // Before the partial ordering is done, certain transformations are |
| 964 | // performed on the types used for partial ordering: |
| 965 | // - If P is a reference type, P is replaced by the type referred to. |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 966 | const ReferenceType *ParamRef = Param->getAs<ReferenceType>(); |
| 967 | if (ParamRef) |
| 968 | Param = ParamRef->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 969 | |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 970 | // - If A is a reference type, A is replaced by the type referred to. |
| 971 | const ReferenceType *ArgRef = Arg->getAs<ReferenceType>(); |
| 972 | if (ArgRef) |
| 973 | Arg = ArgRef->getPointeeType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 974 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 975 | if (ParamRef && ArgRef && S.Context.hasSameUnqualifiedType(Param, Arg)) { |
| 976 | // C++11 [temp.deduct.partial]p9: |
| 977 | // If, for a given type, deduction succeeds in both directions (i.e., |
| 978 | // the types are identical after the transformations above) and both |
| 979 | // P and A were reference types [...]: |
| 980 | // - if [one type] was an lvalue reference and [the other type] was |
| 981 | // not, [the other type] is not considered to be at least as |
| 982 | // specialized as [the first type] |
| 983 | // - if [one type] is more cv-qualified than [the other type], |
| 984 | // [the other type] is not considered to be at least as specialized |
| 985 | // as [the first type] |
| 986 | // Objective-C ARC adds: |
| 987 | // - [one type] has non-trivial lifetime, [the other type] has |
| 988 | // __unsafe_unretained lifetime, and the types are otherwise |
| 989 | // identical |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 990 | // |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 991 | // A is "considered to be at least as specialized" as P iff deduction |
| 992 | // succeeds, so we model this as a deduction failure. Note that |
| 993 | // [the first type] is P and [the other type] is A here; the standard |
| 994 | // gets this backwards. |
Douglas Gregor | 769d0cc | 2011-04-30 17:07:52 +0000 | [diff] [blame] | 995 | Qualifiers ParamQuals = Param.getQualifiers(); |
| 996 | Qualifiers ArgQuals = Arg.getQualifiers(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 997 | if ((ParamRef->isLValueReferenceType() && |
| 998 | !ArgRef->isLValueReferenceType()) || |
| 999 | ParamQuals.isStrictSupersetOf(ArgQuals) || |
| 1000 | (ParamQuals.hasNonTrivialObjCLifetime() && |
| 1001 | ArgQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone && |
| 1002 | ParamQuals.withoutObjCLifetime() == |
| 1003 | ArgQuals.withoutObjCLifetime())) { |
| 1004 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1005 | Info.SecondArg = TemplateArgument(ArgIn); |
| 1006 | return Sema::TDK_NonDeducedMismatch; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1007 | } |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1008 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1009 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 1010 | // C++11 [temp.deduct.partial]p7: |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1011 | // Remove any top-level cv-qualifiers: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1012 | // - If P is a cv-qualified type, P is replaced by the cv-unqualified |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1013 | // version of P. |
| 1014 | Param = Param.getUnqualifiedType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1015 | // - If A is a cv-qualified type, A is replaced by the cv-unqualified |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1016 | // version of A. |
| 1017 | Arg = Arg.getUnqualifiedType(); |
| 1018 | } else { |
| 1019 | // C++0x [temp.deduct.call]p4 bullet 1: |
| 1020 | // - If the original P is a reference type, the deduced A (i.e., the type |
| 1021 | // referred to by the reference) can be more cv-qualified than the |
| 1022 | // transformed A. |
| 1023 | if (TDF & TDF_ParamWithReferenceType) { |
| 1024 | Qualifiers Quals; |
| 1025 | QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals); |
| 1026 | Quals.setCVRQualifiers(Quals.getCVRQualifiers() & |
John McCall | 62c28c8 | 2011-01-18 07:41:22 +0000 | [diff] [blame] | 1027 | Arg.getCVRQualifiers()); |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 1028 | Param = S.Context.getQualifiedType(UnqualParam, Quals); |
| 1029 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1030 | |
Douglas Gregor | 73b3cf6 | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1031 | if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) { |
| 1032 | // C++0x [temp.deduct.type]p10: |
| 1033 | // If P and A are function types that originated from deduction when |
| 1034 | // taking the address of a function template (14.8.2.2) or when deducing |
| 1035 | // template arguments from a function declaration (14.8.2.6) and Pi and |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1036 | // Ai are parameters of the top-level parameter-type-list of P and A, |
| 1037 | // respectively, Pi is adjusted if it is an rvalue reference to a |
| 1038 | // cv-unqualified template parameter and Ai is an lvalue reference, in |
| 1039 | // which case the type of Pi is changed to be the template parameter |
Douglas Gregor | 73b3cf6 | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1040 | // type (i.e., T&& is changed to simply T). [ Note: As a result, when |
| 1041 | // Pi is T&& and Ai is X&, the adjusted Pi will be T, causing T to be |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 1042 | // deduced as X&. - end note ] |
Douglas Gregor | 73b3cf6 | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1043 | TDF &= ~TDF_TopLevelParameterTypeList; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1044 | |
Douglas Gregor | 73b3cf6 | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1045 | if (const RValueReferenceType *ParamRef |
| 1046 | = Param->getAs<RValueReferenceType>()) { |
| 1047 | if (isa<TemplateTypeParmType>(ParamRef->getPointeeType()) && |
| 1048 | !ParamRef->getPointeeType().getQualifiers()) |
| 1049 | if (Arg->isLValueReferenceType()) |
| 1050 | Param = ParamRef->getPointeeType(); |
| 1051 | } |
| 1052 | } |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1053 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1054 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1055 | // C++ [temp.deduct.type]p9: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1056 | // A template type argument T, a template template argument TT or a |
| 1057 | // template non-type argument i can be deduced if P and A have one of |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1058 | // the following forms: |
| 1059 | // |
| 1060 | // T |
| 1061 | // cv-list T |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1062 | if (const TemplateTypeParmType *TemplateTypeParm |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1063 | = Param->getAs<TemplateTypeParmType>()) { |
Douglas Gregor | af130823 | 2011-09-22 15:57:07 +0000 | [diff] [blame] | 1064 | // Just skip any attempts to deduce from a placeholder type. |
| 1065 | if (Arg->isPlaceholderType()) |
| 1066 | return Sema::TDK_Success; |
| 1067 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1068 | unsigned Index = TemplateTypeParm->getIndex(); |
Douglas Gregor | f290e0d | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1069 | bool RecanonicalizeArg = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1070 | |
Douglas Gregor | 9e9fae4 | 2009-07-22 20:02:25 +0000 | [diff] [blame] | 1071 | // If the argument type is an array type, move the qualifiers up to the |
| 1072 | // top level, so they can be matched with the qualifiers on the parameter. |
Douglas Gregor | f290e0d | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1073 | if (isa<ArrayType>(Arg)) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1074 | Qualifiers Quals; |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1075 | Arg = S.Context.getUnqualifiedArrayType(Arg, Quals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1076 | if (Quals) { |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1077 | Arg = S.Context.getQualifiedType(Arg, Quals); |
Douglas Gregor | f290e0d | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1078 | RecanonicalizeArg = true; |
| 1079 | } |
| 1080 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1081 | |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1082 | // The argument type can not be less qualified than the parameter |
| 1083 | // type. |
Douglas Gregor | 61d0b6b | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1084 | if (!(TDF & TDF_IgnoreQualifiers) && |
| 1085 | hasInconsistentOrSupersetQualifiersOf(Param, Arg)) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1086 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1087 | Info.FirstArg = TemplateArgument(Param); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1088 | Info.SecondArg = TemplateArgument(Arg); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 1089 | return Sema::TDK_Underqualified; |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1090 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1091 | |
| 1092 | assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0"); |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1093 | assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function"); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1094 | QualType DeducedType = Arg; |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 1095 | |
Douglas Gregor | 61d0b6b | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1096 | // Remove any qualifiers on the parameter from the deduced type. |
| 1097 | // We checked the qualifiers for consistency above. |
| 1098 | Qualifiers DeducedQs = DeducedType.getQualifiers(); |
| 1099 | Qualifiers ParamQs = Param.getQualifiers(); |
| 1100 | DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers()); |
| 1101 | if (ParamQs.hasObjCGCAttr()) |
| 1102 | DeducedQs.removeObjCGCAttr(); |
| 1103 | if (ParamQs.hasAddressSpace()) |
| 1104 | DeducedQs.removeAddressSpace(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1105 | if (ParamQs.hasObjCLifetime()) |
| 1106 | DeducedQs.removeObjCLifetime(); |
Douglas Gregor | e559ca1 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1107 | |
| 1108 | // Objective-C ARC: |
Douglas Gregor | da8b249 | 2011-07-26 14:53:44 +0000 | [diff] [blame] | 1109 | // If template deduction would produce a lifetime qualifier on a type |
| 1110 | // that is not a lifetime type, template argument deduction fails. |
| 1111 | if (ParamQs.hasObjCLifetime() && !DeducedType->isObjCLifetimeType() && |
| 1112 | !DeducedType->isDependentType()) { |
| 1113 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1114 | Info.FirstArg = TemplateArgument(Param); |
| 1115 | Info.SecondArg = TemplateArgument(Arg); |
| 1116 | return Sema::TDK_Underqualified; |
| 1117 | } |
| 1118 | |
| 1119 | // Objective-C ARC: |
Douglas Gregor | e559ca1 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1120 | // If template deduction would produce an argument type with lifetime type |
| 1121 | // but no lifetime qualifier, the __strong lifetime qualifier is inferred. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1122 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | e559ca1 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1123 | DeducedType->isObjCLifetimeType() && |
| 1124 | !DeducedQs.hasObjCLifetime()) |
| 1125 | DeducedQs.setObjCLifetime(Qualifiers::OCL_Strong); |
| 1126 | |
Douglas Gregor | 61d0b6b | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1127 | DeducedType = S.Context.getQualifiedType(DeducedType.getUnqualifiedType(), |
| 1128 | DeducedQs); |
| 1129 | |
Douglas Gregor | f290e0d | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 1130 | if (RecanonicalizeArg) |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1131 | DeducedType = S.Context.getCanonicalType(DeducedType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1132 | |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1133 | DeducedTemplateArgument NewDeduced(DeducedType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1134 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1135 | Deduced[Index], |
| 1136 | NewDeduced); |
| 1137 | if (Result.isNull()) { |
| 1138 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 1139 | Info.FirstArg = Deduced[Index]; |
| 1140 | Info.SecondArg = NewDeduced; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1141 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1142 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1143 | |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1144 | Deduced[Index] = Result; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1145 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1148 | // Set up the template argument deduction information for a failure. |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1149 | Info.FirstArg = TemplateArgument(ParamIn); |
| 1150 | Info.SecondArg = TemplateArgument(ArgIn); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1151 | |
Douglas Gregor | 0bc15d9 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 1152 | // If the parameter is an already-substituted template parameter |
| 1153 | // pack, do nothing: we don't know which of its arguments to look |
| 1154 | // at, so we have to wait until all of the parameter packs in this |
| 1155 | // expansion have arguments. |
| 1156 | if (isa<SubstTemplateTypeParmPackType>(Param)) |
| 1157 | return Sema::TDK_Success; |
| 1158 | |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1159 | // Check the cv-qualifiers on the parameter and argument types. |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1160 | CanQualType CanParam = S.Context.getCanonicalType(Param); |
| 1161 | CanQualType CanArg = S.Context.getCanonicalType(Arg); |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1162 | if (!(TDF & TDF_IgnoreQualifiers)) { |
| 1163 | if (TDF & TDF_ParamWithReferenceType) { |
Douglas Gregor | 61d0b6b | 2011-04-28 00:56:09 +0000 | [diff] [blame] | 1164 | if (hasInconsistentOrSupersetQualifiersOf(Param, Arg)) |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1165 | return Sema::TDK_NonDeducedMismatch; |
John McCall | cd05e81 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 1166 | } else if (!IsPossiblyOpaquelyQualifiedType(Param)) { |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1167 | if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1168 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1169 | } |
Douglas Gregor | fc55a82 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1170 | |
| 1171 | // If the parameter type is not dependent, there is nothing to deduce. |
| 1172 | if (!Param->isDependentType()) { |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1173 | if (!(TDF & TDF_SkipNonDependent)) { |
| 1174 | bool NonDeduced = (TDF & TDF_InOverloadResolution)? |
| 1175 | !S.isSameOrCompatibleFunctionType(CanParam, CanArg) : |
| 1176 | Param != Arg; |
| 1177 | if (NonDeduced) { |
| 1178 | return Sema::TDK_NonDeducedMismatch; |
| 1179 | } |
| 1180 | } |
Douglas Gregor | fc55a82 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1181 | return Sema::TDK_Success; |
| 1182 | } |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 1183 | } else if (!Param->isDependentType()) { |
| 1184 | CanQualType ParamUnqualType = CanParam.getUnqualifiedType(), |
| 1185 | ArgUnqualType = CanArg.getUnqualifiedType(); |
| 1186 | bool Success = (TDF & TDF_InOverloadResolution)? |
| 1187 | S.isSameOrCompatibleFunctionType(ParamUnqualType, |
| 1188 | ArgUnqualType) : |
| 1189 | ParamUnqualType == ArgUnqualType; |
| 1190 | if (Success) |
| 1191 | return Sema::TDK_Success; |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1192 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1193 | |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1194 | switch (Param->getTypeClass()) { |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1195 | // Non-canonical types cannot appear here. |
| 1196 | #define NON_CANONICAL_TYPE(Class, Base) \ |
| 1197 | case Type::Class: llvm_unreachable("deducing non-canonical type: " #Class); |
| 1198 | #define TYPE(Class, Base) |
| 1199 | #include "clang/AST/TypeNodes.def" |
| 1200 | |
| 1201 | case Type::TemplateTypeParm: |
| 1202 | case Type::SubstTemplateTypeParmPack: |
| 1203 | llvm_unreachable("Type nodes handled above"); |
Douglas Gregor | fc55a82 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1204 | |
| 1205 | // These types cannot be dependent, so simply check whether the types are |
| 1206 | // the same. |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1207 | case Type::Builtin: |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1208 | case Type::VariableArray: |
| 1209 | case Type::Vector: |
| 1210 | case Type::FunctionNoProto: |
| 1211 | case Type::Record: |
| 1212 | case Type::Enum: |
| 1213 | case Type::ObjCObject: |
| 1214 | case Type::ObjCInterface: |
Douglas Gregor | fc55a82 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1215 | case Type::ObjCObjectPointer: { |
| 1216 | if (TDF & TDF_SkipNonDependent) |
| 1217 | return Sema::TDK_Success; |
| 1218 | |
| 1219 | if (TDF & TDF_IgnoreQualifiers) { |
| 1220 | Param = Param.getUnqualifiedType(); |
| 1221 | Arg = Arg.getUnqualifiedType(); |
| 1222 | } |
| 1223 | |
| 1224 | return Param == Arg? Sema::TDK_Success : Sema::TDK_NonDeducedMismatch; |
| 1225 | } |
| 1226 | |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1227 | // _Complex T [placeholder extension] |
| 1228 | case Type::Complex: |
| 1229 | if (const ComplexType *ComplexArg = Arg->getAs<ComplexType>()) |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1230 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1231 | cast<ComplexType>(Param)->getElementType(), |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1232 | ComplexArg->getElementType(), |
| 1233 | Info, Deduced, TDF); |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1234 | |
| 1235 | return Sema::TDK_NonDeducedMismatch; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1236 | |
| 1237 | // _Atomic T [extension] |
| 1238 | case Type::Atomic: |
| 1239 | if (const AtomicType *AtomicArg = Arg->getAs<AtomicType>()) |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1240 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 1241 | cast<AtomicType>(Param)->getValueType(), |
| 1242 | AtomicArg->getValueType(), |
| 1243 | Info, Deduced, TDF); |
| 1244 | |
| 1245 | return Sema::TDK_NonDeducedMismatch; |
| 1246 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1247 | // T * |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1248 | case Type::Pointer: { |
John McCall | c000834 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1249 | QualType PointeeType; |
| 1250 | if (const PointerType *PointerArg = Arg->getAs<PointerType>()) { |
| 1251 | PointeeType = PointerArg->getPointeeType(); |
| 1252 | } else if (const ObjCObjectPointerType *PointerArg |
| 1253 | = Arg->getAs<ObjCObjectPointerType>()) { |
| 1254 | PointeeType = PointerArg->getPointeeType(); |
| 1255 | } else { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1256 | return Sema::TDK_NonDeducedMismatch; |
John McCall | c000834 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1257 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1258 | |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1259 | unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1260 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1261 | cast<PointerType>(Param)->getPointeeType(), |
John McCall | c000834 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 1262 | PointeeType, |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1263 | Info, Deduced, SubTDF); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1264 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1265 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1266 | // T & |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1267 | case Type::LValueReference: { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1268 | const LValueReferenceType *ReferenceArg = |
| 1269 | Arg->getAs<LValueReferenceType>(); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1270 | if (!ReferenceArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1271 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1272 | |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1273 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1274 | cast<LValueReferenceType>(Param)->getPointeeType(), |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1275 | ReferenceArg->getPointeeType(), Info, Deduced, 0); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1276 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1277 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1278 | // T && [C++0x] |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1279 | case Type::RValueReference: { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1280 | const RValueReferenceType *ReferenceArg = |
| 1281 | Arg->getAs<RValueReferenceType>(); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1282 | if (!ReferenceArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1283 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1284 | |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1285 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1286 | cast<RValueReferenceType>(Param)->getPointeeType(), |
| 1287 | ReferenceArg->getPointeeType(), |
| 1288 | Info, Deduced, 0); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1289 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1290 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1291 | // T [] (implied, but not stated explicitly) |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1292 | case Type::IncompleteArray: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1293 | const IncompleteArrayType *IncompleteArrayArg = |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1294 | S.Context.getAsIncompleteArrayType(Arg); |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1295 | if (!IncompleteArrayArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1296 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1297 | |
John McCall | e4f26e5 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1298 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1299 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1300 | S.Context.getAsIncompleteArrayType(Param)->getElementType(), |
| 1301 | IncompleteArrayArg->getElementType(), |
| 1302 | Info, Deduced, SubTDF); |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1303 | } |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1304 | |
| 1305 | // T [integer-constant] |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1306 | case Type::ConstantArray: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1307 | const ConstantArrayType *ConstantArrayArg = |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1308 | S.Context.getAsConstantArrayType(Arg); |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1309 | if (!ConstantArrayArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1310 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1311 | |
| 1312 | const ConstantArrayType *ConstantArrayParm = |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1313 | S.Context.getAsConstantArrayType(Param); |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1314 | if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1315 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1316 | |
John McCall | e4f26e5 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1317 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1318 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1319 | ConstantArrayParm->getElementType(), |
| 1320 | ConstantArrayArg->getElementType(), |
| 1321 | Info, Deduced, SubTDF); |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1324 | // type [i] |
| 1325 | case Type::DependentSizedArray: { |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1326 | const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg); |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1327 | if (!ArrayArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1328 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1329 | |
John McCall | e4f26e5 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 1330 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
| 1331 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1332 | // Check the element type of the arrays |
| 1333 | const DependentSizedArrayType *DependentArrayParm |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1334 | = S.Context.getAsDependentSizedArrayType(Param); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1335 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1336 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1337 | DependentArrayParm->getElementType(), |
| 1338 | ArrayArg->getElementType(), |
| 1339 | Info, Deduced, SubTDF)) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1340 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1341 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1342 | // Determine the array bound is something we can deduce. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1343 | NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1344 | = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr()); |
| 1345 | if (!NTTP) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1346 | return Sema::TDK_Success; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1347 | |
| 1348 | // We can perform template argument deduction for the given non-type |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1349 | // template parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1350 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1351 | "Cannot deduce non-type template argument at depth > 0"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1352 | if (const ConstantArrayType *ConstantArrayArg |
Anders Carlsson | 335e24a | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1353 | = dyn_cast<ConstantArrayType>(ArrayArg)) { |
| 1354 | llvm::APSInt Size(ConstantArrayArg->getSize()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1355 | return DeduceNonTypeTemplateArgument(S, NTTP, Size, |
Douglas Gregor | 9d0e441 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1356 | S.Context.getSizeType(), |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1357 | /*ArrayBound=*/true, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1358 | Info, Deduced); |
Anders Carlsson | 335e24a | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 1359 | } |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1360 | if (const DependentSizedArrayType *DependentArrayArg |
| 1361 | = dyn_cast<DependentSizedArrayType>(ArrayArg)) |
Douglas Gregor | 34c2f8c | 2010-12-22 23:15:38 +0000 | [diff] [blame] | 1362 | if (DependentArrayArg->getSizeExpr()) |
| 1363 | return DeduceNonTypeTemplateArgument(S, NTTP, |
| 1364 | DependentArrayArg->getSizeExpr(), |
| 1365 | Info, Deduced); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1366 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1367 | // Incomplete type does not match a dependently-sized array type |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1368 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1369 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1370 | |
| 1371 | // type(*)(T) |
| 1372 | // T(*)() |
| 1373 | // T(*)(T) |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1374 | case Type::FunctionProto: { |
Douglas Gregor | 73b3cf6 | 2011-01-25 17:19:08 +0000 | [diff] [blame] | 1375 | unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1376 | const FunctionProtoType *FunctionProtoArg = |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1377 | dyn_cast<FunctionProtoType>(Arg); |
| 1378 | if (!FunctionProtoArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1379 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1380 | |
| 1381 | const FunctionProtoType *FunctionProtoParam = |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1382 | cast<FunctionProtoType>(Param); |
Anders Carlsson | 994b6cb | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1383 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1384 | if (FunctionProtoParam->getTypeQuals() |
Douglas Gregor | e3c7a7c | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1385 | != FunctionProtoArg->getTypeQuals() || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1386 | FunctionProtoParam->getRefQualifier() |
Douglas Gregor | e3c7a7c | 2011-01-26 16:50:54 +0000 | [diff] [blame] | 1387 | != FunctionProtoArg->getRefQualifier() || |
| 1388 | FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1389 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 994b6cb | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 1390 | |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1391 | // Check return types. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1392 | if (Sema::TemplateDeductionResult Result = |
| 1393 | DeduceTemplateArgumentsByTypeMatch( |
| 1394 | S, TemplateParams, FunctionProtoParam->getReturnType(), |
| 1395 | FunctionProtoArg->getReturnType(), Info, Deduced, 0)) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1396 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1397 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1398 | return DeduceTemplateArguments( |
| 1399 | S, TemplateParams, FunctionProtoParam->param_type_begin(), |
| 1400 | FunctionProtoParam->getNumParams(), |
| 1401 | FunctionProtoArg->param_type_begin(), |
| 1402 | FunctionProtoArg->getNumParams(), Info, Deduced, SubTDF); |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 1403 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1405 | case Type::InjectedClassName: { |
| 1406 | // Treat a template's injected-class-name as if the template |
| 1407 | // specialization type had been used. |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 1408 | Param = cast<InjectedClassNameType>(Param) |
| 1409 | ->getInjectedSpecializationType(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 1410 | assert(isa<TemplateSpecializationType>(Param) && |
| 1411 | "injected class name is not a template specialization type"); |
| 1412 | // fall through |
| 1413 | } |
| 1414 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1415 | // template-name<T> (where template-name refers to a class template) |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1416 | // template-name<i> |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1417 | // TT<T> |
| 1418 | // TT<i> |
| 1419 | // TT<> |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1420 | case Type::TemplateSpecialization: { |
| 1421 | const TemplateSpecializationType *SpecParam |
| 1422 | = cast<TemplateSpecializationType>(Param); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1423 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1424 | // Try to deduce template arguments from the template-id. |
| 1425 | Sema::TemplateDeductionResult Result |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1426 | = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg, |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1427 | Info, Deduced); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | |
Douglas Gregor | 4a5c15f | 2009-09-30 22:13:51 +0000 | [diff] [blame] | 1429 | if (Result && (TDF & TDF_DerivedClass)) { |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1430 | // C++ [temp.deduct.call]p3b3: |
| 1431 | // If P is a class, and P has the form template-id, then A can be a |
| 1432 | // derived class of the deduced A. Likewise, if P is a pointer to a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1433 | // class of the form template-id, A can be a pointer to a derived |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1434 | // class pointed to by the deduced A. |
| 1435 | // |
| 1436 | // More importantly: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1437 | // These alternatives are considered only if type deduction would |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1438 | // otherwise fail. |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1439 | if (const RecordType *RecordT = Arg->getAs<RecordType>()) { |
| 1440 | // We cannot inspect base classes as part of deduction when the type |
| 1441 | // is incomplete, so either instantiate any templates necessary to |
| 1442 | // complete the type, or skip over it if it cannot be completed. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1443 | if (S.RequireCompleteType(Info.getLocation(), Arg, 0)) |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1444 | return Result; |
| 1445 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1446 | // Use data recursion to crawl through the list of base classes. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1447 | // Visited contains the set of nodes we have already visited, while |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1448 | // ToVisit is our stack of records that we still need to visit. |
| 1449 | llvm::SmallPtrSet<const RecordType *, 8> Visited; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1450 | SmallVector<const RecordType *, 8> ToVisit; |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1451 | ToVisit.push_back(RecordT); |
| 1452 | bool Successful = false; |
Benjamin Kramer | 74fe661 | 2012-01-20 16:39:18 +0000 | [diff] [blame] | 1453 | SmallVector<DeducedTemplateArgument, 8> DeducedOrig(Deduced.begin(), |
| 1454 | Deduced.end()); |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1455 | while (!ToVisit.empty()) { |
| 1456 | // Retrieve the next class in the inheritance hierarchy. |
Robert Wilhelm | 344472e | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 1457 | const RecordType *NextT = ToVisit.pop_back_val(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1459 | // If we have already seen this type, skip it. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1460 | if (!Visited.insert(NextT).second) |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1461 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1462 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1463 | // If this is a base class, try to perform template argument |
| 1464 | // deduction from it. |
| 1465 | if (NextT != RecordT) { |
Richard Trieu | 910515b | 2012-11-07 21:17:13 +0000 | [diff] [blame] | 1466 | TemplateDeductionInfo BaseInfo(Info.getLocation()); |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1467 | Sema::TemplateDeductionResult BaseResult |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1468 | = DeduceTemplateArguments(S, TemplateParams, SpecParam, |
Richard Trieu | 910515b | 2012-11-07 21:17:13 +0000 | [diff] [blame] | 1469 | QualType(NextT, 0), BaseInfo, |
| 1470 | Deduced); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1471 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1472 | // If template argument deduction for this base was successful, |
Douglas Gregor | 053105d | 2010-11-02 00:02:34 +0000 | [diff] [blame] | 1473 | // note that we had some success. Otherwise, ignore any deductions |
| 1474 | // from this base class. |
| 1475 | if (BaseResult == Sema::TDK_Success) { |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1476 | Successful = true; |
Benjamin Kramer | 74fe661 | 2012-01-20 16:39:18 +0000 | [diff] [blame] | 1477 | DeducedOrig.clear(); |
| 1478 | DeducedOrig.append(Deduced.begin(), Deduced.end()); |
Richard Trieu | 910515b | 2012-11-07 21:17:13 +0000 | [diff] [blame] | 1479 | Info.Param = BaseInfo.Param; |
| 1480 | Info.FirstArg = BaseInfo.FirstArg; |
| 1481 | Info.SecondArg = BaseInfo.SecondArg; |
Douglas Gregor | 053105d | 2010-11-02 00:02:34 +0000 | [diff] [blame] | 1482 | } |
| 1483 | else |
| 1484 | Deduced = DeducedOrig; |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1485 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1486 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1487 | // Visit base classes |
| 1488 | CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1489 | for (const auto &Base : Next->bases()) { |
| 1490 | assert(Base.getType()->isRecordType() && |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1491 | "Base class that isn't a record?"); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1492 | ToVisit.push_back(Base.getType()->getAs<RecordType>()); |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1493 | } |
| 1494 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1495 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1496 | if (Successful) |
| 1497 | return Sema::TDK_Success; |
| 1498 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1499 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1500 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1502 | return Result; |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1505 | // T type::* |
| 1506 | // T T::* |
| 1507 | // T (type::*)() |
| 1508 | // type (T::*)() |
| 1509 | // type (type::*)(T) |
| 1510 | // type (T::*)(T) |
| 1511 | // T (type::*)(T) |
| 1512 | // T (T::*)() |
| 1513 | // T (T::*)(T) |
| 1514 | case Type::MemberPointer: { |
| 1515 | const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); |
| 1516 | const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); |
| 1517 | if (!MemPtrArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1518 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1519 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1520 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1521 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1522 | MemPtrParam->getPointeeType(), |
| 1523 | MemPtrArg->getPointeeType(), |
| 1524 | Info, Deduced, |
| 1525 | TDF & TDF_IgnoreQualifiers)) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1526 | return Result; |
| 1527 | |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1528 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1529 | QualType(MemPtrParam->getClass(), 0), |
| 1530 | QualType(MemPtrArg->getClass(), 0), |
Douglas Gregor | fc55a82 | 2012-03-11 03:29:50 +0000 | [diff] [blame] | 1531 | Info, Deduced, |
| 1532 | TDF & TDF_IgnoreQualifiers); |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1535 | // (clang extension) |
| 1536 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1537 | // type(^)(T) |
| 1538 | // T(^)() |
| 1539 | // T(^)(T) |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1540 | case Type::BlockPointer: { |
| 1541 | const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); |
| 1542 | const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1543 | |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1544 | if (!BlockPtrArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1545 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1546 | |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1547 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1548 | BlockPtrParam->getPointeeType(), |
| 1549 | BlockPtrArg->getPointeeType(), |
| 1550 | Info, Deduced, 0); |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1553 | // (clang extension) |
| 1554 | // |
| 1555 | // T __attribute__(((ext_vector_type(<integral constant>)))) |
| 1556 | case Type::ExtVector: { |
| 1557 | const ExtVectorType *VectorParam = cast<ExtVectorType>(Param); |
| 1558 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1559 | // Make sure that the vectors have the same number of elements. |
| 1560 | if (VectorParam->getNumElements() != VectorArg->getNumElements()) |
| 1561 | return Sema::TDK_NonDeducedMismatch; |
| 1562 | |
| 1563 | // Perform deduction on the element types. |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1564 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1565 | VectorParam->getElementType(), |
| 1566 | VectorArg->getElementType(), |
| 1567 | Info, Deduced, TDF); |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | if (const DependentSizedExtVectorType *VectorArg |
| 1571 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1572 | // We can't check the number of elements, since the argument has a |
| 1573 | // dependent number of elements. This can only occur during partial |
| 1574 | // ordering. |
| 1575 | |
| 1576 | // Perform deduction on the element types. |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1577 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1578 | VectorParam->getElementType(), |
| 1579 | VectorArg->getElementType(), |
| 1580 | Info, Deduced, TDF); |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | return Sema::TDK_NonDeducedMismatch; |
| 1584 | } |
| 1585 | |
| 1586 | // (clang extension) |
| 1587 | // |
| 1588 | // T __attribute__(((ext_vector_type(N)))) |
| 1589 | case Type::DependentSizedExtVector: { |
| 1590 | const DependentSizedExtVectorType *VectorParam |
| 1591 | = cast<DependentSizedExtVectorType>(Param); |
| 1592 | |
| 1593 | if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { |
| 1594 | // Perform deduction on the element types. |
| 1595 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1596 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1597 | VectorParam->getElementType(), |
| 1598 | VectorArg->getElementType(), |
| 1599 | Info, Deduced, TDF)) |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1600 | return Result; |
| 1601 | |
| 1602 | // Perform deduction on the vector size, if we can. |
| 1603 | NonTypeTemplateParmDecl *NTTP |
| 1604 | = getDeducedParameterFromExpr(VectorParam->getSizeExpr()); |
| 1605 | if (!NTTP) |
| 1606 | return Sema::TDK_Success; |
| 1607 | |
| 1608 | llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); |
| 1609 | ArgSize = VectorArg->getNumElements(); |
| 1610 | return DeduceNonTypeTemplateArgument(S, NTTP, ArgSize, S.Context.IntTy, |
| 1611 | false, Info, Deduced); |
| 1612 | } |
| 1613 | |
| 1614 | if (const DependentSizedExtVectorType *VectorArg |
| 1615 | = dyn_cast<DependentSizedExtVectorType>(Arg)) { |
| 1616 | // Perform deduction on the element types. |
| 1617 | if (Sema::TemplateDeductionResult Result |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1618 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1619 | VectorParam->getElementType(), |
| 1620 | VectorArg->getElementType(), |
| 1621 | Info, Deduced, TDF)) |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1622 | return Result; |
| 1623 | |
| 1624 | // Perform deduction on the vector size, if we can. |
| 1625 | NonTypeTemplateParmDecl *NTTP |
| 1626 | = getDeducedParameterFromExpr(VectorParam->getSizeExpr()); |
| 1627 | if (!NTTP) |
| 1628 | return Sema::TDK_Success; |
| 1629 | |
| 1630 | return DeduceNonTypeTemplateArgument(S, NTTP, VectorArg->getSizeExpr(), |
| 1631 | Info, Deduced); |
| 1632 | } |
| 1633 | |
| 1634 | return Sema::TDK_NonDeducedMismatch; |
| 1635 | } |
| 1636 | |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1637 | case Type::TypeOfExpr: |
| 1638 | case Type::TypeOf: |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 1639 | case Type::DependentName: |
Douglas Gregor | 4ac0140 | 2011-06-15 16:02:29 +0000 | [diff] [blame] | 1640 | case Type::UnresolvedUsing: |
| 1641 | case Type::Decltype: |
| 1642 | case Type::UnaryTransform: |
| 1643 | case Type::Auto: |
| 1644 | case Type::DependentTemplateSpecialization: |
| 1645 | case Type::PackExpansion: |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1646 | // No template argument deduction for these types |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1647 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1650 | llvm_unreachable("Invalid Type Class!"); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1653 | static Sema::TemplateDeductionResult |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1654 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1655 | TemplateParameterList *TemplateParams, |
| 1656 | const TemplateArgument &Param, |
Douglas Gregor | 77d6bb9 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1657 | TemplateArgument Arg, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1658 | TemplateDeductionInfo &Info, |
Craig Topper | 1310aac | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 1659 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 77d6bb9 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1660 | // If the template argument is a pack expansion, perform template argument |
| 1661 | // deduction against the pattern of that expansion. This only occurs during |
| 1662 | // partial ordering. |
| 1663 | if (Arg.isPackExpansion()) |
| 1664 | Arg = Arg.getPackExpansionPattern(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1665 | |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1666 | switch (Param.getKind()) { |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1667 | case TemplateArgument::Null: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1668 | llvm_unreachable("Null template argument in parameter list"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1669 | |
| 1670 | case TemplateArgument::Type: |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1671 | if (Arg.getKind() == TemplateArgument::Type) |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 1672 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 1673 | Param.getAsType(), |
| 1674 | Arg.getAsType(), |
| 1675 | Info, Deduced, 0); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1676 | Info.FirstArg = Param; |
| 1677 | Info.SecondArg = Arg; |
| 1678 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1679 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1680 | case TemplateArgument::Template: |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1681 | if (Arg.getKind() == TemplateArgument::Template) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1682 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1683 | Param.getAsTemplate(), |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1684 | Arg.getAsTemplate(), Info, Deduced); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1685 | Info.FirstArg = Param; |
| 1686 | Info.SecondArg = Arg; |
| 1687 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1688 | |
| 1689 | case TemplateArgument::TemplateExpansion: |
| 1690 | llvm_unreachable("caller should handle pack expansions"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1691 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1692 | case TemplateArgument::Declaration: |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1693 | if (Arg.getKind() == TemplateArgument::Declaration && |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1694 | isSameDeclaration(Param.getAsDecl(), Arg.getAsDecl())) |
Eli Friedman | d7a6b16 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 1695 | return Sema::TDK_Success; |
| 1696 | |
| 1697 | Info.FirstArg = Param; |
| 1698 | Info.SecondArg = Arg; |
| 1699 | return Sema::TDK_NonDeducedMismatch; |
| 1700 | |
| 1701 | case TemplateArgument::NullPtr: |
| 1702 | if (Arg.getKind() == TemplateArgument::NullPtr && |
| 1703 | S.Context.hasSameType(Param.getNullPtrType(), Arg.getNullPtrType())) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1704 | return Sema::TDK_Success; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1705 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1706 | Info.FirstArg = Param; |
| 1707 | Info.SecondArg = Arg; |
| 1708 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1709 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1710 | case TemplateArgument::Integral: |
| 1711 | if (Arg.getKind() == TemplateArgument::Integral) { |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1712 | if (hasSameExtendedValue(Param.getAsIntegral(), Arg.getAsIntegral())) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1713 | return Sema::TDK_Success; |
| 1714 | |
| 1715 | Info.FirstArg = Param; |
| 1716 | Info.SecondArg = Arg; |
| 1717 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1718 | } |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1719 | |
| 1720 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 1721 | Info.FirstArg = Param; |
| 1722 | Info.SecondArg = Arg; |
| 1723 | return Sema::TDK_NonDeducedMismatch; |
| 1724 | } |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1725 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1726 | Info.FirstArg = Param; |
| 1727 | Info.SecondArg = Arg; |
| 1728 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1729 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1730 | case TemplateArgument::Expression: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1731 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1732 | = getDeducedParameterFromExpr(Param.getAsExpr())) { |
| 1733 | if (Arg.getKind() == TemplateArgument::Integral) |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1734 | return DeduceNonTypeTemplateArgument(S, NTTP, |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1735 | Arg.getAsIntegral(), |
Douglas Gregor | 9d0e441 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1736 | Arg.getIntegralType(), |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1737 | /*ArrayBound=*/false, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1738 | Info, Deduced); |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1739 | if (Arg.getKind() == TemplateArgument::Expression) |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1740 | return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1741 | Info, Deduced); |
Douglas Gregor | 15755cb | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 1742 | if (Arg.getKind() == TemplateArgument::Declaration) |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1743 | return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(), |
Douglas Gregor | 15755cb | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 1744 | Info, Deduced); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1745 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1746 | Info.FirstArg = Param; |
| 1747 | Info.SecondArg = Arg; |
| 1748 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1749 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1750 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1751 | // Can't deduce anything, but that's okay. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1752 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1753 | } |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 1754 | case TemplateArgument::Pack: |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1755 | llvm_unreachable("Argument packs should be expanded by the caller!"); |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1756 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1757 | |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1758 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1761 | /// \brief Determine whether there is a template argument to be used for |
| 1762 | /// deduction. |
| 1763 | /// |
| 1764 | /// This routine "expands" argument packs in-place, overriding its input |
| 1765 | /// parameters so that \c Args[ArgIdx] will be the available template argument. |
| 1766 | /// |
| 1767 | /// \returns true if there is another template argument (which will be at |
| 1768 | /// \c Args[ArgIdx]), false otherwise. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1769 | static bool hasTemplateArgumentForDeduction(const TemplateArgument *&Args, |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1770 | unsigned &ArgIdx, |
| 1771 | unsigned &NumArgs) { |
| 1772 | if (ArgIdx == NumArgs) |
| 1773 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1774 | |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1775 | const TemplateArgument &Arg = Args[ArgIdx]; |
| 1776 | if (Arg.getKind() != TemplateArgument::Pack) |
| 1777 | return true; |
| 1778 | |
| 1779 | assert(ArgIdx == NumArgs - 1 && "Pack not at the end of argument list?"); |
| 1780 | Args = Arg.pack_begin(); |
| 1781 | NumArgs = Arg.pack_size(); |
| 1782 | ArgIdx = 0; |
| 1783 | return ArgIdx < NumArgs; |
| 1784 | } |
| 1785 | |
Douglas Gregor | 7b976ec | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1786 | /// \brief Determine whether the given set of template arguments has a pack |
| 1787 | /// expansion that is not the last template argument. |
| 1788 | static bool hasPackExpansionBeforeEnd(const TemplateArgument *Args, |
| 1789 | unsigned NumArgs) { |
| 1790 | unsigned ArgIdx = 0; |
| 1791 | while (ArgIdx < NumArgs) { |
| 1792 | const TemplateArgument &Arg = Args[ArgIdx]; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1793 | |
Douglas Gregor | 7b976ec | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1794 | // Unwrap argument packs. |
| 1795 | if (Args[ArgIdx].getKind() == TemplateArgument::Pack) { |
| 1796 | Args = Arg.pack_begin(); |
| 1797 | NumArgs = Arg.pack_size(); |
| 1798 | ArgIdx = 0; |
| 1799 | continue; |
| 1800 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1801 | |
Douglas Gregor | 7b976ec | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1802 | ++ArgIdx; |
| 1803 | if (ArgIdx == NumArgs) |
| 1804 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1805 | |
Douglas Gregor | 7b976ec | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1806 | if (Arg.isPackExpansion()) |
| 1807 | return true; |
| 1808 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1809 | |
Douglas Gregor | 7b976ec | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1810 | return false; |
| 1811 | } |
| 1812 | |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1813 | static Sema::TemplateDeductionResult |
| 1814 | DeduceTemplateArguments(Sema &S, |
| 1815 | TemplateParameterList *TemplateParams, |
| 1816 | const TemplateArgument *Params, unsigned NumParams, |
| 1817 | const TemplateArgument *Args, unsigned NumArgs, |
| 1818 | TemplateDeductionInfo &Info, |
Richard Smith | 030a664 | 2012-12-06 06:44:44 +0000 | [diff] [blame] | 1819 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1820 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1821 | // If the template argument list of P contains a pack expansion that is not |
| 1822 | // the last template argument, the entire template argument list is a |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1823 | // non-deduced context. |
Douglas Gregor | 7b976ec | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1824 | if (hasPackExpansionBeforeEnd(Params, NumParams)) |
| 1825 | return Sema::TDK_Success; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1826 | |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1827 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1828 | // If P has a form that contains <T> or <i>, then each argument Pi of the |
| 1829 | // respective template argument list P is compared with the corresponding |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1830 | // argument Ai of the corresponding template argument list of A. |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1831 | unsigned ArgIdx = 0, ParamIdx = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1832 | for (; hasTemplateArgumentForDeduction(Params, ParamIdx, NumParams); |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1833 | ++ParamIdx) { |
| 1834 | if (!Params[ParamIdx].isPackExpansion()) { |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1835 | // The simple case: deduce template arguments by matching Pi and Ai. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1836 | |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1837 | // Check whether we have enough arguments. |
| 1838 | if (!hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs)) |
Richard Smith | 030a664 | 2012-12-06 06:44:44 +0000 | [diff] [blame] | 1839 | return Sema::TDK_Success; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1840 | |
Douglas Gregor | 77d6bb9 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1841 | if (Args[ArgIdx].isPackExpansion()) { |
| 1842 | // FIXME: We follow the logic of C++0x [temp.deduct.type]p22 here, |
| 1843 | // but applied to pack expansions that are template arguments. |
Richard Smith | 29805ca | 2013-01-31 05:19:49 +0000 | [diff] [blame] | 1844 | return Sema::TDK_MiscellaneousDeductionFailure; |
Douglas Gregor | 77d6bb9 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1845 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1846 | |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1847 | // Perform deduction for this Pi/Ai pair. |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1848 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | 77d6bb9 | 2011-01-11 22:21:24 +0000 | [diff] [blame] | 1849 | = DeduceTemplateArguments(S, TemplateParams, |
| 1850 | Params[ParamIdx], Args[ArgIdx], |
| 1851 | Info, Deduced)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1852 | return Result; |
| 1853 | |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1854 | // Move to the next argument. |
| 1855 | ++ArgIdx; |
| 1856 | continue; |
| 1857 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1858 | |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1859 | // The parameter is a pack expansion. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1860 | |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1861 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1862 | // If Pi is a pack expansion, then the pattern of Pi is compared with |
| 1863 | // each remaining argument in the template argument list of A. Each |
| 1864 | // comparison deduces template arguments for subsequent positions in the |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1865 | // template parameter packs expanded by Pi. |
| 1866 | TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1867 | |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1868 | // FIXME: If there are no remaining arguments, we can bail out early |
| 1869 | // and set any deduced parameter packs to an empty argument pack. |
| 1870 | // The latter part of this is a (minor) correctness issue. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1871 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1872 | // Prepare to deduce the packs within the pattern. |
| 1873 | PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1874 | |
| 1875 | // Keep track of the deduced template arguments for each parameter pack |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1876 | // expanded by this pack expansion (the outer index) and for each |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1877 | // template argument (the inner SmallVectors). |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1878 | bool HasAnyArguments = false; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1879 | for (; hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs); ++ArgIdx) { |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1880 | HasAnyArguments = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1881 | |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1882 | // Deduce template arguments from the pattern. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1883 | if (Sema::TemplateDeductionResult Result |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1884 | = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx], |
| 1885 | Info, Deduced)) |
| 1886 | return Result; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1887 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1888 | PackScope.nextPackElement(); |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1889 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1890 | |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1891 | // Build argument packs for each of the parameter packs expanded by this |
| 1892 | // pack expansion. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1893 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1894 | return Result; |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1895 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1896 | |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1897 | return Sema::TDK_Success; |
| 1898 | } |
| 1899 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1900 | static Sema::TemplateDeductionResult |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1901 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1902 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1903 | const TemplateArgumentList &ParamList, |
| 1904 | const TemplateArgumentList &ArgList, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1905 | TemplateDeductionInfo &Info, |
Craig Topper | 1310aac | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 1906 | SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1907 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1908 | ParamList.data(), ParamList.size(), |
| 1909 | ArgList.data(), ArgList.size(), |
| 1910 | Info, Deduced); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1913 | /// \brief Determine whether two template arguments are the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1914 | static bool isSameTemplateArg(ASTContext &Context, |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1915 | const TemplateArgument &X, |
| 1916 | const TemplateArgument &Y) { |
| 1917 | if (X.getKind() != Y.getKind()) |
| 1918 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1919 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1920 | switch (X.getKind()) { |
| 1921 | case TemplateArgument::Null: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1922 | llvm_unreachable("Comparing NULL template argument"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1923 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1924 | case TemplateArgument::Type: |
| 1925 | return Context.getCanonicalType(X.getAsType()) == |
| 1926 | Context.getCanonicalType(Y.getAsType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1927 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1928 | case TemplateArgument::Declaration: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 1929 | return isSameDeclaration(X.getAsDecl(), Y.getAsDecl()); |
Eli Friedman | d7a6b16 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 1930 | |
| 1931 | case TemplateArgument::NullPtr: |
| 1932 | return Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1933 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1934 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1935 | case TemplateArgument::TemplateExpansion: |
| 1936 | return Context.getCanonicalTemplateName( |
| 1937 | X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() == |
| 1938 | Context.getCanonicalTemplateName( |
| 1939 | Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1940 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1941 | case TemplateArgument::Integral: |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 1942 | return X.getAsIntegral() == Y.getAsIntegral(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1943 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1944 | case TemplateArgument::Expression: { |
| 1945 | llvm::FoldingSetNodeID XID, YID; |
| 1946 | X.getAsExpr()->Profile(XID, Context, true); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1947 | Y.getAsExpr()->Profile(YID, Context, true); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1948 | return XID == YID; |
| 1949 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1950 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1951 | case TemplateArgument::Pack: |
| 1952 | if (X.pack_size() != Y.pack_size()) |
| 1953 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1954 | |
| 1955 | for (TemplateArgument::pack_iterator XP = X.pack_begin(), |
| 1956 | XPEnd = X.pack_end(), |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1957 | YP = Y.pack_begin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1958 | XP != XPEnd; ++XP, ++YP) |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1959 | if (!isSameTemplateArg(Context, *XP, *YP)) |
| 1960 | return false; |
| 1961 | |
| 1962 | return true; |
| 1963 | } |
| 1964 | |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1965 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1966 | } |
| 1967 | |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1968 | /// \brief Allocate a TemplateArgumentLoc where all locations have |
| 1969 | /// been initialized to the given location. |
| 1970 | /// |
| 1971 | /// \param S The semantic analysis object. |
| 1972 | /// |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 1973 | /// \param Arg The template argument we are producing template argument |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1974 | /// location information for. |
| 1975 | /// |
| 1976 | /// \param NTTPType For a declaration template argument, the type of |
| 1977 | /// the non-type template parameter that corresponds to this template |
| 1978 | /// argument. |
| 1979 | /// |
| 1980 | /// \param Loc The source location to use for the resulting template |
| 1981 | /// argument. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1982 | static TemplateArgumentLoc |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1983 | getTrivialTemplateArgumentLoc(Sema &S, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1984 | const TemplateArgument &Arg, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1985 | QualType NTTPType, |
| 1986 | SourceLocation Loc) { |
| 1987 | switch (Arg.getKind()) { |
| 1988 | case TemplateArgument::Null: |
| 1989 | llvm_unreachable("Can't get a NULL template argument here"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1990 | |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1991 | case TemplateArgument::Type: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1992 | return TemplateArgumentLoc(Arg, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1993 | S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1994 | |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1995 | case TemplateArgument::Declaration: { |
| 1996 | Expr *E |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 1997 | = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 1998 | .getAs<Expr>(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1999 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2000 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2001 | |
Eli Friedman | d7a6b16 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2002 | case TemplateArgument::NullPtr: { |
| 2003 | Expr *E |
| 2004 | = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2005 | .getAs<Expr>(); |
Eli Friedman | d7a6b16 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 2006 | return TemplateArgumentLoc(TemplateArgument(NTTPType, /*isNullPtr*/true), |
| 2007 | E); |
| 2008 | } |
| 2009 | |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2010 | case TemplateArgument::Integral: { |
| 2011 | Expr *E |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2012 | = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).getAs<Expr>(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2013 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 2014 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2015 | |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2016 | case TemplateArgument::Template: |
| 2017 | case TemplateArgument::TemplateExpansion: { |
| 2018 | NestedNameSpecifierLocBuilder Builder; |
| 2019 | TemplateName Template = Arg.getAsTemplate(); |
| 2020 | if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) |
| 2021 | Builder.MakeTrivial(S.Context, DTN->getQualifier(), Loc); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2022 | else if (QualifiedTemplateName *QTN = |
| 2023 | Template.getAsQualifiedTemplateName()) |
Douglas Gregor | b6744ef | 2011-03-02 17:09:35 +0000 | [diff] [blame] | 2024 | Builder.MakeTrivial(S.Context, QTN->getQualifier(), Loc); |
| 2025 | |
| 2026 | if (Arg.getKind() == TemplateArgument::Template) |
| 2027 | return TemplateArgumentLoc(Arg, |
| 2028 | Builder.getWithLocInContext(S.Context), |
| 2029 | Loc); |
| 2030 | |
| 2031 | |
| 2032 | return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(S.Context), |
| 2033 | Loc, Loc); |
| 2034 | } |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 2035 | |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2036 | case TemplateArgument::Expression: |
| 2037 | return TemplateArgumentLoc(Arg, Arg.getAsExpr()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2038 | |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2039 | case TemplateArgument::Pack: |
| 2040 | return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo()); |
| 2041 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2042 | |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2043 | llvm_unreachable("Invalid TemplateArgument Kind!"); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2044 | } |
| 2045 | |
| 2046 | |
| 2047 | /// \brief Convert the given deduced template argument and add it to the set of |
| 2048 | /// fully-converted template arguments. |
Craig Topper | 1310aac | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 2049 | static bool |
| 2050 | ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param, |
| 2051 | DeducedTemplateArgument Arg, |
| 2052 | NamedDecl *Template, |
| 2053 | QualType NTTPType, |
| 2054 | unsigned ArgumentPackIndex, |
| 2055 | TemplateDeductionInfo &Info, |
| 2056 | bool InFunctionTemplate, |
| 2057 | SmallVectorImpl<TemplateArgument> &Output) { |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2058 | if (Arg.getKind() == TemplateArgument::Pack) { |
| 2059 | // This is a template argument pack, so check each of its arguments against |
| 2060 | // the template parameter. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2061 | SmallVector<TemplateArgument, 2> PackedArgsBuilder; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2062 | for (const auto &P : Arg.pack_elements()) { |
Douglas Gregor | d53e16a | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2063 | // When converting the deduced template argument, append it to the |
| 2064 | // general output list. We need to do this so that the template argument |
| 2065 | // checking logic has all of the prior template arguments available. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2066 | DeducedTemplateArgument InnerArg(P); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2067 | InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2068 | if (ConvertDeducedTemplateArgument(S, Param, InnerArg, Template, |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2069 | NTTPType, PackedArgsBuilder.size(), |
| 2070 | Info, InFunctionTemplate, Output)) |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2071 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2072 | |
Douglas Gregor | d53e16a | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2073 | // Move the converted template argument into our argument pack. |
Robert Wilhelm | 344472e | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 2074 | PackedArgsBuilder.push_back(Output.pop_back_val()); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2075 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2076 | |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2077 | // Create the resulting argument pack. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2078 | Output.push_back(TemplateArgument::CreatePackCopy(S.Context, |
Douglas Gregor | 203e6a3 | 2011-01-11 23:09:57 +0000 | [diff] [blame] | 2079 | PackedArgsBuilder.data(), |
| 2080 | PackedArgsBuilder.size())); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2081 | return false; |
| 2082 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2083 | |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2084 | // Convert the deduced template argument into a template |
| 2085 | // argument that we can check, almost as if the user had written |
| 2086 | // the template argument explicitly. |
| 2087 | TemplateArgumentLoc ArgLoc = getTrivialTemplateArgumentLoc(S, Arg, NTTPType, |
| 2088 | Info.getLocation()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2089 | |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2090 | // Check the template argument, converting it as necessary. |
| 2091 | return S.CheckTemplateArgument(Param, ArgLoc, |
| 2092 | Template, |
| 2093 | Template->getLocation(), |
| 2094 | Template->getSourceRange().getEnd(), |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2095 | ArgumentPackIndex, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2096 | Output, |
| 2097 | InFunctionTemplate |
| 2098 | ? (Arg.wasDeducedFromArrayBound() |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2099 | ? Sema::CTAK_DeducedFromArrayBound |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2100 | : Sema::CTAK_Deduced) |
| 2101 | : Sema::CTAK_Specified); |
| 2102 | } |
| 2103 | |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2104 | /// Complete template argument deduction for a class template partial |
| 2105 | /// specialization. |
| 2106 | static Sema::TemplateDeductionResult |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2107 | FinishTemplateArgumentDeduction(Sema &S, |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2108 | ClassTemplatePartialSpecializationDecl *Partial, |
| 2109 | const TemplateArgumentList &TemplateArgs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2110 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2111 | TemplateDeductionInfo &Info) { |
Eli Friedman | 59a839c | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2112 | // Unevaluated SFINAE context. |
| 2113 | EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated); |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2114 | Sema::SFINAETrap Trap(S); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2115 | |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2116 | Sema::ContextRAII SavedContext(S, Partial); |
| 2117 | |
| 2118 | // C++ [temp.deduct.type]p2: |
| 2119 | // [...] or if any template argument remains neither deduced nor |
| 2120 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2121 | SmallVector<TemplateArgument, 4> Builder; |
Douglas Gregor | 033a3ca | 2011-01-04 22:23:38 +0000 | [diff] [blame] | 2122 | TemplateParameterList *PartialParams = Partial->getTemplateParameters(); |
| 2123 | for (unsigned I = 0, N = PartialParams->size(); I != N; ++I) { |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2124 | NamedDecl *Param = PartialParams->getParam(I); |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2125 | if (Deduced[I].isNull()) { |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2126 | Info.Param = makeTemplateParameter(Param); |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2127 | return Sema::TDK_Incomplete; |
| 2128 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2129 | |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2130 | // We have deduced this argument, so it still needs to be |
| 2131 | // checked and converted. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2132 | |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2133 | // First, for a non-type template parameter type that is |
| 2134 | // initialized by a declaration, we need the type of the |
| 2135 | // corresponding non-type template parameter. |
| 2136 | QualType NTTPType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2137 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | d53e16a | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2138 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2139 | NTTPType = NTTP->getType(); |
Douglas Gregor | d53e16a | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2140 | if (NTTPType->isDependentType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2141 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | d53e16a | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2142 | Builder.data(), Builder.size()); |
| 2143 | NTTPType = S.SubstType(NTTPType, |
| 2144 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2145 | NTTP->getLocation(), |
| 2146 | NTTP->getDeclName()); |
| 2147 | if (NTTPType.isNull()) { |
| 2148 | Info.Param = makeTemplateParameter(Param); |
| 2149 | // FIXME: These template arguments are temporary. Free them! |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2150 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, |
| 2151 | Builder.data(), |
Douglas Gregor | d53e16a | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 2152 | Builder.size())); |
| 2153 | return Sema::TDK_SubstitutionFailure; |
| 2154 | } |
| 2155 | } |
| 2156 | } |
| 2157 | |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2158 | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2159 | Partial, NTTPType, 0, Info, false, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2160 | Builder)) { |
| 2161 | Info.Param = makeTemplateParameter(Param); |
| 2162 | // FIXME: These template arguments are temporary. Free them! |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2163 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(), |
| 2164 | Builder.size())); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2165 | return Sema::TDK_SubstitutionFailure; |
| 2166 | } |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2167 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2168 | |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2169 | // Form the template argument list from the deduced template arguments. |
| 2170 | TemplateArgumentList *DeducedArgumentList |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2171 | = TemplateArgumentList::CreateCopy(S.Context, Builder.data(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2172 | Builder.size()); |
| 2173 | |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2174 | Info.reset(DeducedArgumentList); |
| 2175 | |
| 2176 | // Substitute the deduced template arguments into the template |
| 2177 | // arguments of the class template partial specialization, and |
| 2178 | // verify that the instantiated template arguments are both valid |
| 2179 | // and are equivalent to the template arguments originally provided |
| 2180 | // to the class template. |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2181 | LocalInstantiationScope InstScope(S); |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2182 | ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate(); |
Enea Zaffanella | c1cef08 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2183 | const ASTTemplateArgumentListInfo *PartialTemplArgInfo |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2184 | = Partial->getTemplateArgsAsWritten(); |
Enea Zaffanella | c1cef08 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2185 | const TemplateArgumentLoc *PartialTemplateArgs |
| 2186 | = PartialTemplArgInfo->getTemplateArgs(); |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2187 | |
Enea Zaffanella | c1cef08 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2188 | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, |
| 2189 | PartialTemplArgInfo->RAngleLoc); |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2190 | |
Enea Zaffanella | c1cef08 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2191 | if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs, |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 2192 | InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { |
| 2193 | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; |
| 2194 | if (ParamIdx >= Partial->getTemplateParameters()->size()) |
| 2195 | ParamIdx = Partial->getTemplateParameters()->size() - 1; |
| 2196 | |
| 2197 | Decl *Param |
| 2198 | = const_cast<NamedDecl *>( |
| 2199 | Partial->getTemplateParameters()->getParam(ParamIdx)); |
| 2200 | Info.Param = makeTemplateParameter(Param); |
| 2201 | Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); |
| 2202 | return Sema::TDK_SubstitutionFailure; |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2203 | } |
| 2204 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2205 | SmallVector<TemplateArgument, 4> ConvertedInstArgs; |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2206 | if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(), |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2207 | InstArgs, false, ConvertedInstArgs)) |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2208 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2209 | |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2210 | TemplateParameterList *TemplateParams |
| 2211 | = ClassTemplate->getTemplateParameters(); |
| 2212 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2213 | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2214 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 2215 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2216 | Info.FirstArg = TemplateArgs[I]; |
| 2217 | Info.SecondArg = InstArg; |
| 2218 | return Sema::TDK_NonDeducedMismatch; |
| 2219 | } |
| 2220 | } |
| 2221 | |
| 2222 | if (Trap.hasErrorOccurred()) |
| 2223 | return Sema::TDK_SubstitutionFailure; |
| 2224 | |
| 2225 | return Sema::TDK_Success; |
| 2226 | } |
| 2227 | |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2228 | /// \brief Perform template argument deduction to determine whether |
Larisse Voufo | 2521813 | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 2229 | /// the given template arguments match the given class template |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2230 | /// partial specialization per C++ [temp.class.spec.match]. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2231 | Sema::TemplateDeductionResult |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2232 | Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2233 | const TemplateArgumentList &TemplateArgs, |
| 2234 | TemplateDeductionInfo &Info) { |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 2235 | if (Partial->isInvalidDecl()) |
| 2236 | return TDK_Invalid; |
| 2237 | |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 2238 | // C++ [temp.class.spec.match]p2: |
| 2239 | // A partial specialization matches a given actual template |
| 2240 | // argument list if the template arguments of the partial |
| 2241 | // specialization can be deduced from the actual template argument |
| 2242 | // list (14.8.2). |
Eli Friedman | 59a839c | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2243 | |
| 2244 | // Unevaluated SFINAE context. |
| 2245 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Douglas Gregor | bb26041 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2246 | SFINAETrap Trap(*this); |
Eli Friedman | 59a839c | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2247 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2248 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2249 | Deduced.resize(Partial->getTemplateParameters()->size()); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2250 | if (TemplateDeductionResult Result |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2251 | = ::DeduceTemplateArguments(*this, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2252 | Partial->getTemplateParameters(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2253 | Partial->getTemplateArgs(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2254 | TemplateArgs, Info, Deduced)) |
| 2255 | return Result; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 2256 | |
Richard Smith | 7e54fb5 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2257 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2258 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2259 | Info); |
Alp Toker | d69f37b | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2260 | if (Inst.isInvalid()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 2261 | return TDK_InstantiationDepth; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 2262 | |
Douglas Gregor | bb26041 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 2263 | if (Trap.hasErrorOccurred()) |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2264 | return Sema::TDK_SubstitutionFailure; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2265 | |
| 2266 | return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs, |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 2267 | Deduced, Info); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 2268 | } |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2269 | |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2270 | /// Complete template argument deduction for a variable template partial |
| 2271 | /// specialization. |
Larisse Voufo | 8d2a5ea | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2272 | /// TODO: Unify with ClassTemplatePartialSpecializationDecl version? |
| 2273 | /// May require unifying ClassTemplate(Partial)SpecializationDecl and |
| 2274 | /// VarTemplate(Partial)SpecializationDecl with a new data |
| 2275 | /// structure Template(Partial)SpecializationDecl, and |
| 2276 | /// using Template(Partial)SpecializationDecl as input type. |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2277 | static Sema::TemplateDeductionResult FinishTemplateArgumentDeduction( |
| 2278 | Sema &S, VarTemplatePartialSpecializationDecl *Partial, |
| 2279 | const TemplateArgumentList &TemplateArgs, |
| 2280 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2281 | TemplateDeductionInfo &Info) { |
| 2282 | // Unevaluated SFINAE context. |
| 2283 | EnterExpressionEvaluationContext Unevaluated(S, Sema::Unevaluated); |
| 2284 | Sema::SFINAETrap Trap(S); |
| 2285 | |
| 2286 | // C++ [temp.deduct.type]p2: |
| 2287 | // [...] or if any template argument remains neither deduced nor |
| 2288 | // explicitly specified, template argument deduction fails. |
| 2289 | SmallVector<TemplateArgument, 4> Builder; |
| 2290 | TemplateParameterList *PartialParams = Partial->getTemplateParameters(); |
| 2291 | for (unsigned I = 0, N = PartialParams->size(); I != N; ++I) { |
| 2292 | NamedDecl *Param = PartialParams->getParam(I); |
| 2293 | if (Deduced[I].isNull()) { |
| 2294 | Info.Param = makeTemplateParameter(Param); |
| 2295 | return Sema::TDK_Incomplete; |
| 2296 | } |
| 2297 | |
| 2298 | // We have deduced this argument, so it still needs to be |
| 2299 | // checked and converted. |
| 2300 | |
| 2301 | // First, for a non-type template parameter type that is |
| 2302 | // initialized by a declaration, we need the type of the |
| 2303 | // corresponding non-type template parameter. |
| 2304 | QualType NTTPType; |
| 2305 | if (NonTypeTemplateParmDecl *NTTP = |
| 2306 | dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 2307 | NTTPType = NTTP->getType(); |
| 2308 | if (NTTPType->isDependentType()) { |
| 2309 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 2310 | Builder.data(), Builder.size()); |
| 2311 | NTTPType = |
| 2312 | S.SubstType(NTTPType, MultiLevelTemplateArgumentList(TemplateArgs), |
| 2313 | NTTP->getLocation(), NTTP->getDeclName()); |
| 2314 | if (NTTPType.isNull()) { |
| 2315 | Info.Param = makeTemplateParameter(Param); |
| 2316 | // FIXME: These template arguments are temporary. Free them! |
| 2317 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(), |
| 2318 | Builder.size())); |
| 2319 | return Sema::TDK_SubstitutionFailure; |
| 2320 | } |
| 2321 | } |
| 2322 | } |
| 2323 | |
| 2324 | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Partial, NTTPType, |
| 2325 | 0, Info, false, Builder)) { |
| 2326 | Info.Param = makeTemplateParameter(Param); |
| 2327 | // FIXME: These template arguments are temporary. Free them! |
| 2328 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(), |
| 2329 | Builder.size())); |
| 2330 | return Sema::TDK_SubstitutionFailure; |
| 2331 | } |
| 2332 | } |
| 2333 | |
| 2334 | // Form the template argument list from the deduced template arguments. |
| 2335 | TemplateArgumentList *DeducedArgumentList = TemplateArgumentList::CreateCopy( |
| 2336 | S.Context, Builder.data(), Builder.size()); |
| 2337 | |
| 2338 | Info.reset(DeducedArgumentList); |
| 2339 | |
| 2340 | // Substitute the deduced template arguments into the template |
| 2341 | // arguments of the class template partial specialization, and |
| 2342 | // verify that the instantiated template arguments are both valid |
| 2343 | // and are equivalent to the template arguments originally provided |
| 2344 | // to the class template. |
| 2345 | LocalInstantiationScope InstScope(S); |
| 2346 | VarTemplateDecl *VarTemplate = Partial->getSpecializedTemplate(); |
Enea Zaffanella | c1cef08 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2347 | const ASTTemplateArgumentListInfo *PartialTemplArgInfo |
| 2348 | = Partial->getTemplateArgsAsWritten(); |
| 2349 | const TemplateArgumentLoc *PartialTemplateArgs |
| 2350 | = PartialTemplArgInfo->getTemplateArgs(); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2351 | |
Enea Zaffanella | c1cef08 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2352 | TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, |
| 2353 | PartialTemplArgInfo->RAngleLoc); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2354 | |
Enea Zaffanella | c1cef08 | 2013-08-10 07:24:53 +0000 | [diff] [blame] | 2355 | if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs, |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2356 | InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { |
| 2357 | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; |
| 2358 | if (ParamIdx >= Partial->getTemplateParameters()->size()) |
| 2359 | ParamIdx = Partial->getTemplateParameters()->size() - 1; |
| 2360 | |
| 2361 | Decl *Param = const_cast<NamedDecl *>( |
| 2362 | Partial->getTemplateParameters()->getParam(ParamIdx)); |
| 2363 | Info.Param = makeTemplateParameter(Param); |
| 2364 | Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); |
| 2365 | return Sema::TDK_SubstitutionFailure; |
| 2366 | } |
| 2367 | SmallVector<TemplateArgument, 4> ConvertedInstArgs; |
| 2368 | if (S.CheckTemplateArgumentList(VarTemplate, Partial->getLocation(), InstArgs, |
| 2369 | false, ConvertedInstArgs)) |
| 2370 | return Sema::TDK_SubstitutionFailure; |
| 2371 | |
| 2372 | TemplateParameterList *TemplateParams = VarTemplate->getTemplateParameters(); |
| 2373 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
| 2374 | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; |
| 2375 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { |
| 2376 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
| 2377 | Info.FirstArg = TemplateArgs[I]; |
| 2378 | Info.SecondArg = InstArg; |
| 2379 | return Sema::TDK_NonDeducedMismatch; |
| 2380 | } |
| 2381 | } |
| 2382 | |
| 2383 | if (Trap.hasErrorOccurred()) |
| 2384 | return Sema::TDK_SubstitutionFailure; |
| 2385 | |
| 2386 | return Sema::TDK_Success; |
| 2387 | } |
| 2388 | |
| 2389 | /// \brief Perform template argument deduction to determine whether |
| 2390 | /// the given template arguments match the given variable template |
| 2391 | /// partial specialization per C++ [temp.class.spec.match]. |
Larisse Voufo | 8d2a5ea | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 2392 | /// TODO: Unify with ClassTemplatePartialSpecializationDecl version? |
| 2393 | /// May require unifying ClassTemplate(Partial)SpecializationDecl and |
| 2394 | /// VarTemplate(Partial)SpecializationDecl with a new data |
| 2395 | /// structure Template(Partial)SpecializationDecl, and |
| 2396 | /// using Template(Partial)SpecializationDecl as input type. |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2397 | Sema::TemplateDeductionResult |
| 2398 | Sema::DeduceTemplateArguments(VarTemplatePartialSpecializationDecl *Partial, |
| 2399 | const TemplateArgumentList &TemplateArgs, |
| 2400 | TemplateDeductionInfo &Info) { |
| 2401 | if (Partial->isInvalidDecl()) |
| 2402 | return TDK_Invalid; |
| 2403 | |
| 2404 | // C++ [temp.class.spec.match]p2: |
| 2405 | // A partial specialization matches a given actual template |
| 2406 | // argument list if the template arguments of the partial |
| 2407 | // specialization can be deduced from the actual template argument |
| 2408 | // list (14.8.2). |
| 2409 | |
| 2410 | // Unevaluated SFINAE context. |
| 2411 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
| 2412 | SFINAETrap Trap(*this); |
| 2413 | |
| 2414 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 2415 | Deduced.resize(Partial->getTemplateParameters()->size()); |
| 2416 | if (TemplateDeductionResult Result = ::DeduceTemplateArguments( |
| 2417 | *this, Partial->getTemplateParameters(), Partial->getTemplateArgs(), |
| 2418 | TemplateArgs, Info, Deduced)) |
| 2419 | return Result; |
| 2420 | |
| 2421 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2422 | InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, |
| 2423 | Info); |
Alp Toker | d69f37b | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2424 | if (Inst.isInvalid()) |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 2425 | return TDK_InstantiationDepth; |
| 2426 | |
| 2427 | if (Trap.hasErrorOccurred()) |
| 2428 | return Sema::TDK_SubstitutionFailure; |
| 2429 | |
| 2430 | return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs, |
| 2431 | Deduced, Info); |
| 2432 | } |
| 2433 | |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2434 | /// \brief Determine whether the given type T is a simple-template-id type. |
| 2435 | static bool isSimpleTemplateIdType(QualType T) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2436 | if (const TemplateSpecializationType *Spec |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2437 | = T->getAs<TemplateSpecializationType>()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2438 | return Spec->getTemplateName().getAsTemplateDecl() != nullptr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2439 | |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 2440 | return false; |
| 2441 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2442 | |
| 2443 | /// \brief Substitute the explicitly-provided template arguments into the |
| 2444 | /// given function template according to C++ [temp.arg.explicit]. |
| 2445 | /// |
| 2446 | /// \param FunctionTemplate the function template into which the explicit |
| 2447 | /// template arguments will be substituted. |
| 2448 | /// |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 2449 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2450 | /// arguments. |
| 2451 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2452 | /// \param Deduced the deduced template arguments, which will be populated |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2453 | /// with the converted and checked explicit template arguments. |
| 2454 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2455 | /// \param ParamTypes will be populated with the instantiated function |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2456 | /// parameters. |
| 2457 | /// |
| 2458 | /// \param FunctionType if non-NULL, the result type of the function template |
| 2459 | /// will also be instantiated and the pointed-to value will be updated with |
| 2460 | /// the instantiated function type. |
| 2461 | /// |
| 2462 | /// \param Info if substitution fails for any reason, this object will be |
| 2463 | /// populated with more information about the failure. |
| 2464 | /// |
| 2465 | /// \returns TDK_Success if substitution was successful, or some failure |
| 2466 | /// condition. |
| 2467 | Sema::TemplateDeductionResult |
| 2468 | Sema::SubstituteExplicitTemplateArguments( |
| 2469 | FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 2470 | TemplateArgumentListInfo &ExplicitTemplateArgs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2471 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2472 | SmallVectorImpl<QualType> &ParamTypes, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2473 | QualType *FunctionType, |
| 2474 | TemplateDeductionInfo &Info) { |
| 2475 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 2476 | TemplateParameterList *TemplateParams |
| 2477 | = FunctionTemplate->getTemplateParameters(); |
| 2478 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2479 | if (ExplicitTemplateArgs.size() == 0) { |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2480 | // No arguments to substitute; just copy over the parameter types and |
| 2481 | // fill in the function type. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2482 | for (auto P : Function->params()) |
| 2483 | ParamTypes.push_back(P->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2484 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2485 | if (FunctionType) |
| 2486 | *FunctionType = Function->getType(); |
| 2487 | return TDK_Success; |
| 2488 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2489 | |
Eli Friedman | 59a839c | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2490 | // Unevaluated SFINAE context. |
| 2491 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2492 | SFINAETrap Trap(*this); |
| 2493 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2494 | // C++ [temp.arg.explicit]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2495 | // Template arguments that are present shall be specified in the |
| 2496 | // declaration order of their corresponding template-parameters. The |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2497 | // template argument list shall not specify more template-arguments than |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2498 | // there are corresponding template-parameters. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2499 | SmallVector<TemplateArgument, 4> Builder; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2500 | |
| 2501 | // Enter a new template instantiation context where we check the |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2502 | // explicitly-specified template arguments against this function template, |
| 2503 | // and then substitute them into the function parameter types. |
Richard Smith | 7e54fb5 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2504 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2505 | InstantiatingTemplate Inst(*this, Info.getLocation(), FunctionTemplate, |
| 2506 | DeducedArgs, |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2507 | ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution, |
| 2508 | Info); |
Alp Toker | d69f37b | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2509 | if (Inst.isInvalid()) |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2510 | return TDK_InstantiationDepth; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2511 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2512 | if (CheckTemplateArgumentList(FunctionTemplate, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2513 | SourceLocation(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2514 | ExplicitTemplateArgs, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2515 | true, |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 2516 | Builder) || Trap.hasErrorOccurred()) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2517 | unsigned Index = Builder.size(); |
Douglas Gregor | fe52c91 | 2010-05-09 01:26:06 +0000 | [diff] [blame] | 2518 | if (Index >= TemplateParams->size()) |
| 2519 | Index = TemplateParams->size() - 1; |
| 2520 | Info.Param = makeTemplateParameter(TemplateParams->getParam(Index)); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2521 | return TDK_InvalidExplicitArguments; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 2522 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2523 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2524 | // Form the template argument list from the explicitly-specified |
| 2525 | // template arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2526 | TemplateArgumentList *ExplicitArgumentList |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2527 | = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2528 | Info.reset(ExplicitArgumentList); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2529 | |
John McCall | df41f18 | 2010-10-12 19:40:14 +0000 | [diff] [blame] | 2530 | // Template argument deduction and the final substitution should be |
| 2531 | // done in the context of the templated declaration. Explicit |
| 2532 | // argument substitution, on the other hand, needs to happen in the |
| 2533 | // calling context. |
| 2534 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
| 2535 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2536 | // If we deduced template arguments for a template parameter pack, |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2537 | // note that the template argument pack is partially substituted and record |
| 2538 | // the explicit template arguments. They'll be used as part of deduction |
| 2539 | // for this template parameter pack. |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2540 | for (unsigned I = 0, N = Builder.size(); I != N; ++I) { |
| 2541 | const TemplateArgument &Arg = Builder[I]; |
| 2542 | if (Arg.getKind() == TemplateArgument::Pack) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2543 | CurrentInstantiationScope->SetPartiallySubstitutedPack( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2544 | TemplateParams->getParam(I), |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2545 | Arg.pack_begin(), |
| 2546 | Arg.pack_size()); |
| 2547 | break; |
| 2548 | } |
| 2549 | } |
| 2550 | |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2551 | const FunctionProtoType *Proto |
| 2552 | = Function->getType()->getAs<FunctionProtoType>(); |
| 2553 | assert(Proto && "Function template does not have a prototype?"); |
| 2554 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2555 | // Isolate our substituted parameters from our caller. |
| 2556 | LocalInstantiationScope InstScope(*this, /*MergeWithOuterScope*/true); |
| 2557 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2558 | // Instantiate the types of each of the function parameters given the |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2559 | // explicitly-specified template arguments. If the function has a trailing |
| 2560 | // return type, substitute it after the arguments to ensure we substitute |
| 2561 | // in lexical order. |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2562 | if (Proto->hasTrailingReturn()) { |
| 2563 | if (SubstParmTypes(Function->getLocation(), |
| 2564 | Function->param_begin(), Function->getNumParams(), |
| 2565 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 2566 | ParamTypes)) |
| 2567 | return TDK_SubstitutionFailure; |
| 2568 | } |
| 2569 | |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2570 | // Instantiate the return type. |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2571 | QualType ResultType; |
| 2572 | { |
| 2573 | // C++11 [expr.prim.general]p3: |
| 2574 | // If a declaration declares a member function or member function |
| 2575 | // template of a class X, the expression this is a prvalue of type |
| 2576 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
| 2577 | // and the end of the function-definition, member-declarator, or |
| 2578 | // declarator. |
| 2579 | unsigned ThisTypeQuals = 0; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 2580 | CXXRecordDecl *ThisContext = nullptr; |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2581 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { |
| 2582 | ThisContext = Method->getParent(); |
| 2583 | ThisTypeQuals = Method->getTypeQualifiers(); |
| 2584 | } |
| 2585 | |
| 2586 | CXXThisScopeRAII ThisScope(*this, ThisContext, ThisTypeQuals, |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2587 | getLangOpts().CPlusPlus11); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2588 | |
| 2589 | ResultType = |
| 2590 | SubstType(Proto->getReturnType(), |
| 2591 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 2592 | Function->getTypeSpecStartLoc(), Function->getDeclName()); |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 2593 | if (ResultType.isNull() || Trap.hasErrorOccurred()) |
| 2594 | return TDK_SubstitutionFailure; |
| 2595 | } |
| 2596 | |
Richard Smith | eefb3d5 | 2012-02-10 09:58:53 +0000 | [diff] [blame] | 2597 | // Instantiate the types of each of the function parameters given the |
| 2598 | // explicitly-specified template arguments if we didn't do so earlier. |
| 2599 | if (!Proto->hasTrailingReturn() && |
| 2600 | SubstParmTypes(Function->getLocation(), |
| 2601 | Function->param_begin(), Function->getNumParams(), |
| 2602 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 2603 | ParamTypes)) |
| 2604 | return TDK_SubstitutionFailure; |
| 2605 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2606 | if (FunctionType) { |
Jordan Rose | bea522f | 2013-03-08 21:51:21 +0000 | [diff] [blame] | 2607 | *FunctionType = BuildFunctionType(ResultType, ParamTypes, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2608 | Function->getLocation(), |
Eli Friedman | fa86954 | 2010-08-05 02:54:05 +0000 | [diff] [blame] | 2609 | Function->getDeclName(), |
Jordan Rose | 0918989 | 2013-03-08 22:25:36 +0000 | [diff] [blame] | 2610 | Proto->getExtProtoInfo()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2611 | if (FunctionType->isNull() || Trap.hasErrorOccurred()) |
| 2612 | return TDK_SubstitutionFailure; |
| 2613 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2614 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2615 | // C++ [temp.arg.explicit]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2616 | // Trailing template arguments that can be deduced (14.8.2) may be |
| 2617 | // omitted from the list of explicit template-arguments. If all of the |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2618 | // template arguments can be deduced, they may all be omitted; in this |
| 2619 | // case, the empty template argument list <> itself may also be omitted. |
| 2620 | // |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2621 | // Take all of the explicitly-specified arguments and put them into |
| 2622 | // the set of deduced template arguments. Explicitly-specified |
| 2623 | // parameter packs, however, will be set to NULL since the deduction |
| 2624 | // mechanisms handle explicitly-specified argument packs directly. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2625 | Deduced.reserve(TemplateParams->size()); |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2626 | for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) { |
| 2627 | const TemplateArgument &Arg = ExplicitArgumentList->get(I); |
| 2628 | if (Arg.getKind() == TemplateArgument::Pack) |
| 2629 | Deduced.push_back(DeducedTemplateArgument()); |
| 2630 | else |
| 2631 | Deduced.push_back(Arg); |
| 2632 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2633 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2634 | return TDK_Success; |
| 2635 | } |
| 2636 | |
Douglas Gregor | b7edc4f | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2637 | /// \brief Check whether the deduced argument type for a call to a function |
| 2638 | /// template matches the actual argument type per C++ [temp.deduct.call]p4. |
| 2639 | static bool |
| 2640 | CheckOriginalCallArgDeduction(Sema &S, Sema::OriginalCallArg OriginalArg, |
| 2641 | QualType DeducedA) { |
| 2642 | ASTContext &Context = S.Context; |
| 2643 | |
| 2644 | QualType A = OriginalArg.OriginalArgType; |
| 2645 | QualType OriginalParamType = OriginalArg.OriginalParamType; |
| 2646 | |
| 2647 | // Check for type equality (top-level cv-qualifiers are ignored). |
| 2648 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| 2649 | return false; |
| 2650 | |
| 2651 | // Strip off references on the argument types; they aren't needed for |
| 2652 | // the following checks. |
| 2653 | if (const ReferenceType *DeducedARef = DeducedA->getAs<ReferenceType>()) |
| 2654 | DeducedA = DeducedARef->getPointeeType(); |
| 2655 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
| 2656 | A = ARef->getPointeeType(); |
| 2657 | |
| 2658 | // C++ [temp.deduct.call]p4: |
| 2659 | // [...] However, there are three cases that allow a difference: |
| 2660 | // - If the original P is a reference type, the deduced A (i.e., the |
| 2661 | // type referred to by the reference) can be more cv-qualified than |
| 2662 | // the transformed A. |
| 2663 | if (const ReferenceType *OriginalParamRef |
| 2664 | = OriginalParamType->getAs<ReferenceType>()) { |
| 2665 | // We don't want to keep the reference around any more. |
| 2666 | OriginalParamType = OriginalParamRef->getPointeeType(); |
| 2667 | |
| 2668 | Qualifiers AQuals = A.getQualifiers(); |
| 2669 | Qualifiers DeducedAQuals = DeducedA.getQualifiers(); |
Douglas Gregor | b251302 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2670 | |
Douglas Gregor | 3940e3b | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2671 | // Under Objective-C++ ARC, the deduced type may have implicitly |
| 2672 | // been given strong or (when dealing with a const reference) |
| 2673 | // unsafe_unretained lifetime. If so, update the original |
| 2674 | // qualifiers to include this lifetime. |
Douglas Gregor | b251302 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2675 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | 3940e3b | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2676 | ((DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_Strong && |
| 2677 | AQuals.getObjCLifetime() == Qualifiers::OCL_None) || |
| 2678 | (DeducedAQuals.hasConst() && |
| 2679 | DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone))) { |
| 2680 | AQuals.setObjCLifetime(DeducedAQuals.getObjCLifetime()); |
Douglas Gregor | b251302 | 2012-07-18 00:14:59 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
Douglas Gregor | b7edc4f | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2683 | if (AQuals == DeducedAQuals) { |
| 2684 | // Qualifiers match; there's nothing to do. |
| 2685 | } else if (!DeducedAQuals.compatiblyIncludes(AQuals)) { |
Douglas Gregor | c99f0ec | 2011-06-17 14:36:00 +0000 | [diff] [blame] | 2686 | return true; |
Douglas Gregor | b7edc4f | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2687 | } else { |
| 2688 | // Qualifiers are compatible, so have the argument type adopt the |
| 2689 | // deduced argument type's qualifiers as if we had performed the |
| 2690 | // qualification conversion. |
| 2691 | A = Context.getQualifiedType(A.getUnqualifiedType(), DeducedAQuals); |
| 2692 | } |
| 2693 | } |
| 2694 | |
| 2695 | // - The transformed A can be another pointer or pointer to member |
| 2696 | // type that can be converted to the deduced A via a qualification |
| 2697 | // conversion. |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2698 | // |
| 2699 | // Also allow conversions which merely strip [[noreturn]] from function types |
| 2700 | // (recursively) as an extension. |
Douglas Gregor | 3940e3b | 2013-11-08 02:04:24 +0000 | [diff] [blame] | 2701 | // FIXME: Currently, this doesn't play nicely with qualification conversions. |
Douglas Gregor | b7edc4f | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2702 | bool ObjCLifetimeConversion = false; |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2703 | QualType ResultTy; |
Douglas Gregor | b7edc4f | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2704 | if ((A->isAnyPointerType() || A->isMemberPointerType()) && |
Chandler Carruth | 18e0461 | 2011-06-18 01:19:03 +0000 | [diff] [blame] | 2705 | (S.IsQualificationConversion(A, DeducedA, false, |
| 2706 | ObjCLifetimeConversion) || |
| 2707 | S.IsNoReturnConversion(A, DeducedA, ResultTy))) |
Douglas Gregor | b7edc4f | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2708 | return false; |
| 2709 | |
| 2710 | |
| 2711 | // - If P is a class and P has the form simple-template-id, then the |
| 2712 | // transformed A can be a derived class of the deduced A. [...] |
| 2713 | // [...] Likewise, if P is a pointer to a class of the form |
| 2714 | // simple-template-id, the transformed A can be a pointer to a |
| 2715 | // derived class pointed to by the deduced A. |
| 2716 | if (const PointerType *OriginalParamPtr |
| 2717 | = OriginalParamType->getAs<PointerType>()) { |
| 2718 | if (const PointerType *DeducedAPtr = DeducedA->getAs<PointerType>()) { |
| 2719 | if (const PointerType *APtr = A->getAs<PointerType>()) { |
| 2720 | if (A->getPointeeType()->isRecordType()) { |
| 2721 | OriginalParamType = OriginalParamPtr->getPointeeType(); |
| 2722 | DeducedA = DeducedAPtr->getPointeeType(); |
| 2723 | A = APtr->getPointeeType(); |
| 2724 | } |
| 2725 | } |
| 2726 | } |
| 2727 | } |
| 2728 | |
| 2729 | if (Context.hasSameUnqualifiedType(A, DeducedA)) |
| 2730 | return false; |
| 2731 | |
| 2732 | if (A->isRecordType() && isSimpleTemplateIdType(OriginalParamType) && |
| 2733 | S.IsDerivedFrom(A, DeducedA)) |
| 2734 | return false; |
| 2735 | |
| 2736 | return true; |
| 2737 | } |
| 2738 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2739 | /// \brief Finish template argument deduction for a function template, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2740 | /// checking the deduced template arguments for completeness and forming |
| 2741 | /// the function template specialization. |
Douglas Gregor | dbfb371 | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2742 | /// |
| 2743 | /// \param OriginalCallArgs If non-NULL, the original call arguments against |
| 2744 | /// which the deduced argument types should be compared. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2745 | Sema::TemplateDeductionResult |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2746 | Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2747 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2748 | unsigned NumExplicitlySpecified, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2749 | FunctionDecl *&Specialization, |
Douglas Gregor | dbfb371 | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2750 | TemplateDeductionInfo &Info, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2751 | SmallVectorImpl<OriginalCallArg> const *OriginalCallArgs, |
| 2752 | bool PartialOverloading) { |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2753 | TemplateParameterList *TemplateParams |
| 2754 | = FunctionTemplate->getTemplateParameters(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2755 | |
Eli Friedman | 59a839c | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 2756 | // Unevaluated SFINAE context. |
| 2757 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2758 | SFINAETrap Trap(*this); |
| 2759 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2760 | // Enter a new template instantiation context while we instantiate the |
| 2761 | // actual function declaration. |
Richard Smith | 7e54fb5 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 2762 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2763 | InstantiatingTemplate Inst(*this, Info.getLocation(), FunctionTemplate, |
| 2764 | DeducedArgs, |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2765 | ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution, |
| 2766 | Info); |
Alp Toker | d69f37b | 2013-10-08 08:09:04 +0000 | [diff] [blame] | 2767 | if (Inst.isInvalid()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2768 | return TDK_InstantiationDepth; |
| 2769 | |
John McCall | 96db310 | 2010-04-29 01:18:58 +0000 | [diff] [blame] | 2770 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
John McCall | f581382 | 2010-04-29 00:35:03 +0000 | [diff] [blame] | 2771 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2772 | // C++ [temp.deduct.type]p2: |
| 2773 | // [...] or if any template argument remains neither deduced nor |
| 2774 | // explicitly specified, template argument deduction fails. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2775 | SmallVector<TemplateArgument, 4> Builder; |
Douglas Gregor | b9a7d6f | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2776 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 2777 | NamedDecl *Param = TemplateParams->getParam(I); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2778 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2779 | if (!Deduced[I].isNull()) { |
Douglas Gregor | 3273b0c | 2010-10-12 18:51:08 +0000 | [diff] [blame] | 2780 | if (I < NumExplicitlySpecified) { |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2781 | // We have already fully type-checked and converted this |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2782 | // argument, because it was explicitly-specified. Just record the |
Douglas Gregor | 3273b0c | 2010-10-12 18:51:08 +0000 | [diff] [blame] | 2783 | // presence of this argument. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2784 | Builder.push_back(Deduced[I]); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 2785 | // We may have had explicitly-specified template arguments for a |
| 2786 | // template parameter pack (that may or may not have been extended |
| 2787 | // via additional deduced arguments). |
| 2788 | if (Param->isParameterPack() && CurrentInstantiationScope) { |
| 2789 | if (CurrentInstantiationScope->getPartiallySubstitutedPack() == |
| 2790 | Param) { |
| 2791 | // Forget the partially-substituted pack; its substitution is now |
| 2792 | // complete. |
| 2793 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2794 | } |
| 2795 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2796 | continue; |
| 2797 | } |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2798 | // We have deduced this argument, so it still needs to be |
| 2799 | // checked and converted. |
| 2800 | |
| 2801 | // First, for a non-type template parameter type that is |
| 2802 | // initialized by a declaration, we need the type of the |
| 2803 | // corresponding non-type template parameter. |
| 2804 | QualType NTTPType; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2805 | if (NonTypeTemplateParmDecl *NTTP |
| 2806 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | b9a7d6f | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2807 | NTTPType = NTTP->getType(); |
| 2808 | if (NTTPType->isDependentType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2809 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
Douglas Gregor | b9a7d6f | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2810 | Builder.data(), Builder.size()); |
| 2811 | NTTPType = SubstType(NTTPType, |
| 2812 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 2813 | NTTP->getLocation(), |
| 2814 | NTTP->getDeclName()); |
| 2815 | if (NTTPType.isNull()) { |
| 2816 | Info.Param = makeTemplateParameter(Param); |
| 2817 | // FIXME: These template arguments are temporary. Free them! |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2818 | Info.reset(TemplateArgumentList::CreateCopy(Context, |
| 2819 | Builder.data(), |
Douglas Gregor | b9a7d6f | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2820 | Builder.size())); |
| 2821 | return TDK_SubstitutionFailure; |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2822 | } |
| 2823 | } |
| 2824 | } |
| 2825 | |
Douglas Gregor | b9a7d6f | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2826 | if (ConvertDeducedTemplateArgument(*this, Param, Deduced[I], |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2827 | FunctionTemplate, NTTPType, 0, Info, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2828 | true, Builder)) { |
Douglas Gregor | b9a7d6f | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2829 | Info.Param = makeTemplateParameter(Param); |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2830 | // FIXME: These template arguments are temporary. Free them! |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2831 | Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(), |
| 2832 | Builder.size())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2833 | return TDK_SubstitutionFailure; |
| 2834 | } |
| 2835 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2836 | continue; |
| 2837 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2838 | |
Douglas Gregor | ea6c96f | 2010-12-23 01:52:01 +0000 | [diff] [blame] | 2839 | // C++0x [temp.arg.explicit]p3: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2840 | // A trailing template parameter pack (14.5.3) not otherwise deduced will |
Douglas Gregor | ea6c96f | 2010-12-23 01:52:01 +0000 | [diff] [blame] | 2841 | // be deduced to an empty sequence of template arguments. |
| 2842 | // FIXME: Where did the word "trailing" come from? |
| 2843 | if (Param->isTemplateParameterPack()) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2844 | // We may have had explicitly-specified template arguments for this |
| 2845 | // template parameter pack. If so, our empty deduction extends the |
| 2846 | // explicitly-specified set (C++0x [temp.arg.explicit]p9). |
| 2847 | const TemplateArgument *ExplicitArgs; |
| 2848 | unsigned NumExplicitArgs; |
Richard Smith | a8eaf00 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 2849 | if (CurrentInstantiationScope && |
| 2850 | CurrentInstantiationScope->getPartiallySubstitutedPack(&ExplicitArgs, |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2851 | &NumExplicitArgs) |
Douglas Gregor | 22eaced | 2013-01-18 22:27:09 +0000 | [diff] [blame] | 2852 | == Param) { |
Douglas Gregor | d373119 | 2011-01-10 07:32:04 +0000 | [diff] [blame] | 2853 | Builder.push_back(TemplateArgument(ExplicitArgs, NumExplicitArgs)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2854 | |
Douglas Gregor | 22eaced | 2013-01-18 22:27:09 +0000 | [diff] [blame] | 2855 | // Forget the partially-substituted pack; it's substitution is now |
| 2856 | // complete. |
| 2857 | CurrentInstantiationScope->ResetPartiallySubstitutedPack(); |
| 2858 | } else { |
| 2859 | Builder.push_back(TemplateArgument::getEmptyPack()); |
| 2860 | } |
Douglas Gregor | ea6c96f | 2010-12-23 01:52:01 +0000 | [diff] [blame] | 2861 | continue; |
| 2862 | } |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2863 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2864 | // Substitute into the default template argument, if available. |
Richard Smith | 305e5b4 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 2865 | bool HasDefaultArg = false; |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2866 | TemplateArgumentLoc DefArg |
| 2867 | = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate, |
| 2868 | FunctionTemplate->getLocation(), |
| 2869 | FunctionTemplate->getSourceRange().getEnd(), |
| 2870 | Param, |
Richard Smith | 305e5b4 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 2871 | Builder, HasDefaultArg); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2872 | |
| 2873 | // If there was no default argument, deduction is incomplete. |
| 2874 | if (DefArg.getArgument().isNull()) { |
| 2875 | Info.Param = makeTemplateParameter( |
| 2876 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
Richard Smith | 305e5b4 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 2877 | Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(), |
| 2878 | Builder.size())); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 2879 | if (PartialOverloading) break; |
| 2880 | |
Richard Smith | 305e5b4 | 2013-07-04 01:01:24 +0000 | [diff] [blame] | 2881 | return HasDefaultArg ? TDK_SubstitutionFailure : TDK_Incomplete; |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2882 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2883 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2884 | // Check whether we can actually use the default argument. |
| 2885 | if (CheckTemplateArgument(Param, DefArg, |
| 2886 | FunctionTemplate, |
| 2887 | FunctionTemplate->getLocation(), |
| 2888 | FunctionTemplate->getSourceRange().getEnd(), |
Douglas Gregor | 6952f1e | 2011-01-19 20:10:05 +0000 | [diff] [blame] | 2889 | 0, Builder, |
Douglas Gregor | 8735b29 | 2011-06-03 02:59:40 +0000 | [diff] [blame] | 2890 | CTAK_Specified)) { |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2891 | Info.Param = makeTemplateParameter( |
| 2892 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2893 | // FIXME: These template arguments are temporary. Free them! |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2894 | Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(), |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2895 | Builder.size())); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2896 | return TDK_SubstitutionFailure; |
| 2897 | } |
| 2898 | |
| 2899 | // If we get here, we successfully used the default template argument. |
| 2900 | } |
| 2901 | |
| 2902 | // Form the template argument list from the deduced template arguments. |
| 2903 | TemplateArgumentList *DeducedArgumentList |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2904 | = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size()); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2905 | Info.reset(DeducedArgumentList); |
| 2906 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2907 | // Substitute the deduced template arguments into the function template |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2908 | // declaration to produce the function template specialization. |
Douglas Gregor | d4598a2 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 2909 | DeclContext *Owner = FunctionTemplate->getDeclContext(); |
| 2910 | if (FunctionTemplate->getFriendObjectKind()) |
| 2911 | Owner = FunctionTemplate->getLexicalDeclContext(); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2912 | Specialization = cast_or_null<FunctionDecl>( |
Douglas Gregor | d4598a2 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 2913 | SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner, |
Douglas Gregor | 357bbd0 | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 2914 | MultiLevelTemplateArgumentList(*DeducedArgumentList))); |
Douglas Gregor | 5fad9b8 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 2915 | if (!Specialization || Specialization->isInvalidDecl()) |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2916 | return TDK_SubstitutionFailure; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2917 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2918 | assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == |
Douglas Gregor | f882574 | 2009-09-15 18:26:13 +0000 | [diff] [blame] | 2919 | FunctionTemplate->getCanonicalDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2920 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2921 | // If the template argument list is owned by the function template |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2922 | // specialization, release it. |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 2923 | if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList && |
| 2924 | !Trap.hasErrorOccurred()) |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2925 | Info.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2926 | |
Douglas Gregor | 5fad9b8 | 2011-10-12 20:35:48 +0000 | [diff] [blame] | 2927 | // There may have been an error that did not prevent us from constructing a |
| 2928 | // declaration. Mark the declaration invalid and return with a substitution |
| 2929 | // failure. |
| 2930 | if (Trap.hasErrorOccurred()) { |
| 2931 | Specialization->setInvalidDecl(true); |
| 2932 | return TDK_SubstitutionFailure; |
| 2933 | } |
| 2934 | |
Douglas Gregor | dbfb371 | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2935 | if (OriginalCallArgs) { |
| 2936 | // C++ [temp.deduct.call]p4: |
| 2937 | // In general, the deduction process attempts to find template argument |
| 2938 | // values that will make the deduced A identical to A (after the type A |
| 2939 | // is transformed as described above). [...] |
| 2940 | for (unsigned I = 0, N = OriginalCallArgs->size(); I != N; ++I) { |
| 2941 | OriginalCallArg OriginalArg = (*OriginalCallArgs)[I]; |
Douglas Gregor | dbfb371 | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2942 | unsigned ParamIdx = OriginalArg.ArgIdx; |
| 2943 | |
| 2944 | if (ParamIdx >= Specialization->getNumParams()) |
| 2945 | continue; |
| 2946 | |
| 2947 | QualType DeducedA = Specialization->getParamDecl(ParamIdx)->getType(); |
Douglas Gregor | b7edc4f | 2011-06-17 05:18:17 +0000 | [diff] [blame] | 2948 | if (CheckOriginalCallArgDeduction(*this, OriginalArg, DeducedA)) |
| 2949 | return Sema::TDK_SubstitutionFailure; |
Douglas Gregor | dbfb371 | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 2950 | } |
| 2951 | } |
| 2952 | |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2953 | // If we suppressed any diagnostics while performing template argument |
| 2954 | // deduction, and if we haven't already instantiated this declaration, |
| 2955 | // keep track of these diagnostics. They'll be emitted if this specialization |
| 2956 | // is actually used. |
| 2957 | if (Info.diag_begin() != Info.diag_end()) { |
Craig Topper | ee0a479 | 2013-07-05 04:33:53 +0000 | [diff] [blame] | 2958 | SuppressedDiagnosticsMap::iterator |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2959 | Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl()); |
| 2960 | if (Pos == SuppressedDiagnostics.end()) |
| 2961 | SuppressedDiagnostics[Specialization->getCanonicalDecl()] |
| 2962 | .append(Info.diag_begin(), Info.diag_end()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2963 | } |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2964 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2965 | return TDK_Success; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2966 | } |
| 2967 | |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2968 | /// Gets the type of a function for template-argument-deducton |
| 2969 | /// purposes when it's considered as part of an overload set. |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2970 | static QualType GetTypeOfFunction(Sema &S, const OverloadExpr::FindResult &R, |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2971 | FunctionDecl *Fn) { |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2972 | // We may need to deduce the return type of the function now. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 2973 | if (S.getLangOpts().CPlusPlus14 && Fn->getReturnType()->isUndeducedType() && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2974 | S.DeduceReturnType(Fn, R.Expression->getExprLoc(), /*Diagnose*/ false)) |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2975 | return QualType(); |
| 2976 | |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2977 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2978 | if (Method->isInstance()) { |
| 2979 | // An instance method that's referenced in a form that doesn't |
| 2980 | // look like a member pointer is just invalid. |
| 2981 | if (!R.HasFormOfMemberPointer) return QualType(); |
| 2982 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2983 | return S.Context.getMemberPointerType(Fn->getType(), |
| 2984 | S.Context.getTypeDeclType(Method->getParent()).getTypePtr()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2985 | } |
| 2986 | |
| 2987 | if (!R.IsAddressOfOperand) return Fn->getType(); |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 2988 | return S.Context.getPointerType(Fn->getType()); |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2989 | } |
| 2990 | |
| 2991 | /// Apply the deduction rules for overload sets. |
| 2992 | /// |
| 2993 | /// \return the null type if this argument should be treated as an |
| 2994 | /// undeduced context |
| 2995 | static QualType |
| 2996 | ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams, |
Douglas Gregor | 75f21af | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 2997 | Expr *Arg, QualType ParamType, |
| 2998 | bool ParamWasReference) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2999 | |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3000 | OverloadExpr::FindResult R = OverloadExpr::find(Arg); |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3001 | |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3002 | OverloadExpr *Ovl = R.Expression; |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3003 | |
Douglas Gregor | 75f21af | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3004 | // C++0x [temp.deduct.call]p4 |
| 3005 | unsigned TDF = 0; |
| 3006 | if (ParamWasReference) |
| 3007 | TDF |= TDF_ParamWithReferenceType; |
| 3008 | if (R.IsAddressOfOperand) |
| 3009 | TDF |= TDF_IgnoreQualifiers; |
| 3010 | |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3011 | // C++0x [temp.deduct.call]p6: |
| 3012 | // When P is a function type, pointer to function type, or pointer |
| 3013 | // to member function type: |
| 3014 | |
| 3015 | if (!ParamType->isFunctionType() && |
| 3016 | !ParamType->isFunctionPointerType() && |
Douglas Gregor | 860d9b7 | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3017 | !ParamType->isMemberFunctionPointerType()) { |
| 3018 | if (Ovl->hasExplicitTemplateArgs()) { |
| 3019 | // But we can still look for an explicit specialization. |
| 3020 | if (FunctionDecl *ExplicitSpec |
| 3021 | = S.ResolveSingleFunctionTemplateSpecialization(Ovl)) |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3022 | return GetTypeOfFunction(S, R, ExplicitSpec); |
Douglas Gregor | 860d9b7 | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3023 | } |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3024 | |
Douglas Gregor | 860d9b7 | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3025 | return QualType(); |
| 3026 | } |
| 3027 | |
| 3028 | // Gather the explicit template arguments, if any. |
| 3029 | TemplateArgumentListInfo ExplicitTemplateArgs; |
| 3030 | if (Ovl->hasExplicitTemplateArgs()) |
| 3031 | Ovl->getExplicitTemplateArgs().copyInto(ExplicitTemplateArgs); |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3032 | QualType Match; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 3033 | for (UnresolvedSetIterator I = Ovl->decls_begin(), |
| 3034 | E = Ovl->decls_end(); I != E; ++I) { |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3035 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 3036 | |
Douglas Gregor | 860d9b7 | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3037 | if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) { |
| 3038 | // - If the argument is an overload set containing one or more |
| 3039 | // function templates, the parameter is treated as a |
| 3040 | // non-deduced context. |
| 3041 | if (!Ovl->hasExplicitTemplateArgs()) |
| 3042 | return QualType(); |
| 3043 | |
| 3044 | // Otherwise, see if we can resolve a function type |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3045 | FunctionDecl *Specialization = nullptr; |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3046 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
Douglas Gregor | 860d9b7 | 2012-03-12 21:09:16 +0000 | [diff] [blame] | 3047 | if (S.DeduceTemplateArguments(FunTmpl, &ExplicitTemplateArgs, |
| 3048 | Specialization, Info)) |
| 3049 | continue; |
| 3050 | |
| 3051 | D = Specialization; |
| 3052 | } |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3053 | |
| 3054 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3055 | QualType ArgType = GetTypeOfFunction(S, R, Fn); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 3056 | if (ArgType.isNull()) continue; |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3057 | |
Douglas Gregor | 75f21af | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3058 | // Function-to-pointer conversion. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3059 | if (!ParamWasReference && ParamType->isPointerType() && |
Douglas Gregor | 75f21af | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3060 | ArgType->isFunctionType()) |
| 3061 | ArgType = S.Context.getPointerType(ArgType); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3062 | |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3063 | // - If the argument is an overload set (not containing function |
| 3064 | // templates), trial argument deduction is attempted using each |
| 3065 | // of the members of the set. If deduction succeeds for only one |
| 3066 | // of the overload set members, that member is used as the |
| 3067 | // argument value for the deduction. If deduction succeeds for |
| 3068 | // more than one member of the overload set the parameter is |
| 3069 | // treated as a non-deduced context. |
| 3070 | |
| 3071 | // We do all of this in a fresh context per C++0x [temp.deduct.type]p2: |
| 3072 | // Type deduction is done independently for each P/A pair, and |
| 3073 | // the deduced template argument values are then combined. |
| 3074 | // So we do not reject deductions which were made elsewhere. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3075 | SmallVector<DeducedTemplateArgument, 8> |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3076 | Deduced(TemplateParams->size()); |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3077 | TemplateDeductionInfo Info(Ovl->getNameLoc()); |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3078 | Sema::TemplateDeductionResult Result |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3079 | = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
| 3080 | ArgType, Info, Deduced, TDF); |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3081 | if (Result) continue; |
| 3082 | if (!Match.isNull()) return QualType(); |
| 3083 | Match = ArgType; |
| 3084 | } |
| 3085 | |
| 3086 | return Match; |
| 3087 | } |
| 3088 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3089 | /// \brief Perform the adjustments to the parameter and argument types |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3090 | /// described in C++ [temp.deduct.call]. |
| 3091 | /// |
| 3092 | /// \returns true if the caller should not attempt to perform any template |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3093 | /// argument deduction based on this P/A pair because the argument is an |
| 3094 | /// overloaded function set that could not be resolved. |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3095 | static bool AdjustFunctionParmAndArgTypesForDeduction(Sema &S, |
| 3096 | TemplateParameterList *TemplateParams, |
| 3097 | QualType &ParamType, |
| 3098 | QualType &ArgType, |
| 3099 | Expr *Arg, |
| 3100 | unsigned &TDF) { |
| 3101 | // C++0x [temp.deduct.call]p3: |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3102 | // If P is a cv-qualified type, the top level cv-qualifiers of P's type |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3103 | // are ignored for type deduction. |
Douglas Gregor | a459cc2 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3104 | if (ParamType.hasQualifiers()) |
| 3105 | ParamType = ParamType.getUnqualifiedType(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3106 | |
| 3107 | // [...] If P is a reference type, the type referred to by P is |
| 3108 | // used for type deduction. |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3109 | const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3110 | if (ParamRefType) |
| 3111 | ParamType = ParamRefType->getPointeeType(); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3112 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3113 | // Overload sets usually make this parameter an undeduced context, |
| 3114 | // but there are sometimes special circumstances. Typically |
| 3115 | // involving a template-id-expr. |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3116 | if (ArgType == S.Context.OverloadTy) { |
| 3117 | ArgType = ResolveOverloadForDeduction(S, TemplateParams, |
| 3118 | Arg, ParamType, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3119 | ParamRefType != nullptr); |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3120 | if (ArgType.isNull()) |
| 3121 | return true; |
| 3122 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3123 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3124 | if (ParamRefType) { |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3125 | // If the argument has incomplete array type, try to complete its type. |
| 3126 | if (ArgType->isIncompleteArrayType() && !S.RequireCompleteExprType(Arg, 0)) |
| 3127 | ArgType = Arg->getType(); |
| 3128 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3129 | // C++0x [temp.deduct.call]p3: |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3130 | // If P is an rvalue reference to a cv-unqualified template |
| 3131 | // parameter and the argument is an lvalue, the type "lvalue |
| 3132 | // reference to A" is used in place of A for type deduction. |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3133 | if (ParamRefType->isRValueReferenceType() && |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3134 | !ParamType.getQualifiers() && |
| 3135 | isa<TemplateTypeParmType>(ParamType) && |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3136 | Arg->isLValue()) |
| 3137 | ArgType = S.Context.getLValueReferenceType(ArgType); |
| 3138 | } else { |
| 3139 | // C++ [temp.deduct.call]p2: |
| 3140 | // If P is not a reference type: |
| 3141 | // - If A is an array type, the pointer type produced by the |
| 3142 | // array-to-pointer standard conversion (4.2) is used in place of |
| 3143 | // A for type deduction; otherwise, |
| 3144 | if (ArgType->isArrayType()) |
| 3145 | ArgType = S.Context.getArrayDecayedType(ArgType); |
| 3146 | // - If A is a function type, the pointer type produced by the |
| 3147 | // function-to-pointer standard conversion (4.3) is used in place |
| 3148 | // of A for type deduction; otherwise, |
| 3149 | else if (ArgType->isFunctionType()) |
| 3150 | ArgType = S.Context.getPointerType(ArgType); |
| 3151 | else { |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3152 | // - If A is a cv-qualified type, the top level cv-qualifiers of A's |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3153 | // type are ignored for type deduction. |
Douglas Gregor | a459cc2 | 2011-04-27 23:34:22 +0000 | [diff] [blame] | 3154 | ArgType = ArgType.getUnqualifiedType(); |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3155 | } |
| 3156 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3157 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3158 | // C++0x [temp.deduct.call]p4: |
| 3159 | // In general, the deduction process attempts to find template argument |
| 3160 | // values that will make the deduced A identical to A (after the type A |
| 3161 | // is transformed as described above). [...] |
| 3162 | TDF = TDF_SkipNonDependent; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3163 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3164 | // - If the original P is a reference type, the deduced A (i.e., the |
| 3165 | // type referred to by the reference) can be more cv-qualified than |
| 3166 | // the transformed A. |
| 3167 | if (ParamRefType) |
| 3168 | TDF |= TDF_ParamWithReferenceType; |
| 3169 | // - The transformed A can be another pointer or pointer to member |
| 3170 | // type that can be converted to the deduced A via a qualification |
| 3171 | // conversion (4.4). |
| 3172 | if (ArgType->isPointerType() || ArgType->isMemberPointerType() || |
| 3173 | ArgType->isObjCObjectPointerType()) |
| 3174 | TDF |= TDF_IgnoreQualifiers; |
| 3175 | // - If P is a class and P has the form simple-template-id, then the |
| 3176 | // transformed A can be a derived class of the deduced A. Likewise, |
| 3177 | // if P is a pointer to a class of the form simple-template-id, the |
| 3178 | // transformed A can be a pointer to a derived class pointed to by |
| 3179 | // the deduced A. |
| 3180 | if (isSimpleTemplateIdType(ParamType) || |
| 3181 | (isa<PointerType>(ParamType) && |
| 3182 | isSimpleTemplateIdType( |
| 3183 | ParamType->getAs<PointerType>()->getPointeeType()))) |
| 3184 | TDF |= TDF_DerivedClass; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3185 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3186 | return false; |
| 3187 | } |
| 3188 | |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3189 | static bool |
| 3190 | hasDeducibleTemplateParameters(Sema &S, FunctionTemplateDecl *FunctionTemplate, |
| 3191 | QualType T); |
Douglas Gregor | dbfb371 | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3192 | |
Sebastian Redl | 4b911e6 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3193 | /// \brief Perform template argument deduction by matching a parameter type |
| 3194 | /// against a single expression, where the expression is an element of |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3195 | /// an initializer list that was originally matched against a parameter |
| 3196 | /// of type \c initializer_list\<ParamType\>. |
Sebastian Redl | 4b911e6 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3197 | static Sema::TemplateDeductionResult |
| 3198 | DeduceTemplateArgumentByListElement(Sema &S, |
| 3199 | TemplateParameterList *TemplateParams, |
| 3200 | QualType ParamType, Expr *Arg, |
| 3201 | TemplateDeductionInfo &Info, |
| 3202 | SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 3203 | unsigned TDF) { |
| 3204 | // Handle the case where an init list contains another init list as the |
| 3205 | // element. |
| 3206 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
| 3207 | QualType X; |
| 3208 | if (!S.isStdInitializerList(ParamType.getNonReferenceType(), &X)) |
| 3209 | return Sema::TDK_Success; // Just ignore this expression. |
| 3210 | |
| 3211 | // Recurse down into the init list. |
| 3212 | for (unsigned i = 0, e = ILE->getNumInits(); i < e; ++i) { |
| 3213 | if (Sema::TemplateDeductionResult Result = |
| 3214 | DeduceTemplateArgumentByListElement(S, TemplateParams, X, |
| 3215 | ILE->getInit(i), |
| 3216 | Info, Deduced, TDF)) |
| 3217 | return Result; |
| 3218 | } |
| 3219 | return Sema::TDK_Success; |
| 3220 | } |
| 3221 | |
| 3222 | // For all other cases, just match by type. |
Douglas Gregor | d280389 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3223 | QualType ArgType = Arg->getType(); |
| 3224 | if (AdjustFunctionParmAndArgTypesForDeduction(S, TemplateParams, ParamType, |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3225 | ArgType, Arg, TDF)) { |
| 3226 | Info.Expression = Arg; |
Douglas Gregor | d280389 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3227 | return Sema::TDK_FailedOverloadResolution; |
Richard Smith | 0efa62f | 2013-01-31 04:03:12 +0000 | [diff] [blame] | 3228 | } |
Sebastian Redl | 4b911e6 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3229 | return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, |
Douglas Gregor | d280389 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3230 | ArgType, Info, Deduced, TDF); |
Sebastian Redl | 4b911e6 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3231 | } |
| 3232 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3233 | /// \brief Perform template argument deduction from a function call |
| 3234 | /// (C++ [temp.deduct.call]). |
| 3235 | /// |
| 3236 | /// \param FunctionTemplate the function template for which we are performing |
| 3237 | /// template argument deduction. |
| 3238 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3239 | /// \param ExplicitTemplateArgs the explicit template arguments provided |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 3240 | /// for this call. |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3241 | /// |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3242 | /// \param Args the function call arguments |
| 3243 | /// |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3244 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3245 | /// this will be set to the function template specialization produced by |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3246 | /// template argument deduction. |
| 3247 | /// |
| 3248 | /// \param Info the argument will be updated to provide additional information |
| 3249 | /// about template argument deduction. |
| 3250 | /// |
| 3251 | /// \returns the result of template argument deduction. |
Robert Wilhelm | 834c058 | 2013-08-09 18:02:13 +0000 | [diff] [blame] | 3252 | Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( |
| 3253 | FunctionTemplateDecl *FunctionTemplate, |
| 3254 | TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3255 | FunctionDecl *&Specialization, TemplateDeductionInfo &Info, |
| 3256 | bool PartialOverloading) { |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3257 | if (FunctionTemplate->isInvalidDecl()) |
| 3258 | return TDK_Invalid; |
| 3259 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3260 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3261 | unsigned NumParams = Function->getNumParams(); |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3262 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3263 | // C++ [temp.deduct.call]p1: |
| 3264 | // Template argument deduction is done by comparing each function template |
| 3265 | // parameter type (call it P) with the type of the corresponding argument |
| 3266 | // of the call (call it A) as described below. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3267 | unsigned CheckArgs = Args.size(); |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3268 | if (Args.size() < Function->getMinRequiredArguments() && !PartialOverloading) |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3269 | return TDK_TooFewArguments; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3270 | else if (TooManyArguments(NumParams, Args.size(), PartialOverloading)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3271 | const FunctionProtoType *Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3272 | = Function->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3273 | if (Proto->isTemplateVariadic()) |
| 3274 | /* Do nothing */; |
| 3275 | else if (Proto->isVariadic()) |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3276 | CheckArgs = NumParams; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3277 | else |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3278 | return TDK_TooManyArguments; |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3279 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3280 | |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3281 | // The types of the parameters from which we will perform template argument |
| 3282 | // deduction. |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3283 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3284 | TemplateParameterList *TemplateParams |
| 3285 | = FunctionTemplate->getTemplateParameters(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3286 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 3287 | SmallVector<QualType, 4> ParamTypes; |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3288 | unsigned NumExplicitlySpecified = 0; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3289 | if (ExplicitTemplateArgs) { |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3290 | TemplateDeductionResult Result = |
| 3291 | SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3292 | *ExplicitTemplateArgs, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3293 | Deduced, |
| 3294 | ParamTypes, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3295 | nullptr, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3296 | Info); |
| 3297 | if (Result) |
| 3298 | return Result; |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3299 | |
| 3300 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3301 | } else { |
| 3302 | // Just fill in the parameter types from the function declaration. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3303 | for (unsigned I = 0; I != NumParams; ++I) |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3304 | ParamTypes.push_back(Function->getParamDecl(I)->getType()); |
| 3305 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3306 | |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 3307 | // Deduce template arguments from the function parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3308 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3309 | unsigned ArgIdx = 0; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3310 | SmallVector<OriginalCallArg, 4> OriginalCallArgs; |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3311 | for (unsigned ParamIdx = 0, NumParamTypes = ParamTypes.size(); |
| 3312 | ParamIdx != NumParamTypes; ++ParamIdx) { |
Douglas Gregor | dbfb371 | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3313 | QualType OrigParamType = ParamTypes[ParamIdx]; |
| 3314 | QualType ParamType = OrigParamType; |
| 3315 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3316 | const PackExpansionType *ParamExpansion |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3317 | = dyn_cast<PackExpansionType>(ParamType); |
| 3318 | if (!ParamExpansion) { |
| 3319 | // Simple case: matching a function parameter to a function argument. |
| 3320 | if (ArgIdx >= CheckArgs) |
| 3321 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3322 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3323 | Expr *Arg = Args[ArgIdx++]; |
| 3324 | QualType ArgType = Arg->getType(); |
Douglas Gregor | dbfb371 | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3325 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3326 | unsigned TDF = 0; |
| 3327 | if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams, |
| 3328 | ParamType, ArgType, Arg, |
| 3329 | TDF)) |
| 3330 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3331 | |
Douglas Gregor | d8f5b33 | 2011-10-09 22:06:46 +0000 | [diff] [blame] | 3332 | // If we have nothing to deduce, we're done. |
| 3333 | if (!hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
| 3334 | continue; |
| 3335 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3336 | // If the argument is an initializer list ... |
| 3337 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
| 3338 | // ... then the parameter is an undeduced context, unless the parameter |
| 3339 | // type is (reference to cv) std::initializer_list<P'>, in which case |
| 3340 | // deduction is done for each element of the initializer list, and the |
| 3341 | // result is the deduced type if it's the same for all elements. |
| 3342 | QualType X; |
| 3343 | // Removing references was already done. |
| 3344 | if (!isStdInitializerList(ParamType, &X)) |
| 3345 | continue; |
| 3346 | |
| 3347 | for (unsigned i = 0, e = ILE->getNumInits(); i < e; ++i) { |
| 3348 | if (TemplateDeductionResult Result = |
Sebastian Redl | 4b911e6 | 2012-03-15 21:40:51 +0000 | [diff] [blame] | 3349 | DeduceTemplateArgumentByListElement(*this, TemplateParams, X, |
| 3350 | ILE->getInit(i), |
| 3351 | Info, Deduced, TDF)) |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3352 | return Result; |
| 3353 | } |
| 3354 | // Don't track the argument type, since an initializer list has none. |
| 3355 | continue; |
| 3356 | } |
| 3357 | |
Douglas Gregor | dbfb371 | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3358 | // Keep track of the argument type and corresponding parameter index, |
| 3359 | // so we can check for compatibility between the deduced A and A. |
Douglas Gregor | d8f5b33 | 2011-10-09 22:06:46 +0000 | [diff] [blame] | 3360 | OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx-1, |
| 3361 | ArgType)); |
Douglas Gregor | dbfb371 | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3362 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3363 | if (TemplateDeductionResult Result |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3364 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3365 | ParamType, ArgType, |
| 3366 | Info, Deduced, TDF)) |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3367 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3368 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3369 | continue; |
Douglas Gregor | 75f21af | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 3370 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3371 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3372 | // C++0x [temp.deduct.call]p1: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3373 | // For a function parameter pack that occurs at the end of the |
| 3374 | // parameter-declaration-list, the type A of each remaining argument of |
| 3375 | // the call is compared with the type P of the declarator-id of the |
| 3376 | // function parameter pack. Each comparison deduces template arguments |
| 3377 | // for subsequent positions in the template parameter packs expanded by |
Douglas Gregor | 7d5c0c1 | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3378 | // the function parameter pack. For a function parameter pack that does |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3379 | // not occur at the end of the parameter-declaration-list, the type of |
Douglas Gregor | 7d5c0c1 | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3380 | // the parameter pack is a non-deduced context. |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3381 | if (ParamIdx + 1 < NumParamTypes) |
Douglas Gregor | 7d5c0c1 | 2011-01-11 01:52:23 +0000 | [diff] [blame] | 3382 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3383 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3384 | QualType ParamPattern = ParamExpansion->getPattern(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3385 | PackDeductionScope PackScope(*this, TemplateParams, Deduced, Info, |
| 3386 | ParamPattern); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3387 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3388 | bool HasAnyArguments = false; |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3389 | for (; ArgIdx < Args.size(); ++ArgIdx) { |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3390 | HasAnyArguments = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3391 | |
Douglas Gregor | dbfb371 | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3392 | QualType OrigParamType = ParamPattern; |
| 3393 | ParamType = OrigParamType; |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3394 | Expr *Arg = Args[ArgIdx]; |
| 3395 | QualType ArgType = Arg->getType(); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3396 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3397 | unsigned TDF = 0; |
| 3398 | if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams, |
| 3399 | ParamType, ArgType, Arg, |
| 3400 | TDF)) { |
| 3401 | // We can't actually perform any deduction for this argument, so stop |
| 3402 | // deduction at this point. |
| 3403 | ++ArgIdx; |
| 3404 | break; |
| 3405 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3406 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3407 | // As above, initializer lists need special handling. |
| 3408 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) { |
| 3409 | QualType X; |
| 3410 | if (!isStdInitializerList(ParamType, &X)) { |
| 3411 | ++ArgIdx; |
| 3412 | break; |
| 3413 | } |
Douglas Gregor | dbfb371 | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 3414 | |
Sebastian Redl | 84760e3 | 2012-01-17 22:49:58 +0000 | [diff] [blame] | 3415 | for (unsigned i = 0, e = ILE->getNumInits(); i < e; ++i) { |
| 3416 | if (TemplateDeductionResult Result = |
| 3417 | DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, X, |
| 3418 | ILE->getInit(i)->getType(), |
| 3419 | Info, Deduced, TDF)) |
| 3420 | return Result; |
| 3421 | } |
| 3422 | } else { |
| 3423 | |
| 3424 | // Keep track of the argument type and corresponding argument index, |
| 3425 | // so we can check for compatibility between the deduced A and A. |
| 3426 | if (hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) |
| 3427 | OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx, |
| 3428 | ArgType)); |
| 3429 | |
| 3430 | if (TemplateDeductionResult Result |
| 3431 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3432 | ParamType, ArgType, Info, |
| 3433 | Deduced, TDF)) |
| 3434 | return Result; |
| 3435 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3436 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3437 | PackScope.nextPackElement(); |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3438 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3439 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3440 | // Build argument packs for each of the parameter packs expanded by this |
| 3441 | // pack expansion. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3442 | if (auto Result = PackScope.finish(HasAnyArguments)) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3443 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3444 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 3445 | // After we've matching against a parameter pack, we're done. |
| 3446 | break; |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3447 | } |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3448 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3449 | return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3450 | NumExplicitlySpecified, Specialization, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3451 | Info, &OriginalCallArgs, |
| 3452 | PartialOverloading); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 3453 | } |
| 3454 | |
Bill Wendling | 37c07bf | 2013-12-02 02:05:28 +0000 | [diff] [blame] | 3455 | QualType Sema::adjustCCAndNoReturn(QualType ArgFunctionType, |
| 3456 | QualType FunctionType) { |
| 3457 | if (ArgFunctionType.isNull()) |
| 3458 | return ArgFunctionType; |
| 3459 | |
| 3460 | const FunctionProtoType *FunctionTypeP = |
| 3461 | FunctionType->castAs<FunctionProtoType>(); |
| 3462 | CallingConv CC = FunctionTypeP->getCallConv(); |
| 3463 | bool NoReturn = FunctionTypeP->getNoReturnAttr(); |
| 3464 | const FunctionProtoType *ArgFunctionTypeP = |
| 3465 | ArgFunctionType->getAs<FunctionProtoType>(); |
| 3466 | if (ArgFunctionTypeP->getCallConv() == CC && |
| 3467 | ArgFunctionTypeP->getNoReturnAttr() == NoReturn) |
| 3468 | return ArgFunctionType; |
| 3469 | |
| 3470 | FunctionType::ExtInfo EI = ArgFunctionTypeP->getExtInfo().withCallingConv(CC); |
| 3471 | EI = EI.withNoReturn(NoReturn); |
| 3472 | ArgFunctionTypeP = |
| 3473 | cast<FunctionProtoType>(Context.adjustFunctionType(ArgFunctionTypeP, EI)); |
| 3474 | return QualType(ArgFunctionTypeP, 0); |
| 3475 | } |
| 3476 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3477 | /// \brief Deduce template arguments when taking the address of a function |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3478 | /// template (C++ [temp.deduct.funcaddr]) or matching a specialization to |
| 3479 | /// a template. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3480 | /// |
| 3481 | /// \param FunctionTemplate the function template for which we are performing |
| 3482 | /// template argument deduction. |
| 3483 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3484 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3485 | /// arguments. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3486 | /// |
| 3487 | /// \param ArgFunctionType the function type that will be used as the |
| 3488 | /// "argument" type (A) when performing template argument deduction from the |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3489 | /// function template's function type. This type may be NULL, if there is no |
| 3490 | /// argument type to compare against, in C++0x [temp.arg.explicit]p3. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3491 | /// |
| 3492 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3493 | /// this will be set to the function template specialization produced by |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3494 | /// template argument deduction. |
| 3495 | /// |
| 3496 | /// \param Info the argument will be updated to provide additional information |
| 3497 | /// about template argument deduction. |
| 3498 | /// |
| 3499 | /// \returns the result of template argument deduction. |
| 3500 | Sema::TemplateDeductionResult |
| 3501 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3502 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3503 | QualType ArgFunctionType, |
| 3504 | FunctionDecl *&Specialization, |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3505 | TemplateDeductionInfo &Info, |
| 3506 | bool InOverloadResolution) { |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3507 | if (FunctionTemplate->isInvalidDecl()) |
| 3508 | return TDK_Invalid; |
| 3509 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3510 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 3511 | TemplateParameterList *TemplateParams |
| 3512 | = FunctionTemplate->getTemplateParameters(); |
| 3513 | QualType FunctionType = Function->getType(); |
Bill Wendling | 37c07bf | 2013-12-02 02:05:28 +0000 | [diff] [blame] | 3514 | if (!InOverloadResolution) |
| 3515 | ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, FunctionType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3516 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3517 | // Substitute any explicit template arguments. |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3518 | LocalInstantiationScope InstScope(*this); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3519 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3520 | unsigned NumExplicitlySpecified = 0; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3521 | SmallVector<QualType, 4> ParamTypes; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3522 | if (ExplicitTemplateArgs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3523 | if (TemplateDeductionResult Result |
| 3524 | = SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 3525 | *ExplicitTemplateArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3526 | Deduced, ParamTypes, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3527 | &FunctionType, Info)) |
| 3528 | return Result; |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3529 | |
| 3530 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3531 | } |
| 3532 | |
Eli Friedman | 59a839c | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3533 | // Unevaluated SFINAE context. |
| 3534 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3535 | SFINAETrap Trap(*this); |
| 3536 | |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 3537 | Deduced.resize(TemplateParams->size()); |
| 3538 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3539 | // If the function has a deduced return type, substitute it for a dependent |
| 3540 | // type so that we treat it as a non-deduced context in what follows. |
Richard Smith | 37e849a | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3541 | bool HasDeducedReturnType = false; |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3542 | if (getLangOpts().CPlusPlus14 && InOverloadResolution && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3543 | Function->getReturnType()->getContainedAutoType()) { |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3544 | FunctionType = SubstAutoType(FunctionType, Context.DependentTy); |
Richard Smith | 37e849a | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3545 | HasDeducedReturnType = true; |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3546 | } |
| 3547 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3548 | if (!ArgFunctionType.isNull()) { |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3549 | unsigned TDF = TDF_TopLevelParameterTypeList; |
| 3550 | if (InOverloadResolution) TDF |= TDF_InOverloadResolution; |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3551 | // Deduce template arguments from the function type. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3552 | if (TemplateDeductionResult Result |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3553 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3554 | FunctionType, ArgFunctionType, |
| 3555 | Info, Deduced, TDF)) |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3556 | return Result; |
| 3557 | } |
Douglas Gregor | fbb6fad | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3558 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3559 | if (TemplateDeductionResult Result |
Douglas Gregor | fbb6fad | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3560 | = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
| 3561 | NumExplicitlySpecified, |
| 3562 | Specialization, Info)) |
| 3563 | return Result; |
| 3564 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3565 | // If the function has a deduced return type, deduce it now, so we can check |
| 3566 | // that the deduced function type matches the requested type. |
Richard Smith | 37e849a | 2013-08-14 20:16:31 +0000 | [diff] [blame] | 3567 | if (HasDeducedReturnType && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3568 | Specialization->getReturnType()->isUndeducedType() && |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3569 | DeduceReturnType(Specialization, Info.getLocation(), false)) |
| 3570 | return TDK_MiscellaneousDeductionFailure; |
| 3571 | |
Douglas Gregor | fbb6fad | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3572 | // If the requested function type does not match the actual type of the |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3573 | // specialization with respect to arguments of compatible pointer to function |
| 3574 | // types, template argument deduction fails. |
| 3575 | if (!ArgFunctionType.isNull()) { |
| 3576 | if (InOverloadResolution && !isSameOrCompatibleFunctionType( |
| 3577 | Context.getCanonicalType(Specialization->getType()), |
| 3578 | Context.getCanonicalType(ArgFunctionType))) |
| 3579 | return TDK_MiscellaneousDeductionFailure; |
| 3580 | else if(!InOverloadResolution && |
| 3581 | !Context.hasSameType(Specialization->getType(), ArgFunctionType)) |
| 3582 | return TDK_MiscellaneousDeductionFailure; |
| 3583 | } |
Douglas Gregor | fbb6fad | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 3584 | |
| 3585 | return TDK_Success; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3586 | } |
| 3587 | |
Faisal Vali | 56fe35b | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3588 | /// \brief Given a function declaration (e.g. a generic lambda conversion |
| 3589 | /// function) that contains an 'auto' in its result type, substitute it |
Faisal Vali | 9bd1c13 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3590 | /// with TypeToReplaceAutoWith. Be careful to pass in the type you want |
| 3591 | /// to replace 'auto' with and not the actual result type you want |
| 3592 | /// to set the function to. |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3593 | static inline void |
Faisal Vali | 9bd1c13 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3594 | SubstAutoWithinFunctionReturnType(FunctionDecl *F, |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3595 | QualType TypeToReplaceAutoWith, Sema &S) { |
Faisal Vali | 9bd1c13 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3596 | assert(!TypeToReplaceAutoWith->getContainedAutoType()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3597 | QualType AutoResultType = F->getReturnType(); |
Faisal Vali | 56fe35b | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3598 | assert(AutoResultType->getContainedAutoType()); |
| 3599 | QualType DeducedResultType = S.SubstAutoType(AutoResultType, |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3600 | TypeToReplaceAutoWith); |
| 3601 | S.Context.adjustDeducedFunctionResultType(F, DeducedResultType); |
| 3602 | } |
Faisal Vali | 9bd1c13 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3603 | |
| 3604 | /// \brief Given a specialized conversion operator of a generic lambda |
| 3605 | /// create the corresponding specializations of the call operator and |
| 3606 | /// the static-invoker. If the return type of the call operator is auto, |
| 3607 | /// deduce its return type and check if that matches the |
| 3608 | /// return type of the destination function ptr. |
| 3609 | |
| 3610 | static inline Sema::TemplateDeductionResult |
| 3611 | SpecializeCorrespondingLambdaCallOperatorAndInvoker( |
| 3612 | CXXConversionDecl *ConversionSpecialized, |
| 3613 | SmallVectorImpl<DeducedTemplateArgument> &DeducedArguments, |
| 3614 | QualType ReturnTypeOfDestFunctionPtr, |
| 3615 | TemplateDeductionInfo &TDInfo, |
| 3616 | Sema &S) { |
| 3617 | |
| 3618 | CXXRecordDecl *LambdaClass = ConversionSpecialized->getParent(); |
| 3619 | assert(LambdaClass && LambdaClass->isGenericLambda()); |
| 3620 | |
| 3621 | CXXMethodDecl *CallOpGeneric = LambdaClass->getLambdaCallOperator(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3622 | QualType CallOpResultType = CallOpGeneric->getReturnType(); |
Faisal Vali | 9bd1c13 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3623 | const bool GenericLambdaCallOperatorHasDeducedReturnType = |
| 3624 | CallOpResultType->getContainedAutoType(); |
| 3625 | |
| 3626 | FunctionTemplateDecl *CallOpTemplate = |
| 3627 | CallOpGeneric->getDescribedFunctionTemplate(); |
| 3628 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3629 | FunctionDecl *CallOpSpecialized = nullptr; |
Faisal Vali | 9bd1c13 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3630 | // Use the deduced arguments of the conversion function, to specialize our |
| 3631 | // generic lambda's call operator. |
| 3632 | if (Sema::TemplateDeductionResult Result |
| 3633 | = S.FinishTemplateArgumentDeduction(CallOpTemplate, |
| 3634 | DeducedArguments, |
| 3635 | 0, CallOpSpecialized, TDInfo)) |
| 3636 | return Result; |
| 3637 | |
| 3638 | // If we need to deduce the return type, do so (instantiates the callop). |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3639 | if (GenericLambdaCallOperatorHasDeducedReturnType && |
| 3640 | CallOpSpecialized->getReturnType()->isUndeducedType()) |
Faisal Vali | 9bd1c13 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3641 | S.DeduceReturnType(CallOpSpecialized, |
| 3642 | CallOpSpecialized->getPointOfInstantiation(), |
| 3643 | /*Diagnose*/ true); |
| 3644 | |
| 3645 | // Check to see if the return type of the destination ptr-to-function |
| 3646 | // matches the return type of the call operator. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3647 | if (!S.Context.hasSameType(CallOpSpecialized->getReturnType(), |
Faisal Vali | 9bd1c13 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3648 | ReturnTypeOfDestFunctionPtr)) |
| 3649 | return Sema::TDK_NonDeducedMismatch; |
| 3650 | // Since we have succeeded in matching the source and destination |
| 3651 | // ptr-to-functions (now including return type), and have successfully |
| 3652 | // specialized our corresponding call operator, we are ready to |
| 3653 | // specialize the static invoker with the deduced arguments of our |
| 3654 | // ptr-to-function. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3655 | FunctionDecl *InvokerSpecialized = nullptr; |
Faisal Vali | 9bd1c13 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3656 | FunctionTemplateDecl *InvokerTemplate = LambdaClass-> |
| 3657 | getLambdaStaticInvoker()->getDescribedFunctionTemplate(); |
| 3658 | |
| 3659 | Sema::TemplateDeductionResult LLVM_ATTRIBUTE_UNUSED Result |
| 3660 | = S.FinishTemplateArgumentDeduction(InvokerTemplate, DeducedArguments, 0, |
| 3661 | InvokerSpecialized, TDInfo); |
| 3662 | assert(Result == Sema::TDK_Success && |
| 3663 | "If the call operator succeeded so should the invoker!"); |
| 3664 | // Set the result type to match the corresponding call operator |
| 3665 | // specialization's result type. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3666 | if (GenericLambdaCallOperatorHasDeducedReturnType && |
| 3667 | InvokerSpecialized->getReturnType()->isUndeducedType()) { |
Faisal Vali | 9bd1c13 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3668 | // Be sure to get the type to replace 'auto' with and not |
| 3669 | // the full result type of the call op specialization |
| 3670 | // to substitute into the 'auto' of the invoker and conversion |
| 3671 | // function. |
| 3672 | // For e.g. |
| 3673 | // int* (*fp)(int*) = [](auto* a) -> auto* { return a; }; |
| 3674 | // We don't want to subst 'int*' into 'auto' to get int**. |
| 3675 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3676 | QualType TypeToReplaceAutoWith = CallOpSpecialized->getReturnType() |
| 3677 | ->getContainedAutoType() |
| 3678 | ->getDeducedType(); |
Faisal Vali | 9bd1c13 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3679 | SubstAutoWithinFunctionReturnType(InvokerSpecialized, |
| 3680 | TypeToReplaceAutoWith, S); |
| 3681 | SubstAutoWithinFunctionReturnType(ConversionSpecialized, |
| 3682 | TypeToReplaceAutoWith, S); |
| 3683 | } |
| 3684 | |
| 3685 | // Ensure that static invoker doesn't have a const qualifier. |
| 3686 | // FIXME: When creating the InvokerTemplate in SemaLambda.cpp |
| 3687 | // do not use the CallOperator's TypeSourceInfo which allows |
| 3688 | // the const qualifier to leak through. |
| 3689 | const FunctionProtoType *InvokerFPT = InvokerSpecialized-> |
| 3690 | getType().getTypePtr()->castAs<FunctionProtoType>(); |
| 3691 | FunctionProtoType::ExtProtoInfo EPI = InvokerFPT->getExtProtoInfo(); |
| 3692 | EPI.TypeQuals = 0; |
| 3693 | InvokerSpecialized->setType(S.Context.getFunctionType( |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3694 | InvokerFPT->getReturnType(), InvokerFPT->getParamTypes(), EPI)); |
Faisal Vali | 9bd1c13 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3695 | return Sema::TDK_Success; |
| 3696 | } |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3697 | /// \brief Deduce template arguments for a templated conversion |
| 3698 | /// function (C++ [temp.deduct.conv]) and, if successful, produce a |
| 3699 | /// conversion function template specialization. |
| 3700 | Sema::TemplateDeductionResult |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3701 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *ConversionTemplate, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3702 | QualType ToType, |
| 3703 | CXXConversionDecl *&Specialization, |
| 3704 | TemplateDeductionInfo &Info) { |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3705 | if (ConversionTemplate->isInvalidDecl()) |
Douglas Gregor | ae19fbb | 2012-09-13 21:01:57 +0000 | [diff] [blame] | 3706 | return TDK_Invalid; |
| 3707 | |
Faisal Vali | 9bd1c13 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3708 | CXXConversionDecl *ConversionGeneric |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3709 | = cast<CXXConversionDecl>(ConversionTemplate->getTemplatedDecl()); |
| 3710 | |
Faisal Vali | 9bd1c13 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3711 | QualType FromType = ConversionGeneric->getConversionType(); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3712 | |
| 3713 | // Canonicalize the types for deduction. |
| 3714 | QualType P = Context.getCanonicalType(FromType); |
| 3715 | QualType A = Context.getCanonicalType(ToType); |
| 3716 | |
Douglas Gregor | 5453d93 | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3717 | // C++0x [temp.deduct.conv]p2: |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3718 | // If P is a reference type, the type referred to by P is used for |
| 3719 | // type deduction. |
| 3720 | if (const ReferenceType *PRef = P->getAs<ReferenceType>()) |
| 3721 | P = PRef->getPointeeType(); |
| 3722 | |
Douglas Gregor | 5453d93 | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3723 | // C++0x [temp.deduct.conv]p4: |
| 3724 | // [...] If A is a reference type, the type referred to by A is used |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3725 | // for type deduction. |
| 3726 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
Douglas Gregor | 5453d93 | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3727 | A = ARef->getPointeeType().getUnqualifiedType(); |
| 3728 | // C++ [temp.deduct.conv]p3: |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3729 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3730 | // If A is not a reference type: |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3731 | else { |
| 3732 | assert(!A->isReferenceType() && "Reference types were handled above"); |
| 3733 | |
| 3734 | // - If P is an array type, the pointer type produced by the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3735 | // array-to-pointer standard conversion (4.2) is used in place |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3736 | // of P for type deduction; otherwise, |
| 3737 | if (P->isArrayType()) |
| 3738 | P = Context.getArrayDecayedType(P); |
| 3739 | // - If P is a function type, the pointer type produced by the |
| 3740 | // function-to-pointer standard conversion (4.3) is used in |
| 3741 | // place of P for type deduction; otherwise, |
| 3742 | else if (P->isFunctionType()) |
| 3743 | P = Context.getPointerType(P); |
| 3744 | // - If P is a cv-qualified type, the top level cv-qualifiers of |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3745 | // P's type are ignored for type deduction. |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3746 | else |
| 3747 | P = P.getUnqualifiedType(); |
| 3748 | |
Douglas Gregor | 5453d93 | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3749 | // C++0x [temp.deduct.conv]p4: |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3750 | // If A is a cv-qualified type, the top level cv-qualifiers of A's |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3751 | // type are ignored for type deduction. If A is a reference type, the type |
Douglas Gregor | 5453d93 | 2011-03-06 09:03:20 +0000 | [diff] [blame] | 3752 | // referred to by A is used for type deduction. |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3753 | A = A.getUnqualifiedType(); |
| 3754 | } |
| 3755 | |
Eli Friedman | 59a839c | 2012-02-08 03:07:05 +0000 | [diff] [blame] | 3756 | // Unevaluated SFINAE context. |
| 3757 | EnterExpressionEvaluationContext Unevaluated(*this, Sema::Unevaluated); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3758 | SFINAETrap Trap(*this); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3759 | |
| 3760 | // C++ [temp.deduct.conv]p1: |
| 3761 | // Template argument deduction is done by comparing the return |
| 3762 | // type of the template conversion function (call it P) with the |
| 3763 | // type that is required as the result of the conversion (call it |
| 3764 | // A) as described in 14.8.2.4. |
| 3765 | TemplateParameterList *TemplateParams |
Faisal Vali | d6992ab | 2013-09-29 08:45:24 +0000 | [diff] [blame] | 3766 | = ConversionTemplate->getTemplateParameters(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3767 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3768 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3769 | |
| 3770 | // C++0x [temp.deduct.conv]p4: |
| 3771 | // In general, the deduction process attempts to find template |
| 3772 | // argument values that will make the deduced A identical to |
| 3773 | // A. However, there are two cases that allow a difference: |
| 3774 | unsigned TDF = 0; |
| 3775 | // - If the original A is a reference type, A can be more |
| 3776 | // cv-qualified than the deduced A (i.e., the type referred to |
| 3777 | // by the reference) |
| 3778 | if (ToType->isReferenceType()) |
| 3779 | TDF |= TDF_ParamWithReferenceType; |
| 3780 | // - The deduced A can be another pointer or pointer to member |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3781 | // type that can be converted to A via a qualification |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3782 | // conversion. |
| 3783 | // |
| 3784 | // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when |
| 3785 | // both P and A are pointers or member pointers. In this case, we |
| 3786 | // just ignore cv-qualifiers completely). |
| 3787 | if ((P->isPointerType() && A->isPointerType()) || |
Douglas Gregor | 2cae1e2 | 2011-08-30 00:37:54 +0000 | [diff] [blame] | 3788 | (P->isMemberPointerType() && A->isMemberPointerType())) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3789 | TDF |= TDF_IgnoreQualifiers; |
| 3790 | if (TemplateDeductionResult Result |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 3791 | = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, |
| 3792 | P, A, Info, Deduced, TDF)) |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3793 | return Result; |
Faisal Vali | 56fe35b | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3794 | |
| 3795 | // Create an Instantiation Scope for finalizing the operator. |
| 3796 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3797 | // Finish template argument deduction. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3798 | FunctionDecl *ConversionSpecialized = nullptr; |
Faisal Vali | 56fe35b | 2013-09-29 17:08:32 +0000 | [diff] [blame] | 3799 | TemplateDeductionResult Result |
Faisal Vali | 9bd1c13 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3800 | = FinishTemplateArgumentDeduction(ConversionTemplate, Deduced, 0, |
| 3801 | ConversionSpecialized, Info); |
| 3802 | Specialization = cast_or_null<CXXConversionDecl>(ConversionSpecialized); |
| 3803 | |
| 3804 | // If the conversion operator is being invoked on a lambda closure to convert |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3805 | // to a ptr-to-function, use the deduced arguments from the conversion |
| 3806 | // function to specialize the corresponding call operator. |
Faisal Vali | 9bd1c13 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3807 | // e.g., int (*fp)(int) = [](auto a) { return a; }; |
| 3808 | if (Result == TDK_Success && isLambdaConversionOperator(ConversionGeneric)) { |
| 3809 | |
| 3810 | // Get the return type of the destination ptr-to-function we are converting |
| 3811 | // to. This is necessary for matching the lambda call operator's return |
| 3812 | // type to that of the destination ptr-to-function's return type. |
| 3813 | assert(A->isPointerType() && |
| 3814 | "Can only convert from lambda to ptr-to-function"); |
| 3815 | const FunctionType *ToFunType = |
| 3816 | A->getPointeeType().getTypePtr()->getAs<FunctionType>(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3817 | const QualType DestFunctionPtrReturnType = ToFunType->getReturnType(); |
| 3818 | |
Faisal Vali | 9bd1c13 | 2013-10-24 23:40:02 +0000 | [diff] [blame] | 3819 | // Create the corresponding specializations of the call operator and |
| 3820 | // the static-invoker; and if the return type is auto, |
| 3821 | // deduce the return type and check if it matches the |
| 3822 | // DestFunctionPtrReturnType. |
| 3823 | // For instance: |
| 3824 | // auto L = [](auto a) { return f(a); }; |
| 3825 | // int (*fp)(int) = L; |
| 3826 | // char (*fp2)(int) = L; <-- Not OK. |
| 3827 | |
| 3828 | Result = SpecializeCorrespondingLambdaCallOperatorAndInvoker( |
| 3829 | Specialization, Deduced, DestFunctionPtrReturnType, |
| 3830 | Info, *this); |
| 3831 | } |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3832 | return Result; |
| 3833 | } |
| 3834 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3835 | /// \brief Deduce template arguments for a function template when there is |
| 3836 | /// nothing to deduce against (C++0x [temp.arg.explicit]p3). |
| 3837 | /// |
| 3838 | /// \param FunctionTemplate the function template for which we are performing |
| 3839 | /// template argument deduction. |
| 3840 | /// |
James Dennett | 40ae666 | 2012-06-22 08:52:37 +0000 | [diff] [blame] | 3841 | /// \param ExplicitTemplateArgs the explicitly-specified template |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3842 | /// arguments. |
| 3843 | /// |
| 3844 | /// \param Specialization if template argument deduction was successful, |
| 3845 | /// this will be set to the function template specialization produced by |
| 3846 | /// template argument deduction. |
| 3847 | /// |
| 3848 | /// \param Info the argument will be updated to provide additional information |
| 3849 | /// about template argument deduction. |
| 3850 | /// |
| 3851 | /// \returns the result of template argument deduction. |
| 3852 | Sema::TemplateDeductionResult |
| 3853 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 6771423 | 2011-03-03 02:41:12 +0000 | [diff] [blame] | 3854 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3855 | FunctionDecl *&Specialization, |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3856 | TemplateDeductionInfo &Info, |
| 3857 | bool InOverloadResolution) { |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3858 | return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
Douglas Gregor | 092140a | 2013-04-17 08:45:07 +0000 | [diff] [blame] | 3859 | QualType(), Specialization, Info, |
| 3860 | InOverloadResolution); |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 3861 | } |
| 3862 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3863 | namespace { |
| 3864 | /// Substitute the 'auto' type specifier within a type for a given replacement |
| 3865 | /// type. |
| 3866 | class SubstituteAutoTransform : |
| 3867 | public TreeTransform<SubstituteAutoTransform> { |
| 3868 | QualType Replacement; |
| 3869 | public: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 3870 | SubstituteAutoTransform(Sema &SemaRef, QualType Replacement) |
| 3871 | : TreeTransform<SubstituteAutoTransform>(SemaRef), |
| 3872 | Replacement(Replacement) {} |
| 3873 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3874 | QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) { |
| 3875 | // If we're building the type pattern to deduce against, don't wrap the |
| 3876 | // substituted type in an AutoType. Certain template deduction rules |
| 3877 | // apply only when a template type parameter appears directly (and not if |
| 3878 | // the parameter is found through desugaring). For instance: |
| 3879 | // auto &&lref = lvalue; |
| 3880 | // must transform into "rvalue reference to T" not "rvalue reference to |
| 3881 | // auto type deduced as T" in order for [temp.deduct.call]p3 to apply. |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3882 | if (!Replacement.isNull() && isa<TemplateTypeParmType>(Replacement)) { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3883 | QualType Result = Replacement; |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 3884 | TemplateTypeParmTypeLoc NewTL = |
| 3885 | TLB.push<TemplateTypeParmTypeLoc>(Result); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3886 | NewTL.setNameLoc(TL.getNameLoc()); |
| 3887 | return Result; |
| 3888 | } else { |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 3889 | bool Dependent = |
| 3890 | !Replacement.isNull() && Replacement->isDependentType(); |
| 3891 | QualType Result = |
| 3892 | SemaRef.Context.getAutoType(Dependent ? QualType() : Replacement, |
| 3893 | TL.getTypePtr()->isDecltypeAuto(), |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 3894 | Dependent); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3895 | AutoTypeLoc NewTL = TLB.push<AutoTypeLoc>(Result); |
| 3896 | NewTL.setNameLoc(TL.getNameLoc()); |
| 3897 | return Result; |
| 3898 | } |
| 3899 | } |
Douglas Gregor | dfca6f5 | 2012-02-13 22:00:16 +0000 | [diff] [blame] | 3900 | |
| 3901 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 3902 | // Lambdas never need to be transformed. |
| 3903 | return E; |
| 3904 | } |
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3905 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3906 | QualType Apply(TypeLoc TL) { |
| 3907 | // Create some scratch storage for the transformed type locations. |
| 3908 | // FIXME: We're just going to throw this information away. Don't build it. |
| 3909 | TypeLocBuilder TLB; |
| 3910 | TLB.reserve(TL.getFullDataSize()); |
| 3911 | return TransformType(TLB, TL); |
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3912 | } |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3913 | }; |
| 3914 | } |
| 3915 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3916 | Sema::DeduceAutoResult |
| 3917 | Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *&Init, QualType &Result) { |
| 3918 | return DeduceAutoType(Type->getTypeLoc(), Init, Result); |
| 3919 | } |
| 3920 | |
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3921 | /// \brief Deduce the type for an auto type-specifier (C++11 [dcl.spec.auto]p6) |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3922 | /// |
| 3923 | /// \param Type the type pattern using the auto type-specifier. |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3924 | /// \param Init the initializer for the variable whose type is to be deduced. |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3925 | /// \param Result if type deduction was successful, this will be set to the |
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3926 | /// deduced type. |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 3927 | Sema::DeduceAutoResult |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3928 | Sema::DeduceAutoType(TypeLoc Type, Expr *&Init, QualType &Result) { |
John McCall | 32509f1 | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 3929 | if (Init->getType()->isNonOverloadPlaceholderType()) { |
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3930 | ExprResult NonPlaceholder = CheckPlaceholderExpr(Init); |
| 3931 | if (NonPlaceholder.isInvalid()) |
| 3932 | return DAR_FailedAlreadyDiagnosed; |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 3933 | Init = NonPlaceholder.get(); |
John McCall | 32509f1 | 2011-11-15 01:35:18 +0000 | [diff] [blame] | 3934 | } |
| 3935 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3936 | if (Init->isTypeDependent() || Type.getType()->isDependentType()) { |
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3937 | Result = SubstituteAutoTransform(*this, Context.DependentTy).Apply(Type); |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3938 | assert(!Result.isNull() && "substituting DependentTy can't fail"); |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 3939 | return DAR_Succeeded; |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3940 | } |
| 3941 | |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 3942 | // If this is a 'decltype(auto)' specifier, do the decltype dance. |
| 3943 | // Since 'decltype(auto)' can only occur at the top of the type, we |
| 3944 | // don't need to go digging for it. |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3945 | if (const AutoType *AT = Type.getType()->getAs<AutoType>()) { |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 3946 | if (AT->isDecltypeAuto()) { |
| 3947 | if (isa<InitListExpr>(Init)) { |
| 3948 | Diag(Init->getLocStart(), diag::err_decltype_auto_initializer_list); |
| 3949 | return DAR_FailedAlreadyDiagnosed; |
| 3950 | } |
| 3951 | |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 3952 | QualType Deduced = BuildDecltypeType(Init, Init->getLocStart(), false); |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 3953 | // FIXME: Support a non-canonical deduced type for 'auto'. |
| 3954 | Deduced = Context.getCanonicalType(Deduced); |
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3955 | Result = SubstituteAutoTransform(*this, Deduced).Apply(Type); |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 3956 | if (Result.isNull()) |
| 3957 | return DAR_FailedAlreadyDiagnosed; |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 3958 | return DAR_Succeeded; |
| 3959 | } |
| 3960 | } |
| 3961 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3962 | SourceLocation Loc = Init->getExprLoc(); |
| 3963 | |
| 3964 | LocalInstantiationScope InstScope(*this); |
| 3965 | |
| 3966 | // Build template<class TemplParam> void Func(FuncParam); |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3967 | TemplateTypeParmDecl *TemplParam = |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 3968 | TemplateTypeParmDecl::Create(Context, nullptr, SourceLocation(), Loc, 0, 0, |
| 3969 | nullptr, false, false); |
Chandler Carruth | 4fb86f8 | 2011-05-01 00:51:33 +0000 | [diff] [blame] | 3970 | QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0); |
| 3971 | NamedDecl *TemplParamPtr = TemplParam; |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 3972 | FixedSizeTemplateParameterList<1> TemplateParams(Loc, Loc, &TemplParamPtr, |
| 3973 | Loc); |
| 3974 | |
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 3975 | QualType FuncParam = SubstituteAutoTransform(*this, TemplArg).Apply(Type); |
| 3976 | assert(!FuncParam.isNull() && |
| 3977 | "substituting template parameter for 'auto' failed"); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3978 | |
| 3979 | // Deduce type of TemplParam in Func(Init) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3980 | SmallVector<DeducedTemplateArgument, 1> Deduced; |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3981 | Deduced.resize(1); |
| 3982 | QualType InitType = Init->getType(); |
| 3983 | unsigned TDF = 0; |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 3984 | |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 3985 | TemplateDeductionInfo Info(Loc); |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 3986 | |
Richard Smith | 8ad6c86 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 3987 | InitListExpr *InitList = dyn_cast<InitListExpr>(Init); |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 3988 | if (InitList) { |
| 3989 | for (unsigned i = 0, e = InitList->getNumInits(); i < e; ++i) { |
Richard Smith | 8ad6c86 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 3990 | if (DeduceTemplateArgumentByListElement(*this, &TemplateParams, |
Douglas Gregor | d280389 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3991 | TemplArg, |
| 3992 | InitList->getInit(i), |
| 3993 | Info, Deduced, TDF)) |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 3994 | return DAR_Failed; |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 3995 | } |
| 3996 | } else { |
Douglas Gregor | d280389 | 2012-04-04 05:10:53 +0000 | [diff] [blame] | 3997 | if (AdjustFunctionParmAndArgTypesForDeduction(*this, &TemplateParams, |
| 3998 | FuncParam, InitType, Init, |
| 3999 | TDF)) |
| 4000 | return DAR_Failed; |
Richard Smith | 8ad6c86 | 2012-07-08 04:13:07 +0000 | [diff] [blame] | 4001 | |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4002 | if (DeduceTemplateArgumentsByTypeMatch(*this, &TemplateParams, FuncParam, |
| 4003 | InitType, Info, Deduced, TDF)) |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4004 | return DAR_Failed; |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4005 | } |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4006 | |
Eli Friedman | 70a0189 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4007 | if (Deduced[0].getKind() != TemplateArgument::Type) |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4008 | return DAR_Failed; |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4009 | |
Eli Friedman | 70a0189 | 2012-11-06 23:56:42 +0000 | [diff] [blame] | 4010 | QualType DeducedType = Deduced[0].getAsType(); |
| 4011 | |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4012 | if (InitList) { |
| 4013 | DeducedType = BuildStdInitializerList(DeducedType, Loc); |
| 4014 | if (DeducedType.isNull()) |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4015 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4016 | } |
| 4017 | |
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4018 | Result = SubstituteAutoTransform(*this, DeducedType).Apply(Type); |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4019 | if (Result.isNull()) |
| 4020 | return DAR_FailedAlreadyDiagnosed; |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4021 | |
Douglas Gregor | 9a636e8 | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4022 | // Check that the deduced argument type is compatible with the original |
| 4023 | // argument type per C++ [temp.deduct.call]p4. |
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4024 | if (!InitList && !Result.isNull() && |
| 4025 | CheckOriginalCallArgDeduction(*this, |
Douglas Gregor | 9a636e8 | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4026 | Sema::OriginalCallArg(FuncParam,0,InitType), |
Richard Smith | 9b13175 | 2013-04-30 21:23:01 +0000 | [diff] [blame] | 4027 | Result)) { |
| 4028 | Result = QualType(); |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4029 | return DAR_Failed; |
Douglas Gregor | 9a636e8 | 2011-06-17 05:31:46 +0000 | [diff] [blame] | 4030 | } |
| 4031 | |
Sebastian Redl | b832f6d | 2012-01-23 22:09:39 +0000 | [diff] [blame] | 4032 | return DAR_Succeeded; |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4033 | } |
| 4034 | |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4035 | QualType Sema::SubstAutoType(QualType TypeWithAuto, |
| 4036 | QualType TypeToReplaceAuto) { |
| 4037 | return SubstituteAutoTransform(*this, TypeToReplaceAuto). |
| 4038 | TransformType(TypeWithAuto); |
| 4039 | } |
| 4040 | |
| 4041 | TypeSourceInfo* Sema::SubstAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto, |
| 4042 | QualType TypeToReplaceAuto) { |
| 4043 | return SubstituteAutoTransform(*this, TypeToReplaceAuto). |
| 4044 | TransformType(TypeWithAuto); |
Richard Smith | dc7a4f5 | 2013-04-30 13:56:41 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4047 | void Sema::DiagnoseAutoDeductionFailure(VarDecl *VDecl, Expr *Init) { |
| 4048 | if (isa<InitListExpr>(Init)) |
| 4049 | Diag(VDecl->getLocation(), |
Richard Smith | 04fa7a3 | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4050 | VDecl->isInitCapture() |
| 4051 | ? diag::err_init_capture_deduction_failure_from_init_list |
| 4052 | : diag::err_auto_var_deduction_failure_from_init_list) |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4053 | << VDecl->getDeclName() << VDecl->getType() << Init->getSourceRange(); |
| 4054 | else |
Richard Smith | 04fa7a3 | 2013-09-28 04:02:39 +0000 | [diff] [blame] | 4055 | Diag(VDecl->getLocation(), |
| 4056 | VDecl->isInitCapture() ? diag::err_init_capture_deduction_failure |
| 4057 | : diag::err_auto_var_deduction_failure) |
Sebastian Redl | 62b7cfb | 2012-01-17 22:50:08 +0000 | [diff] [blame] | 4058 | << VDecl->getDeclName() << VDecl->getType() << Init->getType() |
| 4059 | << Init->getSourceRange(); |
| 4060 | } |
| 4061 | |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4062 | bool Sema::DeduceReturnType(FunctionDecl *FD, SourceLocation Loc, |
| 4063 | bool Diagnose) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4064 | assert(FD->getReturnType()->isUndeducedType()); |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4065 | |
| 4066 | if (FD->getTemplateInstantiationPattern()) |
| 4067 | InstantiateFunctionDefinition(Loc, FD); |
| 4068 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4069 | bool StillUndeduced = FD->getReturnType()->isUndeducedType(); |
Richard Smith | 60e141e | 2013-05-04 07:00:32 +0000 | [diff] [blame] | 4070 | if (StillUndeduced && Diagnose && !FD->isInvalidDecl()) { |
| 4071 | Diag(Loc, diag::err_auto_fn_used_before_defined) << FD; |
| 4072 | Diag(FD->getLocation(), diag::note_callee_decl) << FD; |
| 4073 | } |
| 4074 | |
| 4075 | return StillUndeduced; |
| 4076 | } |
| 4077 | |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4078 | static void |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4079 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4080 | bool OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4081 | unsigned Level, |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4082 | llvm::SmallBitVector &Deduced); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4083 | |
| 4084 | /// \brief If this is a non-static member function, |
Craig Topper | 1310aac | 2013-07-08 04:13:06 +0000 | [diff] [blame] | 4085 | static void |
| 4086 | AddImplicitObjectParameterType(ASTContext &Context, |
| 4087 | CXXMethodDecl *Method, |
| 4088 | SmallVectorImpl<QualType> &ArgTypes) { |
Eli Friedman | 407c847 | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4089 | // C++11 [temp.func.order]p3: |
| 4090 | // [...] The new parameter is of type "reference to cv A," where cv are |
| 4091 | // the cv-qualifiers of the function template (if any) and A is |
| 4092 | // the class of which the function template is a member. |
Douglas Gregor | 77bc572 | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4093 | // |
Eli Friedman | 407c847 | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4094 | // The standard doesn't say explicitly, but we pick the appropriate kind of |
| 4095 | // reference type based on [over.match.funcs]p4. |
Douglas Gregor | 77bc572 | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4096 | QualType ArgTy = Context.getTypeDeclType(Method->getParent()); |
| 4097 | ArgTy = Context.getQualifiedType(ArgTy, |
| 4098 | Qualifiers::fromCVRMask(Method->getTypeQualifiers())); |
Eli Friedman | 407c847 | 2012-09-19 23:52:13 +0000 | [diff] [blame] | 4099 | if (Method->getRefQualifier() == RQ_RValue) |
| 4100 | ArgTy = Context.getRValueReferenceType(ArgTy); |
| 4101 | else |
| 4102 | ArgTy = Context.getLValueReferenceType(ArgTy); |
Douglas Gregor | 77bc572 | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 4103 | ArgTypes.push_back(ArgTy); |
| 4104 | } |
| 4105 | |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4106 | /// \brief Determine whether the function template \p FT1 is at least as |
| 4107 | /// specialized as \p FT2. |
| 4108 | static bool isAtLeastAsSpecializedAs(Sema &S, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4109 | SourceLocation Loc, |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4110 | FunctionTemplateDecl *FT1, |
| 4111 | FunctionTemplateDecl *FT2, |
| 4112 | TemplatePartialOrderingContext TPOC, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4113 | unsigned NumCallArguments1) { |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4114 | FunctionDecl *FD1 = FT1->getTemplatedDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4115 | FunctionDecl *FD2 = FT2->getTemplatedDecl(); |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4116 | const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); |
| 4117 | const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4118 | |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4119 | assert(Proto1 && Proto2 && "Function templates must have prototypes"); |
| 4120 | TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4121 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4122 | Deduced.resize(TemplateParams->size()); |
| 4123 | |
| 4124 | // C++0x [temp.deduct.partial]p3: |
| 4125 | // The types used to determine the ordering depend on the context in which |
| 4126 | // the partial ordering is done: |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4127 | TemplateDeductionInfo Info(Loc); |
Richard Smith | 66118c2 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4128 | SmallVector<QualType, 4> Args2; |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4129 | switch (TPOC) { |
| 4130 | case TPOC_Call: { |
| 4131 | // - In the context of a function call, the function parameter types are |
| 4132 | // used. |
Richard Smith | 66118c2 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4133 | CXXMethodDecl *Method1 = dyn_cast<CXXMethodDecl>(FD1); |
| 4134 | CXXMethodDecl *Method2 = dyn_cast<CXXMethodDecl>(FD2); |
Douglas Gregor | 8d706ec | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4135 | |
Eli Friedman | 9cef006 | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4136 | // C++11 [temp.func.order]p3: |
Douglas Gregor | 8d706ec | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 4137 | // [...] If only one of the function templates is a non-static |
| 4138 | // member, that function template is considered to have a new |
| 4139 | // first parameter inserted in its function parameter list. The |
| 4140 | // new parameter is of type "reference to cv A," where cv are |
| 4141 | // the cv-qualifiers of the function template (if any) and A is |
| 4142 | // the class of which the function template is a member. |
| 4143 | // |
Eli Friedman | 9cef006 | 2012-09-19 23:27:04 +0000 | [diff] [blame] | 4144 | // Note that we interpret this to mean "if one of the function |
| 4145 | // templates is a non-static member and the other is a non-member"; |
| 4146 | // otherwise, the ordering rules for static functions against non-static |
| 4147 | // functions don't make any sense. |
| 4148 | // |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4149 | // C++98/03 doesn't have this provision but we've extended DR532 to cover |
| 4150 | // it as wording was broken prior to it. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4151 | SmallVector<QualType, 4> Args1; |
Richard Smith | 66118c2 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4152 | |
Richard Smith | 66118c2 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4153 | unsigned NumComparedArguments = NumCallArguments1; |
| 4154 | |
| 4155 | if (!Method2 && Method1 && !Method1->isStatic()) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4156 | // Compare 'this' from Method1 against first parameter from Method2. |
| 4157 | AddImplicitObjectParameterType(S.Context, Method1, Args1); |
| 4158 | ++NumComparedArguments; |
Richard Smith | 66118c2 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4159 | } else if (!Method1 && Method2 && !Method2->isStatic()) { |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4160 | // Compare 'this' from Method2 against first parameter from Method1. |
| 4161 | AddImplicitObjectParameterType(S.Context, Method2, Args2); |
Richard Smith | 66118c2 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4162 | } |
| 4163 | |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4164 | Args1.insert(Args1.end(), Proto1->param_type_begin(), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4165 | Proto1->param_type_end()); |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4166 | Args2.insert(Args2.end(), Proto2->param_type_begin(), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4167 | Proto2->param_type_end()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4168 | |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4169 | // C++ [temp.func.order]p5: |
| 4170 | // The presence of unused ellipsis and default arguments has no effect on |
| 4171 | // the partial ordering of function templates. |
Richard Smith | 66118c2 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4172 | if (Args1.size() > NumComparedArguments) |
| 4173 | Args1.resize(NumComparedArguments); |
| 4174 | if (Args2.size() > NumComparedArguments) |
| 4175 | Args2.resize(NumComparedArguments); |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4176 | if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(), |
| 4177 | Args1.data(), Args1.size(), Info, Deduced, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4178 | TDF_None, /*PartialOrdering=*/true)) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 4179 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4180 | |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4181 | break; |
| 4182 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4183 | |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4184 | case TPOC_Conversion: |
| 4185 | // - In the context of a call to a conversion operator, the return types |
| 4186 | // of the conversion function templates are used. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4187 | if (DeduceTemplateArgumentsByTypeMatch( |
| 4188 | S, TemplateParams, Proto2->getReturnType(), Proto1->getReturnType(), |
| 4189 | Info, Deduced, TDF_None, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4190 | /*PartialOrdering=*/true)) |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4191 | return false; |
| 4192 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4193 | |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4194 | case TPOC_Other: |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 4195 | // - In other contexts (14.6.6.2) the function template's function type |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4196 | // is used. |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4197 | if (DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, |
| 4198 | FD2->getType(), FD1->getType(), |
| 4199 | Info, Deduced, TDF_None, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4200 | /*PartialOrdering=*/true)) |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4201 | return false; |
| 4202 | break; |
| 4203 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4204 | |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4205 | // C++0x [temp.deduct.partial]p11: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4206 | // In most cases, all template parameters must have values in order for |
| 4207 | // deduction to succeed, but for partial ordering purposes a template |
| 4208 | // parameter may remain without a value provided it is not used in the |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4209 | // types being used for partial ordering. [ Note: a template parameter used |
| 4210 | // in a non-deduced context is considered used. -end note] |
| 4211 | unsigned ArgIdx = 0, NumArgs = Deduced.size(); |
| 4212 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4213 | if (Deduced[ArgIdx].isNull()) |
| 4214 | break; |
| 4215 | |
| 4216 | if (ArgIdx == NumArgs) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4217 | // All template arguments were deduced. FT1 is at least as specialized |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4218 | // as FT2. |
| 4219 | return true; |
| 4220 | } |
| 4221 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4222 | // Figure out which template parameters were used. |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4223 | llvm::SmallBitVector UsedParameters(TemplateParams->size()); |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4224 | switch (TPOC) { |
Richard Smith | 66118c2 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4225 | case TPOC_Call: |
| 4226 | for (unsigned I = 0, N = Args2.size(); I != N; ++I) |
| 4227 | ::MarkUsedTemplateParameters(S.Context, Args2[I], false, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4228 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4229 | UsedParameters); |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4230 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4231 | |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4232 | case TPOC_Conversion: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4233 | ::MarkUsedTemplateParameters(S.Context, Proto2->getReturnType(), false, |
| 4234 | TemplateParams->getDepth(), UsedParameters); |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4235 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4236 | |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4237 | case TPOC_Other: |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4238 | ::MarkUsedTemplateParameters(S.Context, FD2->getType(), false, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4239 | TemplateParams->getDepth(), |
| 4240 | UsedParameters); |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4241 | break; |
| 4242 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4243 | |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4244 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 4245 | // If this argument had no value deduced but was used in one of the types |
| 4246 | // used for partial ordering, then deduction fails. |
| 4247 | if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) |
| 4248 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4249 | |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4250 | return true; |
| 4251 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4252 | |
Douglas Gregor | 9da95e6 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4253 | /// \brief Determine whether this a function template whose parameter-type-list |
| 4254 | /// ends with a function parameter pack. |
| 4255 | static bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) { |
| 4256 | FunctionDecl *Function = FunTmpl->getTemplatedDecl(); |
| 4257 | unsigned NumParams = Function->getNumParams(); |
| 4258 | if (NumParams == 0) |
| 4259 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4260 | |
Douglas Gregor | 9da95e6 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4261 | ParmVarDecl *Last = Function->getParamDecl(NumParams - 1); |
| 4262 | if (!Last->isParameterPack()) |
| 4263 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4264 | |
Douglas Gregor | 9da95e6 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4265 | // Make sure that no previous parameter is a parameter pack. |
| 4266 | while (--NumParams > 0) { |
| 4267 | if (Function->getParamDecl(NumParams - 1)->isParameterPack()) |
| 4268 | return false; |
| 4269 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4270 | |
Douglas Gregor | 9da95e6 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4271 | return true; |
| 4272 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4273 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4274 | /// \brief Returns the more specialized function template according |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4275 | /// to the rules of function template partial ordering (C++ [temp.func.order]). |
| 4276 | /// |
| 4277 | /// \param FT1 the first function template |
| 4278 | /// |
| 4279 | /// \param FT2 the second function template |
| 4280 | /// |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4281 | /// \param TPOC the context in which we are performing partial ordering of |
| 4282 | /// function templates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4283 | /// |
Richard Smith | 66118c2 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4284 | /// \param NumCallArguments1 The number of arguments in the call to FT1, used |
| 4285 | /// only when \c TPOC is \c TPOC_Call. |
| 4286 | /// |
| 4287 | /// \param NumCallArguments2 The number of arguments in the call to FT2, used |
| 4288 | /// only when \c TPOC is \c TPOC_Call. |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4289 | /// |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4290 | /// \returns the more specialized function template. If neither |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4291 | /// template is more specialized, returns NULL. |
| 4292 | FunctionTemplateDecl * |
| 4293 | Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, |
| 4294 | FunctionTemplateDecl *FT2, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4295 | SourceLocation Loc, |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4296 | TemplatePartialOrderingContext TPOC, |
Richard Smith | 66118c2 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4297 | unsigned NumCallArguments1, |
| 4298 | unsigned NumCallArguments2) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4299 | bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4300 | NumCallArguments1); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4301 | bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4302 | NumCallArguments2); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4303 | |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4304 | if (Better1 != Better2) // We have a clear winner |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4305 | return Better1 ? FT1 : FT2; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4306 | |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4307 | if (!Better1 && !Better2) // Neither is better than the other |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4308 | return nullptr; |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 4309 | |
Douglas Gregor | 9da95e6 | 2011-01-16 16:03:23 +0000 | [diff] [blame] | 4310 | // FIXME: This mimics what GCC implements, but doesn't match up with the |
| 4311 | // proposed resolution for core issue 692. This area needs to be sorted out, |
| 4312 | // but for now we attempt to maintain compatibility. |
| 4313 | bool Variadic1 = isVariadicFunctionTemplate(FT1); |
| 4314 | bool Variadic2 = isVariadicFunctionTemplate(FT2); |
| 4315 | if (Variadic1 != Variadic2) |
| 4316 | return Variadic1? FT2 : FT1; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4317 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4318 | return nullptr; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 4319 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 4320 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4321 | /// \brief Determine if the two templates are equivalent. |
| 4322 | static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) { |
| 4323 | if (T1 == T2) |
| 4324 | return true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4325 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4326 | if (!T1 || !T2) |
| 4327 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4328 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4329 | return T1->getCanonicalDecl() == T2->getCanonicalDecl(); |
| 4330 | } |
| 4331 | |
| 4332 | /// \brief Retrieve the most specialized of the given function template |
| 4333 | /// specializations. |
| 4334 | /// |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4335 | /// \param SpecBegin the start iterator of the function template |
| 4336 | /// specializations that we will be comparing. |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4337 | /// |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4338 | /// \param SpecEnd the end iterator of the function template |
| 4339 | /// specializations, paired with \p SpecBegin. |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4340 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4341 | /// \param Loc the location where the ambiguity or no-specializations |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4342 | /// diagnostic should occur. |
| 4343 | /// |
| 4344 | /// \param NoneDiag partial diagnostic used to diagnose cases where there are |
| 4345 | /// no matching candidates. |
| 4346 | /// |
| 4347 | /// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one |
| 4348 | /// occurs. |
| 4349 | /// |
| 4350 | /// \param CandidateDiag partial diagnostic used for each function template |
| 4351 | /// specialization that is a candidate in the ambiguous ordering. One parameter |
| 4352 | /// in this diagnostic should be unbound, which will correspond to the string |
| 4353 | /// describing the template arguments for the function template specialization. |
| 4354 | /// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4355 | /// \returns the most specialized function template specialization, if |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4356 | /// found. Otherwise, returns SpecEnd. |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4357 | UnresolvedSetIterator Sema::getMostSpecialized( |
| 4358 | UnresolvedSetIterator SpecBegin, UnresolvedSetIterator SpecEnd, |
| 4359 | TemplateSpecCandidateSet &FailedCandidates, |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4360 | SourceLocation Loc, const PartialDiagnostic &NoneDiag, |
| 4361 | const PartialDiagnostic &AmbigDiag, const PartialDiagnostic &CandidateDiag, |
| 4362 | bool Complain, QualType TargetType) { |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4363 | if (SpecBegin == SpecEnd) { |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4364 | if (Complain) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4365 | Diag(Loc, NoneDiag); |
Larisse Voufo | 4384712 | 2013-07-19 23:00:19 +0000 | [diff] [blame] | 4366 | FailedCandidates.NoteCandidates(*this, Loc); |
| 4367 | } |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4368 | return SpecEnd; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4369 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4370 | |
| 4371 | if (SpecBegin + 1 == SpecEnd) |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4372 | return SpecBegin; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4373 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4374 | // Find the function template that is better than all of the templates it |
| 4375 | // has been compared to. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4376 | UnresolvedSetIterator Best = SpecBegin; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4377 | FunctionTemplateDecl *BestTemplate |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4378 | = cast<FunctionDecl>(*Best)->getPrimaryTemplate(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4379 | assert(BestTemplate && "Not a function template specialization?"); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4380 | for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) { |
| 4381 | FunctionTemplateDecl *Challenger |
| 4382 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4383 | assert(Challenger && "Not a function template specialization?"); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4384 | if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | 66118c2 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4385 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4386 | Challenger)) { |
| 4387 | Best = I; |
| 4388 | BestTemplate = Challenger; |
| 4389 | } |
| 4390 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4391 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4392 | // Make sure that the "best" function template is more specialized than all |
| 4393 | // of the others. |
| 4394 | bool Ambiguous = false; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4395 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4396 | FunctionTemplateDecl *Challenger |
| 4397 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4398 | if (I != Best && |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4399 | !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Richard Smith | 66118c2 | 2013-09-11 00:52:39 +0000 | [diff] [blame] | 4400 | Loc, TPOC_Other, 0, 0), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4401 | BestTemplate)) { |
| 4402 | Ambiguous = true; |
| 4403 | break; |
| 4404 | } |
| 4405 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4406 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4407 | if (!Ambiguous) { |
| 4408 | // We found an answer. Return it. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4409 | return Best; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4410 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4411 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4412 | // Diagnose the ambiguity. |
Richard Smith | df6217e | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4413 | if (Complain) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4414 | Diag(Loc, AmbigDiag); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4415 | |
Richard Smith | df6217e | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4416 | // FIXME: Can we order the candidates in some sane way? |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4417 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 4418 | PartialDiagnostic PD = CandidateDiag; |
| 4419 | PD << getTemplateArgumentBindingsText( |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4420 | cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(), |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4421 | *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs()); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 4422 | if (!TargetType.isNull()) |
| 4423 | HandleFunctionTypeMismatch(PD, cast<FunctionDecl>(*I)->getType(), |
| 4424 | TargetType); |
| 4425 | Diag((*I)->getLocation(), PD); |
| 4426 | } |
Richard Smith | df6217e | 2013-05-04 01:51:08 +0000 | [diff] [blame] | 4427 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4428 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4429 | return SpecEnd; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 4430 | } |
| 4431 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4432 | /// \brief Returns the more specialized class template partial specialization |
| 4433 | /// according to the rules of partial ordering of class template partial |
| 4434 | /// specializations (C++ [temp.class.order]). |
| 4435 | /// |
| 4436 | /// \param PS1 the first class template partial specialization |
| 4437 | /// |
| 4438 | /// \param PS2 the second class template partial specialization |
| 4439 | /// |
| 4440 | /// \returns the more specialized class template partial specialization. If |
| 4441 | /// neither partial specialization is more specialized, returns NULL. |
| 4442 | ClassTemplatePartialSpecializationDecl * |
| 4443 | Sema::getMoreSpecializedPartialSpecialization( |
| 4444 | ClassTemplatePartialSpecializationDecl *PS1, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4445 | ClassTemplatePartialSpecializationDecl *PS2, |
| 4446 | SourceLocation Loc) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4447 | // C++ [temp.class.order]p1: |
| 4448 | // For two class template partial specializations, the first is at least as |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4449 | // specialized as the second if, given the following rewrite to two |
| 4450 | // function templates, the first function template is at least as |
| 4451 | // specialized as the second according to the ordering rules for function |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4452 | // templates (14.6.6.2): |
| 4453 | // - the first function template has the same template parameters as the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4454 | // first partial specialization and has a single function parameter |
| 4455 | // whose type is a class template specialization with the template |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4456 | // arguments of the first partial specialization, and |
| 4457 | // - the second function template has the same template parameters as the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4458 | // second partial specialization and has a single function parameter |
| 4459 | // whose type is a class template specialization with the template |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4460 | // arguments of the second partial specialization. |
| 4461 | // |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 4462 | // Rather than synthesize function templates, we merely perform the |
| 4463 | // equivalent partial ordering by performing deduction directly on |
| 4464 | // the template arguments of the class template partial |
| 4465 | // specializations. This computation is slightly simpler than the |
| 4466 | // general problem of function template partial ordering, because |
| 4467 | // class template partial specializations are more constrained. We |
| 4468 | // know that every template parameter is deducible from the class |
| 4469 | // template partial specialization's template arguments, for |
| 4470 | // example. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4471 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
Craig Topper | 93e4599 | 2012-09-19 02:26:47 +0000 | [diff] [blame] | 4472 | TemplateDeductionInfo Info(Loc); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4473 | |
| 4474 | QualType PT1 = PS1->getInjectedSpecializationType(); |
| 4475 | QualType PT2 = PS2->getInjectedSpecializationType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4476 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4477 | // Determine whether PS1 is at least as specialized as PS2 |
| 4478 | Deduced.resize(PS2->getTemplateParameters()->size()); |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4479 | bool Better1 = !DeduceTemplateArgumentsByTypeMatch(*this, |
| 4480 | PS2->getTemplateParameters(), |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4481 | PT2, PT1, Info, Deduced, TDF_None, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4482 | /*PartialOrdering=*/true); |
Argyrios Kyrtzidis | 2c4792c | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4483 | if (Better1) { |
Richard Smith | 7e54fb5 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 4484 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(),Deduced.end()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4485 | InstantiatingTemplate Inst(*this, Loc, PS2, DeducedArgs, Info); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4486 | Better1 = !::FinishTemplateArgumentDeduction( |
| 4487 | *this, PS2, PS1->getTemplateArgs(), Deduced, Info); |
| 4488 | } |
| 4489 | |
| 4490 | // Determine whether PS2 is at least as specialized as PS1 |
| 4491 | Deduced.clear(); |
| 4492 | Deduced.resize(PS1->getTemplateParameters()->size()); |
| 4493 | bool Better2 = !DeduceTemplateArgumentsByTypeMatch( |
| 4494 | *this, PS1->getTemplateParameters(), PT1, PT2, Info, Deduced, TDF_None, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4495 | /*PartialOrdering=*/true); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4496 | if (Better2) { |
| 4497 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), |
| 4498 | Deduced.end()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4499 | InstantiatingTemplate Inst(*this, Loc, PS1, DeducedArgs, Info); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4500 | Better2 = !::FinishTemplateArgumentDeduction( |
| 4501 | *this, PS1, PS2->getTemplateArgs(), Deduced, Info); |
| 4502 | } |
| 4503 | |
| 4504 | if (Better1 == Better2) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4505 | return nullptr; |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4506 | |
| 4507 | return Better1 ? PS1 : PS2; |
| 4508 | } |
| 4509 | |
Larisse Voufo | 8d2a5ea | 2013-08-23 22:21:36 +0000 | [diff] [blame] | 4510 | /// TODO: Unify with ClassTemplatePartialSpecializationDecl version? |
| 4511 | /// May require unifying ClassTemplate(Partial)SpecializationDecl and |
| 4512 | /// VarTemplate(Partial)SpecializationDecl with a new data |
| 4513 | /// structure Template(Partial)SpecializationDecl, and |
| 4514 | /// using Template(Partial)SpecializationDecl as input type. |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4515 | VarTemplatePartialSpecializationDecl * |
| 4516 | Sema::getMoreSpecializedPartialSpecialization( |
| 4517 | VarTemplatePartialSpecializationDecl *PS1, |
| 4518 | VarTemplatePartialSpecializationDecl *PS2, SourceLocation Loc) { |
| 4519 | SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 4520 | TemplateDeductionInfo Info(Loc); |
| 4521 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4522 | assert(PS1->getSpecializedTemplate() == PS2->getSpecializedTemplate() && |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4523 | "the partial specializations being compared should specialize" |
| 4524 | " the same template."); |
| 4525 | TemplateName Name(PS1->getSpecializedTemplate()); |
| 4526 | TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); |
| 4527 | QualType PT1 = Context.getTemplateSpecializationType( |
| 4528 | CanonTemplate, PS1->getTemplateArgs().data(), |
| 4529 | PS1->getTemplateArgs().size()); |
| 4530 | QualType PT2 = Context.getTemplateSpecializationType( |
| 4531 | CanonTemplate, PS2->getTemplateArgs().data(), |
| 4532 | PS2->getTemplateArgs().size()); |
| 4533 | |
| 4534 | // Determine whether PS1 is at least as specialized as PS2 |
| 4535 | Deduced.resize(PS2->getTemplateParameters()->size()); |
| 4536 | bool Better1 = !DeduceTemplateArgumentsByTypeMatch( |
| 4537 | *this, PS2->getTemplateParameters(), PT2, PT1, Info, Deduced, TDF_None, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4538 | /*PartialOrdering=*/true); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 4539 | if (Better1) { |
| 4540 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), |
| 4541 | Deduced.end()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4542 | InstantiatingTemplate Inst(*this, Loc, PS2, DeducedArgs, Info); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4543 | Better1 = !::FinishTemplateArgumentDeduction(*this, PS2, |
| 4544 | PS1->getTemplateArgs(), |
Douglas Gregor | 516e6e0 | 2010-04-29 06:31:36 +0000 | [diff] [blame] | 4545 | Deduced, Info); |
Argyrios Kyrtzidis | 2c4792c | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4546 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4547 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4548 | // Determine whether PS2 is at least as specialized as PS1 |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 4549 | Deduced.clear(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4550 | Deduced.resize(PS1->getTemplateParameters()->size()); |
Sebastian Redl | bb95e51 | 2012-01-17 22:49:52 +0000 | [diff] [blame] | 4551 | bool Better2 = !DeduceTemplateArgumentsByTypeMatch(*this, |
| 4552 | PS1->getTemplateParameters(), |
Douglas Gregor | 5c7bf42 | 2011-01-11 17:34:58 +0000 | [diff] [blame] | 4553 | PT1, PT2, Info, Deduced, TDF_None, |
Stephen Hines | 0e2c34f | 2015-03-23 12:09:02 -0700 | [diff] [blame] | 4554 | /*PartialOrdering=*/true); |
Argyrios Kyrtzidis | 2c4792c | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4555 | if (Better2) { |
Richard Smith | 7e54fb5 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 4556 | SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(),Deduced.end()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4557 | InstantiatingTemplate Inst(*this, Loc, PS1, DeducedArgs, Info); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4558 | Better2 = !::FinishTemplateArgumentDeduction(*this, PS1, |
| 4559 | PS2->getTemplateArgs(), |
Douglas Gregor | 516e6e0 | 2010-04-29 06:31:36 +0000 | [diff] [blame] | 4560 | Deduced, Info); |
Argyrios Kyrtzidis | 2c4792c | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 4561 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4562 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4563 | if (Better1 == Better2) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 4564 | return nullptr; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4565 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 4566 | return Better1? PS1 : PS2; |
| 4567 | } |
| 4568 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4569 | static void |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4570 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4571 | const TemplateArgument &TemplateArg, |
| 4572 | bool OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4573 | unsigned Depth, |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4574 | llvm::SmallBitVector &Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4575 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4576 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4577 | /// expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4578 | static void |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4579 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4580 | const Expr *E, |
| 4581 | bool OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4582 | unsigned Depth, |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4583 | llvm::SmallBitVector &Used) { |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 4584 | // We can deduce from a pack expansion. |
| 4585 | if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E)) |
| 4586 | E = Expansion->getPattern(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4587 | |
Richard Smith | 6098381 | 2012-07-09 03:07:20 +0000 | [diff] [blame] | 4588 | // Skip through any implicit casts we added while type-checking, and any |
| 4589 | // substitutions performed by template alias expansion. |
| 4590 | while (1) { |
| 4591 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 4592 | E = ICE->getSubExpr(); |
| 4593 | else if (const SubstNonTypeTemplateParmExpr *Subst = |
| 4594 | dyn_cast<SubstNonTypeTemplateParmExpr>(E)) |
| 4595 | E = Subst->getReplacement(); |
| 4596 | else |
| 4597 | break; |
| 4598 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4599 | |
| 4600 | // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4601 | // find other occurrences of template parameters. |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4602 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
Douglas Gregor | c781f9c | 2010-01-14 18:13:22 +0000 | [diff] [blame] | 4603 | if (!DRE) |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4604 | return; |
| 4605 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4606 | const NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4607 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 4608 | if (!NTTP) |
| 4609 | return; |
| 4610 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4611 | if (NTTP->getDepth() == Depth) |
| 4612 | Used[NTTP->getIndex()] = true; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4613 | } |
| 4614 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4615 | /// \brief Mark the template parameters that are used by the given |
| 4616 | /// nested name specifier. |
| 4617 | static void |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4618 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4619 | NestedNameSpecifier *NNS, |
| 4620 | bool OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4621 | unsigned Depth, |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4622 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4623 | if (!NNS) |
| 4624 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4625 | |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4626 | MarkUsedTemplateParameters(Ctx, NNS->getPrefix(), OnlyDeduced, Depth, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4627 | Used); |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4628 | MarkUsedTemplateParameters(Ctx, QualType(NNS->getAsType(), 0), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4629 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4630 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4631 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4632 | /// \brief Mark the template parameters that are used by the given |
| 4633 | /// template name. |
| 4634 | static void |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4635 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4636 | TemplateName Name, |
| 4637 | bool OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4638 | unsigned Depth, |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4639 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4640 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 4641 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4642 | = dyn_cast<TemplateTemplateParmDecl>(Template)) { |
| 4643 | if (TTP->getDepth() == Depth) |
| 4644 | Used[TTP->getIndex()] = true; |
| 4645 | } |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4646 | return; |
| 4647 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4648 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4649 | if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4650 | MarkUsedTemplateParameters(Ctx, QTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4651 | Depth, Used); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4652 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4653 | MarkUsedTemplateParameters(Ctx, DTN->getQualifier(), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4654 | Depth, Used); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4655 | } |
| 4656 | |
| 4657 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4658 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4659 | static void |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4660 | MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4661 | bool OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4662 | unsigned Depth, |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4663 | llvm::SmallBitVector &Used) { |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4664 | if (T.isNull()) |
| 4665 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4666 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4667 | // Non-dependent types have nothing deducible |
| 4668 | if (!T->isDependentType()) |
| 4669 | return; |
| 4670 | |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4671 | T = Ctx.getCanonicalType(T); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4672 | switch (T->getTypeClass()) { |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4673 | case Type::Pointer: |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4674 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4675 | cast<PointerType>(T)->getPointeeType(), |
| 4676 | OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4677 | Depth, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4678 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4679 | break; |
| 4680 | |
| 4681 | case Type::BlockPointer: |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4682 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4683 | cast<BlockPointerType>(T)->getPointeeType(), |
| 4684 | OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4685 | Depth, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4686 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4687 | break; |
| 4688 | |
| 4689 | case Type::LValueReference: |
| 4690 | case Type::RValueReference: |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4691 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4692 | cast<ReferenceType>(T)->getPointeeType(), |
| 4693 | OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4694 | Depth, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4695 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4696 | break; |
| 4697 | |
| 4698 | case Type::MemberPointer: { |
| 4699 | const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4700 | MarkUsedTemplateParameters(Ctx, MemPtr->getPointeeType(), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4701 | Depth, Used); |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4702 | MarkUsedTemplateParameters(Ctx, QualType(MemPtr->getClass(), 0), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4703 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4704 | break; |
| 4705 | } |
| 4706 | |
| 4707 | case Type::DependentSizedArray: |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4708 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4709 | cast<DependentSizedArrayType>(T)->getSizeExpr(), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4710 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4711 | // Fall through to check the element type |
| 4712 | |
| 4713 | case Type::ConstantArray: |
| 4714 | case Type::IncompleteArray: |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4715 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4716 | cast<ArrayType>(T)->getElementType(), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4717 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4718 | break; |
| 4719 | |
| 4720 | case Type::Vector: |
| 4721 | case Type::ExtVector: |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4722 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4723 | cast<VectorType>(T)->getElementType(), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4724 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4725 | break; |
| 4726 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4727 | case Type::DependentSizedExtVector: { |
| 4728 | const DependentSizedExtVectorType *VecType |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4729 | = cast<DependentSizedExtVectorType>(T); |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4730 | MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4731 | Depth, Used); |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4732 | MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4733 | Depth, Used); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 4734 | break; |
| 4735 | } |
| 4736 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4737 | case Type::FunctionProto: { |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4738 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4739 | MarkUsedTemplateParameters(Ctx, Proto->getReturnType(), OnlyDeduced, Depth, |
| 4740 | Used); |
| 4741 | for (unsigned I = 0, N = Proto->getNumParams(); I != N; ++I) |
| 4742 | MarkUsedTemplateParameters(Ctx, Proto->getParamType(I), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4743 | Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4744 | break; |
| 4745 | } |
| 4746 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4747 | case Type::TemplateTypeParm: { |
| 4748 | const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T); |
| 4749 | if (TTP->getDepth() == Depth) |
| 4750 | Used[TTP->getIndex()] = true; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4751 | break; |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4752 | } |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4753 | |
Douglas Gregor | 0bc15d9 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4754 | case Type::SubstTemplateTypeParmPack: { |
| 4755 | const SubstTemplateTypeParmPackType *Subst |
| 4756 | = cast<SubstTemplateTypeParmPackType>(T); |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4757 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | 0bc15d9 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4758 | QualType(Subst->getReplacedParameter(), 0), |
| 4759 | OnlyDeduced, Depth, Used); |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4760 | MarkUsedTemplateParameters(Ctx, Subst->getArgumentPack(), |
Douglas Gregor | 0bc15d9 | 2011-01-14 05:11:40 +0000 | [diff] [blame] | 4761 | OnlyDeduced, Depth, Used); |
| 4762 | break; |
| 4763 | } |
| 4764 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 4765 | case Type::InjectedClassName: |
| 4766 | T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); |
| 4767 | // fall through |
| 4768 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4769 | case Type::TemplateSpecialization: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4770 | const TemplateSpecializationType *Spec |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 4771 | = cast<TemplateSpecializationType>(T); |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4772 | MarkUsedTemplateParameters(Ctx, Spec->getTemplateName(), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4773 | Depth, Used); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4774 | |
Douglas Gregor | 7b976ec | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4775 | // C++0x [temp.deduct.type]p9: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4776 | // If the template argument list of P contains a pack expansion that is |
| 4777 | // not the last template argument, the entire template argument list is a |
Douglas Gregor | 7b976ec | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4778 | // non-deduced context. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4779 | if (OnlyDeduced && |
Douglas Gregor | 7b976ec | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4780 | hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs())) |
| 4781 | break; |
| 4782 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4783 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4784 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4785 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4786 | break; |
| 4787 | } |
| 4788 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4789 | case Type::Complex: |
| 4790 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4791 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4792 | cast<ComplexType>(T)->getElementType(), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4793 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4794 | break; |
| 4795 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4796 | case Type::Atomic: |
| 4797 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4798 | MarkUsedTemplateParameters(Ctx, |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4799 | cast<AtomicType>(T)->getValueType(), |
| 4800 | OnlyDeduced, Depth, Used); |
| 4801 | break; |
| 4802 | |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4803 | case Type::DependentName: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4804 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4805 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 4806 | cast<DependentNameType>(T)->getQualifier(), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4807 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4808 | break; |
| 4809 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4810 | case Type::DependentTemplateSpecialization: { |
| 4811 | const DependentTemplateSpecializationType *Spec |
| 4812 | = cast<DependentTemplateSpecializationType>(T); |
| 4813 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4814 | MarkUsedTemplateParameters(Ctx, Spec->getQualifier(), |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4815 | OnlyDeduced, Depth, Used); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4816 | |
Douglas Gregor | 7b976ec | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4817 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4818 | // If the template argument list of P contains a pack expansion that is not |
| 4819 | // the last template argument, the entire template argument list is a |
Douglas Gregor | 7b976ec | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4820 | // non-deduced context. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4821 | if (OnlyDeduced && |
Douglas Gregor | 7b976ec | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4822 | hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs())) |
| 4823 | break; |
| 4824 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4825 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4826 | MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 4827 | Used); |
| 4828 | break; |
| 4829 | } |
| 4830 | |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4831 | case Type::TypeOf: |
| 4832 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4833 | MarkUsedTemplateParameters(Ctx, |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4834 | cast<TypeOfType>(T)->getUnderlyingType(), |
| 4835 | OnlyDeduced, Depth, Used); |
| 4836 | break; |
| 4837 | |
| 4838 | case Type::TypeOfExpr: |
| 4839 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4840 | MarkUsedTemplateParameters(Ctx, |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4841 | cast<TypeOfExprType>(T)->getUnderlyingExpr(), |
| 4842 | OnlyDeduced, Depth, Used); |
| 4843 | break; |
| 4844 | |
| 4845 | case Type::Decltype: |
| 4846 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4847 | MarkUsedTemplateParameters(Ctx, |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 4848 | cast<DecltypeType>(T)->getUnderlyingExpr(), |
| 4849 | OnlyDeduced, Depth, Used); |
| 4850 | break; |
| 4851 | |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4852 | case Type::UnaryTransform: |
| 4853 | if (!OnlyDeduced) |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4854 | MarkUsedTemplateParameters(Ctx, |
Sean Hunt | ca63c20 | 2011-05-24 22:41:36 +0000 | [diff] [blame] | 4855 | cast<UnaryTransformType>(T)->getUnderlyingType(), |
| 4856 | OnlyDeduced, Depth, Used); |
| 4857 | break; |
| 4858 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4859 | case Type::PackExpansion: |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4860 | MarkUsedTemplateParameters(Ctx, |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 4861 | cast<PackExpansionType>(T)->getPattern(), |
| 4862 | OnlyDeduced, Depth, Used); |
| 4863 | break; |
| 4864 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4865 | case Type::Auto: |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4866 | MarkUsedTemplateParameters(Ctx, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 4867 | cast<AutoType>(T)->getDeducedType(), |
| 4868 | OnlyDeduced, Depth, Used); |
| 4869 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4870 | // None of these types have any template parameters in them. |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4871 | case Type::Builtin: |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4872 | case Type::VariableArray: |
| 4873 | case Type::FunctionNoProto: |
| 4874 | case Type::Record: |
| 4875 | case Type::Enum: |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4876 | case Type::ObjCInterface: |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 4877 | case Type::ObjCObject: |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 4878 | case Type::ObjCObjectPointer: |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 4879 | case Type::UnresolvedUsing: |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4880 | #define TYPE(Class, Base) |
| 4881 | #define ABSTRACT_TYPE(Class, Base) |
| 4882 | #define DEPENDENT_TYPE(Class, Base) |
| 4883 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 4884 | #include "clang/AST/TypeNodes.def" |
| 4885 | break; |
| 4886 | } |
| 4887 | } |
| 4888 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4889 | /// \brief Mark the template parameters that are used by this |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4890 | /// template argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4891 | static void |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4892 | MarkUsedTemplateParameters(ASTContext &Ctx, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4893 | const TemplateArgument &TemplateArg, |
| 4894 | bool OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4895 | unsigned Depth, |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4896 | llvm::SmallBitVector &Used) { |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4897 | switch (TemplateArg.getKind()) { |
| 4898 | case TemplateArgument::Null: |
| 4899 | case TemplateArgument::Integral: |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 4900 | case TemplateArgument::Declaration: |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4901 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4902 | |
Eli Friedman | d7a6b16 | 2012-09-26 02:36:12 +0000 | [diff] [blame] | 4903 | case TemplateArgument::NullPtr: |
| 4904 | MarkUsedTemplateParameters(Ctx, TemplateArg.getNullPtrType(), OnlyDeduced, |
| 4905 | Depth, Used); |
| 4906 | break; |
| 4907 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4908 | case TemplateArgument::Type: |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4909 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsType(), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4910 | Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4911 | break; |
| 4912 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4913 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 4914 | case TemplateArgument::TemplateExpansion: |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4915 | MarkUsedTemplateParameters(Ctx, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4916 | TemplateArg.getAsTemplateOrTemplatePattern(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 4917 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4918 | break; |
| 4919 | |
| 4920 | case TemplateArgument::Expression: |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4921 | MarkUsedTemplateParameters(Ctx, TemplateArg.getAsExpr(), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4922 | Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4923 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4924 | |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 4925 | case TemplateArgument::Pack: |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4926 | for (const auto &P : TemplateArg.pack_elements()) |
| 4927 | MarkUsedTemplateParameters(Ctx, P, OnlyDeduced, Depth, Used); |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 4928 | break; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4929 | } |
| 4930 | } |
| 4931 | |
James Dennett | 16ae9de | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 4932 | /// \brief Mark which template parameters can be deduced from a given |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4933 | /// template argument list. |
| 4934 | /// |
| 4935 | /// \param TemplateArgs the template argument list from which template |
| 4936 | /// parameters will be deduced. |
| 4937 | /// |
James Dennett | 16ae9de | 2012-06-22 10:16:05 +0000 | [diff] [blame] | 4938 | /// \param Used a bit vector whose elements will be set to \c true |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4939 | /// to indicate when the corresponding template parameter will be |
| 4940 | /// deduced. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4941 | void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 4942 | Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4943 | bool OnlyDeduced, unsigned Depth, |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4944 | llvm::SmallBitVector &Used) { |
Douglas Gregor | 7b976ec | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4945 | // C++0x [temp.deduct.type]p9: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4946 | // If the template argument list of P contains a pack expansion that is not |
| 4947 | // the last template argument, the entire template argument list is a |
Douglas Gregor | 7b976ec | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4948 | // non-deduced context. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4949 | if (OnlyDeduced && |
Douglas Gregor | 7b976ec | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 4950 | hasPackExpansionBeforeEnd(TemplateArgs.data(), TemplateArgs.size())) |
| 4951 | return; |
| 4952 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4953 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4954 | ::MarkUsedTemplateParameters(Context, TemplateArgs[I], OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4955 | Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 4956 | } |
Douglas Gregor | 63f07c5 | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 4957 | |
| 4958 | /// \brief Marks all of the template parameters that will be deduced by a |
| 4959 | /// call to the given function template. |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 4960 | void Sema::MarkDeducedTemplateParameters( |
| 4961 | ASTContext &Ctx, const FunctionTemplateDecl *FunctionTemplate, |
| 4962 | llvm::SmallBitVector &Deduced) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4963 | TemplateParameterList *TemplateParams |
Douglas Gregor | 63f07c5 | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 4964 | = FunctionTemplate->getTemplateParameters(); |
| 4965 | Deduced.clear(); |
| 4966 | Deduced.resize(TemplateParams->size()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4967 | |
Douglas Gregor | 63f07c5 | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 4968 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 4969 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4970 | ::MarkUsedTemplateParameters(Ctx, Function->getParamDecl(I)->getType(), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 4971 | true, TemplateParams->getDepth(), Deduced); |
Douglas Gregor | 63f07c5 | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 4972 | } |
Douglas Gregor | dbfb371 | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 4973 | |
| 4974 | bool hasDeducibleTemplateParameters(Sema &S, |
| 4975 | FunctionTemplateDecl *FunctionTemplate, |
| 4976 | QualType T) { |
| 4977 | if (!T->isDependentType()) |
| 4978 | return false; |
| 4979 | |
| 4980 | TemplateParameterList *TemplateParams |
| 4981 | = FunctionTemplate->getTemplateParameters(); |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4982 | llvm::SmallBitVector Deduced(TemplateParams->size()); |
Argyrios Kyrtzidis | 6fc9e1d | 2012-01-17 02:15:41 +0000 | [diff] [blame] | 4983 | ::MarkUsedTemplateParameters(S.Context, T, true, TemplateParams->getDepth(), |
Douglas Gregor | dbfb371 | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 4984 | Deduced); |
| 4985 | |
Benjamin Kramer | 013b366 | 2012-01-30 16:17:39 +0000 | [diff] [blame] | 4986 | return Deduced.any(); |
Douglas Gregor | dbfb371 | 2011-06-16 16:50:48 +0000 | [diff] [blame] | 4987 | } |