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