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