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 | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 21 | |
| 22 | namespace clang { |
| 23 | /// \brief Various flags that control template argument deduction. |
| 24 | /// |
| 25 | /// These flags can be bitwise-OR'd together. |
| 26 | enum TemplateDeductionFlags { |
| 27 | /// \brief No template argument deduction flags, which indicates the |
| 28 | /// strictest results for template argument deduction (as used for, e.g., |
| 29 | /// matching class template partial specializations). |
| 30 | TDF_None = 0, |
| 31 | /// \brief Within template argument deduction from a function call, we are |
| 32 | /// matching with a parameter type for which the original parameter was |
| 33 | /// a reference. |
| 34 | TDF_ParamWithReferenceType = 0x1, |
| 35 | /// \brief Within template argument deduction from a function call, we |
| 36 | /// are matching in a case where we ignore cv-qualifiers. |
| 37 | TDF_IgnoreQualifiers = 0x02, |
| 38 | /// \brief Within template argument deduction from a function call, |
| 39 | /// we are matching in a case where we can perform template argument |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 40 | /// deduction from a template-id of a derived class of the argument type. |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 41 | TDF_DerivedClass = 0x04 |
| 42 | }; |
| 43 | } |
| 44 | |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 45 | using namespace clang; |
| 46 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 47 | static Sema::TemplateDeductionResult |
| 48 | DeduceTemplateArguments(ASTContext &Context, |
| 49 | TemplateParameterList *TemplateParams, |
| 50 | const TemplateArgument &Param, |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 51 | const TemplateArgument &Arg, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 52 | Sema::TemplateDeductionInfo &Info, |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 53 | llvm::SmallVectorImpl<TemplateArgument> &Deduced); |
| 54 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 55 | /// \brief If the given expression is of a form that permits the deduction |
| 56 | /// of a non-type template parameter, return the declaration of that |
| 57 | /// non-type template parameter. |
| 58 | static NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) { |
| 59 | if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E)) |
| 60 | E = IC->getSubExpr(); |
| 61 | |
| 62 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 63 | return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | /// \brief Deduce the value of the given non-type template parameter |
| 69 | /// from the given constant. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 70 | static Sema::TemplateDeductionResult |
| 71 | DeduceNonTypeTemplateArgument(ASTContext &Context, |
| 72 | NonTypeTemplateParmDecl *NTTP, |
Anders Carlsson | 335e24a | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 73 | llvm::APSInt Value, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 74 | Sema::TemplateDeductionInfo &Info, |
| 75 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 76 | assert(NTTP->getDepth() == 0 && |
| 77 | "Cannot deduce non-type template argument with depth > 0"); |
| 78 | |
| 79 | if (Deduced[NTTP->getIndex()].isNull()) { |
Anders Carlsson | 25af1ed | 2009-06-16 23:08:29 +0000 | [diff] [blame] | 80 | QualType T = NTTP->getType(); |
| 81 | |
| 82 | // FIXME: Make sure we didn't overflow our data type! |
| 83 | unsigned AllowedBits = Context.getTypeSize(T); |
| 84 | if (Value.getBitWidth() != AllowedBits) |
| 85 | Value.extOrTrunc(AllowedBits); |
| 86 | Value.setIsSigned(T->isSignedIntegerType()); |
| 87 | |
| 88 | Deduced[NTTP->getIndex()] = TemplateArgument(SourceLocation(), Value, T); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 89 | return Sema::TDK_Success; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 92 | assert(Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral); |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 93 | |
| 94 | // If the template argument was previously deduced to a negative value, |
| 95 | // then our deduction fails. |
| 96 | const llvm::APSInt *PrevValuePtr = Deduced[NTTP->getIndex()].getAsIntegral(); |
Anders Carlsson | 335e24a | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 97 | if (PrevValuePtr->isNegative()) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 98 | Info.Param = NTTP; |
| 99 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
Anders Carlsson | 335e24a | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 100 | Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType()); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 101 | return Sema::TDK_Inconsistent; |
| 102 | } |
| 103 | |
Anders Carlsson | 335e24a | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 104 | llvm::APSInt PrevValue = *PrevValuePtr; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 105 | if (Value.getBitWidth() > PrevValue.getBitWidth()) |
| 106 | PrevValue.zext(Value.getBitWidth()); |
| 107 | else if (Value.getBitWidth() < PrevValue.getBitWidth()) |
| 108 | Value.zext(PrevValue.getBitWidth()); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 109 | |
| 110 | if (Value != PrevValue) { |
| 111 | Info.Param = NTTP; |
| 112 | Info.FirstArg = Deduced[NTTP->getIndex()]; |
Anders Carlsson | 335e24a | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 113 | Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType()); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 114 | return Sema::TDK_Inconsistent; |
| 115 | } |
| 116 | |
| 117 | return Sema::TDK_Success; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /// \brief Deduce the value of the given non-type template parameter |
| 121 | /// from the given type- or value-dependent expression. |
| 122 | /// |
| 123 | /// \returns true if deduction succeeded, false otherwise. |
| 124 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 125 | static Sema::TemplateDeductionResult |
| 126 | DeduceNonTypeTemplateArgument(ASTContext &Context, |
| 127 | NonTypeTemplateParmDecl *NTTP, |
| 128 | Expr *Value, |
| 129 | Sema::TemplateDeductionInfo &Info, |
| 130 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 131 | assert(NTTP->getDepth() == 0 && |
| 132 | "Cannot deduce non-type template argument with depth > 0"); |
| 133 | assert((Value->isTypeDependent() || Value->isValueDependent()) && |
| 134 | "Expression template argument must be type- or value-dependent."); |
| 135 | |
| 136 | if (Deduced[NTTP->getIndex()].isNull()) { |
| 137 | // FIXME: Clone the Value? |
| 138 | Deduced[NTTP->getIndex()] = TemplateArgument(Value); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 139 | return Sema::TDK_Success; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) { |
| 143 | // Okay, we deduced a constant in one case and a dependent expression |
| 144 | // in another case. FIXME: Later, we will check that instantiating the |
| 145 | // dependent expression gives us the constant value. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 146 | return Sema::TDK_Success; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // FIXME: Compare the expressions for equality! |
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 | } |
| 152 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 153 | static Sema::TemplateDeductionResult |
| 154 | DeduceTemplateArguments(ASTContext &Context, |
| 155 | TemplateName Param, |
| 156 | TemplateName Arg, |
| 157 | Sema::TemplateDeductionInfo &Info, |
| 158 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 159 | // FIXME: Implement template argument deduction for template |
| 160 | // template parameters. |
| 161 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 162 | // FIXME: this routine does not have enough information to produce |
| 163 | // good diagnostics. |
| 164 | |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 165 | TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); |
| 166 | TemplateDecl *ArgDecl = Arg.getAsTemplateDecl(); |
| 167 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 168 | if (!ParamDecl || !ArgDecl) { |
| 169 | // FIXME: fill in Info.Param/Info.FirstArg |
| 170 | return Sema::TDK_Inconsistent; |
| 171 | } |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 172 | |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 173 | ParamDecl = cast<TemplateDecl>(ParamDecl->getCanonicalDecl()); |
| 174 | ArgDecl = cast<TemplateDecl>(ArgDecl->getCanonicalDecl()); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 175 | if (ParamDecl != ArgDecl) { |
| 176 | // FIXME: fill in Info.Param/Info.FirstArg |
| 177 | return Sema::TDK_Inconsistent; |
| 178 | } |
| 179 | |
| 180 | return Sema::TDK_Success; |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 183 | /// \brief Deduce the template arguments by comparing the template parameter |
| 184 | /// type (which is a template-id) with the template argument type. |
| 185 | /// |
| 186 | /// \param Context the AST context in which this deduction occurs. |
| 187 | /// |
| 188 | /// \param TemplateParams the template parameters that we are deducing |
| 189 | /// |
| 190 | /// \param Param the parameter type |
| 191 | /// |
| 192 | /// \param Arg the argument type |
| 193 | /// |
| 194 | /// \param Info information about the template argument deduction itself |
| 195 | /// |
| 196 | /// \param Deduced the deduced template arguments |
| 197 | /// |
| 198 | /// \returns the result of template argument deduction so far. Note that a |
| 199 | /// "success" result means that template argument deduction has not yet failed, |
| 200 | /// but it may still fail, later, for other reasons. |
| 201 | static Sema::TemplateDeductionResult |
| 202 | DeduceTemplateArguments(ASTContext &Context, |
| 203 | TemplateParameterList *TemplateParams, |
| 204 | const TemplateSpecializationType *Param, |
| 205 | QualType Arg, |
| 206 | Sema::TemplateDeductionInfo &Info, |
| 207 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
| 208 | assert(Arg->isCanonical() && "Argument type must be canonical"); |
| 209 | |
| 210 | // Check whether the template argument is a dependent template-id. |
| 211 | // FIXME: This is untested code; it can be tested when we implement |
| 212 | // partial ordering of class template partial specializations. |
| 213 | if (const TemplateSpecializationType *SpecArg |
| 214 | = dyn_cast<TemplateSpecializationType>(Arg)) { |
| 215 | // Perform template argument deduction for the template name. |
| 216 | if (Sema::TemplateDeductionResult Result |
| 217 | = DeduceTemplateArguments(Context, |
| 218 | Param->getTemplateName(), |
| 219 | SpecArg->getTemplateName(), |
| 220 | Info, Deduced)) |
| 221 | return Result; |
| 222 | |
| 223 | unsigned NumArgs = Param->getNumArgs(); |
| 224 | |
| 225 | // FIXME: When one of the template-names refers to a |
| 226 | // declaration with default template arguments, do we need to |
| 227 | // fill in those default template arguments here? Most likely, |
| 228 | // the answer is "yes", but I don't see any references. This |
| 229 | // issue may be resolved elsewhere, because we may want to |
| 230 | // instantiate default template arguments when we actually write |
| 231 | // the template-id. |
| 232 | if (SpecArg->getNumArgs() != NumArgs) |
| 233 | return Sema::TDK_NonDeducedMismatch; |
| 234 | |
| 235 | // Perform template argument deduction on each template |
| 236 | // argument. |
| 237 | for (unsigned I = 0; I != NumArgs; ++I) |
| 238 | if (Sema::TemplateDeductionResult Result |
| 239 | = DeduceTemplateArguments(Context, TemplateParams, |
| 240 | Param->getArg(I), |
| 241 | SpecArg->getArg(I), |
| 242 | Info, Deduced)) |
| 243 | return Result; |
| 244 | |
| 245 | return Sema::TDK_Success; |
| 246 | } |
| 247 | |
| 248 | // If the argument type is a class template specialization, we |
| 249 | // perform template argument deduction using its template |
| 250 | // arguments. |
| 251 | const RecordType *RecordArg = dyn_cast<RecordType>(Arg); |
| 252 | if (!RecordArg) |
| 253 | return Sema::TDK_NonDeducedMismatch; |
| 254 | |
| 255 | ClassTemplateSpecializationDecl *SpecArg |
| 256 | = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); |
| 257 | if (!SpecArg) |
| 258 | return Sema::TDK_NonDeducedMismatch; |
| 259 | |
| 260 | // Perform template argument deduction for the template name. |
| 261 | if (Sema::TemplateDeductionResult Result |
| 262 | = DeduceTemplateArguments(Context, |
| 263 | Param->getTemplateName(), |
| 264 | TemplateName(SpecArg->getSpecializedTemplate()), |
| 265 | Info, Deduced)) |
| 266 | return Result; |
| 267 | |
| 268 | // FIXME: Can the # of arguments in the parameter and the argument |
| 269 | // differ due to default arguments? |
| 270 | unsigned NumArgs = Param->getNumArgs(); |
| 271 | const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs(); |
| 272 | if (NumArgs != ArgArgs.size()) |
| 273 | return Sema::TDK_NonDeducedMismatch; |
| 274 | |
| 275 | for (unsigned I = 0; I != NumArgs; ++I) |
| 276 | if (Sema::TemplateDeductionResult Result |
| 277 | = DeduceTemplateArguments(Context, TemplateParams, |
| 278 | Param->getArg(I), |
| 279 | ArgArgs.get(I), |
| 280 | Info, Deduced)) |
| 281 | return Result; |
| 282 | |
| 283 | return Sema::TDK_Success; |
| 284 | } |
| 285 | |
Douglas Gregor | f290e0d | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 286 | /// \brief Returns a completely-unqualified array type, capturing the |
| 287 | /// qualifiers in CVRQuals. |
| 288 | /// |
| 289 | /// \param Context the AST context in which the array type was built. |
| 290 | /// |
| 291 | /// \param T a canonical type that may be an array type. |
| 292 | /// |
| 293 | /// \param CVRQuals will receive the set of const/volatile/restrict qualifiers |
| 294 | /// that were applied to the element type of the array. |
| 295 | /// |
| 296 | /// \returns if \p T is an array type, the completely unqualified array type |
| 297 | /// that corresponds to T. Otherwise, returns T. |
| 298 | static QualType getUnqualifiedArrayType(ASTContext &Context, QualType T, |
| 299 | unsigned &CVRQuals) { |
| 300 | assert(T->isCanonical() && "Only operates on canonical types"); |
| 301 | if (!isa<ArrayType>(T)) { |
| 302 | CVRQuals = T.getCVRQualifiers(); |
| 303 | return T.getUnqualifiedType(); |
| 304 | } |
| 305 | |
| 306 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(T)) { |
| 307 | QualType Elt = getUnqualifiedArrayType(Context, CAT->getElementType(), |
| 308 | CVRQuals); |
| 309 | if (Elt == CAT->getElementType()) |
| 310 | return T; |
| 311 | |
| 312 | return Context.getConstantArrayType(Elt, CAT->getSize(), |
| 313 | CAT->getSizeModifier(), 0); |
| 314 | } |
| 315 | |
| 316 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(T)) { |
| 317 | QualType Elt = getUnqualifiedArrayType(Context, IAT->getElementType(), |
| 318 | CVRQuals); |
| 319 | if (Elt == IAT->getElementType()) |
| 320 | return T; |
| 321 | |
| 322 | return Context.getIncompleteArrayType(Elt, IAT->getSizeModifier(), 0); |
| 323 | } |
| 324 | |
| 325 | const DependentSizedArrayType *DSAT = cast<DependentSizedArrayType>(T); |
| 326 | QualType Elt = getUnqualifiedArrayType(Context, DSAT->getElementType(), |
| 327 | CVRQuals); |
| 328 | if (Elt == DSAT->getElementType()) |
| 329 | return T; |
| 330 | |
Anders Carlsson | d497206 | 2009-08-08 02:50:17 +0000 | [diff] [blame] | 331 | return Context.getDependentSizedArrayType(Elt, DSAT->getSizeExpr()->Retain(), |
Douglas Gregor | f290e0d | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 332 | DSAT->getSizeModifier(), 0, |
| 333 | SourceRange()); |
| 334 | } |
| 335 | |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 336 | /// \brief Deduce the template arguments by comparing the parameter type and |
| 337 | /// the argument type (C++ [temp.deduct.type]). |
| 338 | /// |
| 339 | /// \param Context the AST context in which this deduction occurs. |
| 340 | /// |
| 341 | /// \param TemplateParams the template parameters that we are deducing |
| 342 | /// |
| 343 | /// \param ParamIn the parameter type |
| 344 | /// |
| 345 | /// \param ArgIn the argument type |
| 346 | /// |
| 347 | /// \param Info information about the template argument deduction itself |
| 348 | /// |
| 349 | /// \param Deduced the deduced template arguments |
| 350 | /// |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 351 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
| 352 | /// how template argument deduction is performed. |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 353 | /// |
| 354 | /// \returns the result of template argument deduction so far. Note that a |
| 355 | /// "success" result means that template argument deduction has not yet failed, |
| 356 | /// but it may still fail, later, for other reasons. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 357 | static Sema::TemplateDeductionResult |
| 358 | DeduceTemplateArguments(ASTContext &Context, |
| 359 | TemplateParameterList *TemplateParams, |
| 360 | QualType ParamIn, QualType ArgIn, |
| 361 | Sema::TemplateDeductionInfo &Info, |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 362 | llvm::SmallVectorImpl<TemplateArgument> &Deduced, |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 363 | unsigned TDF) { |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 364 | // We only want to look at the canonical types, since typedefs and |
| 365 | // sugar are not part of template argument deduction. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 366 | QualType Param = Context.getCanonicalType(ParamIn); |
| 367 | QualType Arg = Context.getCanonicalType(ArgIn); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 368 | |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 369 | // C++0x [temp.deduct.call]p4 bullet 1: |
| 370 | // - If the original P is a reference type, the deduced A (i.e., the type |
| 371 | // referred to by the reference) can be more cv-qualified than the |
| 372 | // transformed A. |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 373 | if (TDF & TDF_ParamWithReferenceType) { |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 374 | unsigned ExtraQualsOnParam |
| 375 | = Param.getCVRQualifiers() & ~Arg.getCVRQualifiers(); |
| 376 | Param.setCVRQualifiers(Param.getCVRQualifiers() & ~ExtraQualsOnParam); |
| 377 | } |
| 378 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 379 | // If the parameter type is not dependent, there is nothing to deduce. |
| 380 | if (!Param->isDependentType()) |
| 381 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 382 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 383 | // C++ [temp.deduct.type]p9: |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 384 | // A template type argument T, a template template argument TT or a |
| 385 | // template non-type argument i can be deduced if P and A have one of |
| 386 | // the following forms: |
| 387 | // |
| 388 | // T |
| 389 | // cv-list T |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 390 | if (const TemplateTypeParmType *TemplateTypeParm |
| 391 | = Param->getAsTemplateTypeParmType()) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 392 | unsigned Index = TemplateTypeParm->getIndex(); |
Douglas Gregor | f290e0d | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 393 | bool RecanonicalizeArg = false; |
| 394 | |
Douglas Gregor | 9e9fae4 | 2009-07-22 20:02:25 +0000 | [diff] [blame] | 395 | // If the argument type is an array type, move the qualifiers up to the |
| 396 | // top level, so they can be matched with the qualifiers on the parameter. |
| 397 | // FIXME: address spaces, ObjC GC qualifiers |
Douglas Gregor | f290e0d | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 398 | if (isa<ArrayType>(Arg)) { |
| 399 | unsigned CVRQuals = 0; |
| 400 | Arg = getUnqualifiedArrayType(Context, Arg, CVRQuals); |
| 401 | if (CVRQuals) { |
| 402 | Arg = Arg.getWithAdditionalQualifiers(CVRQuals); |
| 403 | RecanonicalizeArg = true; |
| 404 | } |
| 405 | } |
Douglas Gregor | 9e9fae4 | 2009-07-22 20:02:25 +0000 | [diff] [blame] | 406 | |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 407 | // The argument type can not be less qualified than the parameter |
| 408 | // type. |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 409 | if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 410 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 411 | Info.FirstArg = Deduced[Index]; |
| 412 | Info.SecondArg = TemplateArgument(SourceLocation(), Arg); |
| 413 | return Sema::TDK_InconsistentQuals; |
| 414 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 415 | |
| 416 | assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0"); |
| 417 | |
| 418 | unsigned Quals = Arg.getCVRQualifiers() & ~Param.getCVRQualifiers(); |
Douglas Gregor | f290e0d | 2009-07-22 21:30:48 +0000 | [diff] [blame] | 419 | QualType DeducedType = Arg.getQualifiedType(Quals); |
| 420 | if (RecanonicalizeArg) |
| 421 | DeducedType = Context.getCanonicalType(DeducedType); |
| 422 | |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 423 | if (Deduced[Index].isNull()) |
| 424 | Deduced[Index] = TemplateArgument(SourceLocation(), DeducedType); |
| 425 | else { |
| 426 | // C++ [temp.deduct.type]p2: |
| 427 | // [...] If type deduction cannot be done for any P/A pair, or if for |
| 428 | // any pair the deduction leads to more than one possible set of |
| 429 | // deduced values, or if different pairs yield different deduced |
| 430 | // values, or if any template argument remains neither deduced nor |
| 431 | // explicitly specified, template argument deduction fails. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 432 | if (Deduced[Index].getAsType() != DeducedType) { |
| 433 | Info.Param |
| 434 | = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 435 | Info.FirstArg = Deduced[Index]; |
| 436 | Info.SecondArg = TemplateArgument(SourceLocation(), Arg); |
| 437 | return Sema::TDK_Inconsistent; |
| 438 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 439 | } |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 440 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 443 | // Set up the template argument deduction information for a failure. |
| 444 | Info.FirstArg = TemplateArgument(SourceLocation(), ParamIn); |
| 445 | Info.SecondArg = TemplateArgument(SourceLocation(), ArgIn); |
| 446 | |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 447 | // Check the cv-qualifiers on the parameter and argument types. |
| 448 | if (!(TDF & TDF_IgnoreQualifiers)) { |
| 449 | if (TDF & TDF_ParamWithReferenceType) { |
| 450 | if (Param.isMoreQualifiedThan(Arg)) |
| 451 | return Sema::TDK_NonDeducedMismatch; |
| 452 | } else { |
| 453 | if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) |
| 454 | return Sema::TDK_NonDeducedMismatch; |
| 455 | } |
| 456 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 457 | |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 458 | switch (Param->getTypeClass()) { |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 459 | // No deduction possible for these types |
| 460 | case Type::Builtin: |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 461 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 462 | |
| 463 | // T * |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 464 | case Type::Pointer: { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 465 | const PointerType *PointerArg = Arg->getAs<PointerType>(); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 466 | if (!PointerArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 467 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 468 | |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 469 | unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 470 | return DeduceTemplateArguments(Context, TemplateParams, |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 471 | cast<PointerType>(Param)->getPointeeType(), |
| 472 | PointerArg->getPointeeType(), |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 473 | Info, Deduced, SubTDF); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 476 | // T & |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 477 | case Type::LValueReference: { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 478 | const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>(); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 479 | if (!ReferenceArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 480 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 481 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 482 | return DeduceTemplateArguments(Context, TemplateParams, |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 483 | cast<LValueReferenceType>(Param)->getPointeeType(), |
| 484 | ReferenceArg->getPointeeType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 485 | Info, Deduced, 0); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 486 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 487 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 488 | // T && [C++0x] |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 489 | case Type::RValueReference: { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 490 | const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>(); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 491 | if (!ReferenceArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 492 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 493 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 494 | return DeduceTemplateArguments(Context, TemplateParams, |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 495 | cast<RValueReferenceType>(Param)->getPointeeType(), |
| 496 | ReferenceArg->getPointeeType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 497 | Info, Deduced, 0); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 500 | // T [] (implied, but not stated explicitly) |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 501 | case Type::IncompleteArray: { |
| 502 | const IncompleteArrayType *IncompleteArrayArg = |
| 503 | Context.getAsIncompleteArrayType(Arg); |
| 504 | if (!IncompleteArrayArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 505 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 506 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 507 | return DeduceTemplateArguments(Context, TemplateParams, |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 508 | Context.getAsIncompleteArrayType(Param)->getElementType(), |
| 509 | IncompleteArrayArg->getElementType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 510 | Info, Deduced, 0); |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 511 | } |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 512 | |
| 513 | // T [integer-constant] |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 514 | case Type::ConstantArray: { |
| 515 | const ConstantArrayType *ConstantArrayArg = |
| 516 | Context.getAsConstantArrayType(Arg); |
| 517 | if (!ConstantArrayArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 518 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 519 | |
| 520 | const ConstantArrayType *ConstantArrayParm = |
| 521 | Context.getAsConstantArrayType(Param); |
| 522 | if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 523 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 524 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 525 | return DeduceTemplateArguments(Context, TemplateParams, |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 526 | ConstantArrayParm->getElementType(), |
| 527 | ConstantArrayArg->getElementType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 528 | Info, Deduced, 0); |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 531 | // type [i] |
| 532 | case Type::DependentSizedArray: { |
| 533 | const ArrayType *ArrayArg = dyn_cast<ArrayType>(Arg); |
| 534 | if (!ArrayArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 535 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 536 | |
| 537 | // Check the element type of the arrays |
| 538 | const DependentSizedArrayType *DependentArrayParm |
| 539 | = cast<DependentSizedArrayType>(Param); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 540 | if (Sema::TemplateDeductionResult Result |
| 541 | = DeduceTemplateArguments(Context, TemplateParams, |
| 542 | DependentArrayParm->getElementType(), |
| 543 | ArrayArg->getElementType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 544 | Info, Deduced, 0)) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 545 | return Result; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 546 | |
| 547 | // Determine the array bound is something we can deduce. |
| 548 | NonTypeTemplateParmDecl *NTTP |
| 549 | = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr()); |
| 550 | if (!NTTP) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 551 | return Sema::TDK_Success; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 552 | |
| 553 | // We can perform template argument deduction for the given non-type |
| 554 | // template parameter. |
| 555 | assert(NTTP->getDepth() == 0 && |
| 556 | "Cannot deduce non-type template argument at depth > 0"); |
| 557 | if (const ConstantArrayType *ConstantArrayArg |
Anders Carlsson | 335e24a | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 558 | = dyn_cast<ConstantArrayType>(ArrayArg)) { |
| 559 | llvm::APSInt Size(ConstantArrayArg->getSize()); |
| 560 | return DeduceNonTypeTemplateArgument(Context, NTTP, Size, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 561 | Info, Deduced); |
Anders Carlsson | 335e24a | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 562 | } |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 563 | if (const DependentSizedArrayType *DependentArrayArg |
| 564 | = dyn_cast<DependentSizedArrayType>(ArrayArg)) |
| 565 | return DeduceNonTypeTemplateArgument(Context, NTTP, |
| 566 | DependentArrayArg->getSizeExpr(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 567 | Info, Deduced); |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 568 | |
| 569 | // Incomplete type does not match a dependently-sized array type |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 570 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Douglas Gregor | 0fce0ae | 2009-06-08 15:59:14 +0000 | [diff] [blame] | 573 | // type(*)(T) |
| 574 | // T(*)() |
| 575 | // T(*)(T) |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 576 | case Type::FunctionProto: { |
| 577 | const FunctionProtoType *FunctionProtoArg = |
| 578 | dyn_cast<FunctionProtoType>(Arg); |
| 579 | if (!FunctionProtoArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 580 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 581 | |
| 582 | const FunctionProtoType *FunctionProtoParam = |
| 583 | cast<FunctionProtoType>(Param); |
Anders Carlsson | 994b6cb | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 584 | |
| 585 | if (FunctionProtoParam->getTypeQuals() != |
| 586 | FunctionProtoArg->getTypeQuals()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 587 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 588 | |
Anders Carlsson | 994b6cb | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 589 | if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 590 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 994b6cb | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 591 | |
| 592 | if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 593 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 994b6cb | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 594 | |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 595 | // Check return types. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 596 | if (Sema::TemplateDeductionResult Result |
| 597 | = DeduceTemplateArguments(Context, TemplateParams, |
| 598 | FunctionProtoParam->getResultType(), |
| 599 | FunctionProtoArg->getResultType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 600 | Info, Deduced, 0)) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 601 | return Result; |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 602 | |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 603 | for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) { |
| 604 | // Check argument types. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 605 | if (Sema::TemplateDeductionResult Result |
| 606 | = DeduceTemplateArguments(Context, TemplateParams, |
| 607 | FunctionProtoParam->getArgType(I), |
| 608 | FunctionProtoArg->getArgType(I), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 609 | Info, Deduced, 0)) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 610 | return Result; |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 613 | return Sema::TDK_Success; |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 614 | } |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 615 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 616 | // template-name<T> (where template-name refers to a class template) |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 617 | // template-name<i> |
| 618 | // TT<T> (TODO) |
| 619 | // TT<i> (TODO) |
| 620 | // TT<> (TODO) |
| 621 | case Type::TemplateSpecialization: { |
| 622 | const TemplateSpecializationType *SpecParam |
| 623 | = cast<TemplateSpecializationType>(Param); |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 624 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 625 | // Try to deduce template arguments from the template-id. |
| 626 | Sema::TemplateDeductionResult Result |
| 627 | = DeduceTemplateArguments(Context, TemplateParams, SpecParam, Arg, |
| 628 | Info, Deduced); |
| 629 | |
| 630 | if (Result && (TDF & TDF_DerivedClass) && |
| 631 | Result != Sema::TDK_Inconsistent) { |
| 632 | // C++ [temp.deduct.call]p3b3: |
| 633 | // If P is a class, and P has the form template-id, then A can be a |
| 634 | // derived class of the deduced A. Likewise, if P is a pointer to a |
| 635 | // class of the form template-id, A can be a pointer to a derived |
| 636 | // class pointed to by the deduced A. |
| 637 | // |
| 638 | // More importantly: |
| 639 | // These alternatives are considered only if type deduction would |
| 640 | // otherwise fail. |
| 641 | if (const RecordType *RecordT = dyn_cast<RecordType>(Arg)) { |
| 642 | // Use data recursion to crawl through the list of base classes. |
| 643 | // Visited contains the set of nodes we have already visited, while |
| 644 | // ToVisit is our stack of records that we still need to visit. |
| 645 | llvm::SmallPtrSet<const RecordType *, 8> Visited; |
| 646 | llvm::SmallVector<const RecordType *, 8> ToVisit; |
| 647 | ToVisit.push_back(RecordT); |
| 648 | bool Successful = false; |
| 649 | while (!ToVisit.empty()) { |
| 650 | // Retrieve the next class in the inheritance hierarchy. |
| 651 | const RecordType *NextT = ToVisit.back(); |
| 652 | ToVisit.pop_back(); |
| 653 | |
| 654 | // If we have already seen this type, skip it. |
| 655 | if (!Visited.insert(NextT)) |
| 656 | continue; |
| 657 | |
| 658 | // If this is a base class, try to perform template argument |
| 659 | // deduction from it. |
| 660 | if (NextT != RecordT) { |
| 661 | Sema::TemplateDeductionResult BaseResult |
| 662 | = DeduceTemplateArguments(Context, TemplateParams, SpecParam, |
| 663 | QualType(NextT, 0), Info, Deduced); |
| 664 | |
| 665 | // If template argument deduction for this base was successful, |
| 666 | // note that we had some success. |
| 667 | if (BaseResult == Sema::TDK_Success) |
| 668 | Successful = true; |
| 669 | // If deduction against this base resulted in an inconsistent |
| 670 | // set of deduced template arguments, template argument |
| 671 | // deduction fails. |
| 672 | else if (BaseResult == Sema::TDK_Inconsistent) |
| 673 | return BaseResult; |
| 674 | } |
| 675 | |
| 676 | // Visit base classes |
| 677 | CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl()); |
| 678 | for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(), |
| 679 | BaseEnd = Next->bases_end(); |
| 680 | Base != BaseEnd; ++Base) { |
| 681 | assert(Base->getType()->isRecordType() && |
| 682 | "Base class that isn't a record?"); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 683 | ToVisit.push_back(Base->getType()->getAs<RecordType>()); |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
| 687 | if (Successful) |
| 688 | return Sema::TDK_Success; |
| 689 | } |
| 690 | |
| 691 | } |
| 692 | |
| 693 | return Result; |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 696 | // T type::* |
| 697 | // T T::* |
| 698 | // T (type::*)() |
| 699 | // type (T::*)() |
| 700 | // type (type::*)(T) |
| 701 | // type (T::*)(T) |
| 702 | // T (type::*)(T) |
| 703 | // T (T::*)() |
| 704 | // T (T::*)(T) |
| 705 | case Type::MemberPointer: { |
| 706 | const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); |
| 707 | const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); |
| 708 | if (!MemPtrArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 709 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 710 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 711 | if (Sema::TemplateDeductionResult Result |
| 712 | = DeduceTemplateArguments(Context, TemplateParams, |
| 713 | MemPtrParam->getPointeeType(), |
| 714 | MemPtrArg->getPointeeType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 715 | Info, Deduced, |
| 716 | TDF & TDF_IgnoreQualifiers)) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 717 | return Result; |
| 718 | |
| 719 | return DeduceTemplateArguments(Context, TemplateParams, |
| 720 | QualType(MemPtrParam->getClass(), 0), |
| 721 | QualType(MemPtrArg->getClass(), 0), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 722 | Info, Deduced, 0); |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 723 | } |
| 724 | |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 725 | // (clang extension) |
| 726 | // |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 727 | // type(^)(T) |
| 728 | // T(^)() |
| 729 | // T(^)(T) |
| 730 | case Type::BlockPointer: { |
| 731 | const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); |
| 732 | const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); |
| 733 | |
| 734 | if (!BlockPtrArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 735 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 736 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 737 | return DeduceTemplateArguments(Context, TemplateParams, |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 738 | BlockPtrParam->getPointeeType(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 739 | BlockPtrArg->getPointeeType(), Info, |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 740 | Deduced, 0); |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 743 | case Type::TypeOfExpr: |
| 744 | case Type::TypeOf: |
| 745 | case Type::Typename: |
| 746 | // No template argument deduction for these types |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 747 | return Sema::TDK_Success; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 748 | |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 749 | default: |
| 750 | break; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | // FIXME: Many more cases to go (to go). |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 754 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 757 | static Sema::TemplateDeductionResult |
| 758 | DeduceTemplateArguments(ASTContext &Context, |
| 759 | TemplateParameterList *TemplateParams, |
| 760 | const TemplateArgument &Param, |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 761 | const TemplateArgument &Arg, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 762 | Sema::TemplateDeductionInfo &Info, |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 763 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 764 | switch (Param.getKind()) { |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 765 | case TemplateArgument::Null: |
| 766 | assert(false && "Null template argument in parameter list"); |
| 767 | break; |
| 768 | |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 769 | case TemplateArgument::Type: |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 770 | assert(Arg.getKind() == TemplateArgument::Type && "Type/value mismatch"); |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 771 | return DeduceTemplateArguments(Context, TemplateParams, Param.getAsType(), |
| 772 | Arg.getAsType(), Info, Deduced, 0); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 773 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 774 | case TemplateArgument::Declaration: |
| 775 | // FIXME: Implement this check |
| 776 | assert(false && "Unimplemented template argument deduction case"); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 777 | Info.FirstArg = Param; |
| 778 | Info.SecondArg = Arg; |
| 779 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 780 | |
| 781 | case TemplateArgument::Integral: |
| 782 | if (Arg.getKind() == TemplateArgument::Integral) { |
| 783 | // FIXME: Zero extension + sign checking here? |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 784 | if (*Param.getAsIntegral() == *Arg.getAsIntegral()) |
| 785 | return Sema::TDK_Success; |
| 786 | |
| 787 | Info.FirstArg = Param; |
| 788 | Info.SecondArg = Arg; |
| 789 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 790 | } |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 791 | |
| 792 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 793 | Info.FirstArg = Param; |
| 794 | Info.SecondArg = Arg; |
| 795 | return Sema::TDK_NonDeducedMismatch; |
| 796 | } |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 797 | |
| 798 | assert(false && "Type/value mismatch"); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 799 | Info.FirstArg = Param; |
| 800 | Info.SecondArg = Arg; |
| 801 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 802 | |
| 803 | case TemplateArgument::Expression: { |
| 804 | if (NonTypeTemplateParmDecl *NTTP |
| 805 | = getDeducedParameterFromExpr(Param.getAsExpr())) { |
| 806 | if (Arg.getKind() == TemplateArgument::Integral) |
| 807 | // FIXME: Sign problems here |
| 808 | return DeduceNonTypeTemplateArgument(Context, NTTP, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 809 | *Arg.getAsIntegral(), |
| 810 | Info, Deduced); |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 811 | if (Arg.getKind() == TemplateArgument::Expression) |
| 812 | return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 813 | Info, Deduced); |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 814 | |
| 815 | assert(false && "Type/value mismatch"); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 816 | Info.FirstArg = Param; |
| 817 | Info.SecondArg = Arg; |
| 818 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | // Can't deduce anything, but that's okay. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 822 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 823 | } |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 824 | case TemplateArgument::Pack: |
| 825 | assert(0 && "FIXME: Implement!"); |
| 826 | break; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 829 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 832 | static Sema::TemplateDeductionResult |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 833 | DeduceTemplateArguments(ASTContext &Context, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 834 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 835 | const TemplateArgumentList &ParamList, |
| 836 | const TemplateArgumentList &ArgList, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 837 | Sema::TemplateDeductionInfo &Info, |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 838 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
| 839 | assert(ParamList.size() == ArgList.size()); |
| 840 | for (unsigned I = 0, N = ParamList.size(); I != N; ++I) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 841 | if (Sema::TemplateDeductionResult Result |
| 842 | = DeduceTemplateArguments(Context, TemplateParams, |
| 843 | ParamList[I], ArgList[I], |
| 844 | Info, Deduced)) |
| 845 | return Result; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 846 | } |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 847 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 850 | /// \brief Determine whether two template arguments are the same. |
| 851 | static bool isSameTemplateArg(ASTContext &Context, |
| 852 | const TemplateArgument &X, |
| 853 | const TemplateArgument &Y) { |
| 854 | if (X.getKind() != Y.getKind()) |
| 855 | return false; |
| 856 | |
| 857 | switch (X.getKind()) { |
| 858 | case TemplateArgument::Null: |
| 859 | assert(false && "Comparing NULL template argument"); |
| 860 | break; |
| 861 | |
| 862 | case TemplateArgument::Type: |
| 863 | return Context.getCanonicalType(X.getAsType()) == |
| 864 | Context.getCanonicalType(Y.getAsType()); |
| 865 | |
| 866 | case TemplateArgument::Declaration: |
Argyrios Kyrtzidis | 97fbaa2 | 2009-07-18 00:34:25 +0000 | [diff] [blame] | 867 | return X.getAsDecl()->getCanonicalDecl() == |
| 868 | Y.getAsDecl()->getCanonicalDecl(); |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 869 | |
| 870 | case TemplateArgument::Integral: |
| 871 | return *X.getAsIntegral() == *Y.getAsIntegral(); |
| 872 | |
| 873 | case TemplateArgument::Expression: |
| 874 | // FIXME: We assume that all expressions are distinct, but we should |
| 875 | // really check their canonical forms. |
| 876 | return false; |
| 877 | |
| 878 | case TemplateArgument::Pack: |
| 879 | if (X.pack_size() != Y.pack_size()) |
| 880 | return false; |
| 881 | |
| 882 | for (TemplateArgument::pack_iterator XP = X.pack_begin(), |
| 883 | XPEnd = X.pack_end(), |
| 884 | YP = Y.pack_begin(); |
| 885 | XP != XPEnd; ++XP, ++YP) |
| 886 | if (!isSameTemplateArg(Context, *XP, *YP)) |
| 887 | return false; |
| 888 | |
| 889 | return true; |
| 890 | } |
| 891 | |
| 892 | return false; |
| 893 | } |
| 894 | |
| 895 | /// \brief Helper function to build a TemplateParameter when we don't |
| 896 | /// know its type statically. |
| 897 | static TemplateParameter makeTemplateParameter(Decl *D) { |
| 898 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) |
| 899 | return TemplateParameter(TTP); |
| 900 | else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) |
| 901 | return TemplateParameter(NTTP); |
| 902 | |
| 903 | return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); |
| 904 | } |
| 905 | |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 906 | /// \brief Perform template argument deduction to determine whether |
| 907 | /// the given template arguments match the given class template |
| 908 | /// partial specialization per C++ [temp.class.spec.match]. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 909 | Sema::TemplateDeductionResult |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 910 | Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 911 | const TemplateArgumentList &TemplateArgs, |
| 912 | TemplateDeductionInfo &Info) { |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 913 | // C++ [temp.class.spec.match]p2: |
| 914 | // A partial specialization matches a given actual template |
| 915 | // argument list if the template arguments of the partial |
| 916 | // specialization can be deduced from the actual template argument |
| 917 | // list (14.8.2). |
Douglas Gregor | bb26041 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 918 | SFINAETrap Trap(*this); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 919 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
| 920 | Deduced.resize(Partial->getTemplateParameters()->size()); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 921 | if (TemplateDeductionResult Result |
| 922 | = ::DeduceTemplateArguments(Context, |
| 923 | Partial->getTemplateParameters(), |
| 924 | Partial->getTemplateArgs(), |
| 925 | TemplateArgs, Info, Deduced)) |
| 926 | return Result; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 927 | |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 928 | InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial, |
| 929 | Deduced.data(), Deduced.size()); |
| 930 | if (Inst) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 931 | return TDK_InstantiationDepth; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 932 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 933 | // C++ [temp.deduct.type]p2: |
| 934 | // [...] or if any template argument remains neither deduced nor |
| 935 | // explicitly specified, template argument deduction fails. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 936 | TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(), |
| 937 | Deduced.size()); |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 938 | for (unsigned I = 0, N = Deduced.size(); I != N; ++I) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 939 | if (Deduced[I].isNull()) { |
| 940 | Decl *Param |
| 941 | = const_cast<Decl *>(Partial->getTemplateParameters()->getParam(I)); |
| 942 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 943 | Info.Param = TTP; |
| 944 | else if (NonTypeTemplateParmDecl *NTTP |
| 945 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 946 | Info.Param = NTTP; |
| 947 | else |
| 948 | Info.Param = cast<TemplateTemplateParmDecl>(Param); |
| 949 | return TDK_Incomplete; |
| 950 | } |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 951 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 952 | Builder.Append(Deduced[I]); |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | // Form the template argument list from the deduced template arguments. |
| 956 | TemplateArgumentList *DeducedArgumentList |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 957 | = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 958 | Info.reset(DeducedArgumentList); |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 959 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 960 | // Substitute the deduced template arguments into the template |
| 961 | // arguments of the class template partial specialization, and |
| 962 | // verify that the instantiated template arguments are both valid |
| 963 | // and are equivalent to the template arguments originally provided |
Douglas Gregor | c9e5d25 | 2009-06-13 00:59:32 +0000 | [diff] [blame] | 964 | // to the class template. |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 965 | ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate(); |
| 966 | const TemplateArgumentList &PartialTemplateArgs = Partial->getTemplateArgs(); |
| 967 | for (unsigned I = 0, N = PartialTemplateArgs.flat_size(); I != N; ++I) { |
Douglas Gregor | c9e5d25 | 2009-06-13 00:59:32 +0000 | [diff] [blame] | 968 | Decl *Param = const_cast<Decl *>( |
| 969 | ClassTemplate->getTemplateParameters()->getParam(I)); |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 970 | TemplateArgument InstArg = Instantiate(PartialTemplateArgs[I], |
| 971 | *DeducedArgumentList); |
| 972 | if (InstArg.isNull()) { |
| 973 | Info.Param = makeTemplateParameter(Param); |
| 974 | Info.FirstArg = PartialTemplateArgs[I]; |
| 975 | return TDK_SubstitutionFailure; |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 976 | } |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 977 | |
| 978 | if (InstArg.getKind() == TemplateArgument::Expression) { |
| 979 | // When the argument is an expression, check the expression result |
| 980 | // against the actual template parameter to get down to the canonical |
| 981 | // template argument. |
| 982 | Expr *InstExpr = InstArg.getAsExpr(); |
| 983 | if (NonTypeTemplateParmDecl *NTTP |
| 984 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 985 | if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) { |
| 986 | Info.Param = makeTemplateParameter(Param); |
| 987 | Info.FirstArg = PartialTemplateArgs[I]; |
| 988 | return TDK_SubstitutionFailure; |
| 989 | } |
| 990 | } else if (TemplateTemplateParmDecl *TTP |
| 991 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 992 | // FIXME: template template arguments should really resolve to decls |
| 993 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InstExpr); |
| 994 | if (!DRE || CheckTemplateArgument(TTP, DRE)) { |
| 995 | Info.Param = makeTemplateParameter(Param); |
| 996 | Info.FirstArg = PartialTemplateArgs[I]; |
| 997 | return TDK_SubstitutionFailure; |
| 998 | } |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) { |
| 1003 | Info.Param = makeTemplateParameter(Param); |
| 1004 | Info.FirstArg = TemplateArgs[I]; |
| 1005 | Info.SecondArg = InstArg; |
| 1006 | return TDK_NonDeducedMismatch; |
| 1007 | } |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Douglas Gregor | bb26041 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 1010 | if (Trap.hasErrorOccurred()) |
| 1011 | return TDK_SubstitutionFailure; |
| 1012 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 1013 | return TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 1014 | } |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1015 | |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1016 | /// \brief Determine whether the given type T is a simple-template-id type. |
| 1017 | static bool isSimpleTemplateIdType(QualType T) { |
| 1018 | if (const TemplateSpecializationType *Spec |
| 1019 | = T->getAsTemplateSpecializationType()) |
| 1020 | return Spec->getTemplateName().getAsTemplateDecl() != 0; |
| 1021 | |
| 1022 | return false; |
| 1023 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1024 | |
| 1025 | /// \brief Substitute the explicitly-provided template arguments into the |
| 1026 | /// given function template according to C++ [temp.arg.explicit]. |
| 1027 | /// |
| 1028 | /// \param FunctionTemplate the function template into which the explicit |
| 1029 | /// template arguments will be substituted. |
| 1030 | /// |
| 1031 | /// \param ExplicitTemplateArguments the explicitly-specified template |
| 1032 | /// arguments. |
| 1033 | /// |
| 1034 | /// \param NumExplicitTemplateArguments the number of explicitly-specified |
| 1035 | /// template arguments in @p ExplicitTemplateArguments. This value may be zero. |
| 1036 | /// |
| 1037 | /// \param Deduced the deduced template arguments, which will be populated |
| 1038 | /// with the converted and checked explicit template arguments. |
| 1039 | /// |
| 1040 | /// \param ParamTypes will be populated with the instantiated function |
| 1041 | /// parameters. |
| 1042 | /// |
| 1043 | /// \param FunctionType if non-NULL, the result type of the function template |
| 1044 | /// will also be instantiated and the pointed-to value will be updated with |
| 1045 | /// the instantiated function type. |
| 1046 | /// |
| 1047 | /// \param Info if substitution fails for any reason, this object will be |
| 1048 | /// populated with more information about the failure. |
| 1049 | /// |
| 1050 | /// \returns TDK_Success if substitution was successful, or some failure |
| 1051 | /// condition. |
| 1052 | Sema::TemplateDeductionResult |
| 1053 | Sema::SubstituteExplicitTemplateArguments( |
| 1054 | FunctionTemplateDecl *FunctionTemplate, |
| 1055 | const TemplateArgument *ExplicitTemplateArgs, |
| 1056 | unsigned NumExplicitTemplateArgs, |
| 1057 | llvm::SmallVectorImpl<TemplateArgument> &Deduced, |
| 1058 | llvm::SmallVectorImpl<QualType> &ParamTypes, |
| 1059 | QualType *FunctionType, |
| 1060 | TemplateDeductionInfo &Info) { |
| 1061 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 1062 | TemplateParameterList *TemplateParams |
| 1063 | = FunctionTemplate->getTemplateParameters(); |
| 1064 | |
| 1065 | if (NumExplicitTemplateArgs == 0) { |
| 1066 | // No arguments to substitute; just copy over the parameter types and |
| 1067 | // fill in the function type. |
| 1068 | for (FunctionDecl::param_iterator P = Function->param_begin(), |
| 1069 | PEnd = Function->param_end(); |
| 1070 | P != PEnd; |
| 1071 | ++P) |
| 1072 | ParamTypes.push_back((*P)->getType()); |
| 1073 | |
| 1074 | if (FunctionType) |
| 1075 | *FunctionType = Function->getType(); |
| 1076 | return TDK_Success; |
| 1077 | } |
| 1078 | |
| 1079 | // Substitution of the explicit template arguments into a function template |
| 1080 | /// is a SFINAE context. Trap any errors that might occur. |
| 1081 | SFINAETrap Trap(*this); |
| 1082 | |
| 1083 | // C++ [temp.arg.explicit]p3: |
| 1084 | // Template arguments that are present shall be specified in the |
| 1085 | // declaration order of their corresponding template-parameters. The |
| 1086 | // template argument list shall not specify more template-arguments than |
| 1087 | // there are corresponding template-parameters. |
| 1088 | TemplateArgumentListBuilder Builder(TemplateParams, |
| 1089 | NumExplicitTemplateArgs); |
| 1090 | |
| 1091 | // Enter a new template instantiation context where we check the |
| 1092 | // explicitly-specified template arguments against this function template, |
| 1093 | // and then substitute them into the function parameter types. |
| 1094 | InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), |
| 1095 | FunctionTemplate, Deduced.data(), Deduced.size(), |
| 1096 | ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution); |
| 1097 | if (Inst) |
| 1098 | return TDK_InstantiationDepth; |
| 1099 | |
| 1100 | if (CheckTemplateArgumentList(FunctionTemplate, |
| 1101 | SourceLocation(), SourceLocation(), |
| 1102 | ExplicitTemplateArgs, |
| 1103 | NumExplicitTemplateArgs, |
| 1104 | SourceLocation(), |
| 1105 | true, |
| 1106 | Builder) || Trap.hasErrorOccurred()) |
| 1107 | return TDK_InvalidExplicitArguments; |
| 1108 | |
| 1109 | // Form the template argument list from the explicitly-specified |
| 1110 | // template arguments. |
| 1111 | TemplateArgumentList *ExplicitArgumentList |
| 1112 | = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); |
| 1113 | Info.reset(ExplicitArgumentList); |
| 1114 | |
| 1115 | // Instantiate the types of each of the function parameters given the |
| 1116 | // explicitly-specified template arguments. |
| 1117 | for (FunctionDecl::param_iterator P = Function->param_begin(), |
| 1118 | PEnd = Function->param_end(); |
| 1119 | P != PEnd; |
| 1120 | ++P) { |
| 1121 | QualType ParamType = InstantiateType((*P)->getType(), |
| 1122 | *ExplicitArgumentList, |
| 1123 | (*P)->getLocation(), |
| 1124 | (*P)->getDeclName()); |
| 1125 | if (ParamType.isNull() || Trap.hasErrorOccurred()) |
| 1126 | return TDK_SubstitutionFailure; |
| 1127 | |
| 1128 | ParamTypes.push_back(ParamType); |
| 1129 | } |
| 1130 | |
| 1131 | // If the caller wants a full function type back, instantiate the return |
| 1132 | // type and form that function type. |
| 1133 | if (FunctionType) { |
| 1134 | // FIXME: exception-specifications? |
| 1135 | const FunctionProtoType *Proto |
| 1136 | = Function->getType()->getAsFunctionProtoType(); |
| 1137 | assert(Proto && "Function template does not have a prototype?"); |
| 1138 | |
| 1139 | QualType ResultType = InstantiateType(Proto->getResultType(), |
| 1140 | *ExplicitArgumentList, |
| 1141 | Function->getTypeSpecStartLoc(), |
| 1142 | Function->getDeclName()); |
| 1143 | if (ResultType.isNull() || Trap.hasErrorOccurred()) |
| 1144 | return TDK_SubstitutionFailure; |
| 1145 | |
| 1146 | *FunctionType = BuildFunctionType(ResultType, |
| 1147 | ParamTypes.data(), ParamTypes.size(), |
| 1148 | Proto->isVariadic(), |
| 1149 | Proto->getTypeQuals(), |
| 1150 | Function->getLocation(), |
| 1151 | Function->getDeclName()); |
| 1152 | if (FunctionType->isNull() || Trap.hasErrorOccurred()) |
| 1153 | return TDK_SubstitutionFailure; |
| 1154 | } |
| 1155 | |
| 1156 | // C++ [temp.arg.explicit]p2: |
| 1157 | // Trailing template arguments that can be deduced (14.8.2) may be |
| 1158 | // omitted from the list of explicit template-arguments. If all of the |
| 1159 | // template arguments can be deduced, they may all be omitted; in this |
| 1160 | // case, the empty template argument list <> itself may also be omitted. |
| 1161 | // |
| 1162 | // Take all of the explicitly-specified arguments and put them into the |
| 1163 | // set of deduced template arguments. |
| 1164 | Deduced.reserve(TemplateParams->size()); |
| 1165 | for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) |
| 1166 | Deduced.push_back(ExplicitArgumentList->get(I)); |
| 1167 | |
| 1168 | return TDK_Success; |
| 1169 | } |
| 1170 | |
| 1171 | /// \brief Finish template argument deduction for a function template, |
| 1172 | /// checking the deduced template arguments for completeness and forming |
| 1173 | /// the function template specialization. |
| 1174 | Sema::TemplateDeductionResult |
| 1175 | Sema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate, |
| 1176 | llvm::SmallVectorImpl<TemplateArgument> &Deduced, |
| 1177 | FunctionDecl *&Specialization, |
| 1178 | TemplateDeductionInfo &Info) { |
| 1179 | TemplateParameterList *TemplateParams |
| 1180 | = FunctionTemplate->getTemplateParameters(); |
| 1181 | |
| 1182 | // C++ [temp.deduct.type]p2: |
| 1183 | // [...] or if any template argument remains neither deduced nor |
| 1184 | // explicitly specified, template argument deduction fails. |
| 1185 | TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size()); |
| 1186 | for (unsigned I = 0, N = Deduced.size(); I != N; ++I) { |
| 1187 | if (Deduced[I].isNull()) { |
| 1188 | Info.Param = makeTemplateParameter( |
| 1189 | const_cast<Decl *>(TemplateParams->getParam(I))); |
| 1190 | return TDK_Incomplete; |
| 1191 | } |
| 1192 | |
| 1193 | Builder.Append(Deduced[I]); |
| 1194 | } |
| 1195 | |
| 1196 | // Form the template argument list from the deduced template arguments. |
| 1197 | TemplateArgumentList *DeducedArgumentList |
| 1198 | = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); |
| 1199 | Info.reset(DeducedArgumentList); |
| 1200 | |
| 1201 | // Template argument deduction for function templates in a SFINAE context. |
| 1202 | // Trap any errors that might occur. |
| 1203 | SFINAETrap Trap(*this); |
| 1204 | |
| 1205 | // Enter a new template instantiation context while we instantiate the |
| 1206 | // actual function declaration. |
| 1207 | InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), |
| 1208 | FunctionTemplate, Deduced.data(), Deduced.size(), |
| 1209 | ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution); |
| 1210 | if (Inst) |
| 1211 | return TDK_InstantiationDepth; |
| 1212 | |
| 1213 | // Substitute the deduced template arguments into the function template |
| 1214 | // declaration to produce the function template specialization. |
| 1215 | Specialization = cast_or_null<FunctionDecl>( |
| 1216 | InstantiateDecl(FunctionTemplate->getTemplatedDecl(), |
| 1217 | FunctionTemplate->getDeclContext(), |
| 1218 | *DeducedArgumentList)); |
| 1219 | if (!Specialization) |
| 1220 | return TDK_SubstitutionFailure; |
| 1221 | |
| 1222 | // If the template argument list is owned by the function template |
| 1223 | // specialization, release it. |
| 1224 | if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList) |
| 1225 | Info.take(); |
| 1226 | |
| 1227 | // There may have been an error that did not prevent us from constructing a |
| 1228 | // declaration. Mark the declaration invalid and return with a substitution |
| 1229 | // failure. |
| 1230 | if (Trap.hasErrorOccurred()) { |
| 1231 | Specialization->setInvalidDecl(true); |
| 1232 | return TDK_SubstitutionFailure; |
| 1233 | } |
| 1234 | |
| 1235 | return TDK_Success; |
| 1236 | } |
| 1237 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1238 | /// \brief Perform template argument deduction from a function call |
| 1239 | /// (C++ [temp.deduct.call]). |
| 1240 | /// |
| 1241 | /// \param FunctionTemplate the function template for which we are performing |
| 1242 | /// template argument deduction. |
| 1243 | /// |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1244 | /// \param HasExplicitTemplateArgs whether any template arguments were |
| 1245 | /// explicitly specified. |
| 1246 | /// |
| 1247 | /// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, |
| 1248 | /// the explicitly-specified template arguments. |
| 1249 | /// |
| 1250 | /// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, |
| 1251 | /// the number of explicitly-specified template arguments in |
| 1252 | /// @p ExplicitTemplateArguments. This value may be zero. |
| 1253 | /// |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1254 | /// \param Args the function call arguments |
| 1255 | /// |
| 1256 | /// \param NumArgs the number of arguments in Args |
| 1257 | /// |
| 1258 | /// \param Specialization if template argument deduction was successful, |
| 1259 | /// this will be set to the function template specialization produced by |
| 1260 | /// template argument deduction. |
| 1261 | /// |
| 1262 | /// \param Info the argument will be updated to provide additional information |
| 1263 | /// about template argument deduction. |
| 1264 | /// |
| 1265 | /// \returns the result of template argument deduction. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1266 | Sema::TemplateDeductionResult |
| 1267 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1268 | bool HasExplicitTemplateArgs, |
| 1269 | const TemplateArgument *ExplicitTemplateArgs, |
| 1270 | unsigned NumExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1271 | Expr **Args, unsigned NumArgs, |
| 1272 | FunctionDecl *&Specialization, |
| 1273 | TemplateDeductionInfo &Info) { |
| 1274 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1275 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1276 | // C++ [temp.deduct.call]p1: |
| 1277 | // Template argument deduction is done by comparing each function template |
| 1278 | // parameter type (call it P) with the type of the corresponding argument |
| 1279 | // of the call (call it A) as described below. |
| 1280 | unsigned CheckArgs = NumArgs; |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1281 | if (NumArgs < Function->getMinRequiredArguments()) |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1282 | return TDK_TooFewArguments; |
| 1283 | else if (NumArgs > Function->getNumParams()) { |
| 1284 | const FunctionProtoType *Proto |
| 1285 | = Function->getType()->getAsFunctionProtoType(); |
| 1286 | if (!Proto->isVariadic()) |
| 1287 | return TDK_TooManyArguments; |
| 1288 | |
| 1289 | CheckArgs = Function->getNumParams(); |
| 1290 | } |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1291 | |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1292 | // The types of the parameters from which we will perform template argument |
| 1293 | // deduction. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1294 | TemplateParameterList *TemplateParams |
| 1295 | = FunctionTemplate->getTemplateParameters(); |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1296 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
| 1297 | llvm::SmallVector<QualType, 4> ParamTypes; |
| 1298 | if (NumExplicitTemplateArgs) { |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1299 | TemplateDeductionResult Result = |
| 1300 | SubstituteExplicitTemplateArguments(FunctionTemplate, |
| 1301 | ExplicitTemplateArgs, |
| 1302 | NumExplicitTemplateArgs, |
| 1303 | Deduced, |
| 1304 | ParamTypes, |
| 1305 | 0, |
| 1306 | Info); |
| 1307 | if (Result) |
| 1308 | return Result; |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1309 | } else { |
| 1310 | // Just fill in the parameter types from the function declaration. |
| 1311 | for (unsigned I = 0; I != CheckArgs; ++I) |
| 1312 | ParamTypes.push_back(Function->getParamDecl(I)->getType()); |
| 1313 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1314 | |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1315 | // Deduce template arguments from the function parameters. |
| 1316 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1317 | for (unsigned I = 0; I != CheckArgs; ++I) { |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1318 | QualType ParamType = ParamTypes[I]; |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1319 | QualType ArgType = Args[I]->getType(); |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1320 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1321 | // C++ [temp.deduct.call]p2: |
| 1322 | // If P is not a reference type: |
| 1323 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1324 | bool ParamWasReference = isa<ReferenceType>(CanonParamType); |
| 1325 | if (!ParamWasReference) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1326 | // - If A is an array type, the pointer type produced by the |
| 1327 | // array-to-pointer standard conversion (4.2) is used in place of |
| 1328 | // A for type deduction; otherwise, |
| 1329 | if (ArgType->isArrayType()) |
| 1330 | ArgType = Context.getArrayDecayedType(ArgType); |
| 1331 | // - If A is a function type, the pointer type produced by the |
| 1332 | // function-to-pointer standard conversion (4.3) is used in place |
| 1333 | // of A for type deduction; otherwise, |
| 1334 | else if (ArgType->isFunctionType()) |
| 1335 | ArgType = Context.getPointerType(ArgType); |
| 1336 | else { |
| 1337 | // - If A is a cv-qualified type, the top level cv-qualifiers of A’s |
| 1338 | // type are ignored for type deduction. |
| 1339 | QualType CanonArgType = Context.getCanonicalType(ArgType); |
| 1340 | if (CanonArgType.getCVRQualifiers()) |
| 1341 | ArgType = CanonArgType.getUnqualifiedType(); |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | // C++0x [temp.deduct.call]p3: |
| 1346 | // If P is a cv-qualified type, the top level cv-qualifiers of P’s type |
| 1347 | // are ignored for type deduction. |
| 1348 | if (CanonParamType.getCVRQualifiers()) |
| 1349 | ParamType = CanonParamType.getUnqualifiedType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1350 | if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1351 | // [...] If P is a reference type, the type referred to by P is used |
| 1352 | // for type deduction. |
| 1353 | ParamType = ParamRefType->getPointeeType(); |
| 1354 | |
| 1355 | // [...] If P is of the form T&&, where T is a template parameter, and |
| 1356 | // the argument is an lvalue, the type A& is used in place of A for |
| 1357 | // type deduction. |
| 1358 | if (isa<RValueReferenceType>(ParamRefType) && |
| 1359 | ParamRefType->getAsTemplateTypeParmType() && |
| 1360 | Args[I]->isLvalue(Context) == Expr::LV_Valid) |
| 1361 | ArgType = Context.getLValueReferenceType(ArgType); |
| 1362 | } |
| 1363 | |
| 1364 | // C++0x [temp.deduct.call]p4: |
| 1365 | // In general, the deduction process attempts to find template argument |
| 1366 | // values that will make the deduced A identical to A (after the type A |
| 1367 | // is transformed as described above). [...] |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1368 | unsigned TDF = 0; |
| 1369 | |
| 1370 | // - If the original P is a reference type, the deduced A (i.e., the |
| 1371 | // type referred to by the reference) can be more cv-qualified than |
| 1372 | // the transformed A. |
| 1373 | if (ParamWasReference) |
| 1374 | TDF |= TDF_ParamWithReferenceType; |
| 1375 | // - The transformed A can be another pointer or pointer to member |
| 1376 | // type that can be converted to the deduced A via a qualification |
| 1377 | // conversion (4.4). |
| 1378 | if (ArgType->isPointerType() || ArgType->isMemberPointerType()) |
| 1379 | TDF |= TDF_IgnoreQualifiers; |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1380 | // - If P is a class and P has the form simple-template-id, then the |
| 1381 | // transformed A can be a derived class of the deduced A. Likewise, |
| 1382 | // if P is a pointer to a class of the form simple-template-id, the |
| 1383 | // transformed A can be a pointer to a derived class pointed to by |
| 1384 | // the deduced A. |
| 1385 | if (isSimpleTemplateIdType(ParamType) || |
| 1386 | (isa<PointerType>(ParamType) && |
| 1387 | isSimpleTemplateIdType( |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1388 | ParamType->getAs<PointerType>()->getPointeeType()))) |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1389 | TDF |= TDF_DerivedClass; |
| 1390 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1391 | if (TemplateDeductionResult Result |
| 1392 | = ::DeduceTemplateArguments(Context, TemplateParams, |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1393 | ParamType, ArgType, Info, Deduced, |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1394 | TDF)) |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1395 | return Result; |
| 1396 | |
Douglas Gregor | 8fdc3c4 | 2009-07-07 23:12:18 +0000 | [diff] [blame] | 1397 | // FIXME: C++0x [temp.deduct.call] paragraphs 6-9 deal with function |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1398 | // pointer parameters. |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame^] | 1399 | |
| 1400 | // FIXME: we need to check that the deduced A is the same as A, |
| 1401 | // modulo the various allowed differences. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1402 | } |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame^] | 1403 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1404 | return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
| 1405 | Specialization, Info); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1408 | /// \brief Deduce template arguments when taking the address of a function |
| 1409 | /// template (C++ [temp.deduct.funcaddr]). |
| 1410 | /// |
| 1411 | /// \param FunctionTemplate the function template for which we are performing |
| 1412 | /// template argument deduction. |
| 1413 | /// |
| 1414 | /// \param HasExplicitTemplateArgs whether any template arguments were |
| 1415 | /// explicitly specified. |
| 1416 | /// |
| 1417 | /// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, |
| 1418 | /// the explicitly-specified template arguments. |
| 1419 | /// |
| 1420 | /// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, |
| 1421 | /// the number of explicitly-specified template arguments in |
| 1422 | /// @p ExplicitTemplateArguments. This value may be zero. |
| 1423 | /// |
| 1424 | /// \param ArgFunctionType the function type that will be used as the |
| 1425 | /// "argument" type (A) when performing template argument deduction from the |
| 1426 | /// function template's function type. |
| 1427 | /// |
| 1428 | /// \param Specialization if template argument deduction was successful, |
| 1429 | /// this will be set to the function template specialization produced by |
| 1430 | /// template argument deduction. |
| 1431 | /// |
| 1432 | /// \param Info the argument will be updated to provide additional information |
| 1433 | /// about template argument deduction. |
| 1434 | /// |
| 1435 | /// \returns the result of template argument deduction. |
| 1436 | Sema::TemplateDeductionResult |
| 1437 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
| 1438 | bool HasExplicitTemplateArgs, |
| 1439 | const TemplateArgument *ExplicitTemplateArgs, |
| 1440 | unsigned NumExplicitTemplateArgs, |
| 1441 | QualType ArgFunctionType, |
| 1442 | FunctionDecl *&Specialization, |
| 1443 | TemplateDeductionInfo &Info) { |
| 1444 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
| 1445 | TemplateParameterList *TemplateParams |
| 1446 | = FunctionTemplate->getTemplateParameters(); |
| 1447 | QualType FunctionType = Function->getType(); |
| 1448 | |
| 1449 | // Substitute any explicit template arguments. |
| 1450 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
| 1451 | llvm::SmallVector<QualType, 4> ParamTypes; |
| 1452 | if (HasExplicitTemplateArgs) { |
| 1453 | if (TemplateDeductionResult Result |
| 1454 | = SubstituteExplicitTemplateArguments(FunctionTemplate, |
| 1455 | ExplicitTemplateArgs, |
| 1456 | NumExplicitTemplateArgs, |
| 1457 | Deduced, ParamTypes, |
| 1458 | &FunctionType, Info)) |
| 1459 | return Result; |
| 1460 | } |
| 1461 | |
| 1462 | // Template argument deduction for function templates in a SFINAE context. |
| 1463 | // Trap any errors that might occur. |
| 1464 | SFINAETrap Trap(*this); |
| 1465 | |
| 1466 | // Deduce template arguments from the function type. |
| 1467 | Deduced.resize(TemplateParams->size()); |
| 1468 | if (TemplateDeductionResult Result |
| 1469 | = ::DeduceTemplateArguments(Context, TemplateParams, |
| 1470 | FunctionType, ArgFunctionType, Info, |
| 1471 | Deduced, 0)) |
| 1472 | return Result; |
| 1473 | |
| 1474 | return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, |
| 1475 | Specialization, Info); |
| 1476 | } |
| 1477 | |
Douglas Gregor | 65ec1fd | 2009-08-21 23:19:43 +0000 | [diff] [blame^] | 1478 | /// \brief Deduce template arguments for a templated conversion |
| 1479 | /// function (C++ [temp.deduct.conv]) and, if successful, produce a |
| 1480 | /// conversion function template specialization. |
| 1481 | Sema::TemplateDeductionResult |
| 1482 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
| 1483 | QualType ToType, |
| 1484 | CXXConversionDecl *&Specialization, |
| 1485 | TemplateDeductionInfo &Info) { |
| 1486 | CXXConversionDecl *Conv |
| 1487 | = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()); |
| 1488 | QualType FromType = Conv->getConversionType(); |
| 1489 | |
| 1490 | // Canonicalize the types for deduction. |
| 1491 | QualType P = Context.getCanonicalType(FromType); |
| 1492 | QualType A = Context.getCanonicalType(ToType); |
| 1493 | |
| 1494 | // C++0x [temp.deduct.conv]p3: |
| 1495 | // If P is a reference type, the type referred to by P is used for |
| 1496 | // type deduction. |
| 1497 | if (const ReferenceType *PRef = P->getAs<ReferenceType>()) |
| 1498 | P = PRef->getPointeeType(); |
| 1499 | |
| 1500 | // C++0x [temp.deduct.conv]p3: |
| 1501 | // If A is a reference type, the type referred to by A is used |
| 1502 | // for type deduction. |
| 1503 | if (const ReferenceType *ARef = A->getAs<ReferenceType>()) |
| 1504 | A = ARef->getPointeeType(); |
| 1505 | // C++ [temp.deduct.conv]p2: |
| 1506 | // |
| 1507 | // If A is not a reference type: |
| 1508 | else { |
| 1509 | assert(!A->isReferenceType() && "Reference types were handled above"); |
| 1510 | |
| 1511 | // - If P is an array type, the pointer type produced by the |
| 1512 | // array-to-pointer standard conversion (4.2) is used in place |
| 1513 | // of P for type deduction; otherwise, |
| 1514 | if (P->isArrayType()) |
| 1515 | P = Context.getArrayDecayedType(P); |
| 1516 | // - If P is a function type, the pointer type produced by the |
| 1517 | // function-to-pointer standard conversion (4.3) is used in |
| 1518 | // place of P for type deduction; otherwise, |
| 1519 | else if (P->isFunctionType()) |
| 1520 | P = Context.getPointerType(P); |
| 1521 | // - If P is a cv-qualified type, the top level cv-qualifiers of |
| 1522 | // P’s type are ignored for type deduction. |
| 1523 | else |
| 1524 | P = P.getUnqualifiedType(); |
| 1525 | |
| 1526 | // C++0x [temp.deduct.conv]p3: |
| 1527 | // If A is a cv-qualified type, the top level cv-qualifiers of A’s |
| 1528 | // type are ignored for type deduction. |
| 1529 | A = A.getUnqualifiedType(); |
| 1530 | } |
| 1531 | |
| 1532 | // Template argument deduction for function templates in a SFINAE context. |
| 1533 | // Trap any errors that might occur. |
| 1534 | SFINAETrap Trap(*this); |
| 1535 | |
| 1536 | // C++ [temp.deduct.conv]p1: |
| 1537 | // Template argument deduction is done by comparing the return |
| 1538 | // type of the template conversion function (call it P) with the |
| 1539 | // type that is required as the result of the conversion (call it |
| 1540 | // A) as described in 14.8.2.4. |
| 1541 | TemplateParameterList *TemplateParams |
| 1542 | = FunctionTemplate->getTemplateParameters(); |
| 1543 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
| 1544 | Deduced.resize(TemplateParams->size()); |
| 1545 | |
| 1546 | // C++0x [temp.deduct.conv]p4: |
| 1547 | // In general, the deduction process attempts to find template |
| 1548 | // argument values that will make the deduced A identical to |
| 1549 | // A. However, there are two cases that allow a difference: |
| 1550 | unsigned TDF = 0; |
| 1551 | // - If the original A is a reference type, A can be more |
| 1552 | // cv-qualified than the deduced A (i.e., the type referred to |
| 1553 | // by the reference) |
| 1554 | if (ToType->isReferenceType()) |
| 1555 | TDF |= TDF_ParamWithReferenceType; |
| 1556 | // - The deduced A can be another pointer or pointer to member |
| 1557 | // type that can be converted to A via a qualification |
| 1558 | // conversion. |
| 1559 | // |
| 1560 | // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when |
| 1561 | // both P and A are pointers or member pointers. In this case, we |
| 1562 | // just ignore cv-qualifiers completely). |
| 1563 | if ((P->isPointerType() && A->isPointerType()) || |
| 1564 | (P->isMemberPointerType() && P->isMemberPointerType())) |
| 1565 | TDF |= TDF_IgnoreQualifiers; |
| 1566 | if (TemplateDeductionResult Result |
| 1567 | = ::DeduceTemplateArguments(Context, TemplateParams, |
| 1568 | P, A, Info, Deduced, TDF)) |
| 1569 | return Result; |
| 1570 | |
| 1571 | // FIXME: we need to check that the deduced A is the same as A, |
| 1572 | // modulo the various allowed differences. |
| 1573 | |
| 1574 | // Finish template argument deduction. |
| 1575 | FunctionDecl *Spec = 0; |
| 1576 | TemplateDeductionResult Result |
| 1577 | = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, Spec, Info); |
| 1578 | Specialization = cast_or_null<CXXConversionDecl>(Spec); |
| 1579 | return Result; |
| 1580 | } |
| 1581 | |
| 1582 | /// \brief Returns the more specialization function template according |
| 1583 | /// to the rules of function template partial ordering (C++ [temp.func.order]). |
| 1584 | /// |
| 1585 | /// \param FT1 the first function template |
| 1586 | /// |
| 1587 | /// \param FT2 the second function template |
| 1588 | /// |
| 1589 | /// \param isCallContext whether partial ordering is being performed |
| 1590 | /// for a function call (which ignores the return types of the |
| 1591 | /// functions). |
| 1592 | /// |
| 1593 | /// \returns the more specialization function template. If neither |
| 1594 | /// template is more specialized, returns NULL. |
| 1595 | FunctionTemplateDecl * |
| 1596 | Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, |
| 1597 | FunctionTemplateDecl *FT2, |
| 1598 | bool isCallContext) { |
| 1599 | #if 0 |
| 1600 | // FIXME: Implement this |
| 1601 | bool Better1 = isAtLeastAsSpecializedAs(*this, FT1, FT2, isCallContext); |
| 1602 | bool Better2 = isAtLeastAsSpecializedAs(*this, FT2, FT1, isCallContext); |
| 1603 | if (Better1 == Better2) |
| 1604 | return 0; |
| 1605 | if (Better1) |
| 1606 | return FT1; |
| 1607 | return FT2; |
| 1608 | #else |
| 1609 | Diag(SourceLocation(), diag::unsup_function_template_partial_ordering); |
| 1610 | return 0; |
| 1611 | #endif |
| 1612 | } |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 1613 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1614 | static void |
| 1615 | MarkDeducedTemplateParameters(Sema &SemaRef, |
| 1616 | const TemplateArgument &TemplateArg, |
| 1617 | llvm::SmallVectorImpl<bool> &Deduced); |
| 1618 | |
| 1619 | /// \brief Mark the template arguments that are deduced by the given |
| 1620 | /// expression. |
| 1621 | static void |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1622 | MarkDeducedTemplateParameters(const Expr *E, |
| 1623 | llvm::SmallVectorImpl<bool> &Deduced) { |
| 1624 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1625 | if (!E) |
| 1626 | return; |
| 1627 | |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1628 | const NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1629 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 1630 | if (!NTTP) |
| 1631 | return; |
| 1632 | |
| 1633 | Deduced[NTTP->getIndex()] = true; |
| 1634 | } |
| 1635 | |
| 1636 | /// \brief Mark the template parameters that are deduced by the given |
| 1637 | /// type. |
| 1638 | static void |
| 1639 | MarkDeducedTemplateParameters(Sema &SemaRef, QualType T, |
| 1640 | llvm::SmallVectorImpl<bool> &Deduced) { |
| 1641 | // Non-dependent types have nothing deducible |
| 1642 | if (!T->isDependentType()) |
| 1643 | return; |
| 1644 | |
| 1645 | T = SemaRef.Context.getCanonicalType(T); |
| 1646 | switch (T->getTypeClass()) { |
| 1647 | case Type::ExtQual: |
| 1648 | MarkDeducedTemplateParameters(SemaRef, |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1649 | QualType(cast<ExtQualType>(T)->getBaseType(), 0), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1650 | Deduced); |
| 1651 | break; |
| 1652 | |
| 1653 | case Type::Pointer: |
| 1654 | MarkDeducedTemplateParameters(SemaRef, |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1655 | cast<PointerType>(T)->getPointeeType(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1656 | Deduced); |
| 1657 | break; |
| 1658 | |
| 1659 | case Type::BlockPointer: |
| 1660 | MarkDeducedTemplateParameters(SemaRef, |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1661 | cast<BlockPointerType>(T)->getPointeeType(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1662 | Deduced); |
| 1663 | break; |
| 1664 | |
| 1665 | case Type::LValueReference: |
| 1666 | case Type::RValueReference: |
| 1667 | MarkDeducedTemplateParameters(SemaRef, |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1668 | cast<ReferenceType>(T)->getPointeeType(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1669 | Deduced); |
| 1670 | break; |
| 1671 | |
| 1672 | case Type::MemberPointer: { |
| 1673 | const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); |
| 1674 | MarkDeducedTemplateParameters(SemaRef, MemPtr->getPointeeType(), Deduced); |
| 1675 | MarkDeducedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0), |
| 1676 | Deduced); |
| 1677 | break; |
| 1678 | } |
| 1679 | |
| 1680 | case Type::DependentSizedArray: |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1681 | MarkDeducedTemplateParameters(cast<DependentSizedArrayType>(T)->getSizeExpr(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1682 | Deduced); |
| 1683 | // Fall through to check the element type |
| 1684 | |
| 1685 | case Type::ConstantArray: |
| 1686 | case Type::IncompleteArray: |
| 1687 | MarkDeducedTemplateParameters(SemaRef, |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1688 | cast<ArrayType>(T)->getElementType(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1689 | Deduced); |
| 1690 | break; |
| 1691 | |
| 1692 | case Type::Vector: |
| 1693 | case Type::ExtVector: |
| 1694 | MarkDeducedTemplateParameters(SemaRef, |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1695 | cast<VectorType>(T)->getElementType(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1696 | Deduced); |
| 1697 | break; |
| 1698 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1699 | case Type::DependentSizedExtVector: { |
| 1700 | const DependentSizedExtVectorType *VecType |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1701 | = cast<DependentSizedExtVectorType>(T); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1702 | MarkDeducedTemplateParameters(SemaRef, VecType->getElementType(), Deduced); |
| 1703 | MarkDeducedTemplateParameters(VecType->getSizeExpr(), Deduced); |
| 1704 | break; |
| 1705 | } |
| 1706 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1707 | case Type::FunctionProto: { |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1708 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1709 | MarkDeducedTemplateParameters(SemaRef, Proto->getResultType(), Deduced); |
| 1710 | for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I) |
| 1711 | MarkDeducedTemplateParameters(SemaRef, Proto->getArgType(I), Deduced); |
| 1712 | break; |
| 1713 | } |
| 1714 | |
| 1715 | case Type::TemplateTypeParm: |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1716 | Deduced[cast<TemplateTypeParmType>(T)->getIndex()] = true; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1717 | break; |
| 1718 | |
| 1719 | case Type::TemplateSpecialization: { |
| 1720 | const TemplateSpecializationType *Spec |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1721 | = cast<TemplateSpecializationType>(T); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1722 | if (TemplateDecl *Template = Spec->getTemplateName().getAsTemplateDecl()) |
| 1723 | if (TemplateTemplateParmDecl *TTP |
| 1724 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
| 1725 | Deduced[TTP->getIndex()] = true; |
| 1726 | |
| 1727 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
| 1728 | MarkDeducedTemplateParameters(SemaRef, Spec->getArg(I), Deduced); |
| 1729 | |
| 1730 | break; |
| 1731 | } |
| 1732 | |
| 1733 | // None of these types have any deducible parts. |
| 1734 | case Type::Builtin: |
| 1735 | case Type::FixedWidthInt: |
| 1736 | case Type::Complex: |
| 1737 | case Type::VariableArray: |
| 1738 | case Type::FunctionNoProto: |
| 1739 | case Type::Record: |
| 1740 | case Type::Enum: |
| 1741 | case Type::Typename: |
| 1742 | case Type::ObjCInterface: |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1743 | case Type::ObjCObjectPointer: |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1744 | #define TYPE(Class, Base) |
| 1745 | #define ABSTRACT_TYPE(Class, Base) |
| 1746 | #define DEPENDENT_TYPE(Class, Base) |
| 1747 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 1748 | #include "clang/AST/TypeNodes.def" |
| 1749 | break; |
| 1750 | } |
| 1751 | } |
| 1752 | |
| 1753 | /// \brief Mark the template parameters that are deduced by this |
| 1754 | /// template argument. |
| 1755 | static void |
| 1756 | MarkDeducedTemplateParameters(Sema &SemaRef, |
| 1757 | const TemplateArgument &TemplateArg, |
| 1758 | llvm::SmallVectorImpl<bool> &Deduced) { |
| 1759 | switch (TemplateArg.getKind()) { |
| 1760 | case TemplateArgument::Null: |
| 1761 | case TemplateArgument::Integral: |
| 1762 | break; |
| 1763 | |
| 1764 | case TemplateArgument::Type: |
| 1765 | MarkDeducedTemplateParameters(SemaRef, TemplateArg.getAsType(), Deduced); |
| 1766 | break; |
| 1767 | |
| 1768 | case TemplateArgument::Declaration: |
| 1769 | if (TemplateTemplateParmDecl *TTP |
| 1770 | = dyn_cast<TemplateTemplateParmDecl>(TemplateArg.getAsDecl())) |
| 1771 | Deduced[TTP->getIndex()] = true; |
| 1772 | break; |
| 1773 | |
| 1774 | case TemplateArgument::Expression: |
| 1775 | MarkDeducedTemplateParameters(TemplateArg.getAsExpr(), Deduced); |
| 1776 | break; |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 1777 | case TemplateArgument::Pack: |
| 1778 | assert(0 && "FIXME: Implement!"); |
| 1779 | break; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | /// \brief Mark the template parameters can be deduced by the given |
| 1784 | /// template argument list. |
| 1785 | /// |
| 1786 | /// \param TemplateArgs the template argument list from which template |
| 1787 | /// parameters will be deduced. |
| 1788 | /// |
| 1789 | /// \param Deduced a bit vector whose elements will be set to \c true |
| 1790 | /// to indicate when the corresponding template parameter will be |
| 1791 | /// deduced. |
| 1792 | void |
| 1793 | Sema::MarkDeducedTemplateParameters(const TemplateArgumentList &TemplateArgs, |
| 1794 | llvm::SmallVectorImpl<bool> &Deduced) { |
| 1795 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 1796 | ::MarkDeducedTemplateParameters(*this, TemplateArgs[I], Deduced); |
| 1797 | } |