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