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