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