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