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 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | DeduceTemplateArguments(ASTContext &Context, |
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 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 75 | DeduceNonTypeTemplateArgument(ASTContext &Context, |
| 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! |
| 87 | unsigned AllowedBits = Context.getTypeSize(T); |
| 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 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | DeduceNonTypeTemplateArgument(ASTContext &Context, |
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; |
| 155 | Deduced[NTTP->getIndex()].getAsExpr()->Profile(ID1, Context, true); |
| 156 | Value->Profile(ID2, Context, true); |
| 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 |
| 172 | DeduceNonTypeTemplateArgument(ASTContext &Context, |
| 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 |
| 205 | DeduceTemplateArguments(ASTContext &Context, |
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. |
| 224 | ExistingArg = TemplateArgument(Context.getCanonicalTemplateName(Arg)); |
| 225 | return Sema::TDK_Success; |
| 226 | } |
| 227 | |
| 228 | // Verify that the previous binding matches this deduction. |
| 229 | assert(ExistingArg.getKind() == TemplateArgument::Template); |
| 230 | if (Context.hasSameTemplateName(ExistingArg.getAsTemplate(), Arg)) |
| 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. |
| 241 | if (Context.hasSameTemplateName(Param, Arg)) |
| 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 | /// |
| 253 | /// \param Context the AST context in which this deduction occurs. |
| 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 |
| 269 | DeduceTemplateArguments(ASTContext &Context, |
| 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 |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 282 | = DeduceTemplateArguments(Context, 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 |
| 294 | = DeduceTemplateArguments(Context, TemplateParams, |
| 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 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 317 | = DeduceTemplateArguments(Context, |
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 |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 331 | = DeduceTemplateArguments(Context, TemplateParams, |
| 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 | /// |
| 343 | /// \param Context the AST context in which this deduction occurs. |
| 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 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 362 | DeduceTemplateArguments(ASTContext &Context, |
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. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 370 | QualType Param = Context.getCanonicalType(ParamIn); |
| 371 | QualType Arg = 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; |
| 379 | QualType UnqualParam = Context.getUnqualifiedArrayType(Param, Quals); |
| 380 | Quals.setCVRQualifiers(Quals.getCVRQualifiers() & |
| 381 | Arg.getCVRQualifiersThroughArrayTypes()); |
| 382 | Param = 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 | 28e318c | 2009-12-29 07:16:59 +0000 | [diff] [blame] | 412 | Arg = Context.getUnqualifiedArrayType(Arg, Quals); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 413 | if (Quals) { |
| 414 | Arg = 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"); |
Douglas Gregor | c78a69d | 2009-12-21 21:27:38 +0000 | [diff] [blame] | 429 | assert(Arg != 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) |
| 433 | DeducedType = 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); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 482 | return DeduceTemplateArguments(Context, 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 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 494 | return DeduceTemplateArguments(Context, 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 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 506 | return DeduceTemplateArguments(Context, 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 = |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 515 | Context.getAsIncompleteArrayType(Arg); |
| 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 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 519 | return DeduceTemplateArguments(Context, TemplateParams, |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 520 | Context.getAsIncompleteArrayType(Param)->getElementType(), |
| 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 = |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 528 | Context.getAsConstantArrayType(Arg); |
| 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 = |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 533 | Context.getAsConstantArrayType(Param); |
| 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 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 537 | return DeduceTemplateArguments(Context, 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: { |
Douglas Gregor | 4424713 | 2010-01-04 22:11:45 +0000 | [diff] [blame] | 545 | const ArrayType *ArrayArg = 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 |
Douglas Gregor | 4424713 | 2010-01-04 22:11:45 +0000 | [diff] [blame] | 551 | = Context.getAsDependentSizedArrayType(Param); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 552 | if (Sema::TemplateDeductionResult Result |
| 553 | = DeduceTemplateArguments(Context, TemplateParams, |
| 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()); |
| 572 | return DeduceNonTypeTemplateArgument(Context, 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)) |
| 577 | return DeduceNonTypeTemplateArgument(Context, NTTP, |
| 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 |
| 609 | = DeduceTemplateArguments(Context, TemplateParams, |
| 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 |
| 618 | = DeduceTemplateArguments(Context, TemplateParams, |
| 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 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 639 | = DeduceTemplateArguments(Context, 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. |
| 652 | if (const RecordType *RecordT = dyn_cast<RecordType>(Arg)) { |
| 653 | // Use data recursion to crawl through the list of base classes. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | // Visited contains the set of nodes we have already visited, while |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 655 | // ToVisit is our stack of records that we still need to visit. |
| 656 | llvm::SmallPtrSet<const RecordType *, 8> Visited; |
| 657 | llvm::SmallVector<const RecordType *, 8> ToVisit; |
| 658 | ToVisit.push_back(RecordT); |
| 659 | bool Successful = false; |
| 660 | while (!ToVisit.empty()) { |
| 661 | // Retrieve the next class in the inheritance hierarchy. |
| 662 | const RecordType *NextT = ToVisit.back(); |
| 663 | ToVisit.pop_back(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 664 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 665 | // If we have already seen this type, skip it. |
| 666 | if (!Visited.insert(NextT)) |
| 667 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 668 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 669 | // If this is a base class, try to perform template argument |
| 670 | // deduction from it. |
| 671 | if (NextT != RecordT) { |
| 672 | Sema::TemplateDeductionResult BaseResult |
| 673 | = DeduceTemplateArguments(Context, TemplateParams, SpecParam, |
| 674 | QualType(NextT, 0), Info, Deduced); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 675 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 676 | // If template argument deduction for this base was successful, |
| 677 | // note that we had some success. |
| 678 | if (BaseResult == Sema::TDK_Success) |
| 679 | Successful = true; |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 680 | } |
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 | // Visit base classes |
| 683 | CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl()); |
| 684 | for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(), |
| 685 | BaseEnd = Next->bases_end(); |
Sebastian Redl | 9994a34 | 2009-10-25 17:03:50 +0000 | [diff] [blame] | 686 | Base != BaseEnd; ++Base) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 687 | assert(Base->getType()->isRecordType() && |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 688 | "Base class that isn't a record?"); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 689 | ToVisit.push_back(Base->getType()->getAs<RecordType>()); |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 690 | } |
| 691 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 692 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 693 | if (Successful) |
| 694 | return Sema::TDK_Success; |
| 695 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 696 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 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 | return Result; |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 702 | // T type::* |
| 703 | // T T::* |
| 704 | // T (type::*)() |
| 705 | // type (T::*)() |
| 706 | // type (type::*)(T) |
| 707 | // type (T::*)(T) |
| 708 | // T (type::*)(T) |
| 709 | // T (T::*)() |
| 710 | // T (T::*)(T) |
| 711 | case Type::MemberPointer: { |
| 712 | const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); |
| 713 | const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); |
| 714 | if (!MemPtrArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 715 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 716 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 717 | if (Sema::TemplateDeductionResult Result |
| 718 | = DeduceTemplateArguments(Context, TemplateParams, |
| 719 | MemPtrParam->getPointeeType(), |
| 720 | MemPtrArg->getPointeeType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 721 | Info, Deduced, |
| 722 | TDF & TDF_IgnoreQualifiers)) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 723 | return Result; |
| 724 | |
| 725 | return DeduceTemplateArguments(Context, TemplateParams, |
| 726 | QualType(MemPtrParam->getClass(), 0), |
| 727 | QualType(MemPtrArg->getClass(), 0), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 728 | Info, Deduced, 0); |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 731 | // (clang extension) |
| 732 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 733 | // type(^)(T) |
| 734 | // T(^)() |
| 735 | // T(^)(T) |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 736 | case Type::BlockPointer: { |
| 737 | const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); |
| 738 | const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 739 | |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 740 | if (!BlockPtrArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 741 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 742 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 743 | return DeduceTemplateArguments(Context, TemplateParams, |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 744 | BlockPtrParam->getPointeeType(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 745 | BlockPtrArg->getPointeeType(), Info, |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 746 | Deduced, 0); |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 749 | case Type::TypeOfExpr: |
| 750 | case Type::TypeOf: |
| 751 | case Type::Typename: |
| 752 | // No template argument deduction for these types |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 753 | return Sema::TDK_Success; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 754 | |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 755 | default: |
| 756 | break; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | // FIXME: Many more cases to go (to go). |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 760 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 763 | static Sema::TemplateDeductionResult |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 764 | DeduceTemplateArguments(ASTContext &Context, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 765 | TemplateParameterList *TemplateParams, |
| 766 | const TemplateArgument &Param, |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 767 | const TemplateArgument &Arg, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 768 | Sema::TemplateDeductionInfo &Info, |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 769 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 770 | switch (Param.getKind()) { |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 771 | case TemplateArgument::Null: |
| 772 | assert(false && "Null template argument in parameter list"); |
| 773 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 774 | |
| 775 | case TemplateArgument::Type: |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 776 | if (Arg.getKind() == TemplateArgument::Type) |
| 777 | return DeduceTemplateArguments(Context, TemplateParams, Param.getAsType(), |
| 778 | Arg.getAsType(), Info, Deduced, 0); |
| 779 | Info.FirstArg = Param; |
| 780 | Info.SecondArg = Arg; |
| 781 | return Sema::TDK_NonDeducedMismatch; |
| 782 | |
| 783 | case TemplateArgument::Template: |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 784 | if (Arg.getKind() == TemplateArgument::Template) |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 785 | return DeduceTemplateArguments(Context, TemplateParams, |
| 786 | Param.getAsTemplate(), |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 787 | Arg.getAsTemplate(), Info, Deduced); |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 788 | Info.FirstArg = Param; |
| 789 | Info.SecondArg = Arg; |
| 790 | return Sema::TDK_NonDeducedMismatch; |
| 791 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 792 | case TemplateArgument::Declaration: |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 793 | if (Arg.getKind() == TemplateArgument::Declaration && |
| 794 | Param.getAsDecl()->getCanonicalDecl() == |
| 795 | Arg.getAsDecl()->getCanonicalDecl()) |
| 796 | return Sema::TDK_Success; |
| 797 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 798 | Info.FirstArg = Param; |
| 799 | Info.SecondArg = Arg; |
| 800 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 801 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 802 | case TemplateArgument::Integral: |
| 803 | if (Arg.getKind() == TemplateArgument::Integral) { |
| 804 | // FIXME: Zero extension + sign checking here? |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 805 | if (*Param.getAsIntegral() == *Arg.getAsIntegral()) |
| 806 | return Sema::TDK_Success; |
| 807 | |
| 808 | Info.FirstArg = Param; |
| 809 | Info.SecondArg = Arg; |
| 810 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 811 | } |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 812 | |
| 813 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 814 | Info.FirstArg = Param; |
| 815 | Info.SecondArg = Arg; |
| 816 | return Sema::TDK_NonDeducedMismatch; |
| 817 | } |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 818 | |
| 819 | assert(false && "Type/value mismatch"); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 820 | Info.FirstArg = Param; |
| 821 | Info.SecondArg = Arg; |
| 822 | return Sema::TDK_NonDeducedMismatch; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 823 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 824 | case TemplateArgument::Expression: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 826 | = getDeducedParameterFromExpr(Param.getAsExpr())) { |
| 827 | if (Arg.getKind() == TemplateArgument::Integral) |
| 828 | // FIXME: Sign problems here |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 829 | return DeduceNonTypeTemplateArgument(Context, NTTP, |
| 830 | *Arg.getAsIntegral(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 831 | Info, Deduced); |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 832 | if (Arg.getKind() == TemplateArgument::Expression) |
| 833 | return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 834 | Info, Deduced); |
Douglas Gregor | 15755cb | 2009-11-13 23:45:44 +0000 | [diff] [blame] | 835 | if (Arg.getKind() == TemplateArgument::Declaration) |
| 836 | return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsDecl(), |
| 837 | Info, Deduced); |
| 838 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 839 | assert(false && "Type/value mismatch"); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 840 | Info.FirstArg = Param; |
| 841 | Info.SecondArg = Arg; |
| 842 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 843 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 844 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 845 | // Can't deduce anything, but that's okay. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 846 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 847 | } |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 848 | case TemplateArgument::Pack: |
| 849 | assert(0 && "FIXME: Implement!"); |
| 850 | break; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 851 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 852 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 853 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 854 | } |
| 855 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 856 | static Sema::TemplateDeductionResult |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 857 | DeduceTemplateArguments(ASTContext &Context, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 858 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 859 | const TemplateArgumentList &ParamList, |
| 860 | const TemplateArgumentList &ArgList, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 861 | Sema::TemplateDeductionInfo &Info, |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 862 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
| 863 | assert(ParamList.size() == ArgList.size()); |
| 864 | for (unsigned I = 0, N = ParamList.size(); I != N; ++I) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 865 | if (Sema::TemplateDeductionResult Result |
| 866 | = DeduceTemplateArguments(Context, TemplateParams, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 867 | ParamList[I], ArgList[I], |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 868 | Info, Deduced)) |
| 869 | return Result; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 870 | } |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 871 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 874 | /// \brief Determine whether two template arguments are the same. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 875 | static bool isSameTemplateArg(ASTContext &Context, |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 876 | const TemplateArgument &X, |
| 877 | const TemplateArgument &Y) { |
| 878 | if (X.getKind() != Y.getKind()) |
| 879 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 880 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 881 | switch (X.getKind()) { |
| 882 | case TemplateArgument::Null: |
| 883 | assert(false && "Comparing NULL template argument"); |
| 884 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 885 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 886 | case TemplateArgument::Type: |
| 887 | return Context.getCanonicalType(X.getAsType()) == |
| 888 | Context.getCanonicalType(Y.getAsType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 889 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 890 | case TemplateArgument::Declaration: |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 891 | return X.getAsDecl()->getCanonicalDecl() == |
| 892 | Y.getAsDecl()->getCanonicalDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 893 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 894 | case TemplateArgument::Template: |
| 895 | return Context.getCanonicalTemplateName(X.getAsTemplate()) |
| 896 | .getAsVoidPointer() == |
| 897 | Context.getCanonicalTemplateName(Y.getAsTemplate()) |
| 898 | .getAsVoidPointer(); |
| 899 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 900 | case TemplateArgument::Integral: |
| 901 | return *X.getAsIntegral() == *Y.getAsIntegral(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 902 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 903 | case TemplateArgument::Expression: { |
| 904 | llvm::FoldingSetNodeID XID, YID; |
| 905 | X.getAsExpr()->Profile(XID, Context, true); |
| 906 | Y.getAsExpr()->Profile(YID, Context, true); |
| 907 | return XID == YID; |
| 908 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 909 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 910 | case TemplateArgument::Pack: |
| 911 | if (X.pack_size() != Y.pack_size()) |
| 912 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 913 | |
| 914 | for (TemplateArgument::pack_iterator XP = X.pack_begin(), |
| 915 | XPEnd = X.pack_end(), |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 916 | YP = Y.pack_begin(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 917 | XP != XPEnd; ++XP, ++YP) |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 918 | if (!isSameTemplateArg(Context, *XP, *YP)) |
| 919 | return false; |
| 920 | |
| 921 | return true; |
| 922 | } |
| 923 | |
| 924 | return false; |
| 925 | } |
| 926 | |
| 927 | /// \brief Helper function to build a TemplateParameter when we don't |
| 928 | /// know its type statically. |
| 929 | static TemplateParameter makeTemplateParameter(Decl *D) { |
| 930 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) |
| 931 | return TemplateParameter(TTP); |
| 932 | else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) |
| 933 | return TemplateParameter(NTTP); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 934 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 935 | return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); |
| 936 | } |
| 937 | |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 938 | /// \brief Perform template argument deduction to determine whether |
| 939 | /// the given template arguments match the given class template |
| 940 | /// partial specialization per C++ [temp.class.spec.match]. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 941 | Sema::TemplateDeductionResult |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 942 | Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 943 | const TemplateArgumentList &TemplateArgs, |
| 944 | TemplateDeductionInfo &Info) { |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 945 | // C++ [temp.class.spec.match]p2: |
| 946 | // A partial specialization matches a given actual template |
| 947 | // argument list if the template arguments of the partial |
| 948 | // specialization can be deduced from the actual template argument |
| 949 | // list (14.8.2). |
Douglas Gregor | bb26041 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 950 | SFINAETrap Trap(*this); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 951 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
| 952 | Deduced.resize(Partial->getTemplateParameters()->size()); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 953 | if (TemplateDeductionResult Result |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 954 | = ::DeduceTemplateArguments(Context, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 955 | Partial->getTemplateParameters(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 956 | Partial->getTemplateArgs(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 957 | TemplateArgs, Info, Deduced)) |
| 958 | return Result; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 959 | |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 960 | InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial, |
| 961 | Deduced.data(), Deduced.size()); |
| 962 | if (Inst) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 963 | return TDK_InstantiationDepth; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 964 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 965 | // C++ [temp.deduct.type]p2: |
| 966 | // [...] or if any template argument remains neither deduced nor |
| 967 | // explicitly specified, template argument deduction fails. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 968 | TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(), |
| 969 | Deduced.size()); |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 970 | for (unsigned I = 0, N = Deduced.size(); I != N; ++I) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 971 | if (Deduced[I].isNull()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 972 | Decl *Param |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 973 | = const_cast<NamedDecl *>( |
| 974 | Partial->getTemplateParameters()->getParam(I)); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 975 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 976 | Info.Param = TTP; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 977 | else if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 978 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 979 | Info.Param = NTTP; |
| 980 | else |
| 981 | Info.Param = cast<TemplateTemplateParmDecl>(Param); |
| 982 | return TDK_Incomplete; |
| 983 | } |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 984 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 985 | Builder.Append(Deduced[I]); |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | // Form the template argument list from the deduced template arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 989 | TemplateArgumentList *DeducedArgumentList |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 990 | = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 991 | Info.reset(DeducedArgumentList); |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 992 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 993 | // Substitute the deduced template arguments into the template |
| 994 | // arguments of the class template partial specialization, and |
| 995 | // verify that the instantiated template arguments are both valid |
| 996 | // and are equivalent to the template arguments originally provided |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 997 | // to the class template. |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 998 | ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate(); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 999 | const TemplateArgumentLoc *PartialTemplateArgs |
| 1000 | = Partial->getTemplateArgsAsWritten(); |
| 1001 | unsigned N = Partial->getNumTemplateArgsAsWritten(); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1002 | |
| 1003 | // Note that we don't provide the langle and rangle locations. |
| 1004 | TemplateArgumentListInfo InstArgs; |
| 1005 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1006 | for (unsigned I = 0; I != N; ++I) { |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1007 | Decl *Param = const_cast<NamedDecl *>( |
Douglas Gregor | c9e5d25 | 2009-06-13 00:59:32 +0000 | [diff] [blame] | 1008 | ClassTemplate->getTemplateParameters()->getParam(I)); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1009 | TemplateArgumentLoc InstArg; |
| 1010 | if (Subst(PartialTemplateArgs[I], InstArg, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1011 | MultiLevelTemplateArgumentList(*DeducedArgumentList))) { |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1012 | Info.Param = makeTemplateParameter(Param); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1013 | Info.FirstArg = PartialTemplateArgs[I].getArgument(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1014 | return TDK_SubstitutionFailure; |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 1015 | } |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1016 | InstArgs.addArgument(InstArg); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | TemplateArgumentListBuilder ConvertedInstArgs( |
| 1020 | ClassTemplate->getTemplateParameters(), N); |
| 1021 | |
| 1022 | if (CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1023 | InstArgs, false, ConvertedInstArgs)) { |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1024 | // FIXME: fail with more useful information? |
| 1025 | return TDK_SubstitutionFailure; |
| 1026 | } |
| 1027 | |
| 1028 | for (unsigned I = 0, E = ConvertedInstArgs.flatSize(); I != E; ++I) { |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1029 | TemplateArgument InstArg = ConvertedInstArgs.getFlatArguments()[I]; |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1030 | |
| 1031 | Decl *Param = const_cast<NamedDecl *>( |
| 1032 | ClassTemplate->getTemplateParameters()->getParam(I)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1033 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1034 | if (InstArg.getKind() == TemplateArgument::Expression) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1035 | // When the argument is an expression, check the expression result |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1036 | // against the actual template parameter to get down to the canonical |
| 1037 | // template argument. |
| 1038 | Expr *InstExpr = InstArg.getAsExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1039 | if (NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1040 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 1041 | if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) { |
| 1042 | Info.Param = makeTemplateParameter(Param); |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame] | 1043 | Info.FirstArg = Partial->getTemplateArgs()[I]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1044 | return TDK_SubstitutionFailure; |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1045 | } |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1046 | } |
| 1047 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1048 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 1049 | if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) { |
| 1050 | Info.Param = makeTemplateParameter(Param); |
| 1051 | Info.FirstArg = TemplateArgs[I]; |
| 1052 | Info.SecondArg = InstArg; |
| 1053 | return TDK_NonDeducedMismatch; |
| 1054 | } |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Douglas Gregor | bb26041 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 1057 | if (Trap.hasErrorOccurred()) |
| 1058 | return TDK_SubstitutionFailure; |
| 1059 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1060 | return TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1061 | } |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1062 | |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1063 | /// \brief Determine whether the given type T is a simple-template-id type. |
| 1064 | static bool isSimpleTemplateIdType(QualType T) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1065 | if (const TemplateSpecializationType *Spec |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1066 | = T->getAs<TemplateSpecializationType>()) |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1067 | return Spec->getTemplateName().getAsTemplateDecl() != 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1068 | |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1069 | return false; |
| 1070 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1071 | |
| 1072 | /// \brief Substitute the explicitly-provided template arguments into the |
| 1073 | /// given function template according to C++ [temp.arg.explicit]. |
| 1074 | /// |
| 1075 | /// \param FunctionTemplate the function template into which the explicit |
| 1076 | /// template arguments will be substituted. |
| 1077 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1078 | /// \param ExplicitTemplateArguments the explicitly-specified template |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1079 | /// arguments. |
| 1080 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1081 | /// \param Deduced the deduced template arguments, which will be populated |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1082 | /// with the converted and checked explicit template arguments. |
| 1083 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1084 | /// \param ParamTypes will be populated with the instantiated function |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1085 | /// parameters. |
| 1086 | /// |
| 1087 | /// \param FunctionType if non-NULL, the result type of the function template |
| 1088 | /// will also be instantiated and the pointed-to value will be updated with |
| 1089 | /// the instantiated function type. |
| 1090 | /// |
| 1091 | /// \param Info if substitution fails for any reason, this object will be |
| 1092 | /// populated with more information about the failure. |
| 1093 | /// |
| 1094 | /// \returns TDK_Success if substitution was successful, or some failure |
| 1095 | /// condition. |
| 1096 | Sema::TemplateDeductionResult |
| 1097 | Sema::SubstituteExplicitTemplateArguments( |
| 1098 | FunctionTemplateDecl *FunctionTemplate, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1099 | const TemplateArgumentListInfo &ExplicitTemplateArgs, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1100 | llvm::SmallVectorImpl<TemplateArgument> &Deduced, |
| 1101 | llvm::SmallVectorImpl<QualType> &ParamTypes, |
| 1102 | QualType *FunctionType, |
| 1103 | TemplateDeductionInfo &Info) { |
| 1104 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 1105 | TemplateParameterList *TemplateParams |
| 1106 | = FunctionTemplate->getTemplateParameters(); |
| 1107 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1108 | if (ExplicitTemplateArgs.size() == 0) { |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1109 | // No arguments to substitute; just copy over the parameter types and |
| 1110 | // fill in the function type. |
| 1111 | for (FunctionDecl::param_iterator P = Function->param_begin(), |
| 1112 | PEnd = Function->param_end(); |
| 1113 | P != PEnd; |
| 1114 | ++P) |
| 1115 | ParamTypes.push_back((*P)->getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1116 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1117 | if (FunctionType) |
| 1118 | *FunctionType = Function->getType(); |
| 1119 | return TDK_Success; |
| 1120 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1121 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1122 | // Substitution of the explicit template arguments into a function template |
| 1123 | /// is a SFINAE context. Trap any errors that might occur. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1124 | SFINAETrap Trap(*this); |
| 1125 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1126 | // C++ [temp.arg.explicit]p3: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1127 | // Template arguments that are present shall be specified in the |
| 1128 | // declaration order of their corresponding template-parameters. The |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1129 | // template argument list shall not specify more template-arguments than |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1130 | // there are corresponding template-parameters. |
| 1131 | TemplateArgumentListBuilder Builder(TemplateParams, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1132 | ExplicitTemplateArgs.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1133 | |
| 1134 | // Enter a new template instantiation context where we check the |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1135 | // explicitly-specified template arguments against this function template, |
| 1136 | // and then substitute them into the function parameter types. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1137 | InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1138 | FunctionTemplate, Deduced.data(), Deduced.size(), |
| 1139 | ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution); |
| 1140 | if (Inst) |
| 1141 | return TDK_InstantiationDepth; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1142 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1143 | if (CheckTemplateArgumentList(FunctionTemplate, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1144 | SourceLocation(), |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1145 | ExplicitTemplateArgs, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1146 | true, |
| 1147 | Builder) || Trap.hasErrorOccurred()) |
| 1148 | return TDK_InvalidExplicitArguments; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1149 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1150 | // Form the template argument list from the explicitly-specified |
| 1151 | // template arguments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1152 | TemplateArgumentList *ExplicitArgumentList |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1153 | = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); |
| 1154 | Info.reset(ExplicitArgumentList); |
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 | // Instantiate the types of each of the function parameters given the |
| 1157 | // explicitly-specified template arguments. |
| 1158 | for (FunctionDecl::param_iterator P = Function->param_begin(), |
| 1159 | PEnd = Function->param_end(); |
| 1160 | P != PEnd; |
| 1161 | ++P) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1162 | QualType ParamType |
| 1163 | = SubstType((*P)->getType(), |
Douglas Gregor | 357bbd0 | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 1164 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 1165 | (*P)->getLocation(), (*P)->getDeclName()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1166 | if (ParamType.isNull() || Trap.hasErrorOccurred()) |
| 1167 | return TDK_SubstitutionFailure; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1168 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1169 | ParamTypes.push_back(ParamType); |
| 1170 | } |
| 1171 | |
| 1172 | // If the caller wants a full function type back, instantiate the return |
| 1173 | // type and form that function type. |
| 1174 | if (FunctionType) { |
| 1175 | // FIXME: exception-specifications? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1176 | const FunctionProtoType *Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1177 | = Function->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1178 | assert(Proto && "Function template does not have a prototype?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1179 | |
| 1180 | QualType ResultType |
Douglas Gregor | 357bbd0 | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 1181 | = SubstType(Proto->getResultType(), |
| 1182 | MultiLevelTemplateArgumentList(*ExplicitArgumentList), |
| 1183 | Function->getTypeSpecStartLoc(), |
| 1184 | Function->getDeclName()); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1185 | if (ResultType.isNull() || Trap.hasErrorOccurred()) |
| 1186 | return TDK_SubstitutionFailure; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1187 | |
| 1188 | *FunctionType = BuildFunctionType(ResultType, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1189 | ParamTypes.data(), ParamTypes.size(), |
| 1190 | Proto->isVariadic(), |
| 1191 | Proto->getTypeQuals(), |
| 1192 | Function->getLocation(), |
| 1193 | Function->getDeclName()); |
| 1194 | if (FunctionType->isNull() || Trap.hasErrorOccurred()) |
| 1195 | return TDK_SubstitutionFailure; |
| 1196 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1197 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1198 | // C++ [temp.arg.explicit]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 | // Trailing template arguments that can be deduced (14.8.2) may be |
| 1200 | // omitted from the list of explicit template-arguments. If all of the |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1201 | // template arguments can be deduced, they may all be omitted; in this |
| 1202 | // case, the empty template argument list <> itself may also be omitted. |
| 1203 | // |
| 1204 | // Take all of the explicitly-specified arguments and put them into the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1205 | // set of deduced template arguments. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1206 | Deduced.reserve(TemplateParams->size()); |
| 1207 | for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1208 | Deduced.push_back(ExplicitArgumentList->get(I)); |
| 1209 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1210 | return TDK_Success; |
| 1211 | } |
| 1212 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1213 | /// \brief Finish template argument deduction for a function template, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1214 | /// checking the deduced template arguments for completeness and forming |
| 1215 | /// the function template specialization. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1216 | Sema::TemplateDeductionResult |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1217 | Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate, |
| 1218 | llvm::SmallVectorImpl<TemplateArgument> &Deduced, |
| 1219 | FunctionDecl *&Specialization, |
| 1220 | TemplateDeductionInfo &Info) { |
| 1221 | TemplateParameterList *TemplateParams |
| 1222 | = FunctionTemplate->getTemplateParameters(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1223 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1224 | // Template argument deduction for function templates in a SFINAE context. |
| 1225 | // Trap any errors that might occur. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1226 | SFINAETrap Trap(*this); |
| 1227 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1228 | // Enter a new template instantiation context while we instantiate the |
| 1229 | // actual function declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1230 | InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1231 | FunctionTemplate, Deduced.data(), Deduced.size(), |
| 1232 | ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution); |
| 1233 | if (Inst) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1234 | return TDK_InstantiationDepth; |
| 1235 | |
Douglas Gregor | 51ffb0c | 2009-11-25 18:55:14 +0000 | [diff] [blame] | 1236 | // C++ [temp.deduct.type]p2: |
| 1237 | // [...] or if any template argument remains neither deduced nor |
| 1238 | // explicitly specified, template argument deduction fails. |
| 1239 | TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size()); |
| 1240 | for (unsigned I = 0, N = Deduced.size(); I != N; ++I) { |
| 1241 | if (!Deduced[I].isNull()) { |
| 1242 | Builder.Append(Deduced[I]); |
| 1243 | continue; |
| 1244 | } |
| 1245 | |
| 1246 | // Substitute into the default template argument, if available. |
| 1247 | NamedDecl *Param = FunctionTemplate->getTemplateParameters()->getParam(I); |
| 1248 | TemplateArgumentLoc DefArg |
| 1249 | = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate, |
| 1250 | FunctionTemplate->getLocation(), |
| 1251 | FunctionTemplate->getSourceRange().getEnd(), |
| 1252 | Param, |
| 1253 | Builder); |
| 1254 | |
| 1255 | // If there was no default argument, deduction is incomplete. |
| 1256 | if (DefArg.getArgument().isNull()) { |
| 1257 | Info.Param = makeTemplateParameter( |
| 1258 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
| 1259 | return TDK_Incomplete; |
| 1260 | } |
| 1261 | |
| 1262 | // Check whether we can actually use the default argument. |
| 1263 | if (CheckTemplateArgument(Param, DefArg, |
| 1264 | FunctionTemplate, |
| 1265 | FunctionTemplate->getLocation(), |
| 1266 | FunctionTemplate->getSourceRange().getEnd(), |
| 1267 | Builder)) { |
| 1268 | Info.Param = makeTemplateParameter( |
| 1269 | const_cast<NamedDecl *>(TemplateParams->getParam(I))); |
| 1270 | return TDK_SubstitutionFailure; |
| 1271 | } |
| 1272 | |
| 1273 | // If we get here, we successfully used the default template argument. |
| 1274 | } |
| 1275 | |
| 1276 | // Form the template argument list from the deduced template arguments. |
| 1277 | TemplateArgumentList *DeducedArgumentList |
| 1278 | = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); |
| 1279 | Info.reset(DeducedArgumentList); |
| 1280 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1281 | // Substitute the deduced template arguments into the function template |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1282 | // declaration to produce the function template specialization. |
| 1283 | Specialization = cast_or_null<FunctionDecl>( |
John McCall | ce3ff2b | 2009-08-25 22:02:44 +0000 | [diff] [blame] | 1284 | SubstDecl(FunctionTemplate->getTemplatedDecl(), |
| 1285 | FunctionTemplate->getDeclContext(), |
Douglas Gregor | 357bbd0 | 2009-08-28 20:50:45 +0000 | [diff] [blame] | 1286 | MultiLevelTemplateArgumentList(*DeducedArgumentList))); |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1287 | if (!Specialization) |
| 1288 | return TDK_SubstitutionFailure; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1289 | |
Douglas Gregor | f882574 | 2009-09-15 18:26:13 +0000 | [diff] [blame] | 1290 | assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == |
| 1291 | FunctionTemplate->getCanonicalDecl()); |
| 1292 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1293 | // If the template argument list is owned by the function template |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1294 | // specialization, release it. |
| 1295 | if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList) |
| 1296 | Info.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1297 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1298 | // There may have been an error that did not prevent us from constructing a |
| 1299 | // declaration. Mark the declaration invalid and return with a substitution |
| 1300 | // failure. |
| 1301 | if (Trap.hasErrorOccurred()) { |
| 1302 | Specialization->setInvalidDecl(true); |
| 1303 | return TDK_SubstitutionFailure; |
| 1304 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1305 | |
| 1306 | return TDK_Success; |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame^] | 1309 | static QualType GetTypeOfFunction(ASTContext &Context, |
| 1310 | bool isAddressOfOperand, |
| 1311 | FunctionDecl *Fn) { |
| 1312 | if (!isAddressOfOperand) return Fn->getType(); |
| 1313 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) |
| 1314 | if (Method->isInstance()) |
| 1315 | return Context.getMemberPointerType(Fn->getType(), |
| 1316 | Context.getTypeDeclType(Method->getParent()).getTypePtr()); |
| 1317 | return Context.getPointerType(Fn->getType()); |
| 1318 | } |
| 1319 | |
| 1320 | /// Apply the deduction rules for overload sets. |
| 1321 | /// |
| 1322 | /// \return the null type if this argument should be treated as an |
| 1323 | /// undeduced context |
| 1324 | static QualType |
| 1325 | ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams, |
| 1326 | Expr *Arg, QualType ParamType) { |
| 1327 | bool isAddressOfOperand = false; |
| 1328 | |
| 1329 | Arg = Arg->IgnoreParens(); |
| 1330 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(Arg)) { |
| 1331 | assert(UnOp->getOpcode() == UnaryOperator::AddrOf); |
| 1332 | isAddressOfOperand = true; |
| 1333 | Arg = UnOp->getSubExpr()->IgnoreParens(); |
| 1334 | } |
| 1335 | |
| 1336 | const UnresolvedSetImpl *Decls; |
| 1337 | bool HasExplicitArgs; |
| 1338 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(Arg)) { |
| 1339 | Decls = &ULE->getDecls(); |
| 1340 | HasExplicitArgs = ULE->hasExplicitTemplateArgs(); |
| 1341 | } else { |
| 1342 | UnresolvedMemberExpr *UME = cast<UnresolvedMemberExpr>(Arg); |
| 1343 | Decls = &UME->getDecls(); |
| 1344 | HasExplicitArgs = ULE->hasExplicitTemplateArgs(); |
| 1345 | } |
| 1346 | |
| 1347 | // If there were explicit template arguments, we can only find |
| 1348 | // something via C++ [temp.arg.explicit]p3, i.e. if the arguments |
| 1349 | // unambiguously name a full specialization. |
| 1350 | if (HasExplicitArgs) { |
| 1351 | // But we can still look for an explicit specialization. |
| 1352 | if (FunctionDecl *ExplicitSpec |
| 1353 | = S.ResolveSingleFunctionTemplateSpecialization(Arg)) |
| 1354 | return GetTypeOfFunction(S.Context, isAddressOfOperand, ExplicitSpec); |
| 1355 | return QualType(); |
| 1356 | } |
| 1357 | |
| 1358 | // C++0x [temp.deduct.call]p6: |
| 1359 | // When P is a function type, pointer to function type, or pointer |
| 1360 | // to member function type: |
| 1361 | |
| 1362 | if (!ParamType->isFunctionType() && |
| 1363 | !ParamType->isFunctionPointerType() && |
| 1364 | !ParamType->isMemberFunctionPointerType()) |
| 1365 | return QualType(); |
| 1366 | |
| 1367 | QualType Match; |
| 1368 | for (UnresolvedSetIterator I = Decls->begin(), |
| 1369 | E = Decls->end(); I != E; ++I) { |
| 1370 | NamedDecl *D = (*I)->getUnderlyingDecl(); |
| 1371 | |
| 1372 | // - If the argument is an overload set containing one or more |
| 1373 | // function templates, the parameter is treated as a |
| 1374 | // non-deduced context. |
| 1375 | if (isa<FunctionTemplateDecl>(D)) |
| 1376 | return QualType(); |
| 1377 | |
| 1378 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
| 1379 | QualType ArgType = GetTypeOfFunction(S.Context, isAddressOfOperand, Fn); |
| 1380 | |
| 1381 | // - If the argument is an overload set (not containing function |
| 1382 | // templates), trial argument deduction is attempted using each |
| 1383 | // of the members of the set. If deduction succeeds for only one |
| 1384 | // of the overload set members, that member is used as the |
| 1385 | // argument value for the deduction. If deduction succeeds for |
| 1386 | // more than one member of the overload set the parameter is |
| 1387 | // treated as a non-deduced context. |
| 1388 | |
| 1389 | // We do all of this in a fresh context per C++0x [temp.deduct.type]p2: |
| 1390 | // Type deduction is done independently for each P/A pair, and |
| 1391 | // the deduced template argument values are then combined. |
| 1392 | // So we do not reject deductions which were made elsewhere. |
| 1393 | llvm::SmallVector<TemplateArgument, 8> Deduced(TemplateParams->size()); |
| 1394 | Sema::TemplateDeductionInfo Info(S.Context); |
| 1395 | unsigned TDF = 0; |
| 1396 | |
| 1397 | Sema::TemplateDeductionResult Result |
| 1398 | = DeduceTemplateArguments(S.Context, TemplateParams, |
| 1399 | ParamType, ArgType, |
| 1400 | Info, Deduced, TDF); |
| 1401 | if (Result) continue; |
| 1402 | if (!Match.isNull()) return QualType(); |
| 1403 | Match = ArgType; |
| 1404 | } |
| 1405 | |
| 1406 | return Match; |
| 1407 | } |
| 1408 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1409 | /// \brief Perform template argument deduction from a function call |
| 1410 | /// (C++ [temp.deduct.call]). |
| 1411 | /// |
| 1412 | /// \param FunctionTemplate the function template for which we are performing |
| 1413 | /// template argument deduction. |
| 1414 | /// |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1415 | /// \param ExplicitTemplateArguments the explicit template arguments provided |
| 1416 | /// for this call. |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1417 | /// |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1418 | /// \param Args the function call arguments |
| 1419 | /// |
| 1420 | /// \param NumArgs the number of arguments in Args |
| 1421 | /// |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1422 | /// \param Name the name of the function being called. This is only significant |
| 1423 | /// when the function template is a conversion function template, in which |
| 1424 | /// case this routine will also perform template argument deduction based on |
| 1425 | /// the function to which |
| 1426 | /// |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1427 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | /// this will be set to the function template specialization produced by |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1429 | /// template argument deduction. |
| 1430 | /// |
| 1431 | /// \param Info the argument will be updated to provide additional information |
| 1432 | /// about template argument deduction. |
| 1433 | /// |
| 1434 | /// \returns the result of template argument deduction. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1435 | Sema::TemplateDeductionResult |
| 1436 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1437 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1438 | Expr **Args, unsigned NumArgs, |
| 1439 | FunctionDecl *&Specialization, |
| 1440 | TemplateDeductionInfo &Info) { |
| 1441 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1442 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1443 | // C++ [temp.deduct.call]p1: |
| 1444 | // Template argument deduction is done by comparing each function template |
| 1445 | // parameter type (call it P) with the type of the corresponding argument |
| 1446 | // of the call (call it A) as described below. |
| 1447 | unsigned CheckArgs = NumArgs; |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1448 | if (NumArgs < Function->getMinRequiredArguments()) |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1449 | return TDK_TooFewArguments; |
| 1450 | else if (NumArgs > Function->getNumParams()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1451 | const FunctionProtoType *Proto |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1452 | = Function->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1453 | if (!Proto->isVariadic()) |
| 1454 | return TDK_TooManyArguments; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1455 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1456 | CheckArgs = Function->getNumParams(); |
| 1457 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1459 | // The types of the parameters from which we will perform template argument |
| 1460 | // deduction. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1461 | TemplateParameterList *TemplateParams |
| 1462 | = FunctionTemplate->getTemplateParameters(); |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1463 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
| 1464 | llvm::SmallVector<QualType, 4> ParamTypes; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1465 | if (ExplicitTemplateArgs) { |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1466 | TemplateDeductionResult Result = |
| 1467 | SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1468 | *ExplicitTemplateArgs, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1469 | Deduced, |
| 1470 | ParamTypes, |
| 1471 | 0, |
| 1472 | Info); |
| 1473 | if (Result) |
| 1474 | return Result; |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1475 | } else { |
| 1476 | // Just fill in the parameter types from the function declaration. |
| 1477 | for (unsigned I = 0; I != CheckArgs; ++I) |
| 1478 | ParamTypes.push_back(Function->getParamDecl(I)->getType()); |
| 1479 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1480 | |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1481 | // Deduce template arguments from the function parameters. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1482 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1483 | for (unsigned I = 0; I != CheckArgs; ++I) { |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1484 | QualType ParamType = ParamTypes[I]; |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1485 | QualType ArgType = Args[I]->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1486 | |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame^] | 1487 | // Overload sets usually make this parameter an undeduced |
| 1488 | // context, but there are sometimes special circumstances. |
| 1489 | if (ArgType == Context.OverloadTy) { |
| 1490 | ArgType = ResolveOverloadForDeduction(*this, TemplateParams, |
| 1491 | Args[I], ParamType); |
| 1492 | if (ArgType.isNull()) |
| 1493 | continue; |
| 1494 | } |
| 1495 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1496 | // C++ [temp.deduct.call]p2: |
| 1497 | // If P is not a reference type: |
| 1498 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1499 | bool ParamWasReference = isa<ReferenceType>(CanonParamType); |
| 1500 | if (!ParamWasReference) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | // - If A is an array type, the pointer type produced by the |
| 1502 | // array-to-pointer standard conversion (4.2) is used in place of |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1503 | // A for type deduction; otherwise, |
| 1504 | if (ArgType->isArrayType()) |
| 1505 | ArgType = Context.getArrayDecayedType(ArgType); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1506 | // - If A is a function type, the pointer type produced by the |
| 1507 | // function-to-pointer standard conversion (4.3) is used in place |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1508 | // of A for type deduction; otherwise, |
| 1509 | else if (ArgType->isFunctionType()) |
| 1510 | ArgType = Context.getPointerType(ArgType); |
| 1511 | else { |
| 1512 | // - If A is a cv-qualified type, the top level cv-qualifiers of A’s |
| 1513 | // type are ignored for type deduction. |
| 1514 | QualType CanonArgType = Context.getCanonicalType(ArgType); |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1515 | if (CanonArgType.getLocalCVRQualifiers()) |
| 1516 | ArgType = CanonArgType.getLocalUnqualifiedType(); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1517 | } |
| 1518 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1519 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1520 | // C++0x [temp.deduct.call]p3: |
| 1521 | // 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] | 1522 | // are ignored for type deduction. |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 1523 | if (CanonParamType.getLocalCVRQualifiers()) |
| 1524 | ParamType = CanonParamType.getLocalUnqualifiedType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1525 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1526 | // [...] If P is a reference type, the type referred to by P is used |
| 1527 | // for type deduction. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1528 | ParamType = ParamRefType->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1529 | |
| 1530 | // [...] If P is of the form T&&, where T is a template parameter, and |
| 1531 | // 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] | 1532 | // type deduction. |
| 1533 | if (isa<RValueReferenceType>(ParamRefType) && |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1534 | ParamRefType->getAs<TemplateTypeParmType>() && |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1535 | Args[I]->isLvalue(Context) == Expr::LV_Valid) |
| 1536 | ArgType = Context.getLValueReferenceType(ArgType); |
| 1537 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1538 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1539 | // C++0x [temp.deduct.call]p4: |
| 1540 | // In general, the deduction process attempts to find template argument |
| 1541 | // values that will make the deduced A identical to A (after the type A |
| 1542 | // is transformed as described above). [...] |
Douglas Gregor | 1282029 | 2009-09-14 20:00:47 +0000 | [diff] [blame] | 1543 | unsigned TDF = TDF_SkipNonDependent; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1544 | |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1545 | // - If the original P is a reference type, the deduced A (i.e., the |
| 1546 | // type referred to by the reference) can be more cv-qualified than |
| 1547 | // the transformed A. |
| 1548 | if (ParamWasReference) |
| 1549 | TDF |= TDF_ParamWithReferenceType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1550 | // - The transformed A can be another pointer or pointer to member |
| 1551 | // type that can be converted to the deduced A via a qualification |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1552 | // conversion (4.4). |
| 1553 | if (ArgType->isPointerType() || ArgType->isMemberPointerType()) |
| 1554 | TDF |= TDF_IgnoreQualifiers; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | // - 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] | 1556 | // transformed A can be a derived class of the deduced A. Likewise, |
| 1557 | // if P is a pointer to a class of the form simple-template-id, the |
| 1558 | // transformed A can be a pointer to a derived class pointed to by |
| 1559 | // the deduced A. |
| 1560 | if (isSimpleTemplateIdType(ParamType) || |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1561 | (isa<PointerType>(ParamType) && |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1562 | isSimpleTemplateIdType( |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1563 | ParamType->getAs<PointerType>()->getPointeeType()))) |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1564 | TDF |= TDF_DerivedClass; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1565 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1566 | if (TemplateDeductionResult Result |
| 1567 | = ::DeduceTemplateArguments(Context, TemplateParams, |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1568 | ParamType, ArgType, Info, Deduced, |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1569 | TDF)) |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1570 | return Result; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1571 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1572 | // FIXME: we need to check that the deduced A is the same as A, |
| 1573 | // modulo the various allowed differences. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1574 | } |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1575 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1576 | return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1577 | Specialization, Info); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1580 | /// \brief Deduce template arguments when taking the address of a function |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 1581 | /// template (C++ [temp.deduct.funcaddr]) or matching a specialization to |
| 1582 | /// a template. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1583 | /// |
| 1584 | /// \param FunctionTemplate the function template for which we are performing |
| 1585 | /// template argument deduction. |
| 1586 | /// |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 1587 | /// \param ExplicitTemplateArguments the explicitly-specified template |
| 1588 | /// arguments. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1589 | /// |
| 1590 | /// \param ArgFunctionType the function type that will be used as the |
| 1591 | /// "argument" type (A) when performing template argument deduction from the |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 1592 | /// function template's function type. This type may be NULL, if there is no |
| 1593 | /// argument type to compare against, in C++0x [temp.arg.explicit]p3. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1594 | /// |
| 1595 | /// \param Specialization if template argument deduction was successful, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1596 | /// this will be set to the function template specialization produced by |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1597 | /// template argument deduction. |
| 1598 | /// |
| 1599 | /// \param Info the argument will be updated to provide additional information |
| 1600 | /// about template argument deduction. |
| 1601 | /// |
| 1602 | /// \returns the result of template argument deduction. |
| 1603 | Sema::TemplateDeductionResult |
| 1604 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1605 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1606 | QualType ArgFunctionType, |
| 1607 | FunctionDecl *&Specialization, |
| 1608 | TemplateDeductionInfo &Info) { |
| 1609 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 1610 | TemplateParameterList *TemplateParams |
| 1611 | = FunctionTemplate->getTemplateParameters(); |
| 1612 | QualType FunctionType = Function->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1613 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1614 | // Substitute any explicit template arguments. |
| 1615 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
| 1616 | llvm::SmallVector<QualType, 4> ParamTypes; |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1617 | if (ExplicitTemplateArgs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1618 | if (TemplateDeductionResult Result |
| 1619 | = SubstituteExplicitTemplateArguments(FunctionTemplate, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1620 | *ExplicitTemplateArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1621 | Deduced, ParamTypes, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1622 | &FunctionType, Info)) |
| 1623 | return Result; |
| 1624 | } |
| 1625 | |
| 1626 | // Template argument deduction for function templates in a SFINAE context. |
| 1627 | // Trap any errors that might occur. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1628 | SFINAETrap Trap(*this); |
| 1629 | |
John McCall | eff9213 | 2010-02-02 02:21:27 +0000 | [diff] [blame^] | 1630 | Deduced.resize(TemplateParams->size()); |
| 1631 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 1632 | if (!ArgFunctionType.isNull()) { |
| 1633 | // Deduce template arguments from the function type. |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 1634 | if (TemplateDeductionResult Result |
| 1635 | = ::DeduceTemplateArguments(Context, TemplateParams, |
| 1636 | FunctionType, ArgFunctionType, Info, |
| 1637 | Deduced, 0)) |
| 1638 | return Result; |
| 1639 | } |
| 1640 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1641 | return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1642 | Specialization, Info); |
| 1643 | } |
| 1644 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1645 | /// \brief Deduce template arguments for a templated conversion |
| 1646 | /// function (C++ [temp.deduct.conv]) and, if successful, produce a |
| 1647 | /// conversion function template specialization. |
| 1648 | Sema::TemplateDeductionResult |
| 1649 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
| 1650 | QualType ToType, |
| 1651 | CXXConversionDecl *&Specialization, |
| 1652 | TemplateDeductionInfo &Info) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1653 | CXXConversionDecl *Conv |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1654 | = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()); |
| 1655 | QualType FromType = Conv->getConversionType(); |
| 1656 | |
| 1657 | // Canonicalize the types for deduction. |
| 1658 | QualType P = Context.getCanonicalType(FromType); |
| 1659 | QualType A = Context.getCanonicalType(ToType); |
| 1660 | |
| 1661 | // C++0x [temp.deduct.conv]p3: |
| 1662 | // If P is a reference type, the type referred to by P is used for |
| 1663 | // type deduction. |
| 1664 | if (const ReferenceType *PRef = P->getAs<ReferenceType>()) |
| 1665 | P = PRef->getPointeeType(); |
| 1666 | |
| 1667 | // C++0x [temp.deduct.conv]p3: |
| 1668 | // If A is a reference type, the type referred to by A is used |
| 1669 | // for type deduction. |
| 1670 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
| 1671 | A = ARef->getPointeeType(); |
| 1672 | // C++ [temp.deduct.conv]p2: |
| 1673 | // |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1674 | // If A is not a reference type: |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1675 | else { |
| 1676 | assert(!A->isReferenceType() && "Reference types were handled above"); |
| 1677 | |
| 1678 | // - If P is an array type, the pointer type produced by the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1679 | // array-to-pointer standard conversion (4.2) is used in place |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1680 | // of P for type deduction; otherwise, |
| 1681 | if (P->isArrayType()) |
| 1682 | P = Context.getArrayDecayedType(P); |
| 1683 | // - If P is a function type, the pointer type produced by the |
| 1684 | // function-to-pointer standard conversion (4.3) is used in |
| 1685 | // place of P for type deduction; otherwise, |
| 1686 | else if (P->isFunctionType()) |
| 1687 | P = Context.getPointerType(P); |
| 1688 | // - If P is a cv-qualified type, the top level cv-qualifiers of |
| 1689 | // P’s type are ignored for type deduction. |
| 1690 | else |
| 1691 | P = P.getUnqualifiedType(); |
| 1692 | |
| 1693 | // C++0x [temp.deduct.conv]p3: |
| 1694 | // If A is a cv-qualified type, the top level cv-qualifiers of A’s |
| 1695 | // type are ignored for type deduction. |
| 1696 | A = A.getUnqualifiedType(); |
| 1697 | } |
| 1698 | |
| 1699 | // Template argument deduction for function templates in a SFINAE context. |
| 1700 | // Trap any errors that might occur. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1701 | SFINAETrap Trap(*this); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1702 | |
| 1703 | // C++ [temp.deduct.conv]p1: |
| 1704 | // Template argument deduction is done by comparing the return |
| 1705 | // type of the template conversion function (call it P) with the |
| 1706 | // type that is required as the result of the conversion (call it |
| 1707 | // A) as described in 14.8.2.4. |
| 1708 | TemplateParameterList *TemplateParams |
| 1709 | = FunctionTemplate->getTemplateParameters(); |
| 1710 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1711 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1712 | |
| 1713 | // C++0x [temp.deduct.conv]p4: |
| 1714 | // In general, the deduction process attempts to find template |
| 1715 | // argument values that will make the deduced A identical to |
| 1716 | // A. However, there are two cases that allow a difference: |
| 1717 | unsigned TDF = 0; |
| 1718 | // - If the original A is a reference type, A can be more |
| 1719 | // cv-qualified than the deduced A (i.e., the type referred to |
| 1720 | // by the reference) |
| 1721 | if (ToType->isReferenceType()) |
| 1722 | TDF |= TDF_ParamWithReferenceType; |
| 1723 | // - The deduced A can be another pointer or pointer to member |
| 1724 | // type that can be converted to A via a qualification |
| 1725 | // conversion. |
| 1726 | // |
| 1727 | // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when |
| 1728 | // both P and A are pointers or member pointers. In this case, we |
| 1729 | // just ignore cv-qualifiers completely). |
| 1730 | if ((P->isPointerType() && A->isPointerType()) || |
| 1731 | (P->isMemberPointerType() && P->isMemberPointerType())) |
| 1732 | TDF |= TDF_IgnoreQualifiers; |
| 1733 | if (TemplateDeductionResult Result |
| 1734 | = ::DeduceTemplateArguments(Context, TemplateParams, |
| 1735 | P, A, Info, Deduced, TDF)) |
| 1736 | return Result; |
| 1737 | |
| 1738 | // FIXME: we need to check that the deduced A is the same as A, |
| 1739 | // modulo the various allowed differences. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1740 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1741 | // Finish template argument deduction. |
| 1742 | FunctionDecl *Spec = 0; |
| 1743 | TemplateDeductionResult Result |
| 1744 | = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, Spec, Info); |
| 1745 | Specialization = cast_or_null<CXXConversionDecl>(Spec); |
| 1746 | return Result; |
| 1747 | } |
| 1748 | |
Douglas Gregor | 4b52e25 | 2009-12-21 23:17:24 +0000 | [diff] [blame] | 1749 | /// \brief Deduce template arguments for a function template when there is |
| 1750 | /// nothing to deduce against (C++0x [temp.arg.explicit]p3). |
| 1751 | /// |
| 1752 | /// \param FunctionTemplate the function template for which we are performing |
| 1753 | /// template argument deduction. |
| 1754 | /// |
| 1755 | /// \param ExplicitTemplateArguments the explicitly-specified template |
| 1756 | /// arguments. |
| 1757 | /// |
| 1758 | /// \param Specialization if template argument deduction was successful, |
| 1759 | /// this will be set to the function template specialization produced by |
| 1760 | /// template argument deduction. |
| 1761 | /// |
| 1762 | /// \param Info the argument will be updated to provide additional information |
| 1763 | /// about template argument deduction. |
| 1764 | /// |
| 1765 | /// \returns the result of template argument deduction. |
| 1766 | Sema::TemplateDeductionResult |
| 1767 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
| 1768 | const TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 1769 | FunctionDecl *&Specialization, |
| 1770 | TemplateDeductionInfo &Info) { |
| 1771 | return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, |
| 1772 | QualType(), Specialization, Info); |
| 1773 | } |
| 1774 | |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1775 | /// \brief Stores the result of comparing the qualifiers of two types. |
| 1776 | enum DeductionQualifierComparison { |
| 1777 | NeitherMoreQualified = 0, |
| 1778 | ParamMoreQualified, |
| 1779 | ArgMoreQualified |
| 1780 | }; |
| 1781 | |
| 1782 | /// \brief Deduce the template arguments during partial ordering by comparing |
| 1783 | /// the parameter type and the argument type (C++0x [temp.deduct.partial]). |
| 1784 | /// |
| 1785 | /// \param Context the AST context in which this deduction occurs. |
| 1786 | /// |
| 1787 | /// \param TemplateParams the template parameters that we are deducing |
| 1788 | /// |
| 1789 | /// \param ParamIn the parameter type |
| 1790 | /// |
| 1791 | /// \param ArgIn the argument type |
| 1792 | /// |
| 1793 | /// \param Info information about the template argument deduction itself |
| 1794 | /// |
| 1795 | /// \param Deduced the deduced template arguments |
| 1796 | /// |
| 1797 | /// \returns the result of template argument deduction so far. Note that a |
| 1798 | /// "success" result means that template argument deduction has not yet failed, |
| 1799 | /// but it may still fail, later, for other reasons. |
| 1800 | static Sema::TemplateDeductionResult |
| 1801 | DeduceTemplateArgumentsDuringPartialOrdering(ASTContext &Context, |
| 1802 | TemplateParameterList *TemplateParams, |
| 1803 | QualType ParamIn, QualType ArgIn, |
| 1804 | Sema::TemplateDeductionInfo &Info, |
| 1805 | llvm::SmallVectorImpl<TemplateArgument> &Deduced, |
| 1806 | llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) { |
| 1807 | CanQualType Param = Context.getCanonicalType(ParamIn); |
| 1808 | CanQualType Arg = Context.getCanonicalType(ArgIn); |
| 1809 | |
| 1810 | // C++0x [temp.deduct.partial]p5: |
| 1811 | // Before the partial ordering is done, certain transformations are |
| 1812 | // performed on the types used for partial ordering: |
| 1813 | // - If P is a reference type, P is replaced by the type referred to. |
| 1814 | CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>(); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 1815 | if (!ParamRef.isNull()) |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1816 | Param = ParamRef->getPointeeType(); |
| 1817 | |
| 1818 | // - If A is a reference type, A is replaced by the type referred to. |
| 1819 | CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>(); |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 1820 | if (!ArgRef.isNull()) |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1821 | Arg = ArgRef->getPointeeType(); |
| 1822 | |
John McCall | e27ec8a | 2009-10-23 23:03:21 +0000 | [diff] [blame] | 1823 | if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) { |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1824 | // C++0x [temp.deduct.partial]p6: |
| 1825 | // If both P and A were reference types (before being replaced with the |
| 1826 | // type referred to above), determine which of the two types (if any) is |
| 1827 | // more cv-qualified than the other; otherwise the types are considered to |
| 1828 | // be equally cv-qualified for partial ordering purposes. The result of this |
| 1829 | // determination will be used below. |
| 1830 | // |
| 1831 | // We save this information for later, using it only when deduction |
| 1832 | // succeeds in both directions. |
| 1833 | DeductionQualifierComparison QualifierResult = NeitherMoreQualified; |
| 1834 | if (Param.isMoreQualifiedThan(Arg)) |
| 1835 | QualifierResult = ParamMoreQualified; |
| 1836 | else if (Arg.isMoreQualifiedThan(Param)) |
| 1837 | QualifierResult = ArgMoreQualified; |
| 1838 | QualifierComparisons->push_back(QualifierResult); |
| 1839 | } |
| 1840 | |
| 1841 | // C++0x [temp.deduct.partial]p7: |
| 1842 | // Remove any top-level cv-qualifiers: |
| 1843 | // - If P is a cv-qualified type, P is replaced by the cv-unqualified |
| 1844 | // version of P. |
| 1845 | Param = Param.getUnqualifiedType(); |
| 1846 | // - If A is a cv-qualified type, A is replaced by the cv-unqualified |
| 1847 | // version of A. |
| 1848 | Arg = Arg.getUnqualifiedType(); |
| 1849 | |
| 1850 | // C++0x [temp.deduct.partial]p8: |
| 1851 | // Using the resulting types P and A the deduction is then done as |
| 1852 | // described in 14.9.2.5. If deduction succeeds for a given type, the type |
| 1853 | // from the argument template is considered to be at least as specialized |
| 1854 | // as the type from the parameter template. |
| 1855 | return DeduceTemplateArguments(Context, TemplateParams, Param, Arg, Info, |
| 1856 | Deduced, TDF_None); |
| 1857 | } |
| 1858 | |
| 1859 | static void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1860 | MarkUsedTemplateParameters(Sema &SemaRef, QualType T, |
| 1861 | bool OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1862 | unsigned Level, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1863 | llvm::SmallVectorImpl<bool> &Deduced); |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1864 | |
| 1865 | /// \brief Determine whether the function template \p FT1 is at least as |
| 1866 | /// specialized as \p FT2. |
| 1867 | static bool isAtLeastAsSpecializedAs(Sema &S, |
| 1868 | FunctionTemplateDecl *FT1, |
| 1869 | FunctionTemplateDecl *FT2, |
| 1870 | TemplatePartialOrderingContext TPOC, |
| 1871 | llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) { |
| 1872 | FunctionDecl *FD1 = FT1->getTemplatedDecl(); |
| 1873 | FunctionDecl *FD2 = FT2->getTemplatedDecl(); |
| 1874 | const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); |
| 1875 | const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); |
| 1876 | |
| 1877 | assert(Proto1 && Proto2 && "Function templates must have prototypes"); |
| 1878 | TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); |
| 1879 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
| 1880 | Deduced.resize(TemplateParams->size()); |
| 1881 | |
| 1882 | // C++0x [temp.deduct.partial]p3: |
| 1883 | // The types used to determine the ordering depend on the context in which |
| 1884 | // the partial ordering is done: |
| 1885 | Sema::TemplateDeductionInfo Info(S.Context); |
| 1886 | switch (TPOC) { |
| 1887 | case TPOC_Call: { |
| 1888 | // - In the context of a function call, the function parameter types are |
| 1889 | // used. |
| 1890 | unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs()); |
| 1891 | for (unsigned I = 0; I != NumParams; ++I) |
| 1892 | if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context, |
| 1893 | TemplateParams, |
| 1894 | Proto2->getArgType(I), |
| 1895 | Proto1->getArgType(I), |
| 1896 | Info, |
| 1897 | Deduced, |
| 1898 | QualifierComparisons)) |
| 1899 | return false; |
| 1900 | |
| 1901 | break; |
| 1902 | } |
| 1903 | |
| 1904 | case TPOC_Conversion: |
| 1905 | // - In the context of a call to a conversion operator, the return types |
| 1906 | // of the conversion function templates are used. |
| 1907 | if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context, |
| 1908 | TemplateParams, |
| 1909 | Proto2->getResultType(), |
| 1910 | Proto1->getResultType(), |
| 1911 | Info, |
| 1912 | Deduced, |
| 1913 | QualifierComparisons)) |
| 1914 | return false; |
| 1915 | break; |
| 1916 | |
| 1917 | case TPOC_Other: |
| 1918 | // - In other contexts (14.6.6.2) the function template’s function type |
| 1919 | // is used. |
| 1920 | if (DeduceTemplateArgumentsDuringPartialOrdering(S.Context, |
| 1921 | TemplateParams, |
| 1922 | FD2->getType(), |
| 1923 | FD1->getType(), |
| 1924 | Info, |
| 1925 | Deduced, |
| 1926 | QualifierComparisons)) |
| 1927 | return false; |
| 1928 | break; |
| 1929 | } |
| 1930 | |
| 1931 | // C++0x [temp.deduct.partial]p11: |
| 1932 | // In most cases, all template parameters must have values in order for |
| 1933 | // deduction to succeed, but for partial ordering purposes a template |
| 1934 | // parameter may remain without a value provided it is not used in the |
| 1935 | // types being used for partial ordering. [ Note: a template parameter used |
| 1936 | // in a non-deduced context is considered used. -end note] |
| 1937 | unsigned ArgIdx = 0, NumArgs = Deduced.size(); |
| 1938 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 1939 | if (Deduced[ArgIdx].isNull()) |
| 1940 | break; |
| 1941 | |
| 1942 | if (ArgIdx == NumArgs) { |
| 1943 | // All template arguments were deduced. FT1 is at least as specialized |
| 1944 | // as FT2. |
| 1945 | return true; |
| 1946 | } |
| 1947 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1948 | // Figure out which template parameters were used. |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1949 | llvm::SmallVector<bool, 4> UsedParameters; |
| 1950 | UsedParameters.resize(TemplateParams->size()); |
| 1951 | switch (TPOC) { |
| 1952 | case TPOC_Call: { |
| 1953 | unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs()); |
| 1954 | for (unsigned I = 0; I != NumParams; ++I) |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1955 | ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false, |
| 1956 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1957 | UsedParameters); |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1958 | break; |
| 1959 | } |
| 1960 | |
| 1961 | case TPOC_Conversion: |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1962 | ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false, |
| 1963 | TemplateParams->getDepth(), |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 1964 | UsedParameters); |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1965 | break; |
| 1966 | |
| 1967 | case TPOC_Other: |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 1968 | ::MarkUsedTemplateParameters(S, FD2->getType(), false, |
| 1969 | TemplateParams->getDepth(), |
| 1970 | UsedParameters); |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1971 | break; |
| 1972 | } |
| 1973 | |
| 1974 | for (; ArgIdx != NumArgs; ++ArgIdx) |
| 1975 | // If this argument had no value deduced but was used in one of the types |
| 1976 | // used for partial ordering, then deduction fails. |
| 1977 | if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) |
| 1978 | return false; |
| 1979 | |
| 1980 | return true; |
| 1981 | } |
| 1982 | |
| 1983 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1984 | /// \brief Returns the more specialized function template according |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1985 | /// to the rules of function template partial ordering (C++ [temp.func.order]). |
| 1986 | /// |
| 1987 | /// \param FT1 the first function template |
| 1988 | /// |
| 1989 | /// \param FT2 the second function template |
| 1990 | /// |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1991 | /// \param TPOC the context in which we are performing partial ordering of |
| 1992 | /// function templates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1993 | /// |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 1994 | /// \returns the more specialized function template. If neither |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 1995 | /// template is more specialized, returns NULL. |
| 1996 | FunctionTemplateDecl * |
| 1997 | Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, |
| 1998 | FunctionTemplateDecl *FT2, |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 1999 | TemplatePartialOrderingContext TPOC) { |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 2000 | llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons; |
| 2001 | bool Better1 = isAtLeastAsSpecializedAs(*this, FT1, FT2, TPOC, 0); |
| 2002 | bool Better2 = isAtLeastAsSpecializedAs(*this, FT2, FT1, TPOC, |
| 2003 | &QualifierComparisons); |
| 2004 | |
| 2005 | if (Better1 != Better2) // We have a clear winner |
| 2006 | return Better1? FT1 : FT2; |
| 2007 | |
| 2008 | if (!Better1 && !Better2) // Neither is better than the other |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2009 | return 0; |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 2010 | |
| 2011 | |
| 2012 | // C++0x [temp.deduct.partial]p10: |
| 2013 | // If for each type being considered a given template is at least as |
| 2014 | // specialized for all types and more specialized for some set of types and |
| 2015 | // the other template is not more specialized for any types or is not at |
| 2016 | // least as specialized for any types, then the given template is more |
| 2017 | // specialized than the other template. Otherwise, neither template is more |
| 2018 | // specialized than the other. |
| 2019 | Better1 = false; |
| 2020 | Better2 = false; |
| 2021 | for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) { |
| 2022 | // C++0x [temp.deduct.partial]p9: |
| 2023 | // If, for a given type, deduction succeeds in both directions (i.e., the |
| 2024 | // types are identical after the transformations above) and if the type |
| 2025 | // from the argument template is more cv-qualified than the type from the |
| 2026 | // parameter template (as described above) that type is considered to be |
| 2027 | // more specialized than the other. If neither type is more cv-qualified |
| 2028 | // than the other then neither type is more specialized than the other. |
| 2029 | switch (QualifierComparisons[I]) { |
| 2030 | case NeitherMoreQualified: |
| 2031 | break; |
| 2032 | |
| 2033 | case ParamMoreQualified: |
| 2034 | Better1 = true; |
| 2035 | if (Better2) |
| 2036 | return 0; |
| 2037 | break; |
| 2038 | |
| 2039 | case ArgMoreQualified: |
| 2040 | Better2 = true; |
| 2041 | if (Better1) |
| 2042 | return 0; |
| 2043 | break; |
| 2044 | } |
| 2045 | } |
| 2046 | |
| 2047 | assert(!(Better1 && Better2) && "Should have broken out in the loop above"); |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2048 | if (Better1) |
| 2049 | return FT1; |
Douglas Gregor | 8a51491 | 2009-09-14 18:39:43 +0000 | [diff] [blame] | 2050 | else if (Better2) |
| 2051 | return FT2; |
| 2052 | else |
| 2053 | return 0; |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame] | 2054 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 2055 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2056 | /// \brief Determine if the two templates are equivalent. |
| 2057 | static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) { |
| 2058 | if (T1 == T2) |
| 2059 | return true; |
| 2060 | |
| 2061 | if (!T1 || !T2) |
| 2062 | return false; |
| 2063 | |
| 2064 | return T1->getCanonicalDecl() == T2->getCanonicalDecl(); |
| 2065 | } |
| 2066 | |
| 2067 | /// \brief Retrieve the most specialized of the given function template |
| 2068 | /// specializations. |
| 2069 | /// |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2070 | /// \param SpecBegin the start iterator of the function template |
| 2071 | /// specializations that we will be comparing. |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2072 | /// |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2073 | /// \param SpecEnd the end iterator of the function template |
| 2074 | /// specializations, paired with \p SpecBegin. |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2075 | /// |
| 2076 | /// \param TPOC the partial ordering context to use to compare the function |
| 2077 | /// template specializations. |
| 2078 | /// |
| 2079 | /// \param Loc the location where the ambiguity or no-specializations |
| 2080 | /// diagnostic should occur. |
| 2081 | /// |
| 2082 | /// \param NoneDiag partial diagnostic used to diagnose cases where there are |
| 2083 | /// no matching candidates. |
| 2084 | /// |
| 2085 | /// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one |
| 2086 | /// occurs. |
| 2087 | /// |
| 2088 | /// \param CandidateDiag partial diagnostic used for each function template |
| 2089 | /// specialization that is a candidate in the ambiguous ordering. One parameter |
| 2090 | /// in this diagnostic should be unbound, which will correspond to the string |
| 2091 | /// describing the template arguments for the function template specialization. |
| 2092 | /// |
| 2093 | /// \param Index if non-NULL and the result of this function is non-nULL, |
| 2094 | /// receives the index corresponding to the resulting function template |
| 2095 | /// specialization. |
| 2096 | /// |
| 2097 | /// \returns the most specialized function template specialization, if |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2098 | /// found. Otherwise, returns SpecEnd. |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2099 | /// |
| 2100 | /// \todo FIXME: Consider passing in the "also-ran" candidates that failed |
| 2101 | /// template argument deduction. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2102 | UnresolvedSetIterator |
| 2103 | Sema::getMostSpecialized(UnresolvedSetIterator SpecBegin, |
| 2104 | UnresolvedSetIterator SpecEnd, |
| 2105 | TemplatePartialOrderingContext TPOC, |
| 2106 | SourceLocation Loc, |
| 2107 | const PartialDiagnostic &NoneDiag, |
| 2108 | const PartialDiagnostic &AmbigDiag, |
| 2109 | const PartialDiagnostic &CandidateDiag) { |
| 2110 | if (SpecBegin == SpecEnd) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2111 | Diag(Loc, NoneDiag); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2112 | return SpecEnd; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2113 | } |
| 2114 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2115 | if (SpecBegin + 1 == SpecEnd) |
| 2116 | return SpecBegin; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2117 | |
| 2118 | // Find the function template that is better than all of the templates it |
| 2119 | // has been compared to. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2120 | UnresolvedSetIterator Best = SpecBegin; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2121 | FunctionTemplateDecl *BestTemplate |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2122 | = cast<FunctionDecl>(*Best)->getPrimaryTemplate(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2123 | assert(BestTemplate && "Not a function template specialization?"); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2124 | for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) { |
| 2125 | FunctionTemplateDecl *Challenger |
| 2126 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2127 | assert(Challenger && "Not a function template specialization?"); |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2128 | if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2129 | TPOC), |
| 2130 | Challenger)) { |
| 2131 | Best = I; |
| 2132 | BestTemplate = Challenger; |
| 2133 | } |
| 2134 | } |
| 2135 | |
| 2136 | // Make sure that the "best" function template is more specialized than all |
| 2137 | // of the others. |
| 2138 | bool Ambiguous = false; |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2139 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { |
| 2140 | FunctionTemplateDecl *Challenger |
| 2141 | = cast<FunctionDecl>(*I)->getPrimaryTemplate(); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2142 | if (I != Best && |
| 2143 | !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, |
| 2144 | TPOC), |
| 2145 | BestTemplate)) { |
| 2146 | Ambiguous = true; |
| 2147 | break; |
| 2148 | } |
| 2149 | } |
| 2150 | |
| 2151 | if (!Ambiguous) { |
| 2152 | // We found an answer. Return it. |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2153 | return Best; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
| 2156 | // Diagnose the ambiguity. |
| 2157 | Diag(Loc, AmbigDiag); |
| 2158 | |
| 2159 | // FIXME: Can we order the candidates in some sane way? |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2160 | for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) |
| 2161 | Diag((*I)->getLocation(), CandidateDiag) |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2162 | << getTemplateArgumentBindingsText( |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2163 | cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(), |
| 2164 | *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs()); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2165 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2166 | return SpecEnd; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 2169 | /// \brief Returns the more specialized class template partial specialization |
| 2170 | /// according to the rules of partial ordering of class template partial |
| 2171 | /// specializations (C++ [temp.class.order]). |
| 2172 | /// |
| 2173 | /// \param PS1 the first class template partial specialization |
| 2174 | /// |
| 2175 | /// \param PS2 the second class template partial specialization |
| 2176 | /// |
| 2177 | /// \returns the more specialized class template partial specialization. If |
| 2178 | /// neither partial specialization is more specialized, returns NULL. |
| 2179 | ClassTemplatePartialSpecializationDecl * |
| 2180 | Sema::getMoreSpecializedPartialSpecialization( |
| 2181 | ClassTemplatePartialSpecializationDecl *PS1, |
| 2182 | ClassTemplatePartialSpecializationDecl *PS2) { |
| 2183 | // C++ [temp.class.order]p1: |
| 2184 | // For two class template partial specializations, the first is at least as |
| 2185 | // specialized as the second if, given the following rewrite to two |
| 2186 | // function templates, the first function template is at least as |
| 2187 | // specialized as the second according to the ordering rules for function |
| 2188 | // templates (14.6.6.2): |
| 2189 | // - the first function template has the same template parameters as the |
| 2190 | // first partial specialization and has a single function parameter |
| 2191 | // whose type is a class template specialization with the template |
| 2192 | // arguments of the first partial specialization, and |
| 2193 | // - the second function template has the same template parameters as the |
| 2194 | // second partial specialization and has a single function parameter |
| 2195 | // whose type is a class template specialization with the template |
| 2196 | // arguments of the second partial specialization. |
| 2197 | // |
| 2198 | // Rather than synthesize function templates, we merely perform the |
| 2199 | // equivalent partial ordering by performing deduction directly on the |
| 2200 | // template arguments of the class template partial specializations. This |
| 2201 | // computation is slightly simpler than the general problem of function |
| 2202 | // template partial ordering, because class template partial specializations |
| 2203 | // are more constrained. We know that every template parameter is deduc |
| 2204 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
| 2205 | Sema::TemplateDeductionInfo Info(Context); |
| 2206 | |
| 2207 | // Determine whether PS1 is at least as specialized as PS2 |
| 2208 | Deduced.resize(PS2->getTemplateParameters()->size()); |
| 2209 | bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(Context, |
| 2210 | PS2->getTemplateParameters(), |
| 2211 | Context.getTypeDeclType(PS2), |
| 2212 | Context.getTypeDeclType(PS1), |
| 2213 | Info, |
| 2214 | Deduced, |
| 2215 | 0); |
| 2216 | |
| 2217 | // Determine whether PS2 is at least as specialized as PS1 |
Douglas Gregor | db0d4b7 | 2009-11-11 23:06:43 +0000 | [diff] [blame] | 2218 | Deduced.clear(); |
Douglas Gregor | bf4ea56 | 2009-09-15 16:23:51 +0000 | [diff] [blame] | 2219 | Deduced.resize(PS1->getTemplateParameters()->size()); |
| 2220 | bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(Context, |
| 2221 | PS1->getTemplateParameters(), |
| 2222 | Context.getTypeDeclType(PS1), |
| 2223 | Context.getTypeDeclType(PS2), |
| 2224 | Info, |
| 2225 | Deduced, |
| 2226 | 0); |
| 2227 | |
| 2228 | if (Better1 == Better2) |
| 2229 | return 0; |
| 2230 | |
| 2231 | return Better1? PS1 : PS2; |
| 2232 | } |
| 2233 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2234 | static void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2235 | MarkUsedTemplateParameters(Sema &SemaRef, |
| 2236 | const TemplateArgument &TemplateArg, |
| 2237 | bool OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2238 | unsigned Depth, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2239 | llvm::SmallVectorImpl<bool> &Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2240 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2241 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2242 | /// expression. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2243 | static void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2244 | MarkUsedTemplateParameters(Sema &SemaRef, |
| 2245 | const Expr *E, |
| 2246 | bool OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2247 | unsigned Depth, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2248 | llvm::SmallVectorImpl<bool> &Used) { |
| 2249 | // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to |
| 2250 | // find other occurrences of template parameters. |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 2251 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
Douglas Gregor | c781f9c | 2010-01-14 18:13:22 +0000 | [diff] [blame] | 2252 | if (!DRE) |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2253 | return; |
| 2254 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2255 | const NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2256 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 2257 | if (!NTTP) |
| 2258 | return; |
| 2259 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2260 | if (NTTP->getDepth() == Depth) |
| 2261 | Used[NTTP->getIndex()] = true; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2264 | /// \brief Mark the template parameters that are used by the given |
| 2265 | /// nested name specifier. |
| 2266 | static void |
| 2267 | MarkUsedTemplateParameters(Sema &SemaRef, |
| 2268 | NestedNameSpecifier *NNS, |
| 2269 | bool OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2270 | unsigned Depth, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2271 | llvm::SmallVectorImpl<bool> &Used) { |
| 2272 | if (!NNS) |
| 2273 | return; |
| 2274 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2275 | MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth, |
| 2276 | Used); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2277 | MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2278 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
| 2281 | /// \brief Mark the template parameters that are used by the given |
| 2282 | /// template name. |
| 2283 | static void |
| 2284 | MarkUsedTemplateParameters(Sema &SemaRef, |
| 2285 | TemplateName Name, |
| 2286 | bool OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2287 | unsigned Depth, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2288 | llvm::SmallVectorImpl<bool> &Used) { |
| 2289 | if (TemplateDecl *Template = Name.getAsTemplateDecl()) { |
| 2290 | if (TemplateTemplateParmDecl *TTP |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2291 | = dyn_cast<TemplateTemplateParmDecl>(Template)) { |
| 2292 | if (TTP->getDepth() == Depth) |
| 2293 | Used[TTP->getIndex()] = true; |
| 2294 | } |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2295 | return; |
| 2296 | } |
| 2297 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2298 | if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) |
| 2299 | MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced, |
| 2300 | Depth, Used); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2301 | if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2302 | MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced, |
| 2303 | Depth, Used); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2304 | } |
| 2305 | |
| 2306 | /// \brief Mark the template parameters that are used by the given |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2307 | /// type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2308 | static void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2309 | MarkUsedTemplateParameters(Sema &SemaRef, QualType T, |
| 2310 | bool OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2311 | unsigned Depth, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2312 | llvm::SmallVectorImpl<bool> &Used) { |
| 2313 | if (T.isNull()) |
| 2314 | return; |
| 2315 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2316 | // Non-dependent types have nothing deducible |
| 2317 | if (!T->isDependentType()) |
| 2318 | return; |
| 2319 | |
| 2320 | T = SemaRef.Context.getCanonicalType(T); |
| 2321 | switch (T->getTypeClass()) { |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2322 | case Type::Pointer: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2323 | MarkUsedTemplateParameters(SemaRef, |
| 2324 | cast<PointerType>(T)->getPointeeType(), |
| 2325 | OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2326 | Depth, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2327 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2328 | break; |
| 2329 | |
| 2330 | case Type::BlockPointer: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2331 | MarkUsedTemplateParameters(SemaRef, |
| 2332 | cast<BlockPointerType>(T)->getPointeeType(), |
| 2333 | OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2334 | Depth, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2335 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2336 | break; |
| 2337 | |
| 2338 | case Type::LValueReference: |
| 2339 | case Type::RValueReference: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2340 | MarkUsedTemplateParameters(SemaRef, |
| 2341 | cast<ReferenceType>(T)->getPointeeType(), |
| 2342 | OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2343 | Depth, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2344 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2345 | break; |
| 2346 | |
| 2347 | case Type::MemberPointer: { |
| 2348 | const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2349 | MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2350 | Depth, Used); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2351 | MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2352 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2353 | break; |
| 2354 | } |
| 2355 | |
| 2356 | case Type::DependentSizedArray: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2357 | MarkUsedTemplateParameters(SemaRef, |
| 2358 | cast<DependentSizedArrayType>(T)->getSizeExpr(), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2359 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2360 | // Fall through to check the element type |
| 2361 | |
| 2362 | case Type::ConstantArray: |
| 2363 | case Type::IncompleteArray: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2364 | MarkUsedTemplateParameters(SemaRef, |
| 2365 | cast<ArrayType>(T)->getElementType(), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2366 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2367 | break; |
| 2368 | |
| 2369 | case Type::Vector: |
| 2370 | case Type::ExtVector: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2371 | MarkUsedTemplateParameters(SemaRef, |
| 2372 | cast<VectorType>(T)->getElementType(), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2373 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2374 | break; |
| 2375 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2376 | case Type::DependentSizedExtVector: { |
| 2377 | const DependentSizedExtVectorType *VecType |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 2378 | = cast<DependentSizedExtVectorType>(T); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2379 | MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2380 | Depth, Used); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2381 | MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2382 | Depth, Used); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2383 | break; |
| 2384 | } |
| 2385 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2386 | case Type::FunctionProto: { |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 2387 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2388 | MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2389 | Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2390 | for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I) |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2391 | MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2392 | Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2393 | break; |
| 2394 | } |
| 2395 | |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2396 | case Type::TemplateTypeParm: { |
| 2397 | const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T); |
| 2398 | if (TTP->getDepth() == Depth) |
| 2399 | Used[TTP->getIndex()] = true; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2400 | break; |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2401 | } |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2402 | |
| 2403 | case Type::TemplateSpecialization: { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2404 | const TemplateSpecializationType *Spec |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 2405 | = cast<TemplateSpecializationType>(T); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2406 | MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2407 | Depth, Used); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2408 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2409 | MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth, |
| 2410 | Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2411 | break; |
| 2412 | } |
| 2413 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2414 | case Type::Complex: |
| 2415 | if (!OnlyDeduced) |
| 2416 | MarkUsedTemplateParameters(SemaRef, |
| 2417 | cast<ComplexType>(T)->getElementType(), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2418 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2419 | break; |
| 2420 | |
| 2421 | case Type::Typename: |
| 2422 | if (!OnlyDeduced) |
| 2423 | MarkUsedTemplateParameters(SemaRef, |
| 2424 | cast<TypenameType>(T)->getQualifier(), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2425 | OnlyDeduced, Depth, Used); |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2426 | break; |
| 2427 | |
| 2428 | // None of these types have any template parameters in them. |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2429 | case Type::Builtin: |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2430 | case Type::VariableArray: |
| 2431 | case Type::FunctionNoProto: |
| 2432 | case Type::Record: |
| 2433 | case Type::Enum: |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2434 | case Type::ObjCInterface: |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 2435 | case Type::ObjCObjectPointer: |
John McCall | ed97649 | 2009-12-04 22:46:56 +0000 | [diff] [blame] | 2436 | case Type::UnresolvedUsing: |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2437 | #define TYPE(Class, Base) |
| 2438 | #define ABSTRACT_TYPE(Class, Base) |
| 2439 | #define DEPENDENT_TYPE(Class, Base) |
| 2440 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 2441 | #include "clang/AST/TypeNodes.def" |
| 2442 | break; |
| 2443 | } |
| 2444 | } |
| 2445 | |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2446 | /// \brief Mark the template parameters that are used by this |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2447 | /// template argument. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2448 | static void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2449 | MarkUsedTemplateParameters(Sema &SemaRef, |
| 2450 | const TemplateArgument &TemplateArg, |
| 2451 | bool OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2452 | unsigned Depth, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2453 | llvm::SmallVectorImpl<bool> &Used) { |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2454 | switch (TemplateArg.getKind()) { |
| 2455 | case TemplateArgument::Null: |
| 2456 | case TemplateArgument::Integral: |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2457 | case TemplateArgument::Declaration: |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2458 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2459 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2460 | case TemplateArgument::Type: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2461 | MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2462 | Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2463 | break; |
| 2464 | |
Douglas Gregor | 788cd06 | 2009-11-11 01:00:40 +0000 | [diff] [blame] | 2465 | case TemplateArgument::Template: |
| 2466 | MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsTemplate(), |
| 2467 | OnlyDeduced, Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2468 | break; |
| 2469 | |
| 2470 | case TemplateArgument::Expression: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2471 | MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2472 | Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2473 | break; |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2474 | |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 2475 | case TemplateArgument::Pack: |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2476 | for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(), |
| 2477 | PEnd = TemplateArg.pack_end(); |
| 2478 | P != PEnd; ++P) |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2479 | MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used); |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 2480 | break; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2481 | } |
| 2482 | } |
| 2483 | |
| 2484 | /// \brief Mark the template parameters can be deduced by the given |
| 2485 | /// template argument list. |
| 2486 | /// |
| 2487 | /// \param TemplateArgs the template argument list from which template |
| 2488 | /// parameters will be deduced. |
| 2489 | /// |
| 2490 | /// \param Deduced a bit vector whose elements will be set to \c true |
| 2491 | /// to indicate when the corresponding template parameter will be |
| 2492 | /// deduced. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2493 | void |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2494 | Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2495 | bool OnlyDeduced, unsigned Depth, |
Douglas Gregor | e73bb60 | 2009-09-14 21:25:05 +0000 | [diff] [blame] | 2496 | llvm::SmallVectorImpl<bool> &Used) { |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2497 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2498 | ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced, |
| 2499 | Depth, Used); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 2500 | } |
Douglas Gregor | 63f07c5 | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 2501 | |
| 2502 | /// \brief Marks all of the template parameters that will be deduced by a |
| 2503 | /// call to the given function template. |
| 2504 | void Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate, |
| 2505 | llvm::SmallVectorImpl<bool> &Deduced) { |
| 2506 | TemplateParameterList *TemplateParams |
| 2507 | = FunctionTemplate->getTemplateParameters(); |
| 2508 | Deduced.clear(); |
| 2509 | Deduced.resize(TemplateParams->size()); |
| 2510 | |
| 2511 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 2512 | for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) |
| 2513 | ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(), |
Douglas Gregor | ed9c0f9 | 2009-10-29 00:04:11 +0000 | [diff] [blame] | 2514 | true, TemplateParams->getDepth(), Deduced); |
Douglas Gregor | 63f07c5 | 2009-09-18 23:21:38 +0000 | [diff] [blame] | 2515 | } |