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