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