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