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 | |
| 173 | ParamDecl = cast<TemplateDecl>(Context.getCanonicalDecl(ParamDecl)); |
| 174 | ArgDecl = cast<TemplateDecl>(Context.getCanonicalDecl(ArgDecl)); |
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 | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 286 | /// \brief Deduce the template arguments by comparing the parameter type and |
| 287 | /// the argument type (C++ [temp.deduct.type]). |
| 288 | /// |
| 289 | /// \param Context the AST context in which this deduction occurs. |
| 290 | /// |
| 291 | /// \param TemplateParams the template parameters that we are deducing |
| 292 | /// |
| 293 | /// \param ParamIn the parameter type |
| 294 | /// |
| 295 | /// \param ArgIn the argument type |
| 296 | /// |
| 297 | /// \param Info information about the template argument deduction itself |
| 298 | /// |
| 299 | /// \param Deduced the deduced template arguments |
| 300 | /// |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 301 | /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe |
| 302 | /// how template argument deduction is performed. |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 303 | /// |
| 304 | /// \returns the result of template argument deduction so far. Note that a |
| 305 | /// "success" result means that template argument deduction has not yet failed, |
| 306 | /// but it may still fail, later, for other reasons. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 307 | static Sema::TemplateDeductionResult |
| 308 | DeduceTemplateArguments(ASTContext &Context, |
| 309 | TemplateParameterList *TemplateParams, |
| 310 | QualType ParamIn, QualType ArgIn, |
| 311 | Sema::TemplateDeductionInfo &Info, |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 312 | llvm::SmallVectorImpl<TemplateArgument> &Deduced, |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 313 | unsigned TDF) { |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 314 | // We only want to look at the canonical types, since typedefs and |
| 315 | // sugar are not part of template argument deduction. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 316 | QualType Param = Context.getCanonicalType(ParamIn); |
| 317 | QualType Arg = Context.getCanonicalType(ArgIn); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 318 | |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 319 | // C++0x [temp.deduct.call]p4 bullet 1: |
| 320 | // - If the original P is a reference type, the deduced A (i.e., the type |
| 321 | // referred to by the reference) can be more cv-qualified than the |
| 322 | // transformed A. |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 323 | if (TDF & TDF_ParamWithReferenceType) { |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 324 | unsigned ExtraQualsOnParam |
| 325 | = Param.getCVRQualifiers() & ~Arg.getCVRQualifiers(); |
| 326 | Param.setCVRQualifiers(Param.getCVRQualifiers() & ~ExtraQualsOnParam); |
| 327 | } |
| 328 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 329 | // If the parameter type is not dependent, there is nothing to deduce. |
| 330 | if (!Param->isDependentType()) |
| 331 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 332 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 333 | // C++ [temp.deduct.type]p9: |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 334 | // A template type argument T, a template template argument TT or a |
| 335 | // template non-type argument i can be deduced if P and A have one of |
| 336 | // the following forms: |
| 337 | // |
| 338 | // T |
| 339 | // cv-list T |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 340 | if (const TemplateTypeParmType *TemplateTypeParm |
| 341 | = Param->getAsTemplateTypeParmType()) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 342 | unsigned Index = TemplateTypeParm->getIndex(); |
| 343 | |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 344 | // The argument type can not be less qualified than the parameter |
| 345 | // type. |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 346 | if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 347 | Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 348 | Info.FirstArg = Deduced[Index]; |
| 349 | Info.SecondArg = TemplateArgument(SourceLocation(), Arg); |
| 350 | return Sema::TDK_InconsistentQuals; |
| 351 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 352 | |
| 353 | assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0"); |
| 354 | |
| 355 | unsigned Quals = Arg.getCVRQualifiers() & ~Param.getCVRQualifiers(); |
| 356 | QualType DeducedType = Arg.getQualifiedType(Quals); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 357 | |
| 358 | if (Deduced[Index].isNull()) |
| 359 | Deduced[Index] = TemplateArgument(SourceLocation(), DeducedType); |
| 360 | else { |
| 361 | // C++ [temp.deduct.type]p2: |
| 362 | // [...] If type deduction cannot be done for any P/A pair, or if for |
| 363 | // any pair the deduction leads to more than one possible set of |
| 364 | // deduced values, or if different pairs yield different deduced |
| 365 | // values, or if any template argument remains neither deduced nor |
| 366 | // explicitly specified, template argument deduction fails. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 367 | if (Deduced[Index].getAsType() != DeducedType) { |
| 368 | Info.Param |
| 369 | = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); |
| 370 | Info.FirstArg = Deduced[Index]; |
| 371 | Info.SecondArg = TemplateArgument(SourceLocation(), Arg); |
| 372 | return Sema::TDK_Inconsistent; |
| 373 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 374 | } |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 375 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 378 | // Set up the template argument deduction information for a failure. |
| 379 | Info.FirstArg = TemplateArgument(SourceLocation(), ParamIn); |
| 380 | Info.SecondArg = TemplateArgument(SourceLocation(), ArgIn); |
| 381 | |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 382 | // Check the cv-qualifiers on the parameter and argument types. |
| 383 | if (!(TDF & TDF_IgnoreQualifiers)) { |
| 384 | if (TDF & TDF_ParamWithReferenceType) { |
| 385 | if (Param.isMoreQualifiedThan(Arg)) |
| 386 | return Sema::TDK_NonDeducedMismatch; |
| 387 | } else { |
| 388 | if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) |
| 389 | return Sema::TDK_NonDeducedMismatch; |
| 390 | } |
| 391 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 392 | |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 393 | switch (Param->getTypeClass()) { |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 394 | // No deduction possible for these types |
| 395 | case Type::Builtin: |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 396 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 397 | |
| 398 | // T * |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 399 | case Type::Pointer: { |
| 400 | const PointerType *PointerArg = Arg->getAsPointerType(); |
| 401 | if (!PointerArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 402 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 403 | |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 404 | unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 405 | return DeduceTemplateArguments(Context, TemplateParams, |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 406 | cast<PointerType>(Param)->getPointeeType(), |
| 407 | PointerArg->getPointeeType(), |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 408 | Info, Deduced, SubTDF); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 411 | // T & |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 412 | case Type::LValueReference: { |
| 413 | const LValueReferenceType *ReferenceArg = Arg->getAsLValueReferenceType(); |
| 414 | if (!ReferenceArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 415 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 416 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 417 | return DeduceTemplateArguments(Context, TemplateParams, |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 418 | cast<LValueReferenceType>(Param)->getPointeeType(), |
| 419 | ReferenceArg->getPointeeType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 420 | Info, Deduced, 0); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 421 | } |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 422 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 423 | // T && [C++0x] |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 424 | case Type::RValueReference: { |
| 425 | const RValueReferenceType *ReferenceArg = Arg->getAsRValueReferenceType(); |
| 426 | if (!ReferenceArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 427 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 428 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 429 | return DeduceTemplateArguments(Context, TemplateParams, |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 430 | cast<RValueReferenceType>(Param)->getPointeeType(), |
| 431 | ReferenceArg->getPointeeType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 432 | Info, Deduced, 0); |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 435 | // T [] (implied, but not stated explicitly) |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 436 | case Type::IncompleteArray: { |
| 437 | const IncompleteArrayType *IncompleteArrayArg = |
| 438 | Context.getAsIncompleteArrayType(Arg); |
| 439 | if (!IncompleteArrayArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 440 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 441 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 442 | return DeduceTemplateArguments(Context, TemplateParams, |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 443 | Context.getAsIncompleteArrayType(Param)->getElementType(), |
| 444 | IncompleteArrayArg->getElementType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 445 | Info, Deduced, 0); |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 446 | } |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 447 | |
| 448 | // T [integer-constant] |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 449 | case Type::ConstantArray: { |
| 450 | const ConstantArrayType *ConstantArrayArg = |
| 451 | Context.getAsConstantArrayType(Arg); |
| 452 | if (!ConstantArrayArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 453 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 454 | |
| 455 | const ConstantArrayType *ConstantArrayParm = |
| 456 | Context.getAsConstantArrayType(Param); |
| 457 | if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 458 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 459 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 460 | return DeduceTemplateArguments(Context, TemplateParams, |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 461 | ConstantArrayParm->getElementType(), |
| 462 | ConstantArrayArg->getElementType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 463 | Info, Deduced, 0); |
Anders Carlsson | 4d6fb50 | 2009-06-04 04:11:30 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 466 | // type [i] |
| 467 | case Type::DependentSizedArray: { |
| 468 | const ArrayType *ArrayArg = dyn_cast<ArrayType>(Arg); |
| 469 | if (!ArrayArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 470 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 471 | |
| 472 | // Check the element type of the arrays |
| 473 | const DependentSizedArrayType *DependentArrayParm |
| 474 | = cast<DependentSizedArrayType>(Param); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 475 | if (Sema::TemplateDeductionResult Result |
| 476 | = DeduceTemplateArguments(Context, TemplateParams, |
| 477 | DependentArrayParm->getElementType(), |
| 478 | ArrayArg->getElementType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 479 | Info, Deduced, 0)) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 480 | return Result; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 481 | |
| 482 | // Determine the array bound is something we can deduce. |
| 483 | NonTypeTemplateParmDecl *NTTP |
| 484 | = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr()); |
| 485 | if (!NTTP) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 486 | return Sema::TDK_Success; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 487 | |
| 488 | // We can perform template argument deduction for the given non-type |
| 489 | // template parameter. |
| 490 | assert(NTTP->getDepth() == 0 && |
| 491 | "Cannot deduce non-type template argument at depth > 0"); |
| 492 | if (const ConstantArrayType *ConstantArrayArg |
Anders Carlsson | 335e24a | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 493 | = dyn_cast<ConstantArrayType>(ArrayArg)) { |
| 494 | llvm::APSInt Size(ConstantArrayArg->getSize()); |
| 495 | return DeduceNonTypeTemplateArgument(Context, NTTP, Size, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 496 | Info, Deduced); |
Anders Carlsson | 335e24a | 2009-06-16 22:44:31 +0000 | [diff] [blame] | 497 | } |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 498 | if (const DependentSizedArrayType *DependentArrayArg |
| 499 | = dyn_cast<DependentSizedArrayType>(ArrayArg)) |
| 500 | return DeduceNonTypeTemplateArgument(Context, NTTP, |
| 501 | DependentArrayArg->getSizeExpr(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 502 | Info, Deduced); |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 503 | |
| 504 | // Incomplete type does not match a dependently-sized array type |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 505 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Douglas Gregor | 0fce0ae | 2009-06-08 15:59:14 +0000 | [diff] [blame] | 508 | // type(*)(T) |
| 509 | // T(*)() |
| 510 | // T(*)(T) |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 511 | case Type::FunctionProto: { |
| 512 | const FunctionProtoType *FunctionProtoArg = |
| 513 | dyn_cast<FunctionProtoType>(Arg); |
| 514 | if (!FunctionProtoArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 515 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 516 | |
| 517 | const FunctionProtoType *FunctionProtoParam = |
| 518 | cast<FunctionProtoType>(Param); |
Anders Carlsson | 994b6cb | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 519 | |
| 520 | if (FunctionProtoParam->getTypeQuals() != |
| 521 | FunctionProtoArg->getTypeQuals()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 522 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 523 | |
Anders Carlsson | 994b6cb | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 524 | if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 525 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 994b6cb | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 526 | |
| 527 | if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 528 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 994b6cb | 2009-06-08 19:22:23 +0000 | [diff] [blame] | 529 | |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 530 | // Check return types. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 531 | if (Sema::TemplateDeductionResult Result |
| 532 | = DeduceTemplateArguments(Context, TemplateParams, |
| 533 | FunctionProtoParam->getResultType(), |
| 534 | FunctionProtoArg->getResultType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 535 | Info, Deduced, 0)) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 536 | return Result; |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 537 | |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 538 | for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) { |
| 539 | // Check argument types. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 540 | if (Sema::TemplateDeductionResult Result |
| 541 | = DeduceTemplateArguments(Context, TemplateParams, |
| 542 | FunctionProtoParam->getArgType(I), |
| 543 | FunctionProtoArg->getArgType(I), |
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; |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 548 | return Sema::TDK_Success; |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 549 | } |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 550 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 551 | // template-name<T> (where template-name refers to a class template) |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 552 | // template-name<i> |
| 553 | // TT<T> (TODO) |
| 554 | // TT<i> (TODO) |
| 555 | // TT<> (TODO) |
| 556 | case Type::TemplateSpecialization: { |
| 557 | const TemplateSpecializationType *SpecParam |
| 558 | = cast<TemplateSpecializationType>(Param); |
Anders Carlsson | a27fad5 | 2009-06-08 15:19:08 +0000 | [diff] [blame] | 559 | |
Douglas Gregor | de0cb8b | 2009-07-07 23:09:34 +0000 | [diff] [blame^] | 560 | // Try to deduce template arguments from the template-id. |
| 561 | Sema::TemplateDeductionResult Result |
| 562 | = DeduceTemplateArguments(Context, TemplateParams, SpecParam, Arg, |
| 563 | Info, Deduced); |
| 564 | |
| 565 | if (Result && (TDF & TDF_DerivedClass) && |
| 566 | Result != Sema::TDK_Inconsistent) { |
| 567 | // C++ [temp.deduct.call]p3b3: |
| 568 | // If P is a class, and P has the form template-id, then A can be a |
| 569 | // derived class of the deduced A. Likewise, if P is a pointer to a |
| 570 | // class of the form template-id, A can be a pointer to a derived |
| 571 | // class pointed to by the deduced A. |
| 572 | // |
| 573 | // More importantly: |
| 574 | // These alternatives are considered only if type deduction would |
| 575 | // otherwise fail. |
| 576 | if (const RecordType *RecordT = dyn_cast<RecordType>(Arg)) { |
| 577 | // Use data recursion to crawl through the list of base classes. |
| 578 | // Visited contains the set of nodes we have already visited, while |
| 579 | // ToVisit is our stack of records that we still need to visit. |
| 580 | llvm::SmallPtrSet<const RecordType *, 8> Visited; |
| 581 | llvm::SmallVector<const RecordType *, 8> ToVisit; |
| 582 | ToVisit.push_back(RecordT); |
| 583 | bool Successful = false; |
| 584 | while (!ToVisit.empty()) { |
| 585 | // Retrieve the next class in the inheritance hierarchy. |
| 586 | const RecordType *NextT = ToVisit.back(); |
| 587 | ToVisit.pop_back(); |
| 588 | |
| 589 | // If we have already seen this type, skip it. |
| 590 | if (!Visited.insert(NextT)) |
| 591 | continue; |
| 592 | |
| 593 | // If this is a base class, try to perform template argument |
| 594 | // deduction from it. |
| 595 | if (NextT != RecordT) { |
| 596 | Sema::TemplateDeductionResult BaseResult |
| 597 | = DeduceTemplateArguments(Context, TemplateParams, SpecParam, |
| 598 | QualType(NextT, 0), Info, Deduced); |
| 599 | |
| 600 | // If template argument deduction for this base was successful, |
| 601 | // note that we had some success. |
| 602 | if (BaseResult == Sema::TDK_Success) |
| 603 | Successful = true; |
| 604 | // If deduction against this base resulted in an inconsistent |
| 605 | // set of deduced template arguments, template argument |
| 606 | // deduction fails. |
| 607 | else if (BaseResult == Sema::TDK_Inconsistent) |
| 608 | return BaseResult; |
| 609 | } |
| 610 | |
| 611 | // Visit base classes |
| 612 | CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl()); |
| 613 | for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(), |
| 614 | BaseEnd = Next->bases_end(); |
| 615 | Base != BaseEnd; ++Base) { |
| 616 | assert(Base->getType()->isRecordType() && |
| 617 | "Base class that isn't a record?"); |
| 618 | ToVisit.push_back(Base->getType()->getAsRecordType()); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | if (Successful) |
| 623 | return Sema::TDK_Success; |
| 624 | } |
| 625 | |
| 626 | } |
| 627 | |
| 628 | return Result; |
Douglas Gregor | d708c72 | 2009-06-09 16:35:58 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 631 | // T type::* |
| 632 | // T T::* |
| 633 | // T (type::*)() |
| 634 | // type (T::*)() |
| 635 | // type (type::*)(T) |
| 636 | // type (T::*)(T) |
| 637 | // T (type::*)(T) |
| 638 | // T (T::*)() |
| 639 | // T (T::*)(T) |
| 640 | case Type::MemberPointer: { |
| 641 | const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); |
| 642 | const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); |
| 643 | if (!MemPtrArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 644 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 645 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 646 | if (Sema::TemplateDeductionResult Result |
| 647 | = DeduceTemplateArguments(Context, TemplateParams, |
| 648 | MemPtrParam->getPointeeType(), |
| 649 | MemPtrArg->getPointeeType(), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 650 | Info, Deduced, |
| 651 | TDF & TDF_IgnoreQualifiers)) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 652 | return Result; |
| 653 | |
| 654 | return DeduceTemplateArguments(Context, TemplateParams, |
| 655 | QualType(MemPtrParam->getClass(), 0), |
| 656 | QualType(MemPtrArg->getClass(), 0), |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 657 | Info, Deduced, 0); |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Anders Carlsson | 9a917e4 | 2009-06-12 22:56:54 +0000 | [diff] [blame] | 660 | // (clang extension) |
| 661 | // |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 662 | // type(^)(T) |
| 663 | // T(^)() |
| 664 | // T(^)(T) |
| 665 | case Type::BlockPointer: { |
| 666 | const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); |
| 667 | const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); |
| 668 | |
| 669 | if (!BlockPtrArg) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 670 | return Sema::TDK_NonDeducedMismatch; |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 671 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 672 | return DeduceTemplateArguments(Context, TemplateParams, |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 673 | BlockPtrParam->getPointeeType(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 674 | BlockPtrArg->getPointeeType(), Info, |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 675 | Deduced, 0); |
Anders Carlsson | 859ba50 | 2009-06-12 16:23:10 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 678 | case Type::TypeOfExpr: |
| 679 | case Type::TypeOf: |
| 680 | case Type::Typename: |
| 681 | // No template argument deduction for these types |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 682 | return Sema::TDK_Success; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 683 | |
Douglas Gregor | d560d50 | 2009-06-04 00:21:18 +0000 | [diff] [blame] | 684 | default: |
| 685 | break; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | // FIXME: Many more cases to go (to go). |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 689 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 692 | static Sema::TemplateDeductionResult |
| 693 | DeduceTemplateArguments(ASTContext &Context, |
| 694 | TemplateParameterList *TemplateParams, |
| 695 | const TemplateArgument &Param, |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 696 | const TemplateArgument &Arg, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 697 | Sema::TemplateDeductionInfo &Info, |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 698 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 699 | switch (Param.getKind()) { |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 700 | case TemplateArgument::Null: |
| 701 | assert(false && "Null template argument in parameter list"); |
| 702 | break; |
| 703 | |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 704 | case TemplateArgument::Type: |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 705 | assert(Arg.getKind() == TemplateArgument::Type && "Type/value mismatch"); |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 706 | return DeduceTemplateArguments(Context, TemplateParams, Param.getAsType(), |
| 707 | Arg.getAsType(), Info, Deduced, 0); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 708 | |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 709 | case TemplateArgument::Declaration: |
| 710 | // FIXME: Implement this check |
| 711 | assert(false && "Unimplemented template argument deduction case"); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 712 | Info.FirstArg = Param; |
| 713 | Info.SecondArg = Arg; |
| 714 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 715 | |
| 716 | case TemplateArgument::Integral: |
| 717 | if (Arg.getKind() == TemplateArgument::Integral) { |
| 718 | // FIXME: Zero extension + sign checking here? |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 719 | if (*Param.getAsIntegral() == *Arg.getAsIntegral()) |
| 720 | return Sema::TDK_Success; |
| 721 | |
| 722 | Info.FirstArg = Param; |
| 723 | Info.SecondArg = Arg; |
| 724 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 725 | } |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 726 | |
| 727 | if (Arg.getKind() == TemplateArgument::Expression) { |
| 728 | Info.FirstArg = Param; |
| 729 | Info.SecondArg = Arg; |
| 730 | return Sema::TDK_NonDeducedMismatch; |
| 731 | } |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 732 | |
| 733 | assert(false && "Type/value mismatch"); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 734 | Info.FirstArg = Param; |
| 735 | Info.SecondArg = Arg; |
| 736 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 737 | |
| 738 | case TemplateArgument::Expression: { |
| 739 | if (NonTypeTemplateParmDecl *NTTP |
| 740 | = getDeducedParameterFromExpr(Param.getAsExpr())) { |
| 741 | if (Arg.getKind() == TemplateArgument::Integral) |
| 742 | // FIXME: Sign problems here |
| 743 | return DeduceNonTypeTemplateArgument(Context, NTTP, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 744 | *Arg.getAsIntegral(), |
| 745 | Info, Deduced); |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 746 | if (Arg.getKind() == TemplateArgument::Expression) |
| 747 | return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(), |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 748 | Info, Deduced); |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 749 | |
| 750 | assert(false && "Type/value mismatch"); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 751 | Info.FirstArg = Param; |
| 752 | Info.SecondArg = Arg; |
| 753 | return Sema::TDK_NonDeducedMismatch; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 754 | } |
| 755 | |
| 756 | // Can't deduce anything, but that's okay. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 757 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 758 | } |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 759 | case TemplateArgument::Pack: |
| 760 | assert(0 && "FIXME: Implement!"); |
| 761 | break; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 764 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 767 | static Sema::TemplateDeductionResult |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 768 | DeduceTemplateArguments(ASTContext &Context, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 769 | TemplateParameterList *TemplateParams, |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 770 | const TemplateArgumentList &ParamList, |
| 771 | const TemplateArgumentList &ArgList, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 772 | Sema::TemplateDeductionInfo &Info, |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 773 | llvm::SmallVectorImpl<TemplateArgument> &Deduced) { |
| 774 | assert(ParamList.size() == ArgList.size()); |
| 775 | for (unsigned I = 0, N = ParamList.size(); I != N; ++I) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 776 | if (Sema::TemplateDeductionResult Result |
| 777 | = DeduceTemplateArguments(Context, TemplateParams, |
| 778 | ParamList[I], ArgList[I], |
| 779 | Info, Deduced)) |
| 780 | return Result; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 781 | } |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 782 | return Sema::TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 785 | /// \brief Determine whether two template arguments are the same. |
| 786 | static bool isSameTemplateArg(ASTContext &Context, |
| 787 | const TemplateArgument &X, |
| 788 | const TemplateArgument &Y) { |
| 789 | if (X.getKind() != Y.getKind()) |
| 790 | return false; |
| 791 | |
| 792 | switch (X.getKind()) { |
| 793 | case TemplateArgument::Null: |
| 794 | assert(false && "Comparing NULL template argument"); |
| 795 | break; |
| 796 | |
| 797 | case TemplateArgument::Type: |
| 798 | return Context.getCanonicalType(X.getAsType()) == |
| 799 | Context.getCanonicalType(Y.getAsType()); |
| 800 | |
| 801 | case TemplateArgument::Declaration: |
| 802 | return Context.getCanonicalDecl(X.getAsDecl()) == |
| 803 | Context.getCanonicalDecl(Y.getAsDecl()); |
| 804 | |
| 805 | case TemplateArgument::Integral: |
| 806 | return *X.getAsIntegral() == *Y.getAsIntegral(); |
| 807 | |
| 808 | case TemplateArgument::Expression: |
| 809 | // FIXME: We assume that all expressions are distinct, but we should |
| 810 | // really check their canonical forms. |
| 811 | return false; |
| 812 | |
| 813 | case TemplateArgument::Pack: |
| 814 | if (X.pack_size() != Y.pack_size()) |
| 815 | return false; |
| 816 | |
| 817 | for (TemplateArgument::pack_iterator XP = X.pack_begin(), |
| 818 | XPEnd = X.pack_end(), |
| 819 | YP = Y.pack_begin(); |
| 820 | XP != XPEnd; ++XP, ++YP) |
| 821 | if (!isSameTemplateArg(Context, *XP, *YP)) |
| 822 | return false; |
| 823 | |
| 824 | return true; |
| 825 | } |
| 826 | |
| 827 | return false; |
| 828 | } |
| 829 | |
| 830 | /// \brief Helper function to build a TemplateParameter when we don't |
| 831 | /// know its type statically. |
| 832 | static TemplateParameter makeTemplateParameter(Decl *D) { |
| 833 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) |
| 834 | return TemplateParameter(TTP); |
| 835 | else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) |
| 836 | return TemplateParameter(NTTP); |
| 837 | |
| 838 | return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); |
| 839 | } |
| 840 | |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 841 | /// \brief Perform template argument deduction to determine whether |
| 842 | /// the given template arguments match the given class template |
| 843 | /// partial specialization per C++ [temp.class.spec.match]. |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 844 | Sema::TemplateDeductionResult |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 845 | Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 846 | const TemplateArgumentList &TemplateArgs, |
| 847 | TemplateDeductionInfo &Info) { |
Douglas Gregor | c1efb3f | 2009-06-12 22:31:52 +0000 | [diff] [blame] | 848 | // C++ [temp.class.spec.match]p2: |
| 849 | // A partial specialization matches a given actual template |
| 850 | // argument list if the template arguments of the partial |
| 851 | // specialization can be deduced from the actual template argument |
| 852 | // list (14.8.2). |
Douglas Gregor | bb26041 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 853 | SFINAETrap Trap(*this); |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 854 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
| 855 | Deduced.resize(Partial->getTemplateParameters()->size()); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 856 | if (TemplateDeductionResult Result |
| 857 | = ::DeduceTemplateArguments(Context, |
| 858 | Partial->getTemplateParameters(), |
| 859 | Partial->getTemplateArgs(), |
| 860 | TemplateArgs, Info, Deduced)) |
| 861 | return Result; |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 862 | |
Douglas Gregor | 637a409 | 2009-06-10 23:47:09 +0000 | [diff] [blame] | 863 | InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial, |
| 864 | Deduced.data(), Deduced.size()); |
| 865 | if (Inst) |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 866 | return TDK_InstantiationDepth; |
Douglas Gregor | 199d991 | 2009-06-05 00:53:49 +0000 | [diff] [blame] | 867 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 868 | // C++ [temp.deduct.type]p2: |
| 869 | // [...] or if any template argument remains neither deduced nor |
| 870 | // explicitly specified, template argument deduction fails. |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 871 | TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(), |
| 872 | Deduced.size()); |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 873 | for (unsigned I = 0, N = Deduced.size(); I != N; ++I) { |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 874 | if (Deduced[I].isNull()) { |
| 875 | Decl *Param |
| 876 | = const_cast<Decl *>(Partial->getTemplateParameters()->getParam(I)); |
| 877 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 878 | Info.Param = TTP; |
| 879 | else if (NonTypeTemplateParmDecl *NTTP |
| 880 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 881 | Info.Param = NTTP; |
| 882 | else |
| 883 | Info.Param = cast<TemplateTemplateParmDecl>(Param); |
| 884 | return TDK_Incomplete; |
| 885 | } |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 886 | |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 887 | Builder.Append(Deduced[I]); |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | // Form the template argument list from the deduced template arguments. |
| 891 | TemplateArgumentList *DeducedArgumentList |
Anders Carlsson | fb25052 | 2009-06-23 01:26:57 +0000 | [diff] [blame] | 892 | = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 893 | Info.reset(DeducedArgumentList); |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 894 | |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 895 | // Substitute the deduced template arguments into the template |
| 896 | // arguments of the class template partial specialization, and |
| 897 | // verify that the instantiated template arguments are both valid |
| 898 | // and are equivalent to the template arguments originally provided |
Douglas Gregor | c9e5d25 | 2009-06-13 00:59:32 +0000 | [diff] [blame] | 899 | // to the class template. |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 900 | ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate(); |
| 901 | const TemplateArgumentList &PartialTemplateArgs = Partial->getTemplateArgs(); |
| 902 | for (unsigned I = 0, N = PartialTemplateArgs.flat_size(); I != N; ++I) { |
Douglas Gregor | c9e5d25 | 2009-06-13 00:59:32 +0000 | [diff] [blame] | 903 | Decl *Param = const_cast<Decl *>( |
| 904 | ClassTemplate->getTemplateParameters()->getParam(I)); |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 905 | TemplateArgument InstArg = Instantiate(PartialTemplateArgs[I], |
| 906 | *DeducedArgumentList); |
| 907 | if (InstArg.isNull()) { |
| 908 | Info.Param = makeTemplateParameter(Param); |
| 909 | Info.FirstArg = PartialTemplateArgs[I]; |
| 910 | return TDK_SubstitutionFailure; |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 911 | } |
Douglas Gregor | f670c8c | 2009-06-26 20:57:09 +0000 | [diff] [blame] | 912 | |
| 913 | if (InstArg.getKind() == TemplateArgument::Expression) { |
| 914 | // When the argument is an expression, check the expression result |
| 915 | // against the actual template parameter to get down to the canonical |
| 916 | // template argument. |
| 917 | Expr *InstExpr = InstArg.getAsExpr(); |
| 918 | if (NonTypeTemplateParmDecl *NTTP |
| 919 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) { |
| 920 | if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) { |
| 921 | Info.Param = makeTemplateParameter(Param); |
| 922 | Info.FirstArg = PartialTemplateArgs[I]; |
| 923 | return TDK_SubstitutionFailure; |
| 924 | } |
| 925 | } else if (TemplateTemplateParmDecl *TTP |
| 926 | = dyn_cast<TemplateTemplateParmDecl>(Param)) { |
| 927 | // FIXME: template template arguments should really resolve to decls |
| 928 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InstExpr); |
| 929 | if (!DRE || CheckTemplateArgument(TTP, DRE)) { |
| 930 | Info.Param = makeTemplateParameter(Param); |
| 931 | Info.FirstArg = PartialTemplateArgs[I]; |
| 932 | return TDK_SubstitutionFailure; |
| 933 | } |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) { |
| 938 | Info.Param = makeTemplateParameter(Param); |
| 939 | Info.FirstArg = TemplateArgs[I]; |
| 940 | Info.SecondArg = InstArg; |
| 941 | return TDK_NonDeducedMismatch; |
| 942 | } |
Douglas Gregor | 02cbbd2 | 2009-06-11 18:10:32 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Douglas Gregor | bb26041 | 2009-06-14 08:02:22 +0000 | [diff] [blame] | 945 | if (Trap.hasErrorOccurred()) |
| 946 | return TDK_SubstitutionFailure; |
| 947 | |
Douglas Gregor | f67875d | 2009-06-12 18:26:56 +0000 | [diff] [blame] | 948 | return TDK_Success; |
Douglas Gregor | 0b9247f | 2009-06-04 00:03:07 +0000 | [diff] [blame] | 949 | } |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 950 | |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 951 | /// \brief Determine whether the given type T is a simple-template-id type. |
| 952 | static bool isSimpleTemplateIdType(QualType T) { |
| 953 | if (const TemplateSpecializationType *Spec |
| 954 | = T->getAsTemplateSpecializationType()) |
| 955 | return Spec->getTemplateName().getAsTemplateDecl() != 0; |
| 956 | |
| 957 | return false; |
| 958 | } |
| 959 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 960 | /// \brief Perform template argument deduction from a function call |
| 961 | /// (C++ [temp.deduct.call]). |
| 962 | /// |
| 963 | /// \param FunctionTemplate the function template for which we are performing |
| 964 | /// template argument deduction. |
| 965 | /// |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 966 | /// \param HasExplicitTemplateArgs whether any template arguments were |
| 967 | /// explicitly specified. |
| 968 | /// |
| 969 | /// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, |
| 970 | /// the explicitly-specified template arguments. |
| 971 | /// |
| 972 | /// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, |
| 973 | /// the number of explicitly-specified template arguments in |
| 974 | /// @p ExplicitTemplateArguments. This value may be zero. |
| 975 | /// |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 976 | /// \param Args the function call arguments |
| 977 | /// |
| 978 | /// \param NumArgs the number of arguments in Args |
| 979 | /// |
| 980 | /// \param Specialization if template argument deduction was successful, |
| 981 | /// this will be set to the function template specialization produced by |
| 982 | /// template argument deduction. |
| 983 | /// |
| 984 | /// \param Info the argument will be updated to provide additional information |
| 985 | /// about template argument deduction. |
| 986 | /// |
| 987 | /// \returns the result of template argument deduction. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 988 | Sema::TemplateDeductionResult |
| 989 | Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 990 | bool HasExplicitTemplateArgs, |
| 991 | const TemplateArgument *ExplicitTemplateArgs, |
| 992 | unsigned NumExplicitTemplateArgs, |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 993 | Expr **Args, unsigned NumArgs, |
| 994 | FunctionDecl *&Specialization, |
| 995 | TemplateDeductionInfo &Info) { |
| 996 | FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 997 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 998 | // C++ [temp.deduct.call]p1: |
| 999 | // Template argument deduction is done by comparing each function template |
| 1000 | // parameter type (call it P) with the type of the corresponding argument |
| 1001 | // of the call (call it A) as described below. |
| 1002 | unsigned CheckArgs = NumArgs; |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1003 | if (NumArgs < Function->getMinRequiredArguments()) |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1004 | return TDK_TooFewArguments; |
| 1005 | else if (NumArgs > Function->getNumParams()) { |
| 1006 | const FunctionProtoType *Proto |
| 1007 | = Function->getType()->getAsFunctionProtoType(); |
| 1008 | if (!Proto->isVariadic()) |
| 1009 | return TDK_TooManyArguments; |
| 1010 | |
| 1011 | CheckArgs = Function->getNumParams(); |
| 1012 | } |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1013 | |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1014 | // Template argument deduction for function templates in a SFINAE context. |
| 1015 | // Trap any errors that might occur. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1016 | SFINAETrap Trap(*this); |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1017 | |
| 1018 | // The types of the parameters from which we will perform template argument |
| 1019 | // deduction. |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1020 | TemplateParameterList *TemplateParams |
| 1021 | = FunctionTemplate->getTemplateParameters(); |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1022 | llvm::SmallVector<TemplateArgument, 4> Deduced; |
| 1023 | llvm::SmallVector<QualType, 4> ParamTypes; |
| 1024 | if (NumExplicitTemplateArgs) { |
| 1025 | // C++ [temp.arg.explicit]p3: |
| 1026 | // Template arguments that are present shall be specified in the |
| 1027 | // declaration order of their corresponding template-parameters. The |
| 1028 | // template argument list shall not specify more template-arguments than |
| 1029 | // there are corresponding template-parameters. |
| 1030 | TemplateArgumentListBuilder Builder(TemplateParams, |
| 1031 | NumExplicitTemplateArgs); |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1032 | |
| 1033 | // Enter a new template instantiation context where we check the |
| 1034 | // explicitly-specified template arguments against this function template, |
| 1035 | // and then substitute them into the function parameter types. |
| 1036 | InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1037 | FunctionTemplate, Deduced.data(), Deduced.size(), |
| 1038 | ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution); |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1039 | if (Inst) |
| 1040 | return TDK_InstantiationDepth; |
| 1041 | |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1042 | if (CheckTemplateArgumentList(FunctionTemplate, |
| 1043 | SourceLocation(), SourceLocation(), |
| 1044 | ExplicitTemplateArgs, |
| 1045 | NumExplicitTemplateArgs, |
| 1046 | SourceLocation(), |
Douglas Gregor | 16134c6 | 2009-07-01 00:28:38 +0000 | [diff] [blame] | 1047 | true, |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1048 | Builder) || Trap.hasErrorOccurred()) |
| 1049 | return TDK_InvalidExplicitArguments; |
| 1050 | |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1051 | // Form the template argument list from the explicitly-specified |
| 1052 | // template arguments. |
| 1053 | TemplateArgumentList *ExplicitArgumentList |
| 1054 | = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); |
| 1055 | Info.reset(ExplicitArgumentList); |
| 1056 | |
| 1057 | // Instantiate the types of each of the function parameters given the |
| 1058 | // explicitly-specified template arguments. |
| 1059 | for (FunctionDecl::param_iterator P = Function->param_begin(), |
| 1060 | PEnd = Function->param_end(); |
| 1061 | P != PEnd; |
| 1062 | ++P) { |
| 1063 | QualType ParamType = InstantiateType((*P)->getType(), |
| 1064 | *ExplicitArgumentList, |
| 1065 | (*P)->getLocation(), |
| 1066 | (*P)->getDeclName()); |
| 1067 | if (ParamType.isNull() || Trap.hasErrorOccurred()) |
| 1068 | return TDK_SubstitutionFailure; |
| 1069 | |
| 1070 | ParamTypes.push_back(ParamType); |
| 1071 | } |
| 1072 | |
| 1073 | // C++ [temp.arg.explicit]p2: |
| 1074 | // Trailing template arguments that can be deduced (14.8.2) may be |
| 1075 | // omitted from the list of explicit template- arguments. If all of the |
| 1076 | // template arguments can be deduced, they may all be omitted; in this |
| 1077 | // case, the empty template argument list <> itself may also be omitted. |
| 1078 | // |
| 1079 | // Take all of the explicitly-specified arguments and put them into the |
| 1080 | // set of deduced template arguments. |
| 1081 | Deduced.reserve(TemplateParams->size()); |
| 1082 | for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) |
| 1083 | Deduced.push_back(ExplicitArgumentList->get(I)); |
| 1084 | } else { |
| 1085 | // Just fill in the parameter types from the function declaration. |
| 1086 | for (unsigned I = 0; I != CheckArgs; ++I) |
| 1087 | ParamTypes.push_back(Function->getParamDecl(I)->getType()); |
| 1088 | } |
| 1089 | |
| 1090 | // Deduce template arguments from the function parameters. |
| 1091 | Deduced.resize(TemplateParams->size()); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1092 | for (unsigned I = 0; I != CheckArgs; ++I) { |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1093 | QualType ParamType = ParamTypes[I]; |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1094 | QualType ArgType = Args[I]->getType(); |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1095 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1096 | // C++ [temp.deduct.call]p2: |
| 1097 | // If P is not a reference type: |
| 1098 | QualType CanonParamType = Context.getCanonicalType(ParamType); |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1099 | bool ParamWasReference = isa<ReferenceType>(CanonParamType); |
| 1100 | if (!ParamWasReference) { |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1101 | // - If A is an array type, the pointer type produced by the |
| 1102 | // array-to-pointer standard conversion (4.2) is used in place of |
| 1103 | // A for type deduction; otherwise, |
| 1104 | if (ArgType->isArrayType()) |
| 1105 | ArgType = Context.getArrayDecayedType(ArgType); |
| 1106 | // - If A is a function type, the pointer type produced by the |
| 1107 | // function-to-pointer standard conversion (4.3) is used in place |
| 1108 | // of A for type deduction; otherwise, |
| 1109 | else if (ArgType->isFunctionType()) |
| 1110 | ArgType = Context.getPointerType(ArgType); |
| 1111 | else { |
| 1112 | // - If A is a cv-qualified type, the top level cv-qualifiers of A’s |
| 1113 | // type are ignored for type deduction. |
| 1114 | QualType CanonArgType = Context.getCanonicalType(ArgType); |
| 1115 | if (CanonArgType.getCVRQualifiers()) |
| 1116 | ArgType = CanonArgType.getUnqualifiedType(); |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | // C++0x [temp.deduct.call]p3: |
| 1121 | // If P is a cv-qualified type, the top level cv-qualifiers of P’s type |
| 1122 | // are ignored for type deduction. |
| 1123 | if (CanonParamType.getCVRQualifiers()) |
| 1124 | ParamType = CanonParamType.getUnqualifiedType(); |
| 1125 | if (const ReferenceType *ParamRefType = ParamType->getAsReferenceType()) { |
| 1126 | // [...] If P is a reference type, the type referred to by P is used |
| 1127 | // for type deduction. |
| 1128 | ParamType = ParamRefType->getPointeeType(); |
| 1129 | |
| 1130 | // [...] If P is of the form T&&, where T is a template parameter, and |
| 1131 | // the argument is an lvalue, the type A& is used in place of A for |
| 1132 | // type deduction. |
| 1133 | if (isa<RValueReferenceType>(ParamRefType) && |
| 1134 | ParamRefType->getAsTemplateTypeParmType() && |
| 1135 | Args[I]->isLvalue(Context) == Expr::LV_Valid) |
| 1136 | ArgType = Context.getLValueReferenceType(ArgType); |
| 1137 | } |
| 1138 | |
| 1139 | // C++0x [temp.deduct.call]p4: |
| 1140 | // In general, the deduction process attempts to find template argument |
| 1141 | // values that will make the deduced A identical to A (after the type A |
| 1142 | // is transformed as described above). [...] |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1143 | unsigned TDF = 0; |
| 1144 | |
| 1145 | // - If the original P is a reference type, the deduced A (i.e., the |
| 1146 | // type referred to by the reference) can be more cv-qualified than |
| 1147 | // the transformed A. |
| 1148 | if (ParamWasReference) |
| 1149 | TDF |= TDF_ParamWithReferenceType; |
| 1150 | // - The transformed A can be another pointer or pointer to member |
| 1151 | // type that can be converted to the deduced A via a qualification |
| 1152 | // conversion (4.4). |
| 1153 | if (ArgType->isPointerType() || ArgType->isMemberPointerType()) |
| 1154 | TDF |= TDF_IgnoreQualifiers; |
Douglas Gregor | 4112877 | 2009-06-26 23:27:24 +0000 | [diff] [blame] | 1155 | // - If P is a class and P has the form simple-template-id, then the |
| 1156 | // transformed A can be a derived class of the deduced A. Likewise, |
| 1157 | // if P is a pointer to a class of the form simple-template-id, the |
| 1158 | // transformed A can be a pointer to a derived class pointed to by |
| 1159 | // the deduced A. |
| 1160 | if (isSimpleTemplateIdType(ParamType) || |
| 1161 | (isa<PointerType>(ParamType) && |
| 1162 | isSimpleTemplateIdType( |
| 1163 | ParamType->getAsPointerType()->getPointeeType()))) |
| 1164 | TDF |= TDF_DerivedClass; |
| 1165 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1166 | if (TemplateDeductionResult Result |
| 1167 | = ::DeduceTemplateArguments(Context, TemplateParams, |
Douglas Gregor | 500d331 | 2009-06-26 18:27:22 +0000 | [diff] [blame] | 1168 | ParamType, ArgType, Info, Deduced, |
Douglas Gregor | 508f1c8 | 2009-06-26 23:10:12 +0000 | [diff] [blame] | 1169 | TDF)) |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1170 | return Result; |
| 1171 | |
| 1172 | // FIXME: C++ [temp.deduct.call] paragraphs 6-9 deal with function |
| 1173 | // pointer parameters. |
| 1174 | } |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1175 | |
| 1176 | // C++ [temp.deduct.type]p2: |
| 1177 | // [...] or if any template argument remains neither deduced nor |
| 1178 | // explicitly specified, template argument deduction fails. |
| 1179 | TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size()); |
| 1180 | for (unsigned I = 0, N = Deduced.size(); I != N; ++I) { |
| 1181 | if (Deduced[I].isNull()) { |
| 1182 | Decl *Param |
| 1183 | = const_cast<Decl *>(TemplateParams->getParam(I)); |
| 1184 | if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) |
| 1185 | Info.Param = TTP; |
| 1186 | else if (NonTypeTemplateParmDecl *NTTP |
| 1187 | = dyn_cast<NonTypeTemplateParmDecl>(Param)) |
| 1188 | Info.Param = NTTP; |
| 1189 | else |
| 1190 | Info.Param = cast<TemplateTemplateParmDecl>(Param); |
| 1191 | return TDK_Incomplete; |
| 1192 | } |
| 1193 | |
| 1194 | Builder.Append(Deduced[I]); |
| 1195 | } |
| 1196 | |
| 1197 | // Form the template argument list from the deduced template arguments. |
| 1198 | TemplateArgumentList *DeducedArgumentList |
| 1199 | = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); |
| 1200 | Info.reset(DeducedArgumentList); |
| 1201 | |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1202 | // Enter a new template instantiation context while we instantiate the |
| 1203 | // actual function declaration. |
| 1204 | InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), |
Douglas Gregor | cca9e96 | 2009-07-01 22:01:06 +0000 | [diff] [blame] | 1205 | FunctionTemplate, Deduced.data(), Deduced.size(), |
| 1206 | ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution); |
Douglas Gregor | 6db8ed4 | 2009-06-30 23:57:56 +0000 | [diff] [blame] | 1207 | if (Inst) |
| 1208 | return TDK_InstantiationDepth; |
| 1209 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1210 | // Substitute the deduced template arguments into the function template |
| 1211 | // declaration to produce the function template specialization. |
| 1212 | Specialization = cast_or_null<FunctionDecl>( |
| 1213 | InstantiateDecl(FunctionTemplate->getTemplatedDecl(), |
| 1214 | FunctionTemplate->getDeclContext(), |
| 1215 | *DeducedArgumentList)); |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1216 | if (!Specialization) |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1217 | return TDK_SubstitutionFailure; |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1218 | |
| 1219 | // If the template argument list is owned by the function template |
| 1220 | // specialization, release it. |
| 1221 | if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList) |
| 1222 | Info.take(); |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1223 | |
Douglas Gregor | 127102b | 2009-06-29 20:59:39 +0000 | [diff] [blame] | 1224 | // There may have been an error that did not prevent us from constructing a |
| 1225 | // declaration. Mark the declaration invalid and return with a substitution |
| 1226 | // failure. |
| 1227 | if (Trap.hasErrorOccurred()) { |
| 1228 | Specialization->setInvalidDecl(true); |
| 1229 | return TDK_SubstitutionFailure; |
| 1230 | } |
| 1231 | |
Douglas Gregor | e53060f | 2009-06-25 22:08:12 +0000 | [diff] [blame] | 1232 | return TDK_Success; |
| 1233 | } |
| 1234 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1235 | static void |
| 1236 | MarkDeducedTemplateParameters(Sema &SemaRef, |
| 1237 | const TemplateArgument &TemplateArg, |
| 1238 | llvm::SmallVectorImpl<bool> &Deduced); |
| 1239 | |
| 1240 | /// \brief Mark the template arguments that are deduced by the given |
| 1241 | /// expression. |
| 1242 | static void |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1243 | MarkDeducedTemplateParameters(const Expr *E, |
| 1244 | llvm::SmallVectorImpl<bool> &Deduced) { |
| 1245 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1246 | if (!E) |
| 1247 | return; |
| 1248 | |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1249 | const NonTypeTemplateParmDecl *NTTP |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1250 | = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); |
| 1251 | if (!NTTP) |
| 1252 | return; |
| 1253 | |
| 1254 | Deduced[NTTP->getIndex()] = true; |
| 1255 | } |
| 1256 | |
| 1257 | /// \brief Mark the template parameters that are deduced by the given |
| 1258 | /// type. |
| 1259 | static void |
| 1260 | MarkDeducedTemplateParameters(Sema &SemaRef, QualType T, |
| 1261 | llvm::SmallVectorImpl<bool> &Deduced) { |
| 1262 | // Non-dependent types have nothing deducible |
| 1263 | if (!T->isDependentType()) |
| 1264 | return; |
| 1265 | |
| 1266 | T = SemaRef.Context.getCanonicalType(T); |
| 1267 | switch (T->getTypeClass()) { |
| 1268 | case Type::ExtQual: |
| 1269 | MarkDeducedTemplateParameters(SemaRef, |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1270 | QualType(cast<ExtQualType>(T)->getBaseType(), 0), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1271 | Deduced); |
| 1272 | break; |
| 1273 | |
| 1274 | case Type::Pointer: |
| 1275 | MarkDeducedTemplateParameters(SemaRef, |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1276 | cast<PointerType>(T)->getPointeeType(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1277 | Deduced); |
| 1278 | break; |
| 1279 | |
| 1280 | case Type::BlockPointer: |
| 1281 | MarkDeducedTemplateParameters(SemaRef, |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1282 | cast<BlockPointerType>(T)->getPointeeType(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1283 | Deduced); |
| 1284 | break; |
| 1285 | |
| 1286 | case Type::LValueReference: |
| 1287 | case Type::RValueReference: |
| 1288 | MarkDeducedTemplateParameters(SemaRef, |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1289 | cast<ReferenceType>(T)->getPointeeType(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1290 | Deduced); |
| 1291 | break; |
| 1292 | |
| 1293 | case Type::MemberPointer: { |
| 1294 | const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); |
| 1295 | MarkDeducedTemplateParameters(SemaRef, MemPtr->getPointeeType(), Deduced); |
| 1296 | MarkDeducedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0), |
| 1297 | Deduced); |
| 1298 | break; |
| 1299 | } |
| 1300 | |
| 1301 | case Type::DependentSizedArray: |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1302 | MarkDeducedTemplateParameters(cast<DependentSizedArrayType>(T)->getSizeExpr(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1303 | Deduced); |
| 1304 | // Fall through to check the element type |
| 1305 | |
| 1306 | case Type::ConstantArray: |
| 1307 | case Type::IncompleteArray: |
| 1308 | MarkDeducedTemplateParameters(SemaRef, |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1309 | cast<ArrayType>(T)->getElementType(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1310 | Deduced); |
| 1311 | break; |
| 1312 | |
| 1313 | case Type::Vector: |
| 1314 | case Type::ExtVector: |
| 1315 | MarkDeducedTemplateParameters(SemaRef, |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1316 | cast<VectorType>(T)->getElementType(), |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1317 | Deduced); |
| 1318 | break; |
| 1319 | |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1320 | case Type::DependentSizedExtVector: { |
| 1321 | const DependentSizedExtVectorType *VecType |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1322 | = cast<DependentSizedExtVectorType>(T); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1323 | MarkDeducedTemplateParameters(SemaRef, VecType->getElementType(), Deduced); |
| 1324 | MarkDeducedTemplateParameters(VecType->getSizeExpr(), Deduced); |
| 1325 | break; |
| 1326 | } |
| 1327 | |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1328 | case Type::FunctionProto: { |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1329 | const FunctionProtoType *Proto = cast<FunctionProtoType>(T); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1330 | MarkDeducedTemplateParameters(SemaRef, Proto->getResultType(), Deduced); |
| 1331 | for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I) |
| 1332 | MarkDeducedTemplateParameters(SemaRef, Proto->getArgType(I), Deduced); |
| 1333 | break; |
| 1334 | } |
| 1335 | |
| 1336 | case Type::TemplateTypeParm: |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1337 | Deduced[cast<TemplateTypeParmType>(T)->getIndex()] = true; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1338 | break; |
| 1339 | |
| 1340 | case Type::TemplateSpecialization: { |
| 1341 | const TemplateSpecializationType *Spec |
Douglas Gregor | f6ddb73 | 2009-06-18 18:45:36 +0000 | [diff] [blame] | 1342 | = cast<TemplateSpecializationType>(T); |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1343 | if (TemplateDecl *Template = Spec->getTemplateName().getAsTemplateDecl()) |
| 1344 | if (TemplateTemplateParmDecl *TTP |
| 1345 | = dyn_cast<TemplateTemplateParmDecl>(Template)) |
| 1346 | Deduced[TTP->getIndex()] = true; |
| 1347 | |
| 1348 | for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) |
| 1349 | MarkDeducedTemplateParameters(SemaRef, Spec->getArg(I), Deduced); |
| 1350 | |
| 1351 | break; |
| 1352 | } |
| 1353 | |
| 1354 | // None of these types have any deducible parts. |
| 1355 | case Type::Builtin: |
| 1356 | case Type::FixedWidthInt: |
| 1357 | case Type::Complex: |
| 1358 | case Type::VariableArray: |
| 1359 | case Type::FunctionNoProto: |
| 1360 | case Type::Record: |
| 1361 | case Type::Enum: |
| 1362 | case Type::Typename: |
| 1363 | case Type::ObjCInterface: |
| 1364 | case Type::ObjCQualifiedInterface: |
Steve Naroff | d1b3c2d | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 1365 | case Type::ObjCObjectPointer: |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1366 | #define TYPE(Class, Base) |
| 1367 | #define ABSTRACT_TYPE(Class, Base) |
| 1368 | #define DEPENDENT_TYPE(Class, Base) |
| 1369 | #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: |
| 1370 | #include "clang/AST/TypeNodes.def" |
| 1371 | break; |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | /// \brief Mark the template parameters that are deduced by this |
| 1376 | /// template argument. |
| 1377 | static void |
| 1378 | MarkDeducedTemplateParameters(Sema &SemaRef, |
| 1379 | const TemplateArgument &TemplateArg, |
| 1380 | llvm::SmallVectorImpl<bool> &Deduced) { |
| 1381 | switch (TemplateArg.getKind()) { |
| 1382 | case TemplateArgument::Null: |
| 1383 | case TemplateArgument::Integral: |
| 1384 | break; |
| 1385 | |
| 1386 | case TemplateArgument::Type: |
| 1387 | MarkDeducedTemplateParameters(SemaRef, TemplateArg.getAsType(), Deduced); |
| 1388 | break; |
| 1389 | |
| 1390 | case TemplateArgument::Declaration: |
| 1391 | if (TemplateTemplateParmDecl *TTP |
| 1392 | = dyn_cast<TemplateTemplateParmDecl>(TemplateArg.getAsDecl())) |
| 1393 | Deduced[TTP->getIndex()] = true; |
| 1394 | break; |
| 1395 | |
| 1396 | case TemplateArgument::Expression: |
| 1397 | MarkDeducedTemplateParameters(TemplateArg.getAsExpr(), Deduced); |
| 1398 | break; |
Anders Carlsson | d01b1da | 2009-06-15 17:04:53 +0000 | [diff] [blame] | 1399 | case TemplateArgument::Pack: |
| 1400 | assert(0 && "FIXME: Implement!"); |
| 1401 | break; |
Douglas Gregor | 031a588 | 2009-06-13 00:26:55 +0000 | [diff] [blame] | 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | /// \brief Mark the template parameters can be deduced by the given |
| 1406 | /// template argument list. |
| 1407 | /// |
| 1408 | /// \param TemplateArgs the template argument list from which template |
| 1409 | /// parameters will be deduced. |
| 1410 | /// |
| 1411 | /// \param Deduced a bit vector whose elements will be set to \c true |
| 1412 | /// to indicate when the corresponding template parameter will be |
| 1413 | /// deduced. |
| 1414 | void |
| 1415 | Sema::MarkDeducedTemplateParameters(const TemplateArgumentList &TemplateArgs, |
| 1416 | llvm::SmallVectorImpl<bool> &Deduced) { |
| 1417 | for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) |
| 1418 | ::MarkDeducedTemplateParameters(*this, TemplateArgs[I], Deduced); |
| 1419 | } |