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