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