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