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