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 | |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 13 | #include "clang/Sema/Sema.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 14 | #include "clang/Sema/DeclSpec.h" |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/SemaDiagnostic.h" // FIXME: temporary! |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Template.h" |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 17 | #include "clang/Sema/TemplateDeduction.h" |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclTemplate.h" |
| 21 | #include "clang/AST/StmtVisitor.h" |
| 22 | #include "clang/AST/Expr.h" |
| 23 | #include "clang/AST/ExprCXX.h" |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/BitVector.h" |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 25 | #include <algorithm> |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 26 | |
| 27 | namespace clang { |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 28 | using namespace sema; |
| 29 | |
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. |
| 52 | TDF_SkipNonDependent = 0x08 |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 53 | }; |
| 54 | } |
| 55 | |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 56 | using namespace clang; |
| 57 | |
Douglas Gregor | 9d0e441 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 58 | /// \brief Compare two APSInts, extending and switching the sign as |
| 59 | /// necessary to compare their values regardless of underlying type. |
| 60 | static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) { |
| 61 | if (Y.getBitWidth() > X.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 62 | X = X.extend(Y.getBitWidth()); |
Douglas Gregor | 9d0e441 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 63 | else if (Y.getBitWidth() < X.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 64 | Y = Y.extend(X.getBitWidth()); |
Douglas Gregor | 9d0e441 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 65 | |
| 66 | // If there is a signedness mismatch, correct it. |
| 67 | if (X.isSigned() != Y.isSigned()) { |
| 68 | // If the signed value is negative, then the values cannot be the same. |
| 69 | if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative())) |
| 70 | return false; |
| 71 | |
| 72 | Y.setIsSigned(true); |
| 73 | X.setIsSigned(true); |
| 74 | } |
| 75 | |
| 76 | return X == Y; |
| 77 | } |
| 78 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 79 | static Sema::TemplateDeductionResult |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 80 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 81 | TemplateParameterList *TemplateParams, |
| 82 | const TemplateArgument &Param, |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 83 | const TemplateArgument &Arg, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 84 | TemplateDeductionInfo &Info, |
Douglas Gregor | 0972c86 | 2010-12-22 18:55:49 +0000 | [diff] [blame] | 85 | llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced); |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 86 | |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 87 | static Sema::TemplateDeductionResult |
| 88 | DeduceTemplateArguments(Sema &S, |
| 89 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 90 | QualType Param, |
| 91 | QualType Arg, |
| 92 | TemplateDeductionInfo &Info, |
| 93 | llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 94 | unsigned TDF); |
| 95 | |
| 96 | static Sema::TemplateDeductionResult |
| 97 | DeduceTemplateArguments(Sema &S, |
| 98 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 99 | const TemplateArgument *Params, unsigned NumParams, |
| 100 | const TemplateArgument *Args, unsigned NumArgs, |
| 101 | TemplateDeductionInfo &Info, |
Douglas Gregor | 0972c86 | 2010-12-22 18:55:49 +0000 | [diff] [blame] | 102 | llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 103 | bool NumberOfArgumentsMustMatch = true); |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 104 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 105 | /// \brief If the given expression is of a form that permits the deduction |
| 106 | /// of a non-type template parameter, return the declaration of that |
| 107 | /// non-type template parameter. |
| 108 | static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) { |
| 109 | if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E)) |
| 110 | E = IC->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 112 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 113 | return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 115 | return 0; |
| 116 | } |
| 117 | |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 118 | /// \brief Determine whether two declaration pointers refer to the same |
| 119 | /// declaration. |
| 120 | static bool isSameDeclaration(Decl *X, Decl *Y) { |
| 121 | if (!X || !Y) |
| 122 | return !X && !Y; |
| 123 | |
| 124 | if (NamedDecl *NX = dyn_cast<NamedDecl>(X)) |
| 125 | X = NX->getUnderlyingDecl(); |
| 126 | if (NamedDecl *NY = dyn_cast<NamedDecl>(Y)) |
| 127 | Y = NY->getUnderlyingDecl(); |
| 128 | |
| 129 | return X->getCanonicalDecl() == Y->getCanonicalDecl(); |
| 130 | } |
| 131 | |
| 132 | /// \brief Verify that the given, deduced template arguments are compatible. |
| 133 | /// |
| 134 | /// \returns The deduced template argument, or a NULL template argument if |
| 135 | /// the deduced template arguments were incompatible. |
| 136 | static DeducedTemplateArgument |
| 137 | checkDeducedTemplateArguments(ASTContext &Context, |
| 138 | const DeducedTemplateArgument &X, |
| 139 | const DeducedTemplateArgument &Y) { |
| 140 | // We have no deduction for one or both of the arguments; they're compatible. |
| 141 | if (X.isNull()) |
| 142 | return Y; |
| 143 | if (Y.isNull()) |
| 144 | return X; |
| 145 | |
| 146 | switch (X.getKind()) { |
| 147 | case TemplateArgument::Null: |
| 148 | llvm_unreachable("Non-deduced template arguments handled above"); |
| 149 | |
| 150 | case TemplateArgument::Type: |
| 151 | // If two template type arguments have the same type, they're compatible. |
| 152 | if (Y.getKind() == TemplateArgument::Type && |
| 153 | Context.hasSameType(X.getAsType(), Y.getAsType())) |
| 154 | return X; |
| 155 | |
| 156 | return DeducedTemplateArgument(); |
| 157 | |
| 158 | case TemplateArgument::Integral: |
| 159 | // If we deduced a constant in one case and either a dependent expression or |
| 160 | // declaration in another case, keep the integral constant. |
| 161 | // If both are integral constants with the same value, keep that value. |
| 162 | if (Y.getKind() == TemplateArgument::Expression || |
| 163 | Y.getKind() == TemplateArgument::Declaration || |
| 164 | (Y.getKind() == TemplateArgument::Integral && |
| 165 | hasSameExtendedValue(*X.getAsIntegral(), *Y.getAsIntegral()))) |
| 166 | return DeducedTemplateArgument(X, |
| 167 | X.wasDeducedFromArrayBound() && |
| 168 | Y.wasDeducedFromArrayBound()); |
| 169 | |
| 170 | // All other combinations are incompatible. |
| 171 | return DeducedTemplateArgument(); |
| 172 | |
| 173 | case TemplateArgument::Template: |
| 174 | if (Y.getKind() == TemplateArgument::Template && |
| 175 | Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate())) |
| 176 | return X; |
| 177 | |
| 178 | // All other combinations are incompatible. |
| 179 | return DeducedTemplateArgument(); |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 180 | |
| 181 | case TemplateArgument::TemplateExpansion: |
| 182 | if (Y.getKind() == TemplateArgument::TemplateExpansion && |
| 183 | Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(), |
| 184 | Y.getAsTemplateOrTemplatePattern())) |
| 185 | return X; |
| 186 | |
| 187 | // All other combinations are incompatible. |
| 188 | return DeducedTemplateArgument(); |
| 189 | |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 190 | case TemplateArgument::Expression: |
| 191 | // If we deduced a dependent expression in one case and either an integral |
| 192 | // constant or a declaration in another case, keep the integral constant |
| 193 | // or declaration. |
| 194 | if (Y.getKind() == TemplateArgument::Integral || |
| 195 | Y.getKind() == TemplateArgument::Declaration) |
| 196 | return DeducedTemplateArgument(Y, X.wasDeducedFromArrayBound() && |
| 197 | Y.wasDeducedFromArrayBound()); |
| 198 | |
| 199 | if (Y.getKind() == TemplateArgument::Expression) { |
| 200 | // Compare the expressions for equality |
| 201 | llvm::FoldingSetNodeID ID1, ID2; |
| 202 | X.getAsExpr()->Profile(ID1, Context, true); |
| 203 | Y.getAsExpr()->Profile(ID2, Context, true); |
| 204 | if (ID1 == ID2) |
| 205 | return X; |
| 206 | } |
| 207 | |
| 208 | // All other combinations are incompatible. |
| 209 | return DeducedTemplateArgument(); |
| 210 | |
| 211 | case TemplateArgument::Declaration: |
| 212 | // If we deduced a declaration and a dependent expression, keep the |
| 213 | // declaration. |
| 214 | if (Y.getKind() == TemplateArgument::Expression) |
| 215 | return X; |
| 216 | |
| 217 | // If we deduced a declaration and an integral constant, keep the |
| 218 | // integral constant. |
| 219 | if (Y.getKind() == TemplateArgument::Integral) |
| 220 | return Y; |
| 221 | |
| 222 | // If we deduced two declarations, make sure they they refer to the |
| 223 | // same declaration. |
| 224 | if (Y.getKind() == TemplateArgument::Declaration && |
| 225 | isSameDeclaration(X.getAsDecl(), Y.getAsDecl())) |
| 226 | return X; |
| 227 | |
| 228 | // All other combinations are incompatible. |
| 229 | return DeducedTemplateArgument(); |
| 230 | |
| 231 | case TemplateArgument::Pack: |
| 232 | if (Y.getKind() != TemplateArgument::Pack || |
| 233 | X.pack_size() != Y.pack_size()) |
| 234 | return DeducedTemplateArgument(); |
| 235 | |
| 236 | for (TemplateArgument::pack_iterator XA = X.pack_begin(), |
| 237 | XAEnd = X.pack_end(), |
| 238 | YA = Y.pack_begin(); |
| 239 | XA != XAEnd; ++XA, ++YA) { |
Douglas Gregor | 135ffa7 | 2011-01-05 21:00:53 +0000 | [diff] [blame] | 240 | if (checkDeducedTemplateArguments(Context, |
| 241 | DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()), |
| 242 | DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound())) |
| 243 | .isNull()) |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 244 | return DeducedTemplateArgument(); |
| 245 | } |
| 246 | |
| 247 | return X; |
| 248 | } |
| 249 | |
| 250 | return DeducedTemplateArgument(); |
| 251 | } |
| 252 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 253 | /// \brief Deduce the value of the given non-type template parameter |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 254 | /// from the given constant. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 255 | static Sema::TemplateDeductionResult |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 256 | DeduceNonTypeTemplateArgument(Sema &S, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 257 | NonTypeTemplateParmDecl *NTTP, |
Douglas Gregor | 9d0e441 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 258 | llvm::APSInt Value, QualType ValueType, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 259 | bool DeducedFromArrayBound, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 260 | TemplateDeductionInfo &Info, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 261 | llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 262 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 263 | "Cannot deduce non-type template argument with depth > 0"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 264 | |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 265 | DeducedTemplateArgument NewDeduced(Value, ValueType, DeducedFromArrayBound); |
| 266 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
| 267 | Deduced[NTTP->getIndex()], |
| 268 | NewDeduced); |
| 269 | if (Result.isNull()) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 270 | Info.Param = NTTP; |
| 271 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 272 | Info.SecondArg = NewDeduced; |
| 273 | return Sema::TDK_Inconsistent; |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 274 | } |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 275 | |
| 276 | Deduced[NTTP->getIndex()] = Result; |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 277 | return Sema::TDK_Success; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 280 | /// \brief Deduce the value of the given non-type template parameter |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 281 | /// from the given type- or value-dependent expression. |
| 282 | /// |
| 283 | /// \returns true if deduction succeeded, false otherwise. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 284 | static Sema::TemplateDeductionResult |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 285 | DeduceNonTypeTemplateArgument(Sema &S, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 286 | NonTypeTemplateParmDecl *NTTP, |
| 287 | Expr *Value, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 288 | TemplateDeductionInfo &Info, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 289 | llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 291 | "Cannot deduce non-type template argument with depth > 0"); |
| 292 | assert((Value->isTypeDependent() || Value->isValueDependent()) && |
| 293 | "Expression template argument must be type- or value-dependent."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 295 | DeducedTemplateArgument NewDeduced(Value); |
| 296 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
| 297 | Deduced[NTTP->getIndex()], |
| 298 | NewDeduced); |
| 299 | |
| 300 | if (Result.isNull()) { |
| 301 | Info.Param = NTTP; |
| 302 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
| 303 | Info.SecondArg = NewDeduced; |
| 304 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 305 | } |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 306 | |
| 307 | Deduced[NTTP->getIndex()] = Result; |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 308 | return Sema::TDK_Success; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Douglas Gregor | 15755cb | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 311 | /// \brief Deduce the value of the given non-type template parameter |
| 312 | /// from the given declaration. |
| 313 | /// |
| 314 | /// \returns true if deduction succeeded, false otherwise. |
| 315 | static Sema::TemplateDeductionResult |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 316 | DeduceNonTypeTemplateArgument(Sema &S, |
Douglas Gregor | 15755cb | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 317 | NonTypeTemplateParmDecl *NTTP, |
| 318 | Decl *D, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 319 | TemplateDeductionInfo &Info, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 320 | llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 15755cb | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 321 | assert(NTTP->getDepth() == 0 && |
| 322 | "Cannot deduce non-type template argument with depth > 0"); |
| 323 | |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 324 | DeducedTemplateArgument NewDeduced(D? D->getCanonicalDecl() : 0); |
| 325 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
| 326 | Deduced[NTTP->getIndex()], |
| 327 | NewDeduced); |
| 328 | if (Result.isNull()) { |
| 329 | Info.Param = NTTP; |
| 330 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
| 331 | Info.SecondArg = NewDeduced; |
| 332 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 15755cb | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 335 | Deduced[NTTP->getIndex()] = Result; |
Douglas Gregor | 15755cb | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 336 | return Sema::TDK_Success; |
| 337 | } |
| 338 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 339 | static Sema::TemplateDeductionResult |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 340 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 341 | TemplateParameterList *TemplateParams, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 342 | TemplateName Param, |
| 343 | TemplateName Arg, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 344 | TemplateDeductionInfo &Info, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 345 | llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 346 | TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 347 | if (!ParamDecl) { |
| 348 | // The parameter type is dependent and is not a template template parameter, |
| 349 | // so there is nothing that we can deduce. |
| 350 | return Sema::TDK_Success; |
| 351 | } |
| 352 | |
| 353 | if (TemplateTemplateParmDecl *TempParam |
| 354 | = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) { |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 355 | DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg)); |
| 356 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
| 357 | Deduced[TempParam->getIndex()], |
| 358 | NewDeduced); |
| 359 | if (Result.isNull()) { |
| 360 | Info.Param = TempParam; |
| 361 | Info.FirstArg = Deduced[TempParam->getIndex()]; |
| 362 | Info.SecondArg = NewDeduced; |
| 363 | return Sema::TDK_Inconsistent; |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 366 | Deduced[TempParam->getIndex()] = Result; |
| 367 | return Sema::TDK_Success; |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 368 | } |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 369 | |
| 370 | // Verify that the two template names are equivalent. |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 371 | if (S.Context.hasSameTemplateName(Param, Arg)) |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 372 | return Sema::TDK_Success; |
| 373 | |
| 374 | // Mismatch of non-dependent template parameter to argument. |
| 375 | Info.FirstArg = TemplateArgument(Param); |
| 376 | Info.SecondArg = TemplateArgument(Arg); |
| 377 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 380 | /// \brief Deduce the template arguments by comparing the template parameter |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 381 | /// type (which is a template-id) with the template argument type. |
| 382 | /// |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 383 | /// \param S the Sema |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 384 | /// |
| 385 | /// \param TemplateParams the template parameters that we are deducing |
| 386 | /// |
| 387 | /// \param Param the parameter type |
| 388 | /// |
| 389 | /// \param Arg the argument type |
| 390 | /// |
| 391 | /// \param Info information about the template argument deduction itself |
| 392 | /// |
| 393 | /// \param Deduced the deduced template arguments |
| 394 | /// |
| 395 | /// \returns the result of template argument deduction so far. Note that a |
| 396 | /// "success" result means that template argument deduction has not yet failed, |
| 397 | /// but it may still fail, later, for other reasons. |
| 398 | static Sema::TemplateDeductionResult |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 399 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 400 | TemplateParameterList *TemplateParams, |
| 401 | const TemplateSpecializationType *Param, |
| 402 | QualType Arg, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 403 | TemplateDeductionInfo &Info, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 404 | llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
John McCall | 467b27b | 2009-10-22 20:10:53 +0000 | [diff] [blame] | 405 | assert(Arg.isCanonical() && "Argument type must be canonical"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 406 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 407 | // Check whether the template argument is a dependent template-id. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 408 | if (const TemplateSpecializationType *SpecArg |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 409 | = dyn_cast<TemplateSpecializationType>(Arg)) { |
| 410 | // Perform template argument deduction for the template name. |
| 411 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 412 | = DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 413 | Param->getTemplateName(), |
| 414 | SpecArg->getTemplateName(), |
| 415 | Info, Deduced)) |
| 416 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 418 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 419 | // Perform template argument deduction on each template |
Douglas Gregor | 0972c86 | 2010-12-22 18:55:49 +0000 | [diff] [blame] | 420 | // argument. Ignore any missing/extra arguments, since they could be |
| 421 | // filled in by default arguments. |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 422 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | 0972c86 | 2010-12-22 18:55:49 +0000 | [diff] [blame] | 423 | Param->getArgs(), Param->getNumArgs(), |
| 424 | SpecArg->getArgs(), SpecArg->getNumArgs(), |
| 425 | Info, Deduced, |
| 426 | /*NumberOfArgumentsMustMatch=*/false); |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 427 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 428 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 429 | // If the argument type is a class template specialization, we |
| 430 | // perform template argument deduction using its template |
| 431 | // arguments. |
| 432 | const RecordType *RecordArg = dyn_cast<RecordType>(Arg); |
| 433 | if (!RecordArg) |
| 434 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 435 | |
| 436 | ClassTemplateSpecializationDecl *SpecArg |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 437 | = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); |
| 438 | if (!SpecArg) |
| 439 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 440 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 441 | // Perform template argument deduction for the template name. |
| 442 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 443 | = DeduceTemplateArguments(S, |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 444 | TemplateParams, |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 445 | Param->getTemplateName(), |
| 446 | TemplateName(SpecArg->getSpecializedTemplate()), |
| 447 | Info, Deduced)) |
| 448 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 449 | |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 450 | // Perform template argument deduction for the template arguments. |
| 451 | return DeduceTemplateArguments(S, TemplateParams, |
| 452 | Param->getArgs(), Param->getNumArgs(), |
| 453 | SpecArg->getTemplateArgs().data(), |
| 454 | SpecArg->getTemplateArgs().size(), |
| 455 | Info, Deduced); |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 456 | } |
| 457 | |
John McCall | cd05e81 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 458 | /// \brief Determines whether the given type is an opaque type that |
| 459 | /// might be more qualified when instantiated. |
| 460 | static bool IsPossiblyOpaquelyQualifiedType(QualType T) { |
| 461 | switch (T->getTypeClass()) { |
| 462 | case Type::TypeOfExpr: |
| 463 | case Type::TypeOf: |
| 464 | case Type::DependentName: |
| 465 | case Type::Decltype: |
| 466 | case Type::UnresolvedUsing: |
| 467 | return true; |
| 468 | |
| 469 | case Type::ConstantArray: |
| 470 | case Type::IncompleteArray: |
| 471 | case Type::VariableArray: |
| 472 | case Type::DependentSizedArray: |
| 473 | return IsPossiblyOpaquelyQualifiedType( |
| 474 | cast<ArrayType>(T)->getElementType()); |
| 475 | |
| 476 | default: |
| 477 | return false; |
| 478 | } |
| 479 | } |
| 480 | |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 481 | /// \brief Retrieve the depth and index of an unexpanded parameter pack. |
| 482 | static std::pair<unsigned, unsigned> |
| 483 | getDepthAndIndex(UnexpandedParameterPack UPP) { |
| 484 | if (const TemplateTypeParmType *TTP |
| 485 | = UPP.first.dyn_cast<const TemplateTypeParmType *>()) |
| 486 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| 487 | |
| 488 | NamedDecl *ND = UPP.first.get<NamedDecl *>(); |
| 489 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND)) |
| 490 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| 491 | |
| 492 | if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND)) |
| 493 | return std::make_pair(NTTP->getDepth(), NTTP->getIndex()); |
| 494 | |
| 495 | TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND); |
| 496 | return std::make_pair(TTP->getDepth(), TTP->getIndex()); |
| 497 | } |
| 498 | |
| 499 | /// \brief Helper function to build a TemplateParameter when we don't |
| 500 | /// know its type statically. |
| 501 | static TemplateParameter makeTemplateParameter(Decl *D) { |
| 502 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) |
| 503 | return TemplateParameter(TTP); |
| 504 | else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) |
| 505 | return TemplateParameter(NTTP); |
| 506 | |
| 507 | return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); |
| 508 | } |
| 509 | |
| 510 | /// \brief Deduce the template arguments by comparing the list of parameter |
| 511 | /// types to the list of argument types, as in the parameter-type-lists of |
| 512 | /// function types (C++ [temp.deduct.type]p10). |
| 513 | /// |
| 514 | /// \param S The semantic analysis object within which we are deducing |
| 515 | /// |
| 516 | /// \param TemplateParams The template parameters that we are deducing |
| 517 | /// |
| 518 | /// \param Params The list of parameter types |
| 519 | /// |
| 520 | /// \param NumParams The number of types in \c Params |
| 521 | /// |
| 522 | /// \param Args The list of argument types |
| 523 | /// |
| 524 | /// \param NumArgs The number of types in \c Args |
| 525 | /// |
| 526 | /// \param Info information about the template argument deduction itself |
| 527 | /// |
| 528 | /// \param Deduced the deduced template arguments |
| 529 | /// |
| 530 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
| 531 | /// how template argument deduction is performed. |
| 532 | /// |
| 533 | /// \returns the result of template argument deduction so far. Note that a |
| 534 | /// "success" result means that template argument deduction has not yet failed, |
| 535 | /// but it may still fail, later, for other reasons. |
| 536 | static Sema::TemplateDeductionResult |
| 537 | DeduceTemplateArguments(Sema &S, |
| 538 | TemplateParameterList *TemplateParams, |
| 539 | const QualType *Params, unsigned NumParams, |
| 540 | const QualType *Args, unsigned NumArgs, |
| 541 | TemplateDeductionInfo &Info, |
| 542 | llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 543 | unsigned TDF) { |
Douglas Gregor | 0bbacf8 | 2011-01-05 23:23:17 +0000 | [diff] [blame] | 544 | // Fast-path check to see if we have too many/too few arguments. |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 545 | // FIXME: Variadic templates broken! |
Douglas Gregor | 0bbacf8 | 2011-01-05 23:23:17 +0000 | [diff] [blame] | 546 | if (NumParams != NumArgs && |
| 547 | !(NumParams && isa<PackExpansionType>(Params[NumParams - 1])) && |
| 548 | !(NumArgs && isa<PackExpansionType>(Args[NumArgs - 1]))) |
| 549 | return NumArgs < NumParams ? Sema::TDK_TooFewArguments |
| 550 | : Sema::TDK_TooManyArguments; |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 551 | |
| 552 | // C++0x [temp.deduct.type]p10: |
| 553 | // Similarly, if P has a form that contains (T), then each parameter type |
| 554 | // Pi of the respective parameter-type- list of P is compared with the |
| 555 | // corresponding parameter type Ai of the corresponding parameter-type-list |
| 556 | // of A. [...] |
| 557 | unsigned ArgIdx = 0, ParamIdx = 0; |
| 558 | for (; ParamIdx != NumParams; ++ParamIdx) { |
| 559 | // Check argument types. |
| 560 | const PackExpansionType *Expansion |
| 561 | = dyn_cast<PackExpansionType>(Params[ParamIdx]); |
| 562 | if (!Expansion) { |
| 563 | // Simple case: compare the parameter and argument types at this point. |
| 564 | |
| 565 | // Make sure we have an argument. |
| 566 | if (ArgIdx >= NumArgs) |
| 567 | return Sema::TDK_TooFewArguments; |
| 568 | |
| 569 | if (Sema::TemplateDeductionResult Result |
| 570 | = DeduceTemplateArguments(S, TemplateParams, |
| 571 | Params[ParamIdx], |
| 572 | Args[ArgIdx], |
| 573 | Info, Deduced, TDF)) |
| 574 | return Result; |
| 575 | |
| 576 | ++ArgIdx; |
| 577 | continue; |
| 578 | } |
| 579 | |
| 580 | // C++0x [temp.deduct.type]p10: |
| 581 | // If the parameter-declaration corresponding to Pi is a function |
| 582 | // parameter pack, then the type of its declarator- id is compared with |
| 583 | // each remaining parameter type in the parameter-type-list of A. Each |
| 584 | // comparison deduces template arguments for subsequent positions in the |
| 585 | // template parameter packs expanded by the function parameter pack. |
| 586 | |
| 587 | // Compute the set of template parameter indices that correspond to |
| 588 | // parameter packs expanded by the pack expansion. |
| 589 | llvm::SmallVector<unsigned, 2> PackIndices; |
| 590 | QualType Pattern = Expansion->getPattern(); |
| 591 | { |
| 592 | llvm::BitVector SawIndices(TemplateParams->size()); |
| 593 | llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 594 | S.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
| 595 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 596 | unsigned Depth, Index; |
| 597 | llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); |
| 598 | if (Depth == 0 && !SawIndices[Index]) { |
| 599 | SawIndices[Index] = true; |
| 600 | PackIndices.push_back(Index); |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?"); |
| 605 | |
| 606 | // Save the deduced template arguments for each parameter pack expanded |
| 607 | // by this pack expansion, then clear out the deduction. |
| 608 | llvm::SmallVector<DeducedTemplateArgument, 2> |
| 609 | SavedPacks(PackIndices.size()); |
| 610 | for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) { |
| 611 | SavedPacks[I] = Deduced[PackIndices[I]]; |
| 612 | Deduced[PackIndices[I]] = DeducedTemplateArgument(); |
| 613 | } |
| 614 | |
| 615 | // Keep track of the deduced template arguments for each parameter pack |
| 616 | // expanded by this pack expansion (the outer index) and for each |
| 617 | // template argument (the inner SmallVectors). |
| 618 | llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2> |
| 619 | NewlyDeducedPacks(PackIndices.size()); |
| 620 | bool HasAnyArguments = false; |
| 621 | for (; ArgIdx < NumArgs; ++ArgIdx) { |
| 622 | HasAnyArguments = true; |
| 623 | |
| 624 | // Deduce template arguments from the pattern. |
| 625 | if (Sema::TemplateDeductionResult Result |
| 626 | = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx], |
| 627 | Info, Deduced)) |
| 628 | return Result; |
| 629 | |
| 630 | // Capture the deduced template arguments for each parameter pack expanded |
| 631 | // by this pack expansion, add them to the list of arguments we've deduced |
| 632 | // for that pack, then clear out the deduced argument. |
| 633 | for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) { |
| 634 | DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]]; |
| 635 | if (!DeducedArg.isNull()) { |
| 636 | NewlyDeducedPacks[I].push_back(DeducedArg); |
| 637 | DeducedArg = DeducedTemplateArgument(); |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | // Build argument packs for each of the parameter packs expanded by this |
| 643 | // pack expansion. |
| 644 | for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) { |
| 645 | if (HasAnyArguments && NewlyDeducedPacks[I].empty()) { |
| 646 | // We were not able to deduce anything for this parameter pack, |
| 647 | // so just restore the saved argument pack. |
| 648 | Deduced[PackIndices[I]] = SavedPacks[I]; |
| 649 | continue; |
| 650 | } |
| 651 | |
| 652 | DeducedTemplateArgument NewPack; |
| 653 | |
| 654 | if (NewlyDeducedPacks[I].empty()) { |
| 655 | // If we deduced an empty argument pack, create it now. |
| 656 | NewPack = DeducedTemplateArgument(TemplateArgument(0, 0)); |
| 657 | } else { |
| 658 | TemplateArgument *ArgumentPack |
| 659 | = new (S.Context) TemplateArgument [NewlyDeducedPacks[I].size()]; |
| 660 | std::copy(NewlyDeducedPacks[I].begin(), NewlyDeducedPacks[I].end(), |
| 661 | ArgumentPack); |
| 662 | NewPack |
| 663 | = DeducedTemplateArgument(TemplateArgument(ArgumentPack, |
| 664 | NewlyDeducedPacks[I].size()), |
| 665 | NewlyDeducedPacks[I][0].wasDeducedFromArrayBound()); |
| 666 | } |
| 667 | |
| 668 | DeducedTemplateArgument Result |
| 669 | = checkDeducedTemplateArguments(S.Context, SavedPacks[I], NewPack); |
| 670 | if (Result.isNull()) { |
| 671 | Info.Param |
| 672 | = makeTemplateParameter(TemplateParams->getParam(PackIndices[I])); |
| 673 | Info.FirstArg = SavedPacks[I]; |
| 674 | Info.SecondArg = NewPack; |
| 675 | return Sema::TDK_Inconsistent; |
| 676 | } |
| 677 | |
| 678 | Deduced[PackIndices[I]] = Result; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | // Make sure we don't have any extra arguments. |
| 683 | if (ArgIdx < NumArgs) |
| 684 | return Sema::TDK_TooManyArguments; |
| 685 | |
| 686 | return Sema::TDK_Success; |
| 687 | } |
| 688 | |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 689 | /// \brief Deduce the template arguments by comparing the parameter type and |
| 690 | /// the argument type (C++ [temp.deduct.type]). |
| 691 | /// |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 692 | /// \param S the semantic analysis object within which we are deducing |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 693 | /// |
| 694 | /// \param TemplateParams the template parameters that we are deducing |
| 695 | /// |
| 696 | /// \param ParamIn the parameter type |
| 697 | /// |
| 698 | /// \param ArgIn the argument type |
| 699 | /// |
| 700 | /// \param Info information about the template argument deduction itself |
| 701 | /// |
| 702 | /// \param Deduced the deduced template arguments |
| 703 | /// |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 704 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 705 | /// how template argument deduction is performed. |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 706 | /// |
| 707 | /// \returns the result of template argument deduction so far. Note that a |
| 708 | /// "success" result means that template argument deduction has not yet failed, |
| 709 | /// but it may still fail, later, for other reasons. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 710 | static Sema::TemplateDeductionResult |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 711 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 712 | TemplateParameterList *TemplateParams, |
| 713 | QualType ParamIn, QualType ArgIn, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 714 | TemplateDeductionInfo &Info, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 715 | llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 716 | unsigned TDF) { |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 717 | // We only want to look at the canonical types, since typedefs and |
| 718 | // sugar are not part of template argument deduction. |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 719 | QualType Param = S.Context.getCanonicalType(ParamIn); |
| 720 | QualType Arg = S.Context.getCanonicalType(ArgIn); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 721 | |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 722 | // C++0x [temp.deduct.call]p4 bullet 1: |
| 723 | // - If the original P is a reference type, the deduced A (i.e., the type |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 724 | // referred to by the reference) can be more cv-qualified than the |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 725 | // transformed A. |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 726 | if (TDF & TDF_ParamWithReferenceType) { |
Chandler Carruth | e724246 | 2009-12-30 04:10:01 +0000 | [diff] [blame] | 727 | Qualifiers Quals; |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 728 | QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals); |
Chandler Carruth | e724246 | 2009-12-30 04:10:01 +0000 | [diff] [blame] | 729 | Quals.setCVRQualifiers(Quals.getCVRQualifiers() & |
| 730 | Arg.getCVRQualifiersThroughArrayTypes()); |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 731 | Param = S.Context.getQualifiedType(UnqualParam, Quals); |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 732 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 733 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 734 | // If the parameter type is not dependent, there is nothing to deduce. |
Douglas Gregor | 1282029 | 2009-09-14 20:00:47 +0000 | [diff] [blame] | 735 | if (!Param->isDependentType()) { |
| 736 | if (!(TDF & TDF_SkipNonDependent) && Param != Arg) { |
| 737 | |
| 738 | return Sema::TDK_NonDeducedMismatch; |
| 739 | } |
| 740 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 741 | return Sema::TDK_Success; |
Douglas Gregor | 1282029 | 2009-09-14 20:00:47 +0000 | [diff] [blame] | 742 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 743 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 744 | // C++ [temp.deduct.type]p9: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | // A template type argument T, a template template argument TT or a |
| 746 | // 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] | 747 | // the following forms: |
| 748 | // |
| 749 | // T |
| 750 | // cv-list T |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 751 | if (const TemplateTypeParmType *TemplateTypeParm |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 752 | = Param->getAs<TemplateTypeParmType>()) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 753 | unsigned Index = TemplateTypeParm->getIndex(); |
Douglas Gregor | f290e0d | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 754 | bool RecanonicalizeArg = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 755 | |
Douglas Gregor | 9e9fae4 | 2009-07-22 20:02:25 +0000 | [diff] [blame] | 756 | // If the argument type is an array type, move the qualifiers up to the |
| 757 | // top level, so they can be matched with the qualifiers on the parameter. |
| 758 | // FIXME: address spaces, ObjC GC qualifiers |
Douglas Gregor | f290e0d | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 759 | if (isa<ArrayType>(Arg)) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 760 | Qualifiers Quals; |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 761 | Arg = S.Context.getUnqualifiedArrayType(Arg, Quals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 762 | if (Quals) { |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 763 | Arg = S.Context.getQualifiedType(Arg, Quals); |
Douglas Gregor | f290e0d | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 764 | RecanonicalizeArg = true; |
| 765 | } |
| 766 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 767 | |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 768 | // The argument type can not be less qualified than the parameter |
| 769 | // type. |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 770 | if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 771 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 772 | Info.FirstArg = TemplateArgument(Param); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 773 | Info.SecondArg = TemplateArgument(Arg); |
John McCall | 57e9778 | 2010-08-05 09:05:08 +0000 | [diff] [blame] | 774 | return Sema::TDK_Underqualified; |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 775 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 776 | |
| 777 | assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0"); |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 778 | assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function"); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 779 | QualType DeducedType = Arg; |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 780 | |
| 781 | // local manipulation is okay because it's canonical |
| 782 | DeducedType.removeLocalCVRQualifiers(Param.getCVRQualifiers()); |
Douglas Gregor | f290e0d | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 783 | if (RecanonicalizeArg) |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 784 | DeducedType = S.Context.getCanonicalType(DeducedType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 785 | |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 786 | DeducedTemplateArgument NewDeduced(DeducedType); |
| 787 | DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, |
| 788 | Deduced[Index], |
| 789 | NewDeduced); |
| 790 | if (Result.isNull()) { |
| 791 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 792 | Info.FirstArg = Deduced[Index]; |
| 793 | Info.SecondArg = NewDeduced; |
| 794 | return Sema::TDK_Inconsistent; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 795 | } |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 796 | |
| 797 | Deduced[Index] = Result; |
| 798 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 801 | // Set up the template argument deduction information for a failure. |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 802 | Info.FirstArg = TemplateArgument(ParamIn); |
| 803 | Info.SecondArg = TemplateArgument(ArgIn); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 804 | |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 805 | // Check the cv-qualifiers on the parameter and argument types. |
| 806 | if (!(TDF & TDF_IgnoreQualifiers)) { |
| 807 | if (TDF & TDF_ParamWithReferenceType) { |
| 808 | if (Param.isMoreQualifiedThan(Arg)) |
| 809 | return Sema::TDK_NonDeducedMismatch; |
John McCall | cd05e81 | 2010-08-28 22:14:41 +0000 | [diff] [blame] | 810 | } else if (!IsPossiblyOpaquelyQualifiedType(Param)) { |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 811 | if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 812 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 813 | } |
| 814 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 815 | |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 816 | switch (Param->getTypeClass()) { |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 817 | // No deduction possible for these types |
| 818 | case Type::Builtin: |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 819 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 820 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 821 | // T * |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 822 | case Type::Pointer: { |
John McCall | c000834 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 823 | QualType PointeeType; |
| 824 | if (const PointerType *PointerArg = Arg->getAs<PointerType>()) { |
| 825 | PointeeType = PointerArg->getPointeeType(); |
| 826 | } else if (const ObjCObjectPointerType *PointerArg |
| 827 | = Arg->getAs<ObjCObjectPointerType>()) { |
| 828 | PointeeType = PointerArg->getPointeeType(); |
| 829 | } else { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 830 | return Sema::TDK_NonDeducedMismatch; |
John McCall | c000834 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 831 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 832 | |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 833 | unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 834 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 835 | cast<PointerType>(Param)->getPointeeType(), |
John McCall | c000834 | 2010-05-13 07:48:05 +0000 | [diff] [blame] | 836 | PointeeType, |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 837 | Info, Deduced, SubTDF); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 838 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 839 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 840 | // T & |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 841 | case Type::LValueReference: { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 842 | const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>(); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 843 | if (!ReferenceArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 844 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 845 | |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 846 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 847 | cast<LValueReferenceType>(Param)->getPointeeType(), |
| 848 | ReferenceArg->getPointeeType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 849 | Info, Deduced, 0); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 850 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 851 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 852 | // T && [C++0x] |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 853 | case Type::RValueReference: { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 854 | const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>(); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 855 | if (!ReferenceArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 856 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 857 | |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 858 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 859 | cast<RValueReferenceType>(Param)->getPointeeType(), |
| 860 | ReferenceArg->getPointeeType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 861 | Info, Deduced, 0); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 862 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 863 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 864 | // T [] (implied, but not stated explicitly) |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 865 | case Type::IncompleteArray: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 866 | const IncompleteArrayType *IncompleteArrayArg = |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 867 | S.Context.getAsIncompleteArrayType(Arg); |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 868 | if (!IncompleteArrayArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 869 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 870 | |
John McCall | e4f26e5 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 871 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 872 | return DeduceTemplateArguments(S, TemplateParams, |
| 873 | S.Context.getAsIncompleteArrayType(Param)->getElementType(), |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 874 | IncompleteArrayArg->getElementType(), |
John McCall | e4f26e5 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 875 | Info, Deduced, SubTDF); |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 876 | } |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 877 | |
| 878 | // T [integer-constant] |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 879 | case Type::ConstantArray: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 880 | const ConstantArrayType *ConstantArrayArg = |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 881 | S.Context.getAsConstantArrayType(Arg); |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 882 | if (!ConstantArrayArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 883 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 884 | |
| 885 | const ConstantArrayType *ConstantArrayParm = |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 886 | S.Context.getAsConstantArrayType(Param); |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 887 | if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 888 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 889 | |
John McCall | e4f26e5 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 890 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 891 | return DeduceTemplateArguments(S, TemplateParams, |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 892 | ConstantArrayParm->getElementType(), |
| 893 | ConstantArrayArg->getElementType(), |
John McCall | e4f26e5 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 894 | Info, Deduced, SubTDF); |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 897 | // type [i] |
| 898 | case Type::DependentSizedArray: { |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 899 | const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg); |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 900 | if (!ArrayArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 901 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 902 | |
John McCall | e4f26e5 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 903 | unsigned SubTDF = TDF & TDF_IgnoreQualifiers; |
| 904 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 905 | // Check the element type of the arrays |
| 906 | const DependentSizedArrayType *DependentArrayParm |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 907 | = S.Context.getAsDependentSizedArrayType(Param); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 908 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 909 | = DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 910 | DependentArrayParm->getElementType(), |
| 911 | ArrayArg->getElementType(), |
John McCall | e4f26e5 | 2010-08-19 00:20:19 +0000 | [diff] [blame] | 912 | Info, Deduced, SubTDF)) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 913 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 914 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 915 | // Determine the array bound is something we can deduce. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 916 | NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 917 | = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr()); |
| 918 | if (!NTTP) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 919 | return Sema::TDK_Success; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 920 | |
| 921 | // We can perform template argument deduction for the given non-type |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 922 | // template parameter. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 923 | assert(NTTP->getDepth() == 0 && |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 924 | "Cannot deduce non-type template argument at depth > 0"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 925 | if (const ConstantArrayType *ConstantArrayArg |
Anders Carlsson | 335e24a | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 926 | = dyn_cast<ConstantArrayType>(ArrayArg)) { |
| 927 | llvm::APSInt Size(ConstantArrayArg->getSize()); |
Douglas Gregor | 9d0e441 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 928 | return DeduceNonTypeTemplateArgument(S, NTTP, Size, |
| 929 | S.Context.getSizeType(), |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 930 | /*ArrayBound=*/true, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 931 | Info, Deduced); |
Anders Carlsson | 335e24a | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 932 | } |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 933 | if (const DependentSizedArrayType *DependentArrayArg |
| 934 | = dyn_cast<DependentSizedArrayType>(ArrayArg)) |
Douglas Gregor | 34c2f8c | 2010-12-22 23:15:38 +0000 | [diff] [blame] | 935 | if (DependentArrayArg->getSizeExpr()) |
| 936 | return DeduceNonTypeTemplateArgument(S, NTTP, |
| 937 | DependentArrayArg->getSizeExpr(), |
| 938 | Info, Deduced); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 939 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 940 | // Incomplete type does not match a dependently-sized array type |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 941 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 942 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 943 | |
| 944 | // type(*)(T) |
| 945 | // T(*)() |
| 946 | // T(*)(T) |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 947 | case Type::FunctionProto: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 948 | const FunctionProtoType *FunctionProtoArg = |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 949 | dyn_cast<FunctionProtoType>(Arg); |
| 950 | if (!FunctionProtoArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 951 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 952 | |
| 953 | const FunctionProtoType *FunctionProtoParam = |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 954 | cast<FunctionProtoType>(Param); |
Anders Carlsson | 994b6cb | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 955 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 956 | if (FunctionProtoParam->getTypeQuals() != |
Anders Carlsson | 994b6cb | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 957 | FunctionProtoArg->getTypeQuals()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 958 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 959 | |
Anders Carlsson | 994b6cb | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 960 | if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 961 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 994b6cb | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 962 | |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 963 | // Check return types. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 964 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 965 | = DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 966 | FunctionProtoParam->getResultType(), |
| 967 | FunctionProtoArg->getResultType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 968 | Info, Deduced, 0)) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 969 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 970 | |
Douglas Gregor | 603cfb4 | 2011-01-05 23:12:31 +0000 | [diff] [blame] | 971 | return DeduceTemplateArguments(S, TemplateParams, |
| 972 | FunctionProtoParam->arg_type_begin(), |
| 973 | FunctionProtoParam->getNumArgs(), |
| 974 | FunctionProtoArg->arg_type_begin(), |
| 975 | FunctionProtoArg->getNumArgs(), |
| 976 | Info, Deduced, 0); |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 977 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 978 | |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 979 | case Type::InjectedClassName: { |
| 980 | // Treat a template's injected-class-name as if the template |
| 981 | // specialization type had been used. |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 982 | Param = cast<InjectedClassNameType>(Param) |
| 983 | ->getInjectedSpecializationType(); |
John McCall | 3cb0ebd | 2010-03-10 03:28:59 +0000 | [diff] [blame] | 984 | assert(isa<TemplateSpecializationType>(Param) && |
| 985 | "injected class name is not a template specialization type"); |
| 986 | // fall through |
| 987 | } |
| 988 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 989 | // template-name<T> (where template-name refers to a class template) |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 990 | // template-name<i> |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 991 | // TT<T> |
| 992 | // TT<i> |
| 993 | // TT<> |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 994 | case Type::TemplateSpecialization: { |
| 995 | const TemplateSpecializationType *SpecParam |
| 996 | = cast<TemplateSpecializationType>(Param); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 997 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 998 | // Try to deduce template arguments from the template-id. |
| 999 | Sema::TemplateDeductionResult Result |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1000 | = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg, |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1001 | Info, Deduced); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1002 | |
Douglas Gregor | 4a5c15f | 2009-09-30 22:13:51 +0000 | [diff] [blame] | 1003 | if (Result && (TDF & TDF_DerivedClass)) { |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1004 | // C++ [temp.deduct.call]p3b3: |
| 1005 | // If P is a class, and P has the form template-id, then A can be a |
| 1006 | // 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] | 1007 | // 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] | 1008 | // class pointed to by the deduced A. |
| 1009 | // |
| 1010 | // More importantly: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1011 | // These alternatives are considered only if type deduction would |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1012 | // otherwise fail. |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1013 | if (const RecordType *RecordT = Arg->getAs<RecordType>()) { |
| 1014 | // We cannot inspect base classes as part of deduction when the type |
| 1015 | // is incomplete, so either instantiate any templates necessary to |
| 1016 | // complete the type, or skip over it if it cannot be completed. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 1017 | if (S.RequireCompleteType(Info.getLocation(), Arg, 0)) |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1018 | return Result; |
| 1019 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1020 | // Use data recursion to crawl through the list of base classes. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1021 | // Visited contains the set of nodes we have already visited, while |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1022 | // ToVisit is our stack of records that we still need to visit. |
| 1023 | llvm::SmallPtrSet<const RecordType *, 8> Visited; |
| 1024 | llvm::SmallVector<const RecordType *, 8> ToVisit; |
| 1025 | ToVisit.push_back(RecordT); |
| 1026 | bool Successful = false; |
Douglas Gregor | 053105d | 2010-11-02 00:02:34 +0000 | [diff] [blame] | 1027 | llvm::SmallVectorImpl<DeducedTemplateArgument> DeducedOrig(0); |
| 1028 | DeducedOrig = Deduced; |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1029 | while (!ToVisit.empty()) { |
| 1030 | // Retrieve the next class in the inheritance hierarchy. |
| 1031 | const RecordType *NextT = ToVisit.back(); |
| 1032 | ToVisit.pop_back(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1033 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1034 | // If we have already seen this type, skip it. |
| 1035 | if (!Visited.insert(NextT)) |
| 1036 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1037 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1038 | // If this is a base class, try to perform template argument |
| 1039 | // deduction from it. |
| 1040 | if (NextT != RecordT) { |
| 1041 | Sema::TemplateDeductionResult BaseResult |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1042 | = DeduceTemplateArguments(S, TemplateParams, SpecParam, |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1043 | QualType(NextT, 0), Info, Deduced); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1044 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1045 | // If template argument deduction for this base was successful, |
Douglas Gregor | 053105d | 2010-11-02 00:02:34 +0000 | [diff] [blame] | 1046 | // note that we had some success. Otherwise, ignore any deductions |
| 1047 | // from this base class. |
| 1048 | if (BaseResult == Sema::TDK_Success) { |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1049 | Successful = true; |
Douglas Gregor | 053105d | 2010-11-02 00:02:34 +0000 | [diff] [blame] | 1050 | DeducedOrig = Deduced; |
| 1051 | } |
| 1052 | else |
| 1053 | Deduced = DeducedOrig; |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1054 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1055 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1056 | // Visit base classes |
| 1057 | CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl()); |
| 1058 | for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(), |
| 1059 | BaseEnd = Next->bases_end(); |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 1060 | Base != BaseEnd; ++Base) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1061 | assert(Base->getType()->isRecordType() && |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1062 | "Base class that isn't a record?"); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1063 | ToVisit.push_back(Base->getType()->getAs<RecordType>()); |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1064 | } |
| 1065 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1066 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1067 | if (Successful) |
| 1068 | return Sema::TDK_Success; |
| 1069 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1070 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1071 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1072 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 1073 | return Result; |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1076 | // T type::* |
| 1077 | // T T::* |
| 1078 | // T (type::*)() |
| 1079 | // type (T::*)() |
| 1080 | // type (type::*)(T) |
| 1081 | // type (T::*)(T) |
| 1082 | // T (type::*)(T) |
| 1083 | // T (T::*)() |
| 1084 | // T (T::*)(T) |
| 1085 | case Type::MemberPointer: { |
| 1086 | const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); |
| 1087 | const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); |
| 1088 | if (!MemPtrArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1089 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1090 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1091 | if (Sema::TemplateDeductionResult Result |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1092 | = DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1093 | MemPtrParam->getPointeeType(), |
| 1094 | MemPtrArg->getPointeeType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1095 | Info, Deduced, |
| 1096 | TDF & TDF_IgnoreQualifiers)) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1097 | return Result; |
| 1098 | |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1099 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1100 | QualType(MemPtrParam->getClass(), 0), |
| 1101 | QualType(MemPtrArg->getClass(), 0), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1102 | Info, Deduced, 0); |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 1105 | // (clang extension) |
| 1106 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1107 | // type(^)(T) |
| 1108 | // T(^)() |
| 1109 | // T(^)(T) |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1110 | case Type::BlockPointer: { |
| 1111 | const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); |
| 1112 | const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1113 | |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1114 | if (!BlockPtrArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1115 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1116 | |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1117 | return DeduceTemplateArguments(S, TemplateParams, |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1118 | BlockPtrParam->getPointeeType(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1119 | BlockPtrArg->getPointeeType(), Info, |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1120 | Deduced, 0); |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1123 | case Type::TypeOfExpr: |
| 1124 | case Type::TypeOf: |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 1125 | case Type::DependentName: |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1126 | // No template argument deduction for these types |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1127 | return Sema::TDK_Success; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1128 | |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 1129 | default: |
| 1130 | break; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | // FIXME: Many more cases to go (to go). |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1134 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1137 | static Sema::TemplateDeductionResult |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1138 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1139 | TemplateParameterList *TemplateParams, |
| 1140 | const TemplateArgument &Param, |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1141 | const TemplateArgument &Arg, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1142 | TemplateDeductionInfo &Info, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1143 | llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1144 | switch (Param.getKind()) { |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1145 | case TemplateArgument::Null: |
| 1146 | assert(false && "Null template argument in parameter list"); |
| 1147 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1148 | |
| 1149 | case TemplateArgument::Type: |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1150 | if (Arg.getKind() == TemplateArgument::Type) |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1151 | return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1152 | Arg.getAsType(), Info, Deduced, 0); |
| 1153 | Info.FirstArg = Param; |
| 1154 | Info.SecondArg = Arg; |
| 1155 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1156 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1157 | case TemplateArgument::Template: |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1158 | if (Arg.getKind() == TemplateArgument::Template) |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1159 | return DeduceTemplateArguments(S, TemplateParams, |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1160 | Param.getAsTemplate(), |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 1161 | Arg.getAsTemplate(), Info, Deduced); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1162 | Info.FirstArg = Param; |
| 1163 | Info.SecondArg = Arg; |
| 1164 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1165 | |
| 1166 | case TemplateArgument::TemplateExpansion: |
| 1167 | llvm_unreachable("caller should handle pack expansions"); |
| 1168 | break; |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1169 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1170 | case TemplateArgument::Declaration: |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1171 | if (Arg.getKind() == TemplateArgument::Declaration && |
| 1172 | Param.getAsDecl()->getCanonicalDecl() == |
| 1173 | Arg.getAsDecl()->getCanonicalDecl()) |
| 1174 | return Sema::TDK_Success; |
| 1175 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1176 | Info.FirstArg = Param; |
| 1177 | Info.SecondArg = Arg; |
| 1178 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1179 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1180 | case TemplateArgument::Integral: |
| 1181 | if (Arg.getKind() == TemplateArgument::Integral) { |
Douglas Gregor | 9d0e441 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1182 | if (hasSameExtendedValue(*Param.getAsIntegral(), *Arg.getAsIntegral())) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1183 | return Sema::TDK_Success; |
| 1184 | |
| 1185 | Info.FirstArg = Param; |
| 1186 | Info.SecondArg = Arg; |
| 1187 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1188 | } |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1189 | |
| 1190 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 1191 | Info.FirstArg = Param; |
| 1192 | Info.SecondArg = Arg; |
| 1193 | return Sema::TDK_NonDeducedMismatch; |
| 1194 | } |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1195 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1196 | Info.FirstArg = Param; |
| 1197 | Info.SecondArg = Arg; |
| 1198 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1200 | case TemplateArgument::Expression: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1201 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1202 | = getDeducedParameterFromExpr(Param.getAsExpr())) { |
| 1203 | if (Arg.getKind() == TemplateArgument::Integral) |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1204 | return DeduceNonTypeTemplateArgument(S, NTTP, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1205 | *Arg.getAsIntegral(), |
Douglas Gregor | 9d0e441 | 2010-03-26 05:50:28 +0000 | [diff] [blame] | 1206 | Arg.getIntegralType(), |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1207 | /*ArrayBound=*/false, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1208 | Info, Deduced); |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1209 | if (Arg.getKind() == TemplateArgument::Expression) |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1210 | return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1211 | Info, Deduced); |
Douglas Gregor | 15755cb | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 1212 | if (Arg.getKind() == TemplateArgument::Declaration) |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1213 | return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(), |
Douglas Gregor | 15755cb | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 1214 | Info, Deduced); |
| 1215 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1216 | Info.FirstArg = Param; |
| 1217 | Info.SecondArg = Arg; |
| 1218 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1219 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1220 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1221 | // Can't deduce anything, but that's okay. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1222 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1223 | } |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 1224 | case TemplateArgument::Pack: |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1225 | llvm_unreachable("Argument packs should be expanded by the caller!"); |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1226 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1227 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1228 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1231 | /// \brief Determine whether there is a template argument to be used for |
| 1232 | /// deduction. |
| 1233 | /// |
| 1234 | /// This routine "expands" argument packs in-place, overriding its input |
| 1235 | /// parameters so that \c Args[ArgIdx] will be the available template argument. |
| 1236 | /// |
| 1237 | /// \returns true if there is another template argument (which will be at |
| 1238 | /// \c Args[ArgIdx]), false otherwise. |
| 1239 | static bool hasTemplateArgumentForDeduction(const TemplateArgument *&Args, |
| 1240 | unsigned &ArgIdx, |
| 1241 | unsigned &NumArgs) { |
| 1242 | if (ArgIdx == NumArgs) |
| 1243 | return false; |
| 1244 | |
| 1245 | const TemplateArgument &Arg = Args[ArgIdx]; |
| 1246 | if (Arg.getKind() != TemplateArgument::Pack) |
| 1247 | return true; |
| 1248 | |
| 1249 | assert(ArgIdx == NumArgs - 1 && "Pack not at the end of argument list?"); |
| 1250 | Args = Arg.pack_begin(); |
| 1251 | NumArgs = Arg.pack_size(); |
| 1252 | ArgIdx = 0; |
| 1253 | return ArgIdx < NumArgs; |
| 1254 | } |
| 1255 | |
Douglas Gregor | 7b976ec | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1256 | /// \brief Determine whether the given set of template arguments has a pack |
| 1257 | /// expansion that is not the last template argument. |
| 1258 | static bool hasPackExpansionBeforeEnd(const TemplateArgument *Args, |
| 1259 | unsigned NumArgs) { |
| 1260 | unsigned ArgIdx = 0; |
| 1261 | while (ArgIdx < NumArgs) { |
| 1262 | const TemplateArgument &Arg = Args[ArgIdx]; |
| 1263 | |
| 1264 | // Unwrap argument packs. |
| 1265 | if (Args[ArgIdx].getKind() == TemplateArgument::Pack) { |
| 1266 | Args = Arg.pack_begin(); |
| 1267 | NumArgs = Arg.pack_size(); |
| 1268 | ArgIdx = 0; |
| 1269 | continue; |
| 1270 | } |
| 1271 | |
| 1272 | ++ArgIdx; |
| 1273 | if (ArgIdx == NumArgs) |
| 1274 | return false; |
| 1275 | |
| 1276 | if (Arg.isPackExpansion()) |
| 1277 | return true; |
| 1278 | } |
| 1279 | |
| 1280 | return false; |
| 1281 | } |
| 1282 | |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1283 | static Sema::TemplateDeductionResult |
| 1284 | DeduceTemplateArguments(Sema &S, |
| 1285 | TemplateParameterList *TemplateParams, |
| 1286 | const TemplateArgument *Params, unsigned NumParams, |
| 1287 | const TemplateArgument *Args, unsigned NumArgs, |
| 1288 | TemplateDeductionInfo &Info, |
Douglas Gregor | 0972c86 | 2010-12-22 18:55:49 +0000 | [diff] [blame] | 1289 | llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 1290 | bool NumberOfArgumentsMustMatch) { |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1291 | // C++0x [temp.deduct.type]p9: |
| 1292 | // If the template argument list of P contains a pack expansion that is not |
| 1293 | // the last template argument, the entire template argument list is a |
| 1294 | // non-deduced context. |
Douglas Gregor | 7b976ec | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 1295 | if (hasPackExpansionBeforeEnd(Params, NumParams)) |
| 1296 | return Sema::TDK_Success; |
| 1297 | |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1298 | // C++0x [temp.deduct.type]p9: |
| 1299 | // If P has a form that contains <T> or <i>, then each argument Pi of the |
| 1300 | // respective template argument list P is compared with the corresponding |
| 1301 | // argument Ai of the corresponding template argument list of A. |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1302 | unsigned ArgIdx = 0, ParamIdx = 0; |
| 1303 | for (; hasTemplateArgumentForDeduction(Params, ParamIdx, NumParams); |
| 1304 | ++ParamIdx) { |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1305 | // FIXME: Variadic templates. |
| 1306 | // What do we do if the argument is a pack expansion? |
| 1307 | |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1308 | if (!Params[ParamIdx].isPackExpansion()) { |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1309 | // The simple case: deduce template arguments by matching Pi and Ai. |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1310 | |
| 1311 | // Check whether we have enough arguments. |
| 1312 | if (!hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs)) |
Douglas Gregor | 0972c86 | 2010-12-22 18:55:49 +0000 | [diff] [blame] | 1313 | return NumberOfArgumentsMustMatch? Sema::TDK_TooFewArguments |
| 1314 | : Sema::TDK_Success; |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1315 | |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1316 | // Perform deduction for this Pi/Ai pair. |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1317 | if (Sema::TemplateDeductionResult Result |
| 1318 | = DeduceTemplateArguments(S, TemplateParams, |
| 1319 | Params[ParamIdx], Args[ArgIdx], |
| 1320 | Info, Deduced)) |
| 1321 | return Result; |
| 1322 | |
| 1323 | // Move to the next argument. |
| 1324 | ++ArgIdx; |
| 1325 | continue; |
| 1326 | } |
| 1327 | |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1328 | // The parameter is a pack expansion. |
| 1329 | |
| 1330 | // C++0x [temp.deduct.type]p9: |
| 1331 | // If Pi is a pack expansion, then the pattern of Pi is compared with |
| 1332 | // each remaining argument in the template argument list of A. Each |
| 1333 | // comparison deduces template arguments for subsequent positions in the |
| 1334 | // template parameter packs expanded by Pi. |
| 1335 | TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern(); |
| 1336 | |
| 1337 | // Compute the set of template parameter indices that correspond to |
| 1338 | // parameter packs expanded by the pack expansion. |
| 1339 | llvm::SmallVector<unsigned, 2> PackIndices; |
| 1340 | { |
| 1341 | llvm::BitVector SawIndices(TemplateParams->size()); |
| 1342 | llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 1343 | S.collectUnexpandedParameterPacks(Pattern, Unexpanded); |
| 1344 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 1345 | unsigned Depth, Index; |
| 1346 | llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); |
| 1347 | if (Depth == 0 && !SawIndices[Index]) { |
| 1348 | SawIndices[Index] = true; |
| 1349 | PackIndices.push_back(Index); |
| 1350 | } |
| 1351 | } |
| 1352 | } |
| 1353 | assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?"); |
| 1354 | |
| 1355 | // FIXME: If there are no remaining arguments, we can bail out early |
| 1356 | // and set any deduced parameter packs to an empty argument pack. |
| 1357 | // The latter part of this is a (minor) correctness issue. |
| 1358 | |
| 1359 | // Save the deduced template arguments for each parameter pack expanded |
| 1360 | // by this pack expansion, then clear out the deduction. |
| 1361 | llvm::SmallVector<DeducedTemplateArgument, 2> |
| 1362 | SavedPacks(PackIndices.size()); |
| 1363 | for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) { |
| 1364 | SavedPacks[I] = Deduced[PackIndices[I]]; |
| 1365 | Deduced[PackIndices[I]] = DeducedTemplateArgument(); |
| 1366 | } |
| 1367 | |
| 1368 | // Keep track of the deduced template arguments for each parameter pack |
| 1369 | // expanded by this pack expansion (the outer index) and for each |
| 1370 | // template argument (the inner SmallVectors). |
| 1371 | llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2> |
| 1372 | NewlyDeducedPacks(PackIndices.size()); |
| 1373 | bool HasAnyArguments = false; |
| 1374 | while (hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs)) { |
| 1375 | HasAnyArguments = true; |
| 1376 | |
| 1377 | // Deduce template arguments from the pattern. |
| 1378 | if (Sema::TemplateDeductionResult Result |
| 1379 | = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx], |
| 1380 | Info, Deduced)) |
| 1381 | return Result; |
| 1382 | |
| 1383 | // Capture the deduced template arguments for each parameter pack expanded |
| 1384 | // by this pack expansion, add them to the list of arguments we've deduced |
| 1385 | // for that pack, then clear out the deduced argument. |
| 1386 | for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) { |
| 1387 | DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]]; |
| 1388 | if (!DeducedArg.isNull()) { |
| 1389 | NewlyDeducedPacks[I].push_back(DeducedArg); |
| 1390 | DeducedArg = DeducedTemplateArgument(); |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | ++ArgIdx; |
| 1395 | } |
| 1396 | |
| 1397 | // Build argument packs for each of the parameter packs expanded by this |
| 1398 | // pack expansion. |
| 1399 | for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) { |
| 1400 | if (HasAnyArguments && NewlyDeducedPacks[I].empty()) { |
| 1401 | // We were not able to deduce anything for this parameter pack, |
| 1402 | // so just restore the saved argument pack. |
| 1403 | Deduced[PackIndices[I]] = SavedPacks[I]; |
| 1404 | continue; |
| 1405 | } |
| 1406 | |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1407 | DeducedTemplateArgument NewPack; |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1408 | |
| 1409 | if (NewlyDeducedPacks[I].empty()) { |
| 1410 | // If we deduced an empty argument pack, create it now. |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1411 | NewPack = DeducedTemplateArgument(TemplateArgument(0, 0)); |
| 1412 | } else { |
| 1413 | TemplateArgument *ArgumentPack |
| 1414 | = new (S.Context) TemplateArgument [NewlyDeducedPacks[I].size()]; |
| 1415 | std::copy(NewlyDeducedPacks[I].begin(), NewlyDeducedPacks[I].end(), |
| 1416 | ArgumentPack); |
| 1417 | NewPack |
| 1418 | = DeducedTemplateArgument(TemplateArgument(ArgumentPack, |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1419 | NewlyDeducedPacks[I].size()), |
Douglas Gregor | 0d80abc | 2010-12-22 23:09:49 +0000 | [diff] [blame] | 1420 | NewlyDeducedPacks[I][0].wasDeducedFromArrayBound()); |
| 1421 | } |
| 1422 | |
| 1423 | DeducedTemplateArgument Result |
| 1424 | = checkDeducedTemplateArguments(S.Context, SavedPacks[I], NewPack); |
| 1425 | if (Result.isNull()) { |
| 1426 | Info.Param |
| 1427 | = makeTemplateParameter(TemplateParams->getParam(PackIndices[I])); |
| 1428 | Info.FirstArg = SavedPacks[I]; |
| 1429 | Info.SecondArg = NewPack; |
| 1430 | return Sema::TDK_Inconsistent; |
| 1431 | } |
| 1432 | |
| 1433 | Deduced[PackIndices[I]] = Result; |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1434 | } |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | // If there is an argument remaining, then we had too many arguments. |
Douglas Gregor | 0972c86 | 2010-12-22 18:55:49 +0000 | [diff] [blame] | 1438 | if (NumberOfArgumentsMustMatch && |
| 1439 | hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs)) |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1440 | return Sema::TDK_TooManyArguments; |
| 1441 | |
| 1442 | return Sema::TDK_Success; |
| 1443 | } |
| 1444 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1445 | static Sema::TemplateDeductionResult |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1446 | DeduceTemplateArguments(Sema &S, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1447 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1448 | const TemplateArgumentList &ParamList, |
| 1449 | const TemplateArgumentList &ArgList, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1450 | TemplateDeductionInfo &Info, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1451 | llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) { |
Douglas Gregor | 20a55e2 | 2010-12-22 18:17:10 +0000 | [diff] [blame] | 1452 | return DeduceTemplateArguments(S, TemplateParams, |
| 1453 | ParamList.data(), ParamList.size(), |
| 1454 | ArgList.data(), ArgList.size(), |
| 1455 | Info, Deduced); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1458 | /// \brief Determine whether two template arguments are the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1459 | static bool isSameTemplateArg(ASTContext &Context, |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1460 | const TemplateArgument &X, |
| 1461 | const TemplateArgument &Y) { |
| 1462 | if (X.getKind() != Y.getKind()) |
| 1463 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1464 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1465 | switch (X.getKind()) { |
| 1466 | case TemplateArgument::Null: |
| 1467 | assert(false && "Comparing NULL template argument"); |
| 1468 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1469 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1470 | case TemplateArgument::Type: |
| 1471 | return Context.getCanonicalType(X.getAsType()) == |
| 1472 | Context.getCanonicalType(Y.getAsType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1473 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1474 | case TemplateArgument::Declaration: |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 1475 | return X.getAsDecl()->getCanonicalDecl() == |
| 1476 | Y.getAsDecl()->getCanonicalDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1477 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1478 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1479 | case TemplateArgument::TemplateExpansion: |
| 1480 | return Context.getCanonicalTemplateName( |
| 1481 | X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() == |
| 1482 | Context.getCanonicalTemplateName( |
| 1483 | Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer(); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1484 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1485 | case TemplateArgument::Integral: |
| 1486 | return *X.getAsIntegral() == *Y.getAsIntegral(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1487 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 1488 | case TemplateArgument::Expression: { |
| 1489 | llvm::FoldingSetNodeID XID, YID; |
| 1490 | X.getAsExpr()->Profile(XID, Context, true); |
| 1491 | Y.getAsExpr()->Profile(YID, Context, true); |
| 1492 | return XID == YID; |
| 1493 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1494 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1495 | case TemplateArgument::Pack: |
| 1496 | if (X.pack_size() != Y.pack_size()) |
| 1497 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1498 | |
| 1499 | for (TemplateArgument::pack_iterator XP = X.pack_begin(), |
| 1500 | XPEnd = X.pack_end(), |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1501 | YP = Y.pack_begin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1502 | XP != XPEnd; ++XP, ++YP) |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1503 | if (!isSameTemplateArg(Context, *XP, *YP)) |
| 1504 | return false; |
| 1505 | |
| 1506 | return true; |
| 1507 | } |
| 1508 | |
| 1509 | return false; |
| 1510 | } |
| 1511 | |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1512 | /// \brief Allocate a TemplateArgumentLoc where all locations have |
| 1513 | /// been initialized to the given location. |
| 1514 | /// |
| 1515 | /// \param S The semantic analysis object. |
| 1516 | /// |
| 1517 | /// \param The template argument we are producing template argument |
| 1518 | /// location information for. |
| 1519 | /// |
| 1520 | /// \param NTTPType For a declaration template argument, the type of |
| 1521 | /// the non-type template parameter that corresponds to this template |
| 1522 | /// argument. |
| 1523 | /// |
| 1524 | /// \param Loc The source location to use for the resulting template |
| 1525 | /// argument. |
| 1526 | static TemplateArgumentLoc |
| 1527 | getTrivialTemplateArgumentLoc(Sema &S, |
| 1528 | const TemplateArgument &Arg, |
| 1529 | QualType NTTPType, |
| 1530 | SourceLocation Loc) { |
| 1531 | switch (Arg.getKind()) { |
| 1532 | case TemplateArgument::Null: |
| 1533 | llvm_unreachable("Can't get a NULL template argument here"); |
| 1534 | break; |
| 1535 | |
| 1536 | case TemplateArgument::Type: |
| 1537 | return TemplateArgumentLoc(Arg, |
| 1538 | S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc)); |
| 1539 | |
| 1540 | case TemplateArgument::Declaration: { |
| 1541 | Expr *E |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 1542 | = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1543 | .takeAs<Expr>(); |
| 1544 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 1545 | } |
| 1546 | |
| 1547 | case TemplateArgument::Integral: { |
| 1548 | Expr *E |
Douglas Gregor | ba68eca | 2011-01-05 17:40:24 +0000 | [diff] [blame] | 1549 | = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).takeAs<Expr>(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1550 | return TemplateArgumentLoc(TemplateArgument(E), E); |
| 1551 | } |
| 1552 | |
| 1553 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 1554 | return TemplateArgumentLoc(Arg, SourceRange(), Loc); |
| 1555 | |
| 1556 | case TemplateArgument::TemplateExpansion: |
| 1557 | return TemplateArgumentLoc(Arg, SourceRange(), Loc, Loc); |
| 1558 | |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1559 | case TemplateArgument::Expression: |
| 1560 | return TemplateArgumentLoc(Arg, Arg.getAsExpr()); |
| 1561 | |
| 1562 | case TemplateArgument::Pack: |
| 1563 | return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo()); |
| 1564 | } |
| 1565 | |
| 1566 | return TemplateArgumentLoc(); |
| 1567 | } |
| 1568 | |
| 1569 | |
| 1570 | /// \brief Convert the given deduced template argument and add it to the set of |
| 1571 | /// fully-converted template arguments. |
| 1572 | static bool ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param, |
| 1573 | DeducedTemplateArgument Arg, |
| 1574 | NamedDecl *Template, |
| 1575 | QualType NTTPType, |
| 1576 | TemplateDeductionInfo &Info, |
| 1577 | bool InFunctionTemplate, |
| 1578 | llvm::SmallVectorImpl<TemplateArgument> &Output) { |
| 1579 | if (Arg.getKind() == TemplateArgument::Pack) { |
| 1580 | // This is a template argument pack, so check each of its arguments against |
| 1581 | // the template parameter. |
| 1582 | llvm::SmallVector<TemplateArgument, 2> PackedArgsBuilder; |
| 1583 | for (TemplateArgument::pack_iterator PA = Arg.pack_begin(), |
Douglas Gregor | 135ffa7 | 2011-01-05 21:00:53 +0000 | [diff] [blame] | 1584 | PAEnd = Arg.pack_end(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1585 | PA != PAEnd; ++PA) { |
Douglas Gregor | d53e16a | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 1586 | // When converting the deduced template argument, append it to the |
| 1587 | // general output list. We need to do this so that the template argument |
| 1588 | // checking logic has all of the prior template arguments available. |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1589 | DeducedTemplateArgument InnerArg(*PA); |
| 1590 | InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound()); |
| 1591 | if (ConvertDeducedTemplateArgument(S, Param, InnerArg, Template, |
| 1592 | NTTPType, Info, |
Douglas Gregor | d53e16a | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 1593 | InFunctionTemplate, Output)) |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1594 | return true; |
Douglas Gregor | d53e16a | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 1595 | |
| 1596 | // Move the converted template argument into our argument pack. |
| 1597 | PackedArgsBuilder.push_back(Output.back()); |
| 1598 | Output.pop_back(); |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | // Create the resulting argument pack. |
| 1602 | TemplateArgument *PackedArgs = 0; |
| 1603 | if (!PackedArgsBuilder.empty()) { |
| 1604 | PackedArgs = new (S.Context) TemplateArgument[PackedArgsBuilder.size()]; |
| 1605 | std::copy(PackedArgsBuilder.begin(), PackedArgsBuilder.end(), PackedArgs); |
| 1606 | } |
| 1607 | Output.push_back(TemplateArgument(PackedArgs, PackedArgsBuilder.size())); |
| 1608 | return false; |
| 1609 | } |
| 1610 | |
| 1611 | // Convert the deduced template argument into a template |
| 1612 | // argument that we can check, almost as if the user had written |
| 1613 | // the template argument explicitly. |
| 1614 | TemplateArgumentLoc ArgLoc = getTrivialTemplateArgumentLoc(S, Arg, NTTPType, |
| 1615 | Info.getLocation()); |
| 1616 | |
| 1617 | // Check the template argument, converting it as necessary. |
| 1618 | return S.CheckTemplateArgument(Param, ArgLoc, |
| 1619 | Template, |
| 1620 | Template->getLocation(), |
| 1621 | Template->getSourceRange().getEnd(), |
| 1622 | Output, |
| 1623 | InFunctionTemplate |
| 1624 | ? (Arg.wasDeducedFromArrayBound() |
| 1625 | ? Sema::CTAK_DeducedFromArrayBound |
| 1626 | : Sema::CTAK_Deduced) |
| 1627 | : Sema::CTAK_Specified); |
| 1628 | } |
| 1629 | |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 1630 | /// Complete template argument deduction for a class template partial |
| 1631 | /// specialization. |
| 1632 | static Sema::TemplateDeductionResult |
| 1633 | FinishTemplateArgumentDeduction(Sema &S, |
| 1634 | ClassTemplatePartialSpecializationDecl *Partial, |
| 1635 | const TemplateArgumentList &TemplateArgs, |
| 1636 | llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1637 | TemplateDeductionInfo &Info) { |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 1638 | // Trap errors. |
| 1639 | Sema::SFINAETrap Trap(S); |
| 1640 | |
| 1641 | Sema::ContextRAII SavedContext(S, Partial); |
| 1642 | |
| 1643 | // C++ [temp.deduct.type]p2: |
| 1644 | // [...] or if any template argument remains neither deduced nor |
| 1645 | // explicitly specified, template argument deduction fails. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1646 | llvm::SmallVector<TemplateArgument, 4> Builder; |
Douglas Gregor | 033a3ca | 2011-01-04 22:23:38 +0000 | [diff] [blame] | 1647 | TemplateParameterList *PartialParams = Partial->getTemplateParameters(); |
| 1648 | for (unsigned I = 0, N = PartialParams->size(); I != N; ++I) { |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1649 | NamedDecl *Param = PartialParams->getParam(I); |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 1650 | if (Deduced[I].isNull()) { |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1651 | Info.Param = makeTemplateParameter(Param); |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 1652 | return Sema::TDK_Incomplete; |
| 1653 | } |
| 1654 | |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1655 | // We have deduced this argument, so it still needs to be |
| 1656 | // checked and converted. |
| 1657 | |
| 1658 | // First, for a non-type template parameter type that is |
| 1659 | // initialized by a declaration, we need the type of the |
| 1660 | // corresponding non-type template parameter. |
| 1661 | QualType NTTPType; |
| 1662 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | d53e16a | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 1663 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1664 | NTTPType = NTTP->getType(); |
Douglas Gregor | d53e16a | 2011-01-05 20:52:18 +0000 | [diff] [blame] | 1665 | if (NTTPType->isDependentType()) { |
| 1666 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 1667 | Builder.data(), Builder.size()); |
| 1668 | NTTPType = S.SubstType(NTTPType, |
| 1669 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 1670 | NTTP->getLocation(), |
| 1671 | NTTP->getDeclName()); |
| 1672 | if (NTTPType.isNull()) { |
| 1673 | Info.Param = makeTemplateParameter(Param); |
| 1674 | // FIXME: These template arguments are temporary. Free them! |
| 1675 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, |
| 1676 | Builder.data(), |
| 1677 | Builder.size())); |
| 1678 | return Sema::TDK_SubstitutionFailure; |
| 1679 | } |
| 1680 | } |
| 1681 | } |
| 1682 | |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1683 | if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], |
| 1684 | Partial, NTTPType, Info, false, |
| 1685 | Builder)) { |
| 1686 | Info.Param = makeTemplateParameter(Param); |
| 1687 | // FIXME: These template arguments are temporary. Free them! |
| 1688 | Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(), |
| 1689 | Builder.size())); |
| 1690 | return Sema::TDK_SubstitutionFailure; |
| 1691 | } |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | // Form the template argument list from the deduced template arguments. |
| 1695 | TemplateArgumentList *DeducedArgumentList |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1696 | = TemplateArgumentList::CreateCopy(S.Context, Builder.data(), |
| 1697 | Builder.size()); |
| 1698 | |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 1699 | Info.reset(DeducedArgumentList); |
| 1700 | |
| 1701 | // Substitute the deduced template arguments into the template |
| 1702 | // arguments of the class template partial specialization, and |
| 1703 | // verify that the instantiated template arguments are both valid |
| 1704 | // and are equivalent to the template arguments originally provided |
| 1705 | // to the class template. |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 1706 | LocalInstantiationScope InstScope(S); |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 1707 | ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate(); |
| 1708 | const TemplateArgumentLoc *PartialTemplateArgs |
| 1709 | = Partial->getTemplateArgsAsWritten(); |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 1710 | |
| 1711 | // Note that we don't provide the langle and rangle locations. |
| 1712 | TemplateArgumentListInfo InstArgs; |
| 1713 | |
Douglas Gregor | e02e262 | 2010-12-22 21:19:48 +0000 | [diff] [blame] | 1714 | if (S.Subst(PartialTemplateArgs, |
| 1715 | Partial->getNumTemplateArgsAsWritten(), |
| 1716 | InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { |
| 1717 | unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; |
| 1718 | if (ParamIdx >= Partial->getTemplateParameters()->size()) |
| 1719 | ParamIdx = Partial->getTemplateParameters()->size() - 1; |
| 1720 | |
| 1721 | Decl *Param |
| 1722 | = const_cast<NamedDecl *>( |
| 1723 | Partial->getTemplateParameters()->getParam(ParamIdx)); |
| 1724 | Info.Param = makeTemplateParameter(Param); |
| 1725 | Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); |
| 1726 | return Sema::TDK_SubstitutionFailure; |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1729 | llvm::SmallVector<TemplateArgument, 4> ConvertedInstArgs; |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 1730 | if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(), |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1731 | InstArgs, false, ConvertedInstArgs)) |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 1732 | return Sema::TDK_SubstitutionFailure; |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 1733 | |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 1734 | TemplateParameterList *TemplateParams |
| 1735 | = ClassTemplate->getTemplateParameters(); |
| 1736 | for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1737 | TemplateArgument InstArg = ConvertedInstArgs.data()[I]; |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 1738 | if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { |
Douglas Gregor | 2fdc5e8 | 2011-01-05 00:13:17 +0000 | [diff] [blame] | 1739 | Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 1740 | Info.FirstArg = TemplateArgs[I]; |
| 1741 | Info.SecondArg = InstArg; |
| 1742 | return Sema::TDK_NonDeducedMismatch; |
| 1743 | } |
| 1744 | } |
| 1745 | |
| 1746 | if (Trap.hasErrorOccurred()) |
| 1747 | return Sema::TDK_SubstitutionFailure; |
| 1748 | |
| 1749 | return Sema::TDK_Success; |
| 1750 | } |
| 1751 | |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 1752 | /// \brief Perform template argument deduction to determine whether |
| 1753 | /// the given template arguments match the given class template |
| 1754 | /// partial specialization per C++ [temp.class.spec.match]. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1755 | Sema::TemplateDeductionResult |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1756 | Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1757 | const TemplateArgumentList &TemplateArgs, |
| 1758 | TemplateDeductionInfo &Info) { |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 1759 | // C++ [temp.class.spec.match]p2: |
| 1760 | // A partial specialization matches a given actual template |
| 1761 | // argument list if the template arguments of the partial |
| 1762 | // specialization can be deduced from the actual template argument |
| 1763 | // list (14.8.2). |
Douglas Gregor | bb26041 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 1764 | SFINAETrap Trap(*this); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1765 | llvm::SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1766 | Deduced.resize(Partial->getTemplateParameters()->size()); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1767 | if (TemplateDeductionResult Result |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 1768 | = ::DeduceTemplateArguments(*this, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1769 | Partial->getTemplateParameters(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1770 | Partial->getTemplateArgs(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1771 | TemplateArgs, Info, Deduced)) |
| 1772 | return Result; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1773 | |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1774 | InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial, |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1775 | Deduced.data(), Deduced.size(), Info); |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 1776 | if (Inst) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1777 | return TDK_InstantiationDepth; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 1778 | |
Douglas Gregor | bb26041 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 1779 | if (Trap.hasErrorOccurred()) |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 1780 | return Sema::TDK_SubstitutionFailure; |
| 1781 | |
| 1782 | return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs, |
| 1783 | Deduced, Info); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1784 | } |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1785 | |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1786 | /// \brief Determine whether the given type T is a simple-template-id type. |
| 1787 | static bool isSimpleTemplateIdType(QualType T) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1788 | if (const TemplateSpecializationType *Spec |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1789 | = T->getAs<TemplateSpecializationType>()) |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1790 | return Spec->getTemplateName().getAsTemplateDecl() != 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1791 | |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1792 | return false; |
| 1793 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1794 | |
| 1795 | /// \brief Substitute the explicitly-provided template arguments into the |
| 1796 | /// given function template according to C++ [temp.arg.explicit]. |
| 1797 | /// |
| 1798 | /// \param FunctionTemplate the function template into which the explicit |
| 1799 | /// template arguments will be substituted. |
| 1800 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1801 | /// \param ExplicitTemplateArguments the explicitly-specified template |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1802 | /// arguments. |
| 1803 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1804 | /// \param Deduced the deduced template arguments, which will be populated |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1805 | /// with the converted and checked explicit template arguments. |
| 1806 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1807 | /// \param ParamTypes will be populated with the instantiated function |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1808 | /// parameters. |
| 1809 | /// |
| 1810 | /// \param FunctionType if non-NULL, the result type of the function template |
| 1811 | /// will also be instantiated and the pointed-to value will be updated with |
| 1812 | /// the instantiated function type. |
| 1813 | /// |
| 1814 | /// \param Info if substitution fails for any reason, this object will be |
| 1815 | /// populated with more information about the failure. |
| 1816 | /// |
| 1817 | /// \returns TDK_Success if substitution was successful, or some failure |
| 1818 | /// condition. |
| 1819 | Sema::TemplateDeductionResult |
| 1820 | Sema::SubstituteExplicitTemplateArguments( |
| 1821 | FunctionTemplateDecl *FunctionTemplate, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1822 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1823 | llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1824 | llvm::SmallVectorImpl<QualType> &ParamTypes, |
| 1825 | QualType *FunctionType, |
| 1826 | TemplateDeductionInfo &Info) { |
| 1827 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 1828 | TemplateParameterList *TemplateParams |
| 1829 | = FunctionTemplate->getTemplateParameters(); |
| 1830 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1831 | if (ExplicitTemplateArgs.size() == 0) { |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1832 | // No arguments to substitute; just copy over the parameter types and |
| 1833 | // fill in the function type. |
| 1834 | for (FunctionDecl::param_iterator P = Function->param_begin(), |
| 1835 | PEnd = Function->param_end(); |
| 1836 | P != PEnd; |
| 1837 | ++P) |
| 1838 | ParamTypes.push_back((*P)->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1839 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1840 | if (FunctionType) |
| 1841 | *FunctionType = Function->getType(); |
| 1842 | return TDK_Success; |
| 1843 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1844 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1845 | // Substitution of the explicit template arguments into a function template |
| 1846 | /// is a SFINAE context. Trap any errors that might occur. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1847 | SFINAETrap Trap(*this); |
| 1848 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1849 | // C++ [temp.arg.explicit]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1850 | // Template arguments that are present shall be specified in the |
| 1851 | // declaration order of their corresponding template-parameters. The |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1852 | // template argument list shall not specify more template-arguments than |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1853 | // there are corresponding template-parameters. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1854 | llvm::SmallVector<TemplateArgument, 4> Builder; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1855 | |
| 1856 | // Enter a new template instantiation context where we check the |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1857 | // explicitly-specified template arguments against this function template, |
| 1858 | // and then substitute them into the function parameter types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1859 | InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1860 | FunctionTemplate, Deduced.data(), Deduced.size(), |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1861 | ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution, |
| 1862 | Info); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1863 | if (Inst) |
| 1864 | return TDK_InstantiationDepth; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1865 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1866 | if (CheckTemplateArgumentList(FunctionTemplate, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1867 | SourceLocation(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1868 | ExplicitTemplateArgs, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1869 | true, |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 1870 | Builder) || Trap.hasErrorOccurred()) { |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1871 | unsigned Index = Builder.size(); |
Douglas Gregor | fe52c91 | 2010-05-09 01:26:06 +0000 | [diff] [blame] | 1872 | if (Index >= TemplateParams->size()) |
| 1873 | Index = TemplateParams->size() - 1; |
| 1874 | Info.Param = makeTemplateParameter(TemplateParams->getParam(Index)); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1875 | return TDK_InvalidExplicitArguments; |
Douglas Gregor | f1a8445 | 2010-05-08 19:15:54 +0000 | [diff] [blame] | 1876 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1877 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1878 | // Form the template argument list from the explicitly-specified |
| 1879 | // template arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1880 | TemplateArgumentList *ExplicitArgumentList |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1881 | = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1882 | Info.reset(ExplicitArgumentList); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1883 | |
John McCall | df41f18 | 2010-10-12 19:40:14 +0000 | [diff] [blame] | 1884 | // Template argument deduction and the final substitution should be |
| 1885 | // done in the context of the templated declaration. Explicit |
| 1886 | // argument substitution, on the other hand, needs to happen in the |
| 1887 | // calling context. |
| 1888 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
| 1889 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1890 | // Instantiate the types of each of the function parameters given the |
| 1891 | // explicitly-specified template arguments. |
Douglas Gregor | a009b59 | 2011-01-07 00:20:55 +0000 | [diff] [blame^] | 1892 | if (SubstParmTypes(Function->getLocation(), |
| 1893 | Function->param_begin(), Function->getNumParams(), |
| 1894 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 1895 | ParamTypes)) |
| 1896 | return TDK_SubstitutionFailure; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1897 | |
| 1898 | // If the caller wants a full function type back, instantiate the return |
| 1899 | // type and form that function type. |
| 1900 | if (FunctionType) { |
| 1901 | // FIXME: exception-specifications? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1902 | const FunctionProtoType *Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1903 | = Function->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1904 | assert(Proto && "Function template does not have a prototype?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1905 | |
| 1906 | QualType ResultType |
Douglas Gregor | 357bbd0 | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 1907 | = SubstType(Proto->getResultType(), |
| 1908 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 1909 | Function->getTypeSpecStartLoc(), |
| 1910 | Function->getDeclName()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1911 | if (ResultType.isNull() || Trap.hasErrorOccurred()) |
| 1912 | return TDK_SubstitutionFailure; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1913 | |
| 1914 | *FunctionType = BuildFunctionType(ResultType, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1915 | ParamTypes.data(), ParamTypes.size(), |
| 1916 | Proto->isVariadic(), |
| 1917 | Proto->getTypeQuals(), |
| 1918 | Function->getLocation(), |
Eli Friedman | fa86954 | 2010-08-05 02:54:05 +0000 | [diff] [blame] | 1919 | Function->getDeclName(), |
| 1920 | Proto->getExtInfo()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1921 | if (FunctionType->isNull() || Trap.hasErrorOccurred()) |
| 1922 | return TDK_SubstitutionFailure; |
| 1923 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1924 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1925 | // C++ [temp.arg.explicit]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1926 | // Trailing template arguments that can be deduced (14.8.2) may be |
| 1927 | // omitted from the list of explicit template-arguments. If all of the |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1928 | // template arguments can be deduced, they may all be omitted; in this |
| 1929 | // case, the empty template argument list <> itself may also be omitted. |
| 1930 | // |
| 1931 | // Take all of the explicitly-specified arguments and put them into the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1932 | // set of deduced template arguments. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1933 | Deduced.reserve(TemplateParams->size()); |
| 1934 | for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1935 | Deduced.push_back(ExplicitArgumentList->get(I)); |
| 1936 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1937 | return TDK_Success; |
| 1938 | } |
| 1939 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1940 | /// \brief Finish template argument deduction for a function template, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1941 | /// checking the deduced template arguments for completeness and forming |
| 1942 | /// the function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1943 | Sema::TemplateDeductionResult |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1944 | Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1945 | llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 1946 | unsigned NumExplicitlySpecified, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1947 | FunctionDecl *&Specialization, |
| 1948 | TemplateDeductionInfo &Info) { |
| 1949 | TemplateParameterList *TemplateParams |
| 1950 | = FunctionTemplate->getTemplateParameters(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1951 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1952 | // Template argument deduction for function templates in a SFINAE context. |
| 1953 | // Trap any errors that might occur. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1954 | SFINAETrap Trap(*this); |
| 1955 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1956 | // Enter a new template instantiation context while we instantiate the |
| 1957 | // actual function declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1958 | InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1959 | FunctionTemplate, Deduced.data(), Deduced.size(), |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 1960 | ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution, |
| 1961 | Info); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1962 | if (Inst) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1963 | return TDK_InstantiationDepth; |
| 1964 | |
John McCall | 96db310 | 2010-04-29 01:18:58 +0000 | [diff] [blame] | 1965 | ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); |
John McCall | f581382 | 2010-04-29 00:35:03 +0000 | [diff] [blame] | 1966 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 1967 | // C++ [temp.deduct.type]p2: |
| 1968 | // [...] or if any template argument remains neither deduced nor |
| 1969 | // explicitly specified, template argument deduction fails. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1970 | llvm::SmallVector<TemplateArgument, 4> Builder; |
Douglas Gregor | b9a7d6f | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 1971 | for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { |
| 1972 | NamedDecl *Param = TemplateParams->getParam(I); |
Douglas Gregor | ea6c96f | 2010-12-23 01:52:01 +0000 | [diff] [blame] | 1973 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 1974 | if (!Deduced[I].isNull()) { |
Douglas Gregor | 3273b0c | 2010-10-12 18:51:08 +0000 | [diff] [blame] | 1975 | if (I < NumExplicitlySpecified) { |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1976 | // We have already fully type-checked and converted this |
Douglas Gregor | 3273b0c | 2010-10-12 18:51:08 +0000 | [diff] [blame] | 1977 | // argument, because it was explicitly-specified. Just record the |
| 1978 | // presence of this argument. |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 1979 | Builder.push_back(Deduced[I]); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 1980 | continue; |
| 1981 | } |
| 1982 | |
| 1983 | // We have deduced this argument, so it still needs to be |
| 1984 | // checked and converted. |
| 1985 | |
| 1986 | // First, for a non-type template parameter type that is |
| 1987 | // initialized by a declaration, we need the type of the |
| 1988 | // corresponding non-type template parameter. |
| 1989 | QualType NTTPType; |
| 1990 | if (NonTypeTemplateParmDecl *NTTP |
| 1991 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
Douglas Gregor | b9a7d6f | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 1992 | NTTPType = NTTP->getType(); |
| 1993 | if (NTTPType->isDependentType()) { |
| 1994 | TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, |
| 1995 | Builder.data(), Builder.size()); |
| 1996 | NTTPType = SubstType(NTTPType, |
| 1997 | MultiLevelTemplateArgumentList(TemplateArgs), |
| 1998 | NTTP->getLocation(), |
| 1999 | NTTP->getDeclName()); |
| 2000 | if (NTTPType.isNull()) { |
| 2001 | Info.Param = makeTemplateParameter(Param); |
| 2002 | // FIXME: These template arguments are temporary. Free them! |
| 2003 | Info.reset(TemplateArgumentList::CreateCopy(Context, |
| 2004 | Builder.data(), |
| 2005 | Builder.size())); |
| 2006 | return TDK_SubstitutionFailure; |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2007 | } |
| 2008 | } |
| 2009 | } |
| 2010 | |
Douglas Gregor | b9a7d6f | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2011 | if (ConvertDeducedTemplateArgument(*this, Param, Deduced[I], |
| 2012 | FunctionTemplate, NTTPType, Info, |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 2013 | true, Builder)) { |
Douglas Gregor | b9a7d6f | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2014 | Info.Param = makeTemplateParameter(Param); |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2015 | // FIXME: These template arguments are temporary. Free them! |
| 2016 | Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(), |
Douglas Gregor | b9a7d6f | 2011-01-04 22:13:36 +0000 | [diff] [blame] | 2017 | Builder.size())); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2018 | return TDK_SubstitutionFailure; |
| 2019 | } |
| 2020 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2021 | continue; |
| 2022 | } |
Douglas Gregor | ea6c96f | 2010-12-23 01:52:01 +0000 | [diff] [blame] | 2023 | |
| 2024 | // C++0x [temp.arg.explicit]p3: |
| 2025 | // A trailing template parameter pack (14.5.3) not otherwise deduced will |
| 2026 | // be deduced to an empty sequence of template arguments. |
| 2027 | // FIXME: Where did the word "trailing" come from? |
| 2028 | if (Param->isTemplateParameterPack()) { |
| 2029 | Builder.push_back(TemplateArgument(0, 0)); |
| 2030 | continue; |
| 2031 | } |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2032 | |
| 2033 | // Substitute into the default template argument, if available. |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2034 | TemplateArgumentLoc DefArg |
| 2035 | = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate, |
| 2036 | FunctionTemplate->getLocation(), |
| 2037 | FunctionTemplate->getSourceRange().getEnd(), |
| 2038 | Param, |
| 2039 | Builder); |
| 2040 | |
| 2041 | // If there was no default argument, deduction is incomplete. |
| 2042 | if (DefArg.getArgument().isNull()) { |
| 2043 | Info.Param = makeTemplateParameter( |
| 2044 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
| 2045 | return TDK_Incomplete; |
| 2046 | } |
| 2047 | |
| 2048 | // Check whether we can actually use the default argument. |
| 2049 | if (CheckTemplateArgument(Param, DefArg, |
| 2050 | FunctionTemplate, |
| 2051 | FunctionTemplate->getLocation(), |
| 2052 | FunctionTemplate->getSourceRange().getEnd(), |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2053 | Builder, |
| 2054 | CTAK_Deduced)) { |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2055 | Info.Param = makeTemplateParameter( |
| 2056 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2057 | // FIXME: These template arguments are temporary. Free them! |
| 2058 | Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(), |
| 2059 | Builder.size())); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2060 | return TDK_SubstitutionFailure; |
| 2061 | } |
| 2062 | |
| 2063 | // If we get here, we successfully used the default template argument. |
| 2064 | } |
| 2065 | |
| 2066 | // Form the template argument list from the deduced template arguments. |
| 2067 | TemplateArgumentList *DeducedArgumentList |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 2068 | = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size()); |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 2069 | Info.reset(DeducedArgumentList); |
| 2070 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2071 | // Substitute the deduced template arguments into the function template |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2072 | // declaration to produce the function template specialization. |
Douglas Gregor | d4598a2 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 2073 | DeclContext *Owner = FunctionTemplate->getDeclContext(); |
| 2074 | if (FunctionTemplate->getFriendObjectKind()) |
| 2075 | Owner = FunctionTemplate->getLexicalDeclContext(); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2076 | Specialization = cast_or_null<FunctionDecl>( |
Douglas Gregor | d4598a2 | 2010-04-28 04:52:24 +0000 | [diff] [blame] | 2077 | SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner, |
Douglas Gregor | 357bbd0 | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 2078 | MultiLevelTemplateArgumentList(*DeducedArgumentList))); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2079 | if (!Specialization) |
| 2080 | return TDK_SubstitutionFailure; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2081 | |
Douglas Gregor | f882574 | 2009-09-15 18:26:13 +0000 | [diff] [blame] | 2082 | assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == |
| 2083 | FunctionTemplate->getCanonicalDecl()); |
| 2084 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2085 | // If the template argument list is owned by the function template |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2086 | // specialization, release it. |
Douglas Gregor | ec20f46 | 2010-05-08 20:07:26 +0000 | [diff] [blame] | 2087 | if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList && |
| 2088 | !Trap.hasErrorOccurred()) |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2089 | Info.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2090 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2091 | // There may have been an error that did not prevent us from constructing a |
| 2092 | // declaration. Mark the declaration invalid and return with a substitution |
| 2093 | // failure. |
| 2094 | if (Trap.hasErrorOccurred()) { |
| 2095 | Specialization->setInvalidDecl(true); |
| 2096 | return TDK_SubstitutionFailure; |
| 2097 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2098 | |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 2099 | // If we suppressed any diagnostics while performing template argument |
| 2100 | // deduction, and if we haven't already instantiated this declaration, |
| 2101 | // keep track of these diagnostics. They'll be emitted if this specialization |
| 2102 | // is actually used. |
| 2103 | if (Info.diag_begin() != Info.diag_end()) { |
| 2104 | llvm::DenseMap<Decl *, llvm::SmallVector<PartialDiagnosticAt, 1> >::iterator |
| 2105 | Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl()); |
| 2106 | if (Pos == SuppressedDiagnostics.end()) |
| 2107 | SuppressedDiagnostics[Specialization->getCanonicalDecl()] |
| 2108 | .append(Info.diag_begin(), Info.diag_end()); |
| 2109 | } |
| 2110 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2111 | return TDK_Success; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2114 | /// Gets the type of a function for template-argument-deducton |
| 2115 | /// purposes when it's considered as part of an overload set. |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2116 | static QualType GetTypeOfFunction(ASTContext &Context, |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2117 | const OverloadExpr::FindResult &R, |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2118 | FunctionDecl *Fn) { |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2119 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2120 | if (Method->isInstance()) { |
| 2121 | // An instance method that's referenced in a form that doesn't |
| 2122 | // look like a member pointer is just invalid. |
| 2123 | if (!R.HasFormOfMemberPointer) return QualType(); |
| 2124 | |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2125 | return Context.getMemberPointerType(Fn->getType(), |
| 2126 | Context.getTypeDeclType(Method->getParent()).getTypePtr()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2127 | } |
| 2128 | |
| 2129 | if (!R.IsAddressOfOperand) return Fn->getType(); |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2130 | return Context.getPointerType(Fn->getType()); |
| 2131 | } |
| 2132 | |
| 2133 | /// Apply the deduction rules for overload sets. |
| 2134 | /// |
| 2135 | /// \return the null type if this argument should be treated as an |
| 2136 | /// undeduced context |
| 2137 | static QualType |
| 2138 | ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams, |
Douglas Gregor | 75f21af | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 2139 | Expr *Arg, QualType ParamType, |
| 2140 | bool ParamWasReference) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2141 | |
| 2142 | OverloadExpr::FindResult R = OverloadExpr::find(Arg); |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2143 | |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2144 | OverloadExpr *Ovl = R.Expression; |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2145 | |
Douglas Gregor | 75f21af | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 2146 | // C++0x [temp.deduct.call]p4 |
| 2147 | unsigned TDF = 0; |
| 2148 | if (ParamWasReference) |
| 2149 | TDF |= TDF_ParamWithReferenceType; |
| 2150 | if (R.IsAddressOfOperand) |
| 2151 | TDF |= TDF_IgnoreQualifiers; |
| 2152 | |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2153 | // If there were explicit template arguments, we can only find |
| 2154 | // something via C++ [temp.arg.explicit]p3, i.e. if the arguments |
| 2155 | // unambiguously name a full specialization. |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 2156 | if (Ovl->hasExplicitTemplateArgs()) { |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2157 | // But we can still look for an explicit specialization. |
| 2158 | if (FunctionDecl *ExplicitSpec |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 2159 | = S.ResolveSingleFunctionTemplateSpecialization(Ovl)) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2160 | return GetTypeOfFunction(S.Context, R, ExplicitSpec); |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2161 | return QualType(); |
| 2162 | } |
| 2163 | |
| 2164 | // C++0x [temp.deduct.call]p6: |
| 2165 | // When P is a function type, pointer to function type, or pointer |
| 2166 | // to member function type: |
| 2167 | |
| 2168 | if (!ParamType->isFunctionType() && |
| 2169 | !ParamType->isFunctionPointerType() && |
| 2170 | !ParamType->isMemberFunctionPointerType()) |
| 2171 | return QualType(); |
| 2172 | |
| 2173 | QualType Match; |
John McCall | 7bb12da | 2010-02-02 06:20:04 +0000 | [diff] [blame] | 2174 | for (UnresolvedSetIterator I = Ovl->decls_begin(), |
| 2175 | E = Ovl->decls_end(); I != E; ++I) { |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2176 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 2177 | |
| 2178 | // - If the argument is an overload set containing one or more |
| 2179 | // function templates, the parameter is treated as a |
| 2180 | // non-deduced context. |
| 2181 | if (isa<FunctionTemplateDecl>(D)) |
| 2182 | return QualType(); |
| 2183 | |
| 2184 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2185 | QualType ArgType = GetTypeOfFunction(S.Context, R, Fn); |
| 2186 | if (ArgType.isNull()) continue; |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2187 | |
Douglas Gregor | 75f21af | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 2188 | // Function-to-pointer conversion. |
| 2189 | if (!ParamWasReference && ParamType->isPointerType() && |
| 2190 | ArgType->isFunctionType()) |
| 2191 | ArgType = S.Context.getPointerType(ArgType); |
| 2192 | |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2193 | // - If the argument is an overload set (not containing function |
| 2194 | // templates), trial argument deduction is attempted using each |
| 2195 | // of the members of the set. If deduction succeeds for only one |
| 2196 | // of the overload set members, that member is used as the |
| 2197 | // argument value for the deduction. If deduction succeeds for |
| 2198 | // more than one member of the overload set the parameter is |
| 2199 | // treated as a non-deduced context. |
| 2200 | |
| 2201 | // We do all of this in a fresh context per C++0x [temp.deduct.type]p2: |
| 2202 | // Type deduction is done independently for each P/A pair, and |
| 2203 | // the deduced template argument values are then combined. |
| 2204 | // So we do not reject deductions which were made elsewhere. |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2205 | llvm::SmallVector<DeducedTemplateArgument, 8> |
| 2206 | Deduced(TemplateParams->size()); |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2207 | TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc()); |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2208 | Sema::TemplateDeductionResult Result |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2209 | = DeduceTemplateArguments(S, TemplateParams, |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2210 | ParamType, ArgType, |
| 2211 | Info, Deduced, TDF); |
| 2212 | if (Result) continue; |
| 2213 | if (!Match.isNull()) return QualType(); |
| 2214 | Match = ArgType; |
| 2215 | } |
| 2216 | |
| 2217 | return Match; |
| 2218 | } |
| 2219 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 2220 | /// \brief Perform the adjustments to the parameter and argument types |
| 2221 | /// described in C++ [temp.deduct.call]. |
| 2222 | /// |
| 2223 | /// \returns true if the caller should not attempt to perform any template |
| 2224 | /// argument deduction based on this P/A pair. |
| 2225 | static bool AdjustFunctionParmAndArgTypesForDeduction(Sema &S, |
| 2226 | TemplateParameterList *TemplateParams, |
| 2227 | QualType &ParamType, |
| 2228 | QualType &ArgType, |
| 2229 | Expr *Arg, |
| 2230 | unsigned &TDF) { |
| 2231 | // C++0x [temp.deduct.call]p3: |
| 2232 | // If P is a cv-qualified type, the top level cv-qualifiers of P’s type |
| 2233 | // are ignored for type deduction. |
| 2234 | if (ParamType.getCVRQualifiers()) |
| 2235 | ParamType = ParamType.getLocalUnqualifiedType(); |
| 2236 | const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>(); |
| 2237 | if (ParamRefType) { |
| 2238 | // [...] If P is a reference type, the type referred to by P is used |
| 2239 | // for type deduction. |
| 2240 | ParamType = ParamRefType->getPointeeType(); |
| 2241 | } |
| 2242 | |
| 2243 | // Overload sets usually make this parameter an undeduced |
| 2244 | // context, but there are sometimes special circumstances. |
| 2245 | if (ArgType == S.Context.OverloadTy) { |
| 2246 | ArgType = ResolveOverloadForDeduction(S, TemplateParams, |
| 2247 | Arg, ParamType, |
| 2248 | ParamRefType != 0); |
| 2249 | if (ArgType.isNull()) |
| 2250 | return true; |
| 2251 | } |
| 2252 | |
| 2253 | if (ParamRefType) { |
| 2254 | // C++0x [temp.deduct.call]p3: |
| 2255 | // [...] If P is of the form T&&, where T is a template parameter, and |
| 2256 | // the argument is an lvalue, the type A& is used in place of A for |
| 2257 | // type deduction. |
| 2258 | if (ParamRefType->isRValueReferenceType() && |
| 2259 | ParamRefType->getAs<TemplateTypeParmType>() && |
| 2260 | Arg->isLValue()) |
| 2261 | ArgType = S.Context.getLValueReferenceType(ArgType); |
| 2262 | } else { |
| 2263 | // C++ [temp.deduct.call]p2: |
| 2264 | // If P is not a reference type: |
| 2265 | // - If A is an array type, the pointer type produced by the |
| 2266 | // array-to-pointer standard conversion (4.2) is used in place of |
| 2267 | // A for type deduction; otherwise, |
| 2268 | if (ArgType->isArrayType()) |
| 2269 | ArgType = S.Context.getArrayDecayedType(ArgType); |
| 2270 | // - If A is a function type, the pointer type produced by the |
| 2271 | // function-to-pointer standard conversion (4.3) is used in place |
| 2272 | // of A for type deduction; otherwise, |
| 2273 | else if (ArgType->isFunctionType()) |
| 2274 | ArgType = S.Context.getPointerType(ArgType); |
| 2275 | else { |
| 2276 | // - If A is a cv-qualified type, the top level cv-qualifiers of A’s |
| 2277 | // type are ignored for type deduction. |
| 2278 | QualType CanonArgType = S.Context.getCanonicalType(ArgType); |
| 2279 | if (ArgType.getCVRQualifiers()) |
| 2280 | ArgType = ArgType.getUnqualifiedType(); |
| 2281 | } |
| 2282 | } |
| 2283 | |
| 2284 | // C++0x [temp.deduct.call]p4: |
| 2285 | // In general, the deduction process attempts to find template argument |
| 2286 | // values that will make the deduced A identical to A (after the type A |
| 2287 | // is transformed as described above). [...] |
| 2288 | TDF = TDF_SkipNonDependent; |
| 2289 | |
| 2290 | // - If the original P is a reference type, the deduced A (i.e., the |
| 2291 | // type referred to by the reference) can be more cv-qualified than |
| 2292 | // the transformed A. |
| 2293 | if (ParamRefType) |
| 2294 | TDF |= TDF_ParamWithReferenceType; |
| 2295 | // - The transformed A can be another pointer or pointer to member |
| 2296 | // type that can be converted to the deduced A via a qualification |
| 2297 | // conversion (4.4). |
| 2298 | if (ArgType->isPointerType() || ArgType->isMemberPointerType() || |
| 2299 | ArgType->isObjCObjectPointerType()) |
| 2300 | TDF |= TDF_IgnoreQualifiers; |
| 2301 | // - If P is a class and P has the form simple-template-id, then the |
| 2302 | // transformed A can be a derived class of the deduced A. Likewise, |
| 2303 | // if P is a pointer to a class of the form simple-template-id, the |
| 2304 | // transformed A can be a pointer to a derived class pointed to by |
| 2305 | // the deduced A. |
| 2306 | if (isSimpleTemplateIdType(ParamType) || |
| 2307 | (isa<PointerType>(ParamType) && |
| 2308 | isSimpleTemplateIdType( |
| 2309 | ParamType->getAs<PointerType>()->getPointeeType()))) |
| 2310 | TDF |= TDF_DerivedClass; |
| 2311 | |
| 2312 | return false; |
| 2313 | } |
| 2314 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2315 | /// \brief Perform template argument deduction from a function call |
| 2316 | /// (C++ [temp.deduct.call]). |
| 2317 | /// |
| 2318 | /// \param FunctionTemplate the function template for which we are performing |
| 2319 | /// template argument deduction. |
| 2320 | /// |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 2321 | /// \param ExplicitTemplateArguments the explicit template arguments provided |
| 2322 | /// for this call. |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2323 | /// |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2324 | /// \param Args the function call arguments |
| 2325 | /// |
| 2326 | /// \param NumArgs the number of arguments in Args |
| 2327 | /// |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 2328 | /// \param Name the name of the function being called. This is only significant |
| 2329 | /// when the function template is a conversion function template, in which |
| 2330 | /// case this routine will also perform template argument deduction based on |
| 2331 | /// the function to which |
| 2332 | /// |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2333 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2334 | /// this will be set to the function template specialization produced by |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2335 | /// template argument deduction. |
| 2336 | /// |
| 2337 | /// \param Info the argument will be updated to provide additional information |
| 2338 | /// about template argument deduction. |
| 2339 | /// |
| 2340 | /// \returns the result of template argument deduction. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2341 | Sema::TemplateDeductionResult |
| 2342 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 2343 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2344 | Expr **Args, unsigned NumArgs, |
| 2345 | FunctionDecl *&Specialization, |
| 2346 | TemplateDeductionInfo &Info) { |
| 2347 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2348 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2349 | // C++ [temp.deduct.call]p1: |
| 2350 | // Template argument deduction is done by comparing each function template |
| 2351 | // parameter type (call it P) with the type of the corresponding argument |
| 2352 | // of the call (call it A) as described below. |
| 2353 | unsigned CheckArgs = NumArgs; |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2354 | if (NumArgs < Function->getMinRequiredArguments()) |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2355 | return TDK_TooFewArguments; |
| 2356 | else if (NumArgs > Function->getNumParams()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2357 | const FunctionProtoType *Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2358 | = Function->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 2359 | if (Proto->isTemplateVariadic()) |
| 2360 | /* Do nothing */; |
| 2361 | else if (Proto->isVariadic()) |
| 2362 | CheckArgs = Function->getNumParams(); |
| 2363 | else |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2364 | return TDK_TooManyArguments; |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2365 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2366 | |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2367 | // The types of the parameters from which we will perform template argument |
| 2368 | // deduction. |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2369 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2370 | TemplateParameterList *TemplateParams |
| 2371 | = FunctionTemplate->getTemplateParameters(); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2372 | llvm::SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2373 | llvm::SmallVector<QualType, 4> ParamTypes; |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2374 | unsigned NumExplicitlySpecified = 0; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2375 | if (ExplicitTemplateArgs) { |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2376 | TemplateDeductionResult Result = |
| 2377 | SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2378 | *ExplicitTemplateArgs, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2379 | Deduced, |
| 2380 | ParamTypes, |
| 2381 | 0, |
| 2382 | Info); |
| 2383 | if (Result) |
| 2384 | return Result; |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2385 | |
| 2386 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2387 | } else { |
| 2388 | // Just fill in the parameter types from the function declaration. |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 2389 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2390 | ParamTypes.push_back(Function->getParamDecl(I)->getType()); |
| 2391 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2392 | |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 2393 | // Deduce template arguments from the function parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2394 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 2395 | unsigned ArgIdx = 0; |
| 2396 | for (unsigned ParamIdx = 0, NumParams = ParamTypes.size(); |
| 2397 | ParamIdx != NumParams; ++ParamIdx) { |
| 2398 | QualType ParamType = ParamTypes[ParamIdx]; |
| 2399 | |
| 2400 | const PackExpansionType *ParamExpansion |
| 2401 | = dyn_cast<PackExpansionType>(ParamType); |
| 2402 | if (!ParamExpansion) { |
| 2403 | // Simple case: matching a function parameter to a function argument. |
| 2404 | if (ArgIdx >= CheckArgs) |
| 2405 | break; |
| 2406 | |
| 2407 | Expr *Arg = Args[ArgIdx++]; |
| 2408 | QualType ArgType = Arg->getType(); |
| 2409 | unsigned TDF = 0; |
| 2410 | if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams, |
| 2411 | ParamType, ArgType, Arg, |
| 2412 | TDF)) |
| 2413 | continue; |
| 2414 | |
| 2415 | if (TemplateDeductionResult Result |
| 2416 | = ::DeduceTemplateArguments(*this, TemplateParams, |
| 2417 | ParamType, ArgType, Info, Deduced, |
| 2418 | TDF)) |
| 2419 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2420 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 2421 | // FIXME: we need to check that the deduced A is the same as A, |
| 2422 | // modulo the various allowed differences. |
| 2423 | continue; |
Douglas Gregor | 75f21af | 2010-08-30 21:04:23 +0000 | [diff] [blame] | 2424 | } |
| 2425 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 2426 | // C++0x [temp.deduct.call]p1: |
| 2427 | // For a function parameter pack that occurs at the end of the |
| 2428 | // parameter-declaration-list, the type A of each remaining argument of |
| 2429 | // the call is compared with the type P of the declarator-id of the |
| 2430 | // function parameter pack. Each comparison deduces template arguments |
| 2431 | // for subsequent positions in the template parameter packs expanded by |
| 2432 | // the function parameter pack. |
| 2433 | QualType ParamPattern = ParamExpansion->getPattern(); |
| 2434 | llvm::SmallVector<unsigned, 2> PackIndices; |
| 2435 | { |
| 2436 | llvm::BitVector SawIndices(TemplateParams->size()); |
| 2437 | llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded; |
| 2438 | collectUnexpandedParameterPacks(ParamPattern, Unexpanded); |
| 2439 | for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { |
| 2440 | unsigned Depth, Index; |
| 2441 | llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); |
| 2442 | if (Depth == 0 && !SawIndices[Index]) { |
| 2443 | SawIndices[Index] = true; |
| 2444 | PackIndices.push_back(Index); |
| 2445 | } |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2446 | } |
| 2447 | } |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 2448 | assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?"); |
| 2449 | |
| 2450 | // Save the deduced template arguments for each parameter pack expanded |
| 2451 | // by this pack expansion, then clear out the deduction. |
| 2452 | llvm::SmallVector<DeducedTemplateArgument, 2> |
| 2453 | SavedPacks(PackIndices.size()); |
| 2454 | for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) { |
| 2455 | SavedPacks[I] = Deduced[PackIndices[I]]; |
| 2456 | Deduced[PackIndices[I]] = DeducedTemplateArgument(); |
| 2457 | } |
| 2458 | |
| 2459 | // Keep track of the deduced template arguments for each parameter pack |
| 2460 | // expanded by this pack expansion (the outer index) and for each |
| 2461 | // template argument (the inner SmallVectors). |
| 2462 | llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2> |
| 2463 | NewlyDeducedPacks(PackIndices.size()); |
| 2464 | bool HasAnyArguments = false; |
| 2465 | for (; ArgIdx < NumArgs; ++ArgIdx) { |
| 2466 | HasAnyArguments = true; |
| 2467 | |
| 2468 | ParamType = ParamPattern; |
| 2469 | Expr *Arg = Args[ArgIdx]; |
| 2470 | QualType ArgType = Arg->getType(); |
| 2471 | unsigned TDF = 0; |
| 2472 | if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams, |
| 2473 | ParamType, ArgType, Arg, |
| 2474 | TDF)) { |
| 2475 | // We can't actually perform any deduction for this argument, so stop |
| 2476 | // deduction at this point. |
| 2477 | ++ArgIdx; |
| 2478 | break; |
| 2479 | } |
| 2480 | |
| 2481 | if (TemplateDeductionResult Result |
| 2482 | = ::DeduceTemplateArguments(*this, TemplateParams, |
| 2483 | ParamType, ArgType, Info, Deduced, |
| 2484 | TDF)) |
| 2485 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2486 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 2487 | // Capture the deduced template arguments for each parameter pack expanded |
| 2488 | // by this pack expansion, add them to the list of arguments we've deduced |
| 2489 | // for that pack, then clear out the deduced argument. |
| 2490 | for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) { |
| 2491 | DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]]; |
| 2492 | if (!DeducedArg.isNull()) { |
| 2493 | NewlyDeducedPacks[I].push_back(DeducedArg); |
| 2494 | DeducedArg = DeducedTemplateArgument(); |
| 2495 | } |
| 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | // Build argument packs for each of the parameter packs expanded by this |
| 2500 | // pack expansion. |
| 2501 | for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) { |
| 2502 | if (HasAnyArguments && NewlyDeducedPacks[I].empty()) { |
| 2503 | // We were not able to deduce anything for this parameter pack, |
| 2504 | // so just restore the saved argument pack. |
| 2505 | Deduced[PackIndices[I]] = SavedPacks[I]; |
| 2506 | continue; |
| 2507 | } |
| 2508 | |
| 2509 | DeducedTemplateArgument NewPack; |
| 2510 | |
| 2511 | if (NewlyDeducedPacks[I].empty()) { |
| 2512 | // If we deduced an empty argument pack, create it now. |
| 2513 | NewPack = DeducedTemplateArgument(TemplateArgument(0, 0)); |
| 2514 | } else { |
| 2515 | TemplateArgument *ArgumentPack |
| 2516 | = new (Context) TemplateArgument [NewlyDeducedPacks[I].size()]; |
| 2517 | std::copy(NewlyDeducedPacks[I].begin(), NewlyDeducedPacks[I].end(), |
| 2518 | ArgumentPack); |
| 2519 | NewPack |
| 2520 | = DeducedTemplateArgument(TemplateArgument(ArgumentPack, |
| 2521 | NewlyDeducedPacks[I].size()), |
| 2522 | NewlyDeducedPacks[I][0].wasDeducedFromArrayBound()); |
| 2523 | } |
| 2524 | |
| 2525 | DeducedTemplateArgument Result |
| 2526 | = checkDeducedTemplateArguments(Context, SavedPacks[I], NewPack); |
| 2527 | if (Result.isNull()) { |
| 2528 | Info.Param |
| 2529 | = makeTemplateParameter(TemplateParams->getParam(PackIndices[I])); |
| 2530 | Info.FirstArg = SavedPacks[I]; |
| 2531 | Info.SecondArg = NewPack; |
| 2532 | return Sema::TDK_Inconsistent; |
| 2533 | } |
| 2534 | |
| 2535 | Deduced[PackIndices[I]] = Result; |
| 2536 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2537 | |
Douglas Gregor | f5c65ff | 2011-01-06 22:09:01 +0000 | [diff] [blame] | 2538 | // After we've matching against a parameter pack, we're done. |
| 2539 | break; |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2540 | } |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2541 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2542 | return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2543 | NumExplicitlySpecified, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2544 | Specialization, Info); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2547 | /// \brief Deduce template arguments when taking the address of a function |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2548 | /// template (C++ [temp.deduct.funcaddr]) or matching a specialization to |
| 2549 | /// a template. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2550 | /// |
| 2551 | /// \param FunctionTemplate the function template for which we are performing |
| 2552 | /// template argument deduction. |
| 2553 | /// |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2554 | /// \param ExplicitTemplateArguments the explicitly-specified template |
| 2555 | /// arguments. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2556 | /// |
| 2557 | /// \param ArgFunctionType the function type that will be used as the |
| 2558 | /// "argument" type (A) when performing template argument deduction from the |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2559 | /// function template's function type. This type may be NULL, if there is no |
| 2560 | /// argument type to compare against, in C++0x [temp.arg.explicit]p3. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2561 | /// |
| 2562 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2563 | /// this will be set to the function template specialization produced by |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2564 | /// template argument deduction. |
| 2565 | /// |
| 2566 | /// \param Info the argument will be updated to provide additional information |
| 2567 | /// about template argument deduction. |
| 2568 | /// |
| 2569 | /// \returns the result of template argument deduction. |
| 2570 | Sema::TemplateDeductionResult |
| 2571 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2572 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2573 | QualType ArgFunctionType, |
| 2574 | FunctionDecl *&Specialization, |
| 2575 | TemplateDeductionInfo &Info) { |
| 2576 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 2577 | TemplateParameterList *TemplateParams |
| 2578 | = FunctionTemplate->getTemplateParameters(); |
| 2579 | QualType FunctionType = Function->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2580 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2581 | // Substitute any explicit template arguments. |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2582 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2583 | llvm::SmallVector<DeducedTemplateArgument, 4> Deduced; |
| 2584 | unsigned NumExplicitlySpecified = 0; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2585 | llvm::SmallVector<QualType, 4> ParamTypes; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2586 | if (ExplicitTemplateArgs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2587 | if (TemplateDeductionResult Result |
| 2588 | = SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 2589 | *ExplicitTemplateArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2590 | Deduced, ParamTypes, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2591 | &FunctionType, Info)) |
| 2592 | return Result; |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2593 | |
| 2594 | NumExplicitlySpecified = Deduced.size(); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | // Template argument deduction for function templates in a SFINAE context. |
| 2598 | // Trap any errors that might occur. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2599 | SFINAETrap Trap(*this); |
| 2600 | |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame] | 2601 | Deduced.resize(TemplateParams->size()); |
| 2602 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2603 | if (!ArgFunctionType.isNull()) { |
| 2604 | // Deduce template arguments from the function type. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2605 | if (TemplateDeductionResult Result |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2606 | = ::DeduceTemplateArguments(*this, TemplateParams, |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2607 | FunctionType, ArgFunctionType, Info, |
| 2608 | Deduced, 0)) |
| 2609 | return Result; |
| 2610 | } |
Douglas Gregor | fbb6fad | 2010-09-29 21:14:36 +0000 | [diff] [blame] | 2611 | |
| 2612 | if (TemplateDeductionResult Result |
| 2613 | = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
| 2614 | NumExplicitlySpecified, |
| 2615 | Specialization, Info)) |
| 2616 | return Result; |
| 2617 | |
| 2618 | // If the requested function type does not match the actual type of the |
| 2619 | // specialization, template argument deduction fails. |
| 2620 | if (!ArgFunctionType.isNull() && |
| 2621 | !Context.hasSameType(ArgFunctionType, Specialization->getType())) |
| 2622 | return TDK_NonDeducedMismatch; |
| 2623 | |
| 2624 | return TDK_Success; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2625 | } |
| 2626 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2627 | /// \brief Deduce template arguments for a templated conversion |
| 2628 | /// function (C++ [temp.deduct.conv]) and, if successful, produce a |
| 2629 | /// conversion function template specialization. |
| 2630 | Sema::TemplateDeductionResult |
| 2631 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
| 2632 | QualType ToType, |
| 2633 | CXXConversionDecl *&Specialization, |
| 2634 | TemplateDeductionInfo &Info) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2635 | CXXConversionDecl *Conv |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2636 | = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()); |
| 2637 | QualType FromType = Conv->getConversionType(); |
| 2638 | |
| 2639 | // Canonicalize the types for deduction. |
| 2640 | QualType P = Context.getCanonicalType(FromType); |
| 2641 | QualType A = Context.getCanonicalType(ToType); |
| 2642 | |
| 2643 | // C++0x [temp.deduct.conv]p3: |
| 2644 | // If P is a reference type, the type referred to by P is used for |
| 2645 | // type deduction. |
| 2646 | if (const ReferenceType *PRef = P->getAs<ReferenceType>()) |
| 2647 | P = PRef->getPointeeType(); |
| 2648 | |
| 2649 | // C++0x [temp.deduct.conv]p3: |
| 2650 | // If A is a reference type, the type referred to by A is used |
| 2651 | // for type deduction. |
| 2652 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
| 2653 | A = ARef->getPointeeType(); |
| 2654 | // C++ [temp.deduct.conv]p2: |
| 2655 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2656 | // If A is not a reference type: |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2657 | else { |
| 2658 | assert(!A->isReferenceType() && "Reference types were handled above"); |
| 2659 | |
| 2660 | // - If P is an array type, the pointer type produced by the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2661 | // array-to-pointer standard conversion (4.2) is used in place |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2662 | // of P for type deduction; otherwise, |
| 2663 | if (P->isArrayType()) |
| 2664 | P = Context.getArrayDecayedType(P); |
| 2665 | // - If P is a function type, the pointer type produced by the |
| 2666 | // function-to-pointer standard conversion (4.3) is used in |
| 2667 | // place of P for type deduction; otherwise, |
| 2668 | else if (P->isFunctionType()) |
| 2669 | P = Context.getPointerType(P); |
| 2670 | // - If P is a cv-qualified type, the top level cv-qualifiers of |
| 2671 | // P’s type are ignored for type deduction. |
| 2672 | else |
| 2673 | P = P.getUnqualifiedType(); |
| 2674 | |
| 2675 | // C++0x [temp.deduct.conv]p3: |
| 2676 | // If A is a cv-qualified type, the top level cv-qualifiers of A’s |
| 2677 | // type are ignored for type deduction. |
| 2678 | A = A.getUnqualifiedType(); |
| 2679 | } |
| 2680 | |
| 2681 | // Template argument deduction for function templates in a SFINAE context. |
| 2682 | // Trap any errors that might occur. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2683 | SFINAETrap Trap(*this); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2684 | |
| 2685 | // C++ [temp.deduct.conv]p1: |
| 2686 | // Template argument deduction is done by comparing the return |
| 2687 | // type of the template conversion function (call it P) with the |
| 2688 | // type that is required as the result of the conversion (call it |
| 2689 | // A) as described in 14.8.2.4. |
| 2690 | TemplateParameterList *TemplateParams |
| 2691 | = FunctionTemplate->getTemplateParameters(); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2692 | llvm::SmallVector<DeducedTemplateArgument, 4> Deduced; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2693 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2694 | |
| 2695 | // C++0x [temp.deduct.conv]p4: |
| 2696 | // In general, the deduction process attempts to find template |
| 2697 | // argument values that will make the deduced A identical to |
| 2698 | // A. However, there are two cases that allow a difference: |
| 2699 | unsigned TDF = 0; |
| 2700 | // - If the original A is a reference type, A can be more |
| 2701 | // cv-qualified than the deduced A (i.e., the type referred to |
| 2702 | // by the reference) |
| 2703 | if (ToType->isReferenceType()) |
| 2704 | TDF |= TDF_ParamWithReferenceType; |
| 2705 | // - The deduced A can be another pointer or pointer to member |
| 2706 | // type that can be converted to A via a qualification |
| 2707 | // conversion. |
| 2708 | // |
| 2709 | // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when |
| 2710 | // both P and A are pointers or member pointers. In this case, we |
| 2711 | // just ignore cv-qualifiers completely). |
| 2712 | if ((P->isPointerType() && A->isPointerType()) || |
| 2713 | (P->isMemberPointerType() && P->isMemberPointerType())) |
| 2714 | TDF |= TDF_IgnoreQualifiers; |
| 2715 | if (TemplateDeductionResult Result |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2716 | = ::DeduceTemplateArguments(*this, TemplateParams, |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2717 | P, A, Info, Deduced, TDF)) |
| 2718 | return Result; |
| 2719 | |
| 2720 | // FIXME: we need to check that the deduced A is the same as A, |
| 2721 | // modulo the various allowed differences. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2722 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2723 | // Finish template argument deduction. |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2724 | LocalInstantiationScope InstScope(*this); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2725 | FunctionDecl *Spec = 0; |
| 2726 | TemplateDeductionResult Result |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2727 | = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 0, Spec, |
| 2728 | Info); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2729 | Specialization = cast_or_null<CXXConversionDecl>(Spec); |
| 2730 | return Result; |
| 2731 | } |
| 2732 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 2733 | /// \brief Deduce template arguments for a function template when there is |
| 2734 | /// nothing to deduce against (C++0x [temp.arg.explicit]p3). |
| 2735 | /// |
| 2736 | /// \param FunctionTemplate the function template for which we are performing |
| 2737 | /// template argument deduction. |
| 2738 | /// |
| 2739 | /// \param ExplicitTemplateArguments the explicitly-specified template |
| 2740 | /// arguments. |
| 2741 | /// |
| 2742 | /// \param Specialization if template argument deduction was successful, |
| 2743 | /// this will be set to the function template specialization produced by |
| 2744 | /// template argument deduction. |
| 2745 | /// |
| 2746 | /// \param Info the argument will be updated to provide additional information |
| 2747 | /// about template argument deduction. |
| 2748 | /// |
| 2749 | /// \returns the result of template argument deduction. |
| 2750 | Sema::TemplateDeductionResult |
| 2751 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
| 2752 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 2753 | FunctionDecl *&Specialization, |
| 2754 | TemplateDeductionInfo &Info) { |
| 2755 | return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
| 2756 | QualType(), Specialization, Info); |
| 2757 | } |
| 2758 | |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 2759 | /// \brief Stores the result of comparing the qualifiers of two types. |
| 2760 | enum DeductionQualifierComparison { |
| 2761 | NeitherMoreQualified = 0, |
| 2762 | ParamMoreQualified, |
| 2763 | ArgMoreQualified |
| 2764 | }; |
| 2765 | |
| 2766 | /// \brief Deduce the template arguments during partial ordering by comparing |
| 2767 | /// the parameter type and the argument type (C++0x [temp.deduct.partial]). |
| 2768 | /// |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2769 | /// \param S the semantic analysis object within which we are deducing |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 2770 | /// |
| 2771 | /// \param TemplateParams the template parameters that we are deducing |
| 2772 | /// |
| 2773 | /// \param ParamIn the parameter type |
| 2774 | /// |
| 2775 | /// \param ArgIn the argument type |
| 2776 | /// |
| 2777 | /// \param Info information about the template argument deduction itself |
| 2778 | /// |
| 2779 | /// \param Deduced the deduced template arguments |
| 2780 | /// |
| 2781 | /// \returns the result of template argument deduction so far. Note that a |
| 2782 | /// "success" result means that template argument deduction has not yet failed, |
| 2783 | /// but it may still fail, later, for other reasons. |
| 2784 | static Sema::TemplateDeductionResult |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2785 | DeduceTemplateArgumentsDuringPartialOrdering(Sema &S, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2786 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 2787 | QualType ParamIn, QualType ArgIn, |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2788 | TemplateDeductionInfo &Info, |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2789 | llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced, |
| 2790 | llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) { |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2791 | CanQualType Param = S.Context.getCanonicalType(ParamIn); |
| 2792 | CanQualType Arg = S.Context.getCanonicalType(ArgIn); |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 2793 | |
| 2794 | // C++0x [temp.deduct.partial]p5: |
| 2795 | // Before the partial ordering is done, certain transformations are |
| 2796 | // performed on the types used for partial ordering: |
| 2797 | // - If P is a reference type, P is replaced by the type referred to. |
| 2798 | CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>(); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 2799 | if (!ParamRef.isNull()) |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 2800 | Param = ParamRef->getPointeeType(); |
| 2801 | |
| 2802 | // - If A is a reference type, A is replaced by the type referred to. |
| 2803 | CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>(); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 2804 | if (!ArgRef.isNull()) |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 2805 | Arg = ArgRef->getPointeeType(); |
| 2806 | |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 2807 | if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) { |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 2808 | // C++0x [temp.deduct.partial]p6: |
| 2809 | // If both P and A were reference types (before being replaced with the |
| 2810 | // type referred to above), determine which of the two types (if any) is |
| 2811 | // more cv-qualified than the other; otherwise the types are considered to |
| 2812 | // be equally cv-qualified for partial ordering purposes. The result of this |
| 2813 | // determination will be used below. |
| 2814 | // |
| 2815 | // We save this information for later, using it only when deduction |
| 2816 | // succeeds in both directions. |
| 2817 | DeductionQualifierComparison QualifierResult = NeitherMoreQualified; |
| 2818 | if (Param.isMoreQualifiedThan(Arg)) |
| 2819 | QualifierResult = ParamMoreQualified; |
| 2820 | else if (Arg.isMoreQualifiedThan(Param)) |
| 2821 | QualifierResult = ArgMoreQualified; |
| 2822 | QualifierComparisons->push_back(QualifierResult); |
| 2823 | } |
| 2824 | |
| 2825 | // C++0x [temp.deduct.partial]p7: |
| 2826 | // Remove any top-level cv-qualifiers: |
| 2827 | // - If P is a cv-qualified type, P is replaced by the cv-unqualified |
| 2828 | // version of P. |
| 2829 | Param = Param.getUnqualifiedType(); |
| 2830 | // - If A is a cv-qualified type, A is replaced by the cv-unqualified |
| 2831 | // version of A. |
| 2832 | Arg = Arg.getUnqualifiedType(); |
| 2833 | |
| 2834 | // C++0x [temp.deduct.partial]p8: |
| 2835 | // Using the resulting types P and A the deduction is then done as |
| 2836 | // described in 14.9.2.5. If deduction succeeds for a given type, the type |
| 2837 | // from the argument template is considered to be at least as specialized |
| 2838 | // as the type from the parameter template. |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2839 | return DeduceTemplateArguments(S, TemplateParams, Param, Arg, Info, |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 2840 | Deduced, TDF_None); |
| 2841 | } |
| 2842 | |
| 2843 | static void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2844 | MarkUsedTemplateParameters(Sema &SemaRef, QualType T, |
| 2845 | bool OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2846 | unsigned Level, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2847 | llvm::SmallVectorImpl<bool> &Deduced); |
Douglas Gregor | 77bc572 | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 2848 | |
| 2849 | /// \brief If this is a non-static member function, |
| 2850 | static void MaybeAddImplicitObjectParameterType(ASTContext &Context, |
| 2851 | CXXMethodDecl *Method, |
| 2852 | llvm::SmallVectorImpl<QualType> &ArgTypes) { |
| 2853 | if (Method->isStatic()) |
| 2854 | return; |
| 2855 | |
| 2856 | // C++ [over.match.funcs]p4: |
| 2857 | // |
| 2858 | // For non-static member functions, the type of the implicit |
| 2859 | // object parameter is |
| 2860 | // — "lvalue reference to cv X" for functions declared without a |
| 2861 | // ref-qualifier or with the & ref-qualifier |
| 2862 | // - "rvalue reference to cv X" for functions declared with the |
| 2863 | // && ref-qualifier |
| 2864 | // |
| 2865 | // FIXME: We don't have ref-qualifiers yet, so we don't do that part. |
| 2866 | QualType ArgTy = Context.getTypeDeclType(Method->getParent()); |
| 2867 | ArgTy = Context.getQualifiedType(ArgTy, |
| 2868 | Qualifiers::fromCVRMask(Method->getTypeQualifiers())); |
| 2869 | ArgTy = Context.getLValueReferenceType(ArgTy); |
| 2870 | ArgTypes.push_back(ArgTy); |
| 2871 | } |
| 2872 | |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 2873 | /// \brief Determine whether the function template \p FT1 is at least as |
| 2874 | /// specialized as \p FT2. |
| 2875 | static bool isAtLeastAsSpecializedAs(Sema &S, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 2876 | SourceLocation Loc, |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 2877 | FunctionTemplateDecl *FT1, |
| 2878 | FunctionTemplateDecl *FT2, |
| 2879 | TemplatePartialOrderingContext TPOC, |
| 2880 | llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) { |
| 2881 | FunctionDecl *FD1 = FT1->getTemplatedDecl(); |
| 2882 | FunctionDecl *FD2 = FT2->getTemplatedDecl(); |
| 2883 | const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); |
| 2884 | const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); |
| 2885 | |
| 2886 | assert(Proto1 && Proto2 && "Function templates must have prototypes"); |
| 2887 | TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 2888 | llvm::SmallVector<DeducedTemplateArgument, 4> Deduced; |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 2889 | Deduced.resize(TemplateParams->size()); |
| 2890 | |
| 2891 | // C++0x [temp.deduct.partial]p3: |
| 2892 | // The types used to determine the ordering depend on the context in which |
| 2893 | // the partial ordering is done: |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 2894 | TemplateDeductionInfo Info(S.Context, Loc); |
Douglas Gregor | 8d706ec | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 2895 | CXXMethodDecl *Method1 = 0; |
| 2896 | CXXMethodDecl *Method2 = 0; |
| 2897 | bool IsNonStatic2 = false; |
| 2898 | bool IsNonStatic1 = false; |
| 2899 | unsigned Skip2 = 0; |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 2900 | switch (TPOC) { |
| 2901 | case TPOC_Call: { |
| 2902 | // - In the context of a function call, the function parameter types are |
| 2903 | // used. |
Douglas Gregor | 8d706ec | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 2904 | Method1 = dyn_cast<CXXMethodDecl>(FD1); |
| 2905 | Method2 = dyn_cast<CXXMethodDecl>(FD2); |
| 2906 | IsNonStatic1 = Method1 && !Method1->isStatic(); |
| 2907 | IsNonStatic2 = Method2 && !Method2->isStatic(); |
| 2908 | |
| 2909 | // C++0x [temp.func.order]p3: |
| 2910 | // [...] If only one of the function templates is a non-static |
| 2911 | // member, that function template is considered to have a new |
| 2912 | // first parameter inserted in its function parameter list. The |
| 2913 | // new parameter is of type "reference to cv A," where cv are |
| 2914 | // the cv-qualifiers of the function template (if any) and A is |
| 2915 | // the class of which the function template is a member. |
| 2916 | // |
| 2917 | // C++98/03 doesn't have this provision, so instead we drop the |
| 2918 | // first argument of the free function or static member, which |
| 2919 | // seems to match existing practice. |
Douglas Gregor | 77bc572 | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 2920 | llvm::SmallVector<QualType, 4> Args1; |
Douglas Gregor | 8d706ec | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 2921 | unsigned Skip1 = !S.getLangOptions().CPlusPlus0x && |
| 2922 | IsNonStatic2 && !IsNonStatic1; |
| 2923 | if (S.getLangOptions().CPlusPlus0x && IsNonStatic1 && !IsNonStatic2) |
Douglas Gregor | 77bc572 | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 2924 | MaybeAddImplicitObjectParameterType(S.Context, Method1, Args1); |
| 2925 | Args1.insert(Args1.end(), |
Douglas Gregor | 8d706ec | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 2926 | Proto1->arg_type_begin() + Skip1, Proto1->arg_type_end()); |
Douglas Gregor | 77bc572 | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 2927 | |
| 2928 | llvm::SmallVector<QualType, 4> Args2; |
Douglas Gregor | 8d706ec | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 2929 | Skip2 = !S.getLangOptions().CPlusPlus0x && |
| 2930 | IsNonStatic1 && !IsNonStatic2; |
| 2931 | if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1) |
Douglas Gregor | 77bc572 | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 2932 | MaybeAddImplicitObjectParameterType(S.Context, Method2, Args2); |
| 2933 | Args2.insert(Args2.end(), |
Douglas Gregor | 8d706ec | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 2934 | Proto2->arg_type_begin() + Skip2, Proto2->arg_type_end()); |
Douglas Gregor | 77bc572 | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 2935 | |
| 2936 | unsigned NumParams = std::min(Args1.size(), Args2.size()); |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 2937 | for (unsigned I = 0; I != NumParams; ++I) |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2938 | if (DeduceTemplateArgumentsDuringPartialOrdering(S, |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 2939 | TemplateParams, |
Douglas Gregor | 77bc572 | 2010-11-12 23:44:13 +0000 | [diff] [blame] | 2940 | Args2[I], |
| 2941 | Args1[I], |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 2942 | Info, |
| 2943 | Deduced, |
| 2944 | QualifierComparisons)) |
| 2945 | return false; |
| 2946 | |
| 2947 | break; |
| 2948 | } |
| 2949 | |
| 2950 | case TPOC_Conversion: |
| 2951 | // - In the context of a call to a conversion operator, the return types |
| 2952 | // of the conversion function templates are used. |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2953 | if (DeduceTemplateArgumentsDuringPartialOrdering(S, |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 2954 | TemplateParams, |
| 2955 | Proto2->getResultType(), |
| 2956 | Proto1->getResultType(), |
| 2957 | Info, |
| 2958 | Deduced, |
| 2959 | QualifierComparisons)) |
| 2960 | return false; |
| 2961 | break; |
| 2962 | |
| 2963 | case TPOC_Other: |
| 2964 | // - In other contexts (14.6.6.2) the function template’s function type |
| 2965 | // is used. |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 2966 | if (DeduceTemplateArgumentsDuringPartialOrdering(S, |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 2967 | TemplateParams, |
| 2968 | FD2->getType(), |
| 2969 | FD1->getType(), |
| 2970 | Info, |
| 2971 | Deduced, |
| 2972 | QualifierComparisons)) |
| 2973 | return false; |
| 2974 | break; |
| 2975 | } |
| 2976 | |
| 2977 | // C++0x [temp.deduct.partial]p11: |
| 2978 | // In most cases, all template parameters must have values in order for |
| 2979 | // deduction to succeed, but for partial ordering purposes a template |
| 2980 | // parameter may remain without a value provided it is not used in the |
| 2981 | // types being used for partial ordering. [ Note: a template parameter used |
| 2982 | // in a non-deduced context is considered used. -end note] |
| 2983 | unsigned ArgIdx = 0, NumArgs = Deduced.size(); |
| 2984 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 2985 | if (Deduced[ArgIdx].isNull()) |
| 2986 | break; |
| 2987 | |
| 2988 | if (ArgIdx == NumArgs) { |
| 2989 | // All template arguments were deduced. FT1 is at least as specialized |
| 2990 | // as FT2. |
| 2991 | return true; |
| 2992 | } |
| 2993 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2994 | // Figure out which template parameters were used. |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 2995 | llvm::SmallVector<bool, 4> UsedParameters; |
| 2996 | UsedParameters.resize(TemplateParams->size()); |
| 2997 | switch (TPOC) { |
| 2998 | case TPOC_Call: { |
| 2999 | unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs()); |
Douglas Gregor | 8d706ec | 2010-11-15 15:41:16 +0000 | [diff] [blame] | 3000 | if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1) |
| 3001 | ::MarkUsedTemplateParameters(S, Method2->getThisType(S.Context), false, |
| 3002 | TemplateParams->getDepth(), UsedParameters); |
| 3003 | for (unsigned I = Skip2; I < NumParams; ++I) |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3004 | ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false, |
| 3005 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3006 | UsedParameters); |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 3007 | break; |
| 3008 | } |
| 3009 | |
| 3010 | case TPOC_Conversion: |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3011 | ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false, |
| 3012 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3013 | UsedParameters); |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 3014 | break; |
| 3015 | |
| 3016 | case TPOC_Other: |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3017 | ::MarkUsedTemplateParameters(S, FD2->getType(), false, |
| 3018 | TemplateParams->getDepth(), |
| 3019 | UsedParameters); |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 3020 | break; |
| 3021 | } |
| 3022 | |
| 3023 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 3024 | // If this argument had no value deduced but was used in one of the types |
| 3025 | // used for partial ordering, then deduction fails. |
| 3026 | if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) |
| 3027 | return false; |
| 3028 | |
| 3029 | return true; |
| 3030 | } |
| 3031 | |
| 3032 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 3033 | /// \brief Returns the more specialized function template according |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3034 | /// to the rules of function template partial ordering (C++ [temp.func.order]). |
| 3035 | /// |
| 3036 | /// \param FT1 the first function template |
| 3037 | /// |
| 3038 | /// \param FT2 the second function template |
| 3039 | /// |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 3040 | /// \param TPOC the context in which we are performing partial ordering of |
| 3041 | /// function templates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3042 | /// |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 3043 | /// \returns the more specialized function template. If neither |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3044 | /// template is more specialized, returns NULL. |
| 3045 | FunctionTemplateDecl * |
| 3046 | Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, |
| 3047 | FunctionTemplateDecl *FT2, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3048 | SourceLocation Loc, |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 3049 | TemplatePartialOrderingContext TPOC) { |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 3050 | llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons; |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3051 | bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, 0); |
| 3052 | bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC, |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 3053 | &QualifierComparisons); |
| 3054 | |
| 3055 | if (Better1 != Better2) // We have a clear winner |
| 3056 | return Better1? FT1 : FT2; |
| 3057 | |
| 3058 | if (!Better1 && !Better2) // Neither is better than the other |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3059 | return 0; |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 3060 | |
| 3061 | |
| 3062 | // C++0x [temp.deduct.partial]p10: |
| 3063 | // If for each type being considered a given template is at least as |
| 3064 | // specialized for all types and more specialized for some set of types and |
| 3065 | // the other template is not more specialized for any types or is not at |
| 3066 | // least as specialized for any types, then the given template is more |
| 3067 | // specialized than the other template. Otherwise, neither template is more |
| 3068 | // specialized than the other. |
| 3069 | Better1 = false; |
| 3070 | Better2 = false; |
| 3071 | for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) { |
| 3072 | // C++0x [temp.deduct.partial]p9: |
| 3073 | // If, for a given type, deduction succeeds in both directions (i.e., the |
| 3074 | // types are identical after the transformations above) and if the type |
| 3075 | // from the argument template is more cv-qualified than the type from the |
| 3076 | // parameter template (as described above) that type is considered to be |
| 3077 | // more specialized than the other. If neither type is more cv-qualified |
| 3078 | // than the other then neither type is more specialized than the other. |
| 3079 | switch (QualifierComparisons[I]) { |
| 3080 | case NeitherMoreQualified: |
| 3081 | break; |
| 3082 | |
| 3083 | case ParamMoreQualified: |
| 3084 | Better1 = true; |
| 3085 | if (Better2) |
| 3086 | return 0; |
| 3087 | break; |
| 3088 | |
| 3089 | case ArgMoreQualified: |
| 3090 | Better2 = true; |
| 3091 | if (Better1) |
| 3092 | return 0; |
| 3093 | break; |
| 3094 | } |
| 3095 | } |
| 3096 | |
| 3097 | assert(!(Better1 && Better2) && "Should have broken out in the loop above"); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3098 | if (Better1) |
| 3099 | return FT1; |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 3100 | else if (Better2) |
| 3101 | return FT2; |
| 3102 | else |
| 3103 | return 0; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 3104 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 3105 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 3106 | /// \brief Determine if the two templates are equivalent. |
| 3107 | static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) { |
| 3108 | if (T1 == T2) |
| 3109 | return true; |
| 3110 | |
| 3111 | if (!T1 || !T2) |
| 3112 | return false; |
| 3113 | |
| 3114 | return T1->getCanonicalDecl() == T2->getCanonicalDecl(); |
| 3115 | } |
| 3116 | |
| 3117 | /// \brief Retrieve the most specialized of the given function template |
| 3118 | /// specializations. |
| 3119 | /// |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3120 | /// \param SpecBegin the start iterator of the function template |
| 3121 | /// specializations that we will be comparing. |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 3122 | /// |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3123 | /// \param SpecEnd the end iterator of the function template |
| 3124 | /// specializations, paired with \p SpecBegin. |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 3125 | /// |
| 3126 | /// \param TPOC the partial ordering context to use to compare the function |
| 3127 | /// template specializations. |
| 3128 | /// |
| 3129 | /// \param Loc the location where the ambiguity or no-specializations |
| 3130 | /// diagnostic should occur. |
| 3131 | /// |
| 3132 | /// \param NoneDiag partial diagnostic used to diagnose cases where there are |
| 3133 | /// no matching candidates. |
| 3134 | /// |
| 3135 | /// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one |
| 3136 | /// occurs. |
| 3137 | /// |
| 3138 | /// \param CandidateDiag partial diagnostic used for each function template |
| 3139 | /// specialization that is a candidate in the ambiguous ordering. One parameter |
| 3140 | /// in this diagnostic should be unbound, which will correspond to the string |
| 3141 | /// describing the template arguments for the function template specialization. |
| 3142 | /// |
| 3143 | /// \param Index if non-NULL and the result of this function is non-nULL, |
| 3144 | /// receives the index corresponding to the resulting function template |
| 3145 | /// specialization. |
| 3146 | /// |
| 3147 | /// \returns the most specialized function template specialization, if |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3148 | /// found. Otherwise, returns SpecEnd. |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 3149 | /// |
| 3150 | /// \todo FIXME: Consider passing in the "also-ran" candidates that failed |
| 3151 | /// template argument deduction. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3152 | UnresolvedSetIterator |
| 3153 | Sema::getMostSpecialized(UnresolvedSetIterator SpecBegin, |
| 3154 | UnresolvedSetIterator SpecEnd, |
| 3155 | TemplatePartialOrderingContext TPOC, |
| 3156 | SourceLocation Loc, |
| 3157 | const PartialDiagnostic &NoneDiag, |
| 3158 | const PartialDiagnostic &AmbigDiag, |
| 3159 | const PartialDiagnostic &CandidateDiag) { |
| 3160 | if (SpecBegin == SpecEnd) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 3161 | Diag(Loc, NoneDiag); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3162 | return SpecEnd; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 3163 | } |
| 3164 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3165 | if (SpecBegin + 1 == SpecEnd) |
| 3166 | return SpecBegin; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 3167 | |
| 3168 | // Find the function template that is better than all of the templates it |
| 3169 | // has been compared to. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3170 | UnresolvedSetIterator Best = SpecBegin; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 3171 | FunctionTemplateDecl *BestTemplate |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3172 | = cast<FunctionDecl>(*Best)->getPrimaryTemplate(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 3173 | assert(BestTemplate && "Not a function template specialization?"); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3174 | for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) { |
| 3175 | FunctionTemplateDecl *Challenger |
| 3176 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 3177 | assert(Challenger && "Not a function template specialization?"); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3178 | if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3179 | Loc, TPOC), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 3180 | Challenger)) { |
| 3181 | Best = I; |
| 3182 | BestTemplate = Challenger; |
| 3183 | } |
| 3184 | } |
| 3185 | |
| 3186 | // Make sure that the "best" function template is more specialized than all |
| 3187 | // of the others. |
| 3188 | bool Ambiguous = false; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3189 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 3190 | FunctionTemplateDecl *Challenger |
| 3191 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 3192 | if (I != Best && |
| 3193 | !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3194 | Loc, TPOC), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 3195 | BestTemplate)) { |
| 3196 | Ambiguous = true; |
| 3197 | break; |
| 3198 | } |
| 3199 | } |
| 3200 | |
| 3201 | if (!Ambiguous) { |
| 3202 | // We found an answer. Return it. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3203 | return Best; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | // Diagnose the ambiguity. |
| 3207 | Diag(Loc, AmbigDiag); |
| 3208 | |
| 3209 | // FIXME: Can we order the candidates in some sane way? |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3210 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) |
| 3211 | Diag((*I)->getLocation(), CandidateDiag) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 3212 | << getTemplateArgumentBindingsText( |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3213 | cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(), |
| 3214 | *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 3215 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 3216 | return SpecEnd; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 3217 | } |
| 3218 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 3219 | /// \brief Returns the more specialized class template partial specialization |
| 3220 | /// according to the rules of partial ordering of class template partial |
| 3221 | /// specializations (C++ [temp.class.order]). |
| 3222 | /// |
| 3223 | /// \param PS1 the first class template partial specialization |
| 3224 | /// |
| 3225 | /// \param PS2 the second class template partial specialization |
| 3226 | /// |
| 3227 | /// \returns the more specialized class template partial specialization. If |
| 3228 | /// neither partial specialization is more specialized, returns NULL. |
| 3229 | ClassTemplatePartialSpecializationDecl * |
| 3230 | Sema::getMoreSpecializedPartialSpecialization( |
| 3231 | ClassTemplatePartialSpecializationDecl *PS1, |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 3232 | ClassTemplatePartialSpecializationDecl *PS2, |
| 3233 | SourceLocation Loc) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 3234 | // C++ [temp.class.order]p1: |
| 3235 | // For two class template partial specializations, the first is at least as |
| 3236 | // specialized as the second if, given the following rewrite to two |
| 3237 | // function templates, the first function template is at least as |
| 3238 | // specialized as the second according to the ordering rules for function |
| 3239 | // templates (14.6.6.2): |
| 3240 | // - the first function template has the same template parameters as the |
| 3241 | // first partial specialization and has a single function parameter |
| 3242 | // whose type is a class template specialization with the template |
| 3243 | // arguments of the first partial specialization, and |
| 3244 | // - the second function template has the same template parameters as the |
| 3245 | // second partial specialization and has a single function parameter |
| 3246 | // whose type is a class template specialization with the template |
| 3247 | // arguments of the second partial specialization. |
| 3248 | // |
Douglas Gregor | 31dce8f | 2010-04-29 06:21:43 +0000 | [diff] [blame] | 3249 | // Rather than synthesize function templates, we merely perform the |
| 3250 | // equivalent partial ordering by performing deduction directly on |
| 3251 | // the template arguments of the class template partial |
| 3252 | // specializations. This computation is slightly simpler than the |
| 3253 | // general problem of function template partial ordering, because |
| 3254 | // class template partial specializations are more constrained. We |
| 3255 | // know that every template parameter is deducible from the class |
| 3256 | // template partial specialization's template arguments, for |
| 3257 | // example. |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3258 | llvm::SmallVector<DeducedTemplateArgument, 4> Deduced; |
John McCall | 2a7fb27 | 2010-08-25 05:32:35 +0000 | [diff] [blame] | 3259 | TemplateDeductionInfo Info(Context, Loc); |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3260 | |
| 3261 | QualType PT1 = PS1->getInjectedSpecializationType(); |
| 3262 | QualType PT2 = PS2->getInjectedSpecializationType(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 3263 | |
| 3264 | // Determine whether PS1 is at least as specialized as PS2 |
| 3265 | Deduced.resize(PS2->getTemplateParameters()->size()); |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 3266 | bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(*this, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 3267 | PS2->getTemplateParameters(), |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3268 | PT2, |
| 3269 | PT1, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 3270 | Info, |
| 3271 | Deduced, |
| 3272 | 0); |
Argyrios Kyrtzidis | 2c4792c | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 3273 | if (Better1) { |
| 3274 | InstantiatingTemplate Inst(*this, PS2->getLocation(), PS2, |
| 3275 | Deduced.data(), Deduced.size(), Info); |
Douglas Gregor | 516e6e0 | 2010-04-29 06:31:36 +0000 | [diff] [blame] | 3276 | Better1 = !::FinishTemplateArgumentDeduction(*this, PS2, |
| 3277 | PS1->getTemplateArgs(), |
| 3278 | Deduced, Info); |
Argyrios Kyrtzidis | 2c4792c | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 3279 | } |
Douglas Gregor | 516e6e0 | 2010-04-29 06:31:36 +0000 | [diff] [blame] | 3280 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 3281 | // Determine whether PS2 is at least as specialized as PS1 |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 3282 | Deduced.clear(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 3283 | Deduced.resize(PS1->getTemplateParameters()->size()); |
Chandler Carruth | a7ef130 | 2010-02-07 21:33:28 +0000 | [diff] [blame] | 3284 | bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(*this, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 3285 | PS1->getTemplateParameters(), |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3286 | PT1, |
| 3287 | PT2, |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 3288 | Info, |
| 3289 | Deduced, |
| 3290 | 0); |
Argyrios Kyrtzidis | 2c4792c | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 3291 | if (Better2) { |
| 3292 | InstantiatingTemplate Inst(*this, PS1->getLocation(), PS1, |
| 3293 | Deduced.data(), Deduced.size(), Info); |
Douglas Gregor | 516e6e0 | 2010-04-29 06:31:36 +0000 | [diff] [blame] | 3294 | Better2 = !::FinishTemplateArgumentDeduction(*this, PS1, |
| 3295 | PS2->getTemplateArgs(), |
| 3296 | Deduced, Info); |
Argyrios Kyrtzidis | 2c4792c | 2010-11-05 23:25:18 +0000 | [diff] [blame] | 3297 | } |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 3298 | |
| 3299 | if (Better1 == Better2) |
| 3300 | return 0; |
| 3301 | |
| 3302 | return Better1? PS1 : PS2; |
| 3303 | } |
| 3304 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3305 | static void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3306 | MarkUsedTemplateParameters(Sema &SemaRef, |
| 3307 | const TemplateArgument &TemplateArg, |
| 3308 | bool OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3309 | unsigned Depth, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3310 | llvm::SmallVectorImpl<bool> &Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3311 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3312 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3313 | /// expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3314 | static void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3315 | MarkUsedTemplateParameters(Sema &SemaRef, |
| 3316 | const Expr *E, |
| 3317 | bool OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3318 | unsigned Depth, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3319 | llvm::SmallVectorImpl<bool> &Used) { |
Douglas Gregor | be230c3 | 2011-01-03 17:17:50 +0000 | [diff] [blame] | 3320 | // We can deduce from a pack expansion. |
| 3321 | if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E)) |
| 3322 | E = Expansion->getPattern(); |
| 3323 | |
Douglas Gregor | 54c53cc | 2011-01-04 23:35:54 +0000 | [diff] [blame] | 3324 | // Skip through any implicit casts we added while type-checking. |
| 3325 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 3326 | E = ICE->getSubExpr(); |
| 3327 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3328 | // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to |
| 3329 | // find other occurrences of template parameters. |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 3330 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
Douglas Gregor | c781f9c | 2010-01-14 18:13:22 +0000 | [diff] [blame] | 3331 | if (!DRE) |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3332 | return; |
| 3333 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3334 | const NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3335 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 3336 | if (!NTTP) |
| 3337 | return; |
| 3338 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3339 | if (NTTP->getDepth() == Depth) |
| 3340 | Used[NTTP->getIndex()] = true; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3341 | } |
| 3342 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3343 | /// \brief Mark the template parameters that are used by the given |
| 3344 | /// nested name specifier. |
| 3345 | static void |
| 3346 | MarkUsedTemplateParameters(Sema &SemaRef, |
| 3347 | NestedNameSpecifier *NNS, |
| 3348 | bool OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3349 | unsigned Depth, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3350 | llvm::SmallVectorImpl<bool> &Used) { |
| 3351 | if (!NNS) |
| 3352 | return; |
| 3353 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3354 | MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth, |
| 3355 | Used); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3356 | MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3357 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3358 | } |
| 3359 | |
| 3360 | /// \brief Mark the template parameters that are used by the given |
| 3361 | /// template name. |
| 3362 | static void |
| 3363 | MarkUsedTemplateParameters(Sema &SemaRef, |
| 3364 | TemplateName Name, |
| 3365 | bool OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3366 | unsigned Depth, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3367 | llvm::SmallVectorImpl<bool> &Used) { |
| 3368 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 3369 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3370 | = dyn_cast<TemplateTemplateParmDecl>(Template)) { |
| 3371 | if (TTP->getDepth() == Depth) |
| 3372 | Used[TTP->getIndex()] = true; |
| 3373 | } |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3374 | return; |
| 3375 | } |
| 3376 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3377 | if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) |
| 3378 | MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced, |
| 3379 | Depth, Used); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3380 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3381 | MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced, |
| 3382 | Depth, Used); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3383 | } |
| 3384 | |
| 3385 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3386 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3387 | static void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3388 | MarkUsedTemplateParameters(Sema &SemaRef, QualType T, |
| 3389 | bool OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3390 | unsigned Depth, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3391 | llvm::SmallVectorImpl<bool> &Used) { |
| 3392 | if (T.isNull()) |
| 3393 | return; |
| 3394 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3395 | // Non-dependent types have nothing deducible |
| 3396 | if (!T->isDependentType()) |
| 3397 | return; |
| 3398 | |
| 3399 | T = SemaRef.Context.getCanonicalType(T); |
| 3400 | switch (T->getTypeClass()) { |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3401 | case Type::Pointer: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3402 | MarkUsedTemplateParameters(SemaRef, |
| 3403 | cast<PointerType>(T)->getPointeeType(), |
| 3404 | OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3405 | Depth, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3406 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3407 | break; |
| 3408 | |
| 3409 | case Type::BlockPointer: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3410 | MarkUsedTemplateParameters(SemaRef, |
| 3411 | cast<BlockPointerType>(T)->getPointeeType(), |
| 3412 | OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3413 | Depth, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3414 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3415 | break; |
| 3416 | |
| 3417 | case Type::LValueReference: |
| 3418 | case Type::RValueReference: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3419 | MarkUsedTemplateParameters(SemaRef, |
| 3420 | cast<ReferenceType>(T)->getPointeeType(), |
| 3421 | OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3422 | Depth, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3423 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3424 | break; |
| 3425 | |
| 3426 | case Type::MemberPointer: { |
| 3427 | const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3428 | MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3429 | Depth, Used); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3430 | MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3431 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3432 | break; |
| 3433 | } |
| 3434 | |
| 3435 | case Type::DependentSizedArray: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3436 | MarkUsedTemplateParameters(SemaRef, |
| 3437 | cast<DependentSizedArrayType>(T)->getSizeExpr(), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3438 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3439 | // Fall through to check the element type |
| 3440 | |
| 3441 | case Type::ConstantArray: |
| 3442 | case Type::IncompleteArray: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3443 | MarkUsedTemplateParameters(SemaRef, |
| 3444 | cast<ArrayType>(T)->getElementType(), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3445 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3446 | break; |
| 3447 | |
| 3448 | case Type::Vector: |
| 3449 | case Type::ExtVector: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3450 | MarkUsedTemplateParameters(SemaRef, |
| 3451 | cast<VectorType>(T)->getElementType(), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3452 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3453 | break; |
| 3454 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 3455 | case Type::DependentSizedExtVector: { |
| 3456 | const DependentSizedExtVectorType *VecType |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 3457 | = cast<DependentSizedExtVectorType>(T); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3458 | MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3459 | Depth, Used); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3460 | MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3461 | Depth, Used); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 3462 | break; |
| 3463 | } |
| 3464 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3465 | case Type::FunctionProto: { |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 3466 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3467 | MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3468 | Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3469 | for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I) |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3470 | MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3471 | Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3472 | break; |
| 3473 | } |
| 3474 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3475 | case Type::TemplateTypeParm: { |
| 3476 | const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T); |
| 3477 | if (TTP->getDepth() == Depth) |
| 3478 | Used[TTP->getIndex()] = true; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3479 | break; |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3480 | } |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3481 | |
John McCall | 31f17ec | 2010-04-27 00:57:59 +0000 | [diff] [blame] | 3482 | case Type::InjectedClassName: |
| 3483 | T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); |
| 3484 | // fall through |
| 3485 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3486 | case Type::TemplateSpecialization: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3487 | const TemplateSpecializationType *Spec |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 3488 | = cast<TemplateSpecializationType>(T); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3489 | MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3490 | Depth, Used); |
Douglas Gregor | 7b976ec | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 3491 | |
| 3492 | // C++0x [temp.deduct.type]p9: |
| 3493 | // If the template argument list of P contains a pack expansion that is not |
| 3494 | // the last template argument, the entire template argument list is a |
| 3495 | // non-deduced context. |
| 3496 | if (OnlyDeduced && |
| 3497 | hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs())) |
| 3498 | break; |
| 3499 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3500 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3501 | MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth, |
| 3502 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3503 | break; |
| 3504 | } |
| 3505 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3506 | case Type::Complex: |
| 3507 | if (!OnlyDeduced) |
| 3508 | MarkUsedTemplateParameters(SemaRef, |
| 3509 | cast<ComplexType>(T)->getElementType(), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3510 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3511 | break; |
| 3512 | |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 3513 | case Type::DependentName: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3514 | if (!OnlyDeduced) |
| 3515 | MarkUsedTemplateParameters(SemaRef, |
Douglas Gregor | 4714c12 | 2010-03-31 17:34:00 +0000 | [diff] [blame] | 3516 | cast<DependentNameType>(T)->getQualifier(), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3517 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3518 | break; |
| 3519 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3520 | case Type::DependentTemplateSpecialization: { |
| 3521 | const DependentTemplateSpecializationType *Spec |
| 3522 | = cast<DependentTemplateSpecializationType>(T); |
| 3523 | if (!OnlyDeduced) |
| 3524 | MarkUsedTemplateParameters(SemaRef, Spec->getQualifier(), |
| 3525 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 7b976ec | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 3526 | |
| 3527 | // C++0x [temp.deduct.type]p9: |
| 3528 | // If the template argument list of P contains a pack expansion that is not |
| 3529 | // the last template argument, the entire template argument list is a |
| 3530 | // non-deduced context. |
| 3531 | if (OnlyDeduced && |
| 3532 | hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs())) |
| 3533 | break; |
| 3534 | |
John McCall | 3350095 | 2010-06-11 00:33:02 +0000 | [diff] [blame] | 3535 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
| 3536 | MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth, |
| 3537 | Used); |
| 3538 | break; |
| 3539 | } |
| 3540 | |
John McCall | ad5e738 | 2010-03-01 23:49:17 +0000 | [diff] [blame] | 3541 | case Type::TypeOf: |
| 3542 | if (!OnlyDeduced) |
| 3543 | MarkUsedTemplateParameters(SemaRef, |
| 3544 | cast<TypeOfType>(T)->getUnderlyingType(), |
| 3545 | OnlyDeduced, Depth, Used); |
| 3546 | break; |
| 3547 | |
| 3548 | case Type::TypeOfExpr: |
| 3549 | if (!OnlyDeduced) |
| 3550 | MarkUsedTemplateParameters(SemaRef, |
| 3551 | cast<TypeOfExprType>(T)->getUnderlyingExpr(), |
| 3552 | OnlyDeduced, Depth, Used); |
| 3553 | break; |
| 3554 | |
| 3555 | case Type::Decltype: |
| 3556 | if (!OnlyDeduced) |
| 3557 | MarkUsedTemplateParameters(SemaRef, |
| 3558 | cast<DecltypeType>(T)->getUnderlyingExpr(), |
| 3559 | OnlyDeduced, Depth, Used); |
| 3560 | break; |
| 3561 | |
Douglas Gregor | 7536dd5 | 2010-12-20 02:24:11 +0000 | [diff] [blame] | 3562 | case Type::PackExpansion: |
| 3563 | MarkUsedTemplateParameters(SemaRef, |
| 3564 | cast<PackExpansionType>(T)->getPattern(), |
| 3565 | OnlyDeduced, Depth, Used); |
| 3566 | break; |
| 3567 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3568 | // None of these types have any template parameters in them. |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3569 | case Type::Builtin: |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3570 | case Type::VariableArray: |
| 3571 | case Type::FunctionNoProto: |
| 3572 | case Type::Record: |
| 3573 | case Type::Enum: |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3574 | case Type::ObjCInterface: |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3575 | case Type::ObjCObject: |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3576 | case Type::ObjCObjectPointer: |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 3577 | case Type::UnresolvedUsing: |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3578 | #define TYPE(Class, Base) |
| 3579 | #define ABSTRACT_TYPE(Class, Base) |
| 3580 | #define DEPENDENT_TYPE(Class, Base) |
| 3581 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 3582 | #include "clang/AST/TypeNodes.def" |
| 3583 | break; |
| 3584 | } |
| 3585 | } |
| 3586 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3587 | /// \brief Mark the template parameters that are used by this |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3588 | /// template argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3589 | static void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3590 | MarkUsedTemplateParameters(Sema &SemaRef, |
| 3591 | const TemplateArgument &TemplateArg, |
| 3592 | bool OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3593 | unsigned Depth, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3594 | llvm::SmallVectorImpl<bool> &Used) { |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3595 | switch (TemplateArg.getKind()) { |
| 3596 | case TemplateArgument::Null: |
| 3597 | case TemplateArgument::Integral: |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3598 | case TemplateArgument::Declaration: |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3599 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3600 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3601 | case TemplateArgument::Type: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3602 | MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3603 | Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3604 | break; |
| 3605 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3606 | case TemplateArgument::Template: |
Douglas Gregor | a7fc901 | 2011-01-05 18:58:31 +0000 | [diff] [blame] | 3607 | case TemplateArgument::TemplateExpansion: |
| 3608 | MarkUsedTemplateParameters(SemaRef, |
| 3609 | TemplateArg.getAsTemplateOrTemplatePattern(), |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 3610 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3611 | break; |
| 3612 | |
| 3613 | case TemplateArgument::Expression: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3614 | MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3615 | Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3616 | break; |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3617 | |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 3618 | case TemplateArgument::Pack: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3619 | for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(), |
| 3620 | PEnd = TemplateArg.pack_end(); |
| 3621 | P != PEnd; ++P) |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3622 | MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used); |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 3623 | break; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3624 | } |
| 3625 | } |
| 3626 | |
| 3627 | /// \brief Mark the template parameters can be deduced by the given |
| 3628 | /// template argument list. |
| 3629 | /// |
| 3630 | /// \param TemplateArgs the template argument list from which template |
| 3631 | /// parameters will be deduced. |
| 3632 | /// |
| 3633 | /// \param Deduced a bit vector whose elements will be set to \c true |
| 3634 | /// to indicate when the corresponding template parameter will be |
| 3635 | /// deduced. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3636 | void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3637 | Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3638 | bool OnlyDeduced, unsigned Depth, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 3639 | llvm::SmallVectorImpl<bool> &Used) { |
Douglas Gregor | 7b976ec | 2010-12-23 01:24:45 +0000 | [diff] [blame] | 3640 | // C++0x [temp.deduct.type]p9: |
| 3641 | // If the template argument list of P contains a pack expansion that is not |
| 3642 | // the last template argument, the entire template argument list is a |
| 3643 | // non-deduced context. |
| 3644 | if (OnlyDeduced && |
| 3645 | hasPackExpansionBeforeEnd(TemplateArgs.data(), TemplateArgs.size())) |
| 3646 | return; |
| 3647 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3648 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3649 | ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced, |
| 3650 | Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 3651 | } |
Douglas Gregor | 63f07c5 | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 3652 | |
| 3653 | /// \brief Marks all of the template parameters that will be deduced by a |
| 3654 | /// call to the given function template. |
Douglas Gregor | 02024a9 | 2010-03-28 02:42:43 +0000 | [diff] [blame] | 3655 | void |
| 3656 | Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate, |
| 3657 | llvm::SmallVectorImpl<bool> &Deduced) { |
Douglas Gregor | 63f07c5 | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 3658 | TemplateParameterList *TemplateParams |
| 3659 | = FunctionTemplate->getTemplateParameters(); |
| 3660 | Deduced.clear(); |
| 3661 | Deduced.resize(TemplateParams->size()); |
| 3662 | |
| 3663 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 3664 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) |
| 3665 | ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 3666 | true, TemplateParams->getDepth(), Deduced); |
Douglas Gregor | 63f07c5 | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 3667 | } |