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