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" 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 40 /// deduction from a template-id of a derived class of the argument type. 41 TDF_DerivedClass = 0x04 42 }; 43 } 44 45 using namespace clang; 46 47 static Sema::TemplateDeductionResult 48 DeduceTemplateArguments(ASTContext &Context, 49 TemplateParameterList *TemplateParams, 50 const TemplateArgument &Param, 51 const TemplateArgument &Arg, 52 Sema::TemplateDeductionInfo &Info, 53 llvm::SmallVectorImpl<TemplateArgument> &Deduced); 54 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. 70 static Sema::TemplateDeductionResult 71 DeduceNonTypeTemplateArgument(ASTContext &Context, 72 NonTypeTemplateParmDecl *NTTP, 73 llvm::APSInt Value, 74 Sema::TemplateDeductionInfo &Info, 75 llvm::SmallVectorImpl<TemplateArgument> &Deduced) { 76 assert(NTTP->getDepth() == 0 && 77 "Cannot deduce non-type template argument with depth > 0"); 78 79 if (Deduced[NTTP->getIndex()].isNull()) { 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); 89 return Sema::TDK_Success; 90 } 91 92 assert(Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral); 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(); 97 if (PrevValuePtr->isNegative()) { 98 Info.Param = NTTP; 99 Info.FirstArg = Deduced[NTTP->getIndex()]; 100 Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType()); 101 return Sema::TDK_Inconsistent; 102 } 103 104 llvm::APSInt PrevValue = *PrevValuePtr; 105 if (Value.getBitWidth() > PrevValue.getBitWidth()) 106 PrevValue.zext(Value.getBitWidth()); 107 else if (Value.getBitWidth() < PrevValue.getBitWidth()) 108 Value.zext(PrevValue.getBitWidth()); 109 110 if (Value != PrevValue) { 111 Info.Param = NTTP; 112 Info.FirstArg = Deduced[NTTP->getIndex()]; 113 Info.SecondArg = TemplateArgument(SourceLocation(), Value, NTTP->getType()); 114 return Sema::TDK_Inconsistent; 115 } 116 117 return Sema::TDK_Success; 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 125 static Sema::TemplateDeductionResult 126 DeduceNonTypeTemplateArgument(ASTContext &Context, 127 NonTypeTemplateParmDecl *NTTP, 128 Expr *Value, 129 Sema::TemplateDeductionInfo &Info, 130 llvm::SmallVectorImpl<TemplateArgument> &Deduced) { 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); 139 return Sema::TDK_Success; 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. 146 return Sema::TDK_Success; 147 } 148 149 // FIXME: Compare the expressions for equality! 150 return Sema::TDK_Success; 151 } 152 153 static Sema::TemplateDeductionResult 154 DeduceTemplateArguments(ASTContext &Context, 155 TemplateName Param, 156 TemplateName Arg, 157 Sema::TemplateDeductionInfo &Info, 158 llvm::SmallVectorImpl<TemplateArgument> &Deduced) { 159 // FIXME: Implement template argument deduction for template 160 // template parameters. 161 162 // FIXME: this routine does not have enough information to produce 163 // good diagnostics. 164 165 TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); 166 TemplateDecl *ArgDecl = Arg.getAsTemplateDecl(); 167 168 if (!ParamDecl || !ArgDecl) { 169 // FIXME: fill in Info.Param/Info.FirstArg 170 return Sema::TDK_Inconsistent; 171 } 172 173 ParamDecl = cast<TemplateDecl>(Context.getCanonicalDecl(ParamDecl)); 174 ArgDecl = cast<TemplateDecl>(Context.getCanonicalDecl(ArgDecl)); 175 if (ParamDecl != ArgDecl) { 176 // FIXME: fill in Info.Param/Info.FirstArg 177 return Sema::TDK_Inconsistent; 178 } 179 180 return Sema::TDK_Success; 181 } 182 183 /// \brief Deduce the template arguments by comparing the parameter type and 184 /// the argument type (C++ [temp.deduct.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 ParamIn the parameter type 191 /// 192 /// \param ArgIn the argument type 193 /// 194 /// \param Info information about the template argument deduction itself 195 /// 196 /// \param Deduced the deduced template arguments 197 /// 198 /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe 199 /// how template argument deduction is performed. 200 /// 201 /// \returns the result of template argument deduction so far. Note that a 202 /// "success" result means that template argument deduction has not yet failed, 203 /// but it may still fail, later, for other reasons. 204 static Sema::TemplateDeductionResult 205 DeduceTemplateArguments(ASTContext &Context, 206 TemplateParameterList *TemplateParams, 207 QualType ParamIn, QualType ArgIn, 208 Sema::TemplateDeductionInfo &Info, 209 llvm::SmallVectorImpl<TemplateArgument> &Deduced, 210 unsigned TDF) { 211 // We only want to look at the canonical types, since typedefs and 212 // sugar are not part of template argument deduction. 213 QualType Param = Context.getCanonicalType(ParamIn); 214 QualType Arg = Context.getCanonicalType(ArgIn); 215 216 // C++0x [temp.deduct.call]p4 bullet 1: 217 // - If the original P is a reference type, the deduced A (i.e., the type 218 // referred to by the reference) can be more cv-qualified than the 219 // transformed A. 220 if (TDF & TDF_ParamWithReferenceType) { 221 unsigned ExtraQualsOnParam 222 = Param.getCVRQualifiers() & ~Arg.getCVRQualifiers(); 223 Param.setCVRQualifiers(Param.getCVRQualifiers() & ~ExtraQualsOnParam); 224 } 225 226 // If the parameter type is not dependent, there is nothing to deduce. 227 if (!Param->isDependentType()) 228 return Sema::TDK_Success; 229 230 // C++ [temp.deduct.type]p9: 231 // A template type argument T, a template template argument TT or a 232 // template non-type argument i can be deduced if P and A have one of 233 // the following forms: 234 // 235 // T 236 // cv-list T 237 if (const TemplateTypeParmType *TemplateTypeParm 238 = Param->getAsTemplateTypeParmType()) { 239 unsigned Index = TemplateTypeParm->getIndex(); 240 241 // The argument type can not be less qualified than the parameter 242 // type. 243 if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) { 244 Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); 245 Info.FirstArg = Deduced[Index]; 246 Info.SecondArg = TemplateArgument(SourceLocation(), Arg); 247 return Sema::TDK_InconsistentQuals; 248 } 249 250 assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0"); 251 252 unsigned Quals = Arg.getCVRQualifiers() & ~Param.getCVRQualifiers(); 253 QualType DeducedType = Arg.getQualifiedType(Quals); 254 255 if (Deduced[Index].isNull()) 256 Deduced[Index] = TemplateArgument(SourceLocation(), DeducedType); 257 else { 258 // C++ [temp.deduct.type]p2: 259 // [...] If type deduction cannot be done for any P/A pair, or if for 260 // any pair the deduction leads to more than one possible set of 261 // deduced values, or if different pairs yield different deduced 262 // values, or if any template argument remains neither deduced nor 263 // explicitly specified, template argument deduction fails. 264 if (Deduced[Index].getAsType() != DeducedType) { 265 Info.Param 266 = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); 267 Info.FirstArg = Deduced[Index]; 268 Info.SecondArg = TemplateArgument(SourceLocation(), Arg); 269 return Sema::TDK_Inconsistent; 270 } 271 } 272 return Sema::TDK_Success; 273 } 274 275 // Set up the template argument deduction information for a failure. 276 Info.FirstArg = TemplateArgument(SourceLocation(), ParamIn); 277 Info.SecondArg = TemplateArgument(SourceLocation(), ArgIn); 278 279 // Check the cv-qualifiers on the parameter and argument types. 280 if (!(TDF & TDF_IgnoreQualifiers)) { 281 if (TDF & TDF_ParamWithReferenceType) { 282 if (Param.isMoreQualifiedThan(Arg)) 283 return Sema::TDK_NonDeducedMismatch; 284 } else { 285 if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) 286 return Sema::TDK_NonDeducedMismatch; 287 } 288 } 289 290 switch (Param->getTypeClass()) { 291 // No deduction possible for these types 292 case Type::Builtin: 293 return Sema::TDK_NonDeducedMismatch; 294 295 // T * 296 case Type::Pointer: { 297 const PointerType *PointerArg = Arg->getAsPointerType(); 298 if (!PointerArg) 299 return Sema::TDK_NonDeducedMismatch; 300 301 unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); 302 return DeduceTemplateArguments(Context, TemplateParams, 303 cast<PointerType>(Param)->getPointeeType(), 304 PointerArg->getPointeeType(), 305 Info, Deduced, SubTDF); 306 } 307 308 // T & 309 case Type::LValueReference: { 310 const LValueReferenceType *ReferenceArg = Arg->getAsLValueReferenceType(); 311 if (!ReferenceArg) 312 return Sema::TDK_NonDeducedMismatch; 313 314 return DeduceTemplateArguments(Context, TemplateParams, 315 cast<LValueReferenceType>(Param)->getPointeeType(), 316 ReferenceArg->getPointeeType(), 317 Info, Deduced, 0); 318 } 319 320 // T && [C++0x] 321 case Type::RValueReference: { 322 const RValueReferenceType *ReferenceArg = Arg->getAsRValueReferenceType(); 323 if (!ReferenceArg) 324 return Sema::TDK_NonDeducedMismatch; 325 326 return DeduceTemplateArguments(Context, TemplateParams, 327 cast<RValueReferenceType>(Param)->getPointeeType(), 328 ReferenceArg->getPointeeType(), 329 Info, Deduced, 0); 330 } 331 332 // T [] (implied, but not stated explicitly) 333 case Type::IncompleteArray: { 334 const IncompleteArrayType *IncompleteArrayArg = 335 Context.getAsIncompleteArrayType(Arg); 336 if (!IncompleteArrayArg) 337 return Sema::TDK_NonDeducedMismatch; 338 339 return DeduceTemplateArguments(Context, TemplateParams, 340 Context.getAsIncompleteArrayType(Param)->getElementType(), 341 IncompleteArrayArg->getElementType(), 342 Info, Deduced, 0); 343 } 344 345 // T [integer-constant] 346 case Type::ConstantArray: { 347 const ConstantArrayType *ConstantArrayArg = 348 Context.getAsConstantArrayType(Arg); 349 if (!ConstantArrayArg) 350 return Sema::TDK_NonDeducedMismatch; 351 352 const ConstantArrayType *ConstantArrayParm = 353 Context.getAsConstantArrayType(Param); 354 if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) 355 return Sema::TDK_NonDeducedMismatch; 356 357 return DeduceTemplateArguments(Context, TemplateParams, 358 ConstantArrayParm->getElementType(), 359 ConstantArrayArg->getElementType(), 360 Info, Deduced, 0); 361 } 362 363 // type [i] 364 case Type::DependentSizedArray: { 365 const ArrayType *ArrayArg = dyn_cast<ArrayType>(Arg); 366 if (!ArrayArg) 367 return Sema::TDK_NonDeducedMismatch; 368 369 // Check the element type of the arrays 370 const DependentSizedArrayType *DependentArrayParm 371 = cast<DependentSizedArrayType>(Param); 372 if (Sema::TemplateDeductionResult Result 373 = DeduceTemplateArguments(Context, TemplateParams, 374 DependentArrayParm->getElementType(), 375 ArrayArg->getElementType(), 376 Info, Deduced, 0)) 377 return Result; 378 379 // Determine the array bound is something we can deduce. 380 NonTypeTemplateParmDecl *NTTP 381 = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr()); 382 if (!NTTP) 383 return Sema::TDK_Success; 384 385 // We can perform template argument deduction for the given non-type 386 // template parameter. 387 assert(NTTP->getDepth() == 0 && 388 "Cannot deduce non-type template argument at depth > 0"); 389 if (const ConstantArrayType *ConstantArrayArg 390 = dyn_cast<ConstantArrayType>(ArrayArg)) { 391 llvm::APSInt Size(ConstantArrayArg->getSize()); 392 return DeduceNonTypeTemplateArgument(Context, NTTP, Size, 393 Info, Deduced); 394 } 395 if (const DependentSizedArrayType *DependentArrayArg 396 = dyn_cast<DependentSizedArrayType>(ArrayArg)) 397 return DeduceNonTypeTemplateArgument(Context, NTTP, 398 DependentArrayArg->getSizeExpr(), 399 Info, Deduced); 400 401 // Incomplete type does not match a dependently-sized array type 402 return Sema::TDK_NonDeducedMismatch; 403 } 404 405 // type(*)(T) 406 // T(*)() 407 // T(*)(T) 408 case Type::FunctionProto: { 409 const FunctionProtoType *FunctionProtoArg = 410 dyn_cast<FunctionProtoType>(Arg); 411 if (!FunctionProtoArg) 412 return Sema::TDK_NonDeducedMismatch; 413 414 const FunctionProtoType *FunctionProtoParam = 415 cast<FunctionProtoType>(Param); 416 417 if (FunctionProtoParam->getTypeQuals() != 418 FunctionProtoArg->getTypeQuals()) 419 return Sema::TDK_NonDeducedMismatch; 420 421 if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs()) 422 return Sema::TDK_NonDeducedMismatch; 423 424 if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) 425 return Sema::TDK_NonDeducedMismatch; 426 427 // Check return types. 428 if (Sema::TemplateDeductionResult Result 429 = DeduceTemplateArguments(Context, TemplateParams, 430 FunctionProtoParam->getResultType(), 431 FunctionProtoArg->getResultType(), 432 Info, Deduced, 0)) 433 return Result; 434 435 for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) { 436 // Check argument types. 437 if (Sema::TemplateDeductionResult Result 438 = DeduceTemplateArguments(Context, TemplateParams, 439 FunctionProtoParam->getArgType(I), 440 FunctionProtoArg->getArgType(I), 441 Info, Deduced, 0)) 442 return Result; 443 } 444 445 return Sema::TDK_Success; 446 } 447 448 // template-name<T> (where template-name refers to a class template) 449 // template-name<i> 450 // TT<T> (TODO) 451 // TT<i> (TODO) 452 // TT<> (TODO) 453 case Type::TemplateSpecialization: { 454 const TemplateSpecializationType *SpecParam 455 = cast<TemplateSpecializationType>(Param); 456 457 // Check whether the template argument is a dependent template-id. 458 // FIXME: This is untested code; it can be tested when we implement 459 // partial ordering of class template partial specializations. 460 if (const TemplateSpecializationType *SpecArg 461 = dyn_cast<TemplateSpecializationType>(Arg)) { 462 // Perform template argument deduction for the template name. 463 if (Sema::TemplateDeductionResult Result 464 = DeduceTemplateArguments(Context, 465 SpecParam->getTemplateName(), 466 SpecArg->getTemplateName(), 467 Info, Deduced)) 468 return Result; 469 470 unsigned NumArgs = SpecParam->getNumArgs(); 471 472 // FIXME: When one of the template-names refers to a 473 // declaration with default template arguments, do we need to 474 // fill in those default template arguments here? Most likely, 475 // the answer is "yes", but I don't see any references. This 476 // issue may be resolved elsewhere, because we may want to 477 // instantiate default template arguments when 478 if (SpecArg->getNumArgs() != NumArgs) 479 return Sema::TDK_NonDeducedMismatch; 480 481 // Perform template argument deduction on each template 482 // argument. 483 for (unsigned I = 0; I != NumArgs; ++I) 484 if (Sema::TemplateDeductionResult Result 485 = DeduceTemplateArguments(Context, TemplateParams, 486 SpecParam->getArg(I), 487 SpecArg->getArg(I), 488 Info, Deduced)) 489 return Result; 490 491 return Sema::TDK_Success; 492 } 493 494 // If the argument type is a class template specialization, we 495 // perform template argument deduction using its template 496 // arguments. 497 const RecordType *RecordArg = dyn_cast<RecordType>(Arg); 498 if (!RecordArg) 499 return Sema::TDK_NonDeducedMismatch; 500 501 // FIXME: Check TDF_DerivedClass here. When this flag is set, we need 502 // to troll through the base classes of the argument and try matching 503 // all of them. Failure to match does not mean that there is a problem, 504 // of course. 505 506 ClassTemplateSpecializationDecl *SpecArg 507 = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); 508 if (!SpecArg) 509 return Sema::TDK_NonDeducedMismatch; 510 511 // Perform template argument deduction for the template name. 512 if (Sema::TemplateDeductionResult Result 513 = DeduceTemplateArguments(Context, 514 SpecParam->getTemplateName(), 515 TemplateName(SpecArg->getSpecializedTemplate()), 516 Info, Deduced)) 517 return Result; 518 519 // FIXME: Can the # of arguments in the parameter and the argument differ? 520 unsigned NumArgs = SpecParam->getNumArgs(); 521 const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs(); 522 if (NumArgs != ArgArgs.size()) 523 return Sema::TDK_NonDeducedMismatch; 524 525 for (unsigned I = 0; I != NumArgs; ++I) 526 if (Sema::TemplateDeductionResult Result 527 = DeduceTemplateArguments(Context, TemplateParams, 528 SpecParam->getArg(I), 529 ArgArgs.get(I), 530 Info, Deduced)) 531 return Result; 532 533 return Sema::TDK_Success; 534 } 535 536 // T type::* 537 // T T::* 538 // T (type::*)() 539 // type (T::*)() 540 // type (type::*)(T) 541 // type (T::*)(T) 542 // T (type::*)(T) 543 // T (T::*)() 544 // T (T::*)(T) 545 case Type::MemberPointer: { 546 const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); 547 const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); 548 if (!MemPtrArg) 549 return Sema::TDK_NonDeducedMismatch; 550 551 if (Sema::TemplateDeductionResult Result 552 = DeduceTemplateArguments(Context, TemplateParams, 553 MemPtrParam->getPointeeType(), 554 MemPtrArg->getPointeeType(), 555 Info, Deduced, 556 TDF & TDF_IgnoreQualifiers)) 557 return Result; 558 559 return DeduceTemplateArguments(Context, TemplateParams, 560 QualType(MemPtrParam->getClass(), 0), 561 QualType(MemPtrArg->getClass(), 0), 562 Info, Deduced, 0); 563 } 564 565 // (clang extension) 566 // 567 // type(^)(T) 568 // T(^)() 569 // T(^)(T) 570 case Type::BlockPointer: { 571 const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); 572 const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); 573 574 if (!BlockPtrArg) 575 return Sema::TDK_NonDeducedMismatch; 576 577 return DeduceTemplateArguments(Context, TemplateParams, 578 BlockPtrParam->getPointeeType(), 579 BlockPtrArg->getPointeeType(), Info, 580 Deduced, 0); 581 } 582 583 case Type::TypeOfExpr: 584 case Type::TypeOf: 585 case Type::Typename: 586 // No template argument deduction for these types 587 return Sema::TDK_Success; 588 589 default: 590 break; 591 } 592 593 // FIXME: Many more cases to go (to go). 594 return Sema::TDK_Success; 595 } 596 597 static Sema::TemplateDeductionResult 598 DeduceTemplateArguments(ASTContext &Context, 599 TemplateParameterList *TemplateParams, 600 const TemplateArgument &Param, 601 const TemplateArgument &Arg, 602 Sema::TemplateDeductionInfo &Info, 603 llvm::SmallVectorImpl<TemplateArgument> &Deduced) { 604 switch (Param.getKind()) { 605 case TemplateArgument::Null: 606 assert(false && "Null template argument in parameter list"); 607 break; 608 609 case TemplateArgument::Type: 610 assert(Arg.getKind() == TemplateArgument::Type && "Type/value mismatch"); 611 return DeduceTemplateArguments(Context, TemplateParams, Param.getAsType(), 612 Arg.getAsType(), Info, Deduced, 0); 613 614 case TemplateArgument::Declaration: 615 // FIXME: Implement this check 616 assert(false && "Unimplemented template argument deduction case"); 617 Info.FirstArg = Param; 618 Info.SecondArg = Arg; 619 return Sema::TDK_NonDeducedMismatch; 620 621 case TemplateArgument::Integral: 622 if (Arg.getKind() == TemplateArgument::Integral) { 623 // FIXME: Zero extension + sign checking here? 624 if (*Param.getAsIntegral() == *Arg.getAsIntegral()) 625 return Sema::TDK_Success; 626 627 Info.FirstArg = Param; 628 Info.SecondArg = Arg; 629 return Sema::TDK_NonDeducedMismatch; 630 } 631 632 if (Arg.getKind() == TemplateArgument::Expression) { 633 Info.FirstArg = Param; 634 Info.SecondArg = Arg; 635 return Sema::TDK_NonDeducedMismatch; 636 } 637 638 assert(false && "Type/value mismatch"); 639 Info.FirstArg = Param; 640 Info.SecondArg = Arg; 641 return Sema::TDK_NonDeducedMismatch; 642 643 case TemplateArgument::Expression: { 644 if (NonTypeTemplateParmDecl *NTTP 645 = getDeducedParameterFromExpr(Param.getAsExpr())) { 646 if (Arg.getKind() == TemplateArgument::Integral) 647 // FIXME: Sign problems here 648 return DeduceNonTypeTemplateArgument(Context, NTTP, 649 *Arg.getAsIntegral(), 650 Info, Deduced); 651 if (Arg.getKind() == TemplateArgument::Expression) 652 return DeduceNonTypeTemplateArgument(Context, NTTP, Arg.getAsExpr(), 653 Info, Deduced); 654 655 assert(false && "Type/value mismatch"); 656 Info.FirstArg = Param; 657 Info.SecondArg = Arg; 658 return Sema::TDK_NonDeducedMismatch; 659 } 660 661 // Can't deduce anything, but that's okay. 662 return Sema::TDK_Success; 663 } 664 case TemplateArgument::Pack: 665 assert(0 && "FIXME: Implement!"); 666 break; 667 } 668 669 return Sema::TDK_Success; 670 } 671 672 static Sema::TemplateDeductionResult 673 DeduceTemplateArguments(ASTContext &Context, 674 TemplateParameterList *TemplateParams, 675 const TemplateArgumentList &ParamList, 676 const TemplateArgumentList &ArgList, 677 Sema::TemplateDeductionInfo &Info, 678 llvm::SmallVectorImpl<TemplateArgument> &Deduced) { 679 assert(ParamList.size() == ArgList.size()); 680 for (unsigned I = 0, N = ParamList.size(); I != N; ++I) { 681 if (Sema::TemplateDeductionResult Result 682 = DeduceTemplateArguments(Context, TemplateParams, 683 ParamList[I], ArgList[I], 684 Info, Deduced)) 685 return Result; 686 } 687 return Sema::TDK_Success; 688 } 689 690 /// \brief Determine whether two template arguments are the same. 691 static bool isSameTemplateArg(ASTContext &Context, 692 const TemplateArgument &X, 693 const TemplateArgument &Y) { 694 if (X.getKind() != Y.getKind()) 695 return false; 696 697 switch (X.getKind()) { 698 case TemplateArgument::Null: 699 assert(false && "Comparing NULL template argument"); 700 break; 701 702 case TemplateArgument::Type: 703 return Context.getCanonicalType(X.getAsType()) == 704 Context.getCanonicalType(Y.getAsType()); 705 706 case TemplateArgument::Declaration: 707 return Context.getCanonicalDecl(X.getAsDecl()) == 708 Context.getCanonicalDecl(Y.getAsDecl()); 709 710 case TemplateArgument::Integral: 711 return *X.getAsIntegral() == *Y.getAsIntegral(); 712 713 case TemplateArgument::Expression: 714 // FIXME: We assume that all expressions are distinct, but we should 715 // really check their canonical forms. 716 return false; 717 718 case TemplateArgument::Pack: 719 if (X.pack_size() != Y.pack_size()) 720 return false; 721 722 for (TemplateArgument::pack_iterator XP = X.pack_begin(), 723 XPEnd = X.pack_end(), 724 YP = Y.pack_begin(); 725 XP != XPEnd; ++XP, ++YP) 726 if (!isSameTemplateArg(Context, *XP, *YP)) 727 return false; 728 729 return true; 730 } 731 732 return false; 733 } 734 735 /// \brief Helper function to build a TemplateParameter when we don't 736 /// know its type statically. 737 static TemplateParameter makeTemplateParameter(Decl *D) { 738 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) 739 return TemplateParameter(TTP); 740 else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) 741 return TemplateParameter(NTTP); 742 743 return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); 744 } 745 746 /// \brief Perform template argument deduction to determine whether 747 /// the given template arguments match the given class template 748 /// partial specialization per C++ [temp.class.spec.match]. 749 Sema::TemplateDeductionResult 750 Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, 751 const TemplateArgumentList &TemplateArgs, 752 TemplateDeductionInfo &Info) { 753 // C++ [temp.class.spec.match]p2: 754 // A partial specialization matches a given actual template 755 // argument list if the template arguments of the partial 756 // specialization can be deduced from the actual template argument 757 // list (14.8.2). 758 SFINAETrap Trap(*this); 759 llvm::SmallVector<TemplateArgument, 4> Deduced; 760 Deduced.resize(Partial->getTemplateParameters()->size()); 761 if (TemplateDeductionResult Result 762 = ::DeduceTemplateArguments(Context, 763 Partial->getTemplateParameters(), 764 Partial->getTemplateArgs(), 765 TemplateArgs, Info, Deduced)) 766 return Result; 767 768 InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial, 769 Deduced.data(), Deduced.size()); 770 if (Inst) 771 return TDK_InstantiationDepth; 772 773 // C++ [temp.deduct.type]p2: 774 // [...] or if any template argument remains neither deduced nor 775 // explicitly specified, template argument deduction fails. 776 TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(), 777 Deduced.size()); 778 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) { 779 if (Deduced[I].isNull()) { 780 Decl *Param 781 = const_cast<Decl *>(Partial->getTemplateParameters()->getParam(I)); 782 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) 783 Info.Param = TTP; 784 else if (NonTypeTemplateParmDecl *NTTP 785 = dyn_cast<NonTypeTemplateParmDecl>(Param)) 786 Info.Param = NTTP; 787 else 788 Info.Param = cast<TemplateTemplateParmDecl>(Param); 789 return TDK_Incomplete; 790 } 791 792 Builder.Append(Deduced[I]); 793 } 794 795 // Form the template argument list from the deduced template arguments. 796 TemplateArgumentList *DeducedArgumentList 797 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); 798 Info.reset(DeducedArgumentList); 799 800 // Substitute the deduced template arguments into the template 801 // arguments of the class template partial specialization, and 802 // verify that the instantiated template arguments are both valid 803 // and are equivalent to the template arguments originally provided 804 // to the class template. 805 ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate(); 806 const TemplateArgumentList &PartialTemplateArgs = Partial->getTemplateArgs(); 807 for (unsigned I = 0, N = PartialTemplateArgs.flat_size(); I != N; ++I) { 808 Decl *Param = const_cast<Decl *>( 809 ClassTemplate->getTemplateParameters()->getParam(I)); 810 TemplateArgument InstArg = Instantiate(PartialTemplateArgs[I], 811 *DeducedArgumentList); 812 if (InstArg.isNull()) { 813 Info.Param = makeTemplateParameter(Param); 814 Info.FirstArg = PartialTemplateArgs[I]; 815 return TDK_SubstitutionFailure; 816 } 817 818 if (InstArg.getKind() == TemplateArgument::Expression) { 819 // When the argument is an expression, check the expression result 820 // against the actual template parameter to get down to the canonical 821 // template argument. 822 Expr *InstExpr = InstArg.getAsExpr(); 823 if (NonTypeTemplateParmDecl *NTTP 824 = dyn_cast<NonTypeTemplateParmDecl>(Param)) { 825 if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) { 826 Info.Param = makeTemplateParameter(Param); 827 Info.FirstArg = PartialTemplateArgs[I]; 828 return TDK_SubstitutionFailure; 829 } 830 } else if (TemplateTemplateParmDecl *TTP 831 = dyn_cast<TemplateTemplateParmDecl>(Param)) { 832 // FIXME: template template arguments should really resolve to decls 833 DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(InstExpr); 834 if (!DRE || CheckTemplateArgument(TTP, DRE)) { 835 Info.Param = makeTemplateParameter(Param); 836 Info.FirstArg = PartialTemplateArgs[I]; 837 return TDK_SubstitutionFailure; 838 } 839 } 840 } 841 842 if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) { 843 Info.Param = makeTemplateParameter(Param); 844 Info.FirstArg = TemplateArgs[I]; 845 Info.SecondArg = InstArg; 846 return TDK_NonDeducedMismatch; 847 } 848 } 849 850 if (Trap.hasErrorOccurred()) 851 return TDK_SubstitutionFailure; 852 853 return TDK_Success; 854 } 855 856 /// \brief Determine whether the given type T is a simple-template-id type. 857 static bool isSimpleTemplateIdType(QualType T) { 858 if (const TemplateSpecializationType *Spec 859 = T->getAsTemplateSpecializationType()) 860 return Spec->getTemplateName().getAsTemplateDecl() != 0; 861 862 return false; 863 } 864 865 /// \brief Perform template argument deduction from a function call 866 /// (C++ [temp.deduct.call]). 867 /// 868 /// \param FunctionTemplate the function template for which we are performing 869 /// template argument deduction. 870 /// 871 /// \param HasExplicitTemplateArgs whether any template arguments were 872 /// explicitly specified. 873 /// 874 /// \param ExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, 875 /// the explicitly-specified template arguments. 876 /// 877 /// \param NumExplicitTemplateArguments when @p HasExplicitTemplateArgs is true, 878 /// the number of explicitly-specified template arguments in 879 /// @p ExplicitTemplateArguments. This value may be zero. 880 /// 881 /// \param Args the function call arguments 882 /// 883 /// \param NumArgs the number of arguments in Args 884 /// 885 /// \param Specialization if template argument deduction was successful, 886 /// this will be set to the function template specialization produced by 887 /// template argument deduction. 888 /// 889 /// \param Info the argument will be updated to provide additional information 890 /// about template argument deduction. 891 /// 892 /// \returns the result of template argument deduction. 893 Sema::TemplateDeductionResult 894 Sema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, 895 bool HasExplicitTemplateArgs, 896 const TemplateArgument *ExplicitTemplateArgs, 897 unsigned NumExplicitTemplateArgs, 898 Expr **Args, unsigned NumArgs, 899 FunctionDecl *&Specialization, 900 TemplateDeductionInfo &Info) { 901 FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 902 903 // C++ [temp.deduct.call]p1: 904 // Template argument deduction is done by comparing each function template 905 // parameter type (call it P) with the type of the corresponding argument 906 // of the call (call it A) as described below. 907 unsigned CheckArgs = NumArgs; 908 if (NumArgs < Function->getMinRequiredArguments()) 909 return TDK_TooFewArguments; 910 else if (NumArgs > Function->getNumParams()) { 911 const FunctionProtoType *Proto 912 = Function->getType()->getAsFunctionProtoType(); 913 if (!Proto->isVariadic()) 914 return TDK_TooManyArguments; 915 916 CheckArgs = Function->getNumParams(); 917 } 918 919 // Template argument deduction for function templates in a SFINAE context. 920 // Trap any errors that might occur. 921 SFINAETrap Trap(*this); 922 923 // The types of the parameters from which we will perform template argument 924 // deduction. 925 TemplateParameterList *TemplateParams 926 = FunctionTemplate->getTemplateParameters(); 927 llvm::SmallVector<TemplateArgument, 4> Deduced; 928 llvm::SmallVector<QualType, 4> ParamTypes; 929 if (NumExplicitTemplateArgs) { 930 // C++ [temp.arg.explicit]p3: 931 // Template arguments that are present shall be specified in the 932 // declaration order of their corresponding template-parameters. The 933 // template argument list shall not specify more template-arguments than 934 // there are corresponding template-parameters. 935 TemplateArgumentListBuilder Builder(TemplateParams, 936 NumExplicitTemplateArgs); 937 938 // Enter a new template instantiation context where we check the 939 // explicitly-specified template arguments against this function template, 940 // and then substitute them into the function parameter types. 941 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), 942 FunctionTemplate, Deduced.data(), Deduced.size(), 943 ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution); 944 if (Inst) 945 return TDK_InstantiationDepth; 946 947 if (CheckTemplateArgumentList(FunctionTemplate, 948 SourceLocation(), SourceLocation(), 949 ExplicitTemplateArgs, 950 NumExplicitTemplateArgs, 951 SourceLocation(), 952 true, 953 Builder) || Trap.hasErrorOccurred()) 954 return TDK_InvalidExplicitArguments; 955 956 // Form the template argument list from the explicitly-specified 957 // template arguments. 958 TemplateArgumentList *ExplicitArgumentList 959 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); 960 Info.reset(ExplicitArgumentList); 961 962 // Instantiate the types of each of the function parameters given the 963 // explicitly-specified template arguments. 964 for (FunctionDecl::param_iterator P = Function->param_begin(), 965 PEnd = Function->param_end(); 966 P != PEnd; 967 ++P) { 968 QualType ParamType = InstantiateType((*P)->getType(), 969 *ExplicitArgumentList, 970 (*P)->getLocation(), 971 (*P)->getDeclName()); 972 if (ParamType.isNull() || Trap.hasErrorOccurred()) 973 return TDK_SubstitutionFailure; 974 975 ParamTypes.push_back(ParamType); 976 } 977 978 // C++ [temp.arg.explicit]p2: 979 // Trailing template arguments that can be deduced (14.8.2) may be 980 // omitted from the list of explicit template- arguments. If all of the 981 // template arguments can be deduced, they may all be omitted; in this 982 // case, the empty template argument list <> itself may also be omitted. 983 // 984 // Take all of the explicitly-specified arguments and put them into the 985 // set of deduced template arguments. 986 Deduced.reserve(TemplateParams->size()); 987 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) 988 Deduced.push_back(ExplicitArgumentList->get(I)); 989 } else { 990 // Just fill in the parameter types from the function declaration. 991 for (unsigned I = 0; I != CheckArgs; ++I) 992 ParamTypes.push_back(Function->getParamDecl(I)->getType()); 993 } 994 995 // Deduce template arguments from the function parameters. 996 Deduced.resize(TemplateParams->size()); 997 for (unsigned I = 0; I != CheckArgs; ++I) { 998 QualType ParamType = ParamTypes[I]; 999 QualType ArgType = Args[I]->getType(); 1000 1001 // C++ [temp.deduct.call]p2: 1002 // If P is not a reference type: 1003 QualType CanonParamType = Context.getCanonicalType(ParamType); 1004 bool ParamWasReference = isa<ReferenceType>(CanonParamType); 1005 if (!ParamWasReference) { 1006 // - If A is an array type, the pointer type produced by the 1007 // array-to-pointer standard conversion (4.2) is used in place of 1008 // A for type deduction; otherwise, 1009 if (ArgType->isArrayType()) 1010 ArgType = Context.getArrayDecayedType(ArgType); 1011 // - If A is a function type, the pointer type produced by the 1012 // function-to-pointer standard conversion (4.3) is used in place 1013 // of A for type deduction; otherwise, 1014 else if (ArgType->isFunctionType()) 1015 ArgType = Context.getPointerType(ArgType); 1016 else { 1017 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s 1018 // type are ignored for type deduction. 1019 QualType CanonArgType = Context.getCanonicalType(ArgType); 1020 if (CanonArgType.getCVRQualifiers()) 1021 ArgType = CanonArgType.getUnqualifiedType(); 1022 } 1023 } 1024 1025 // C++0x [temp.deduct.call]p3: 1026 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type 1027 // are ignored for type deduction. 1028 if (CanonParamType.getCVRQualifiers()) 1029 ParamType = CanonParamType.getUnqualifiedType(); 1030 if (const ReferenceType *ParamRefType = ParamType->getAsReferenceType()) { 1031 // [...] If P is a reference type, the type referred to by P is used 1032 // for type deduction. 1033 ParamType = ParamRefType->getPointeeType(); 1034 1035 // [...] If P is of the form T&&, where T is a template parameter, and 1036 // the argument is an lvalue, the type A& is used in place of A for 1037 // type deduction. 1038 if (isa<RValueReferenceType>(ParamRefType) && 1039 ParamRefType->getAsTemplateTypeParmType() && 1040 Args[I]->isLvalue(Context) == Expr::LV_Valid) 1041 ArgType = Context.getLValueReferenceType(ArgType); 1042 } 1043 1044 // C++0x [temp.deduct.call]p4: 1045 // In general, the deduction process attempts to find template argument 1046 // values that will make the deduced A identical to A (after the type A 1047 // is transformed as described above). [...] 1048 unsigned TDF = 0; 1049 1050 // - If the original P is a reference type, the deduced A (i.e., the 1051 // type referred to by the reference) can be more cv-qualified than 1052 // the transformed A. 1053 if (ParamWasReference) 1054 TDF |= TDF_ParamWithReferenceType; 1055 // - The transformed A can be another pointer or pointer to member 1056 // type that can be converted to the deduced A via a qualification 1057 // conversion (4.4). 1058 if (ArgType->isPointerType() || ArgType->isMemberPointerType()) 1059 TDF |= TDF_IgnoreQualifiers; 1060 // - If P is a class and P has the form simple-template-id, then the 1061 // transformed A can be a derived class of the deduced A. Likewise, 1062 // if P is a pointer to a class of the form simple-template-id, the 1063 // transformed A can be a pointer to a derived class pointed to by 1064 // the deduced A. 1065 if (isSimpleTemplateIdType(ParamType) || 1066 (isa<PointerType>(ParamType) && 1067 isSimpleTemplateIdType( 1068 ParamType->getAsPointerType()->getPointeeType()))) 1069 TDF |= TDF_DerivedClass; 1070 1071 if (TemplateDeductionResult Result 1072 = ::DeduceTemplateArguments(Context, TemplateParams, 1073 ParamType, ArgType, Info, Deduced, 1074 TDF)) 1075 return Result; 1076 1077 // FIXME: C++ [temp.deduct.call] paragraphs 6-9 deal with function 1078 // pointer parameters. 1079 } 1080 1081 // C++ [temp.deduct.type]p2: 1082 // [...] or if any template argument remains neither deduced nor 1083 // explicitly specified, template argument deduction fails. 1084 TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size()); 1085 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) { 1086 if (Deduced[I].isNull()) { 1087 Decl *Param 1088 = const_cast<Decl *>(TemplateParams->getParam(I)); 1089 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) 1090 Info.Param = TTP; 1091 else if (NonTypeTemplateParmDecl *NTTP 1092 = dyn_cast<NonTypeTemplateParmDecl>(Param)) 1093 Info.Param = NTTP; 1094 else 1095 Info.Param = cast<TemplateTemplateParmDecl>(Param); 1096 return TDK_Incomplete; 1097 } 1098 1099 Builder.Append(Deduced[I]); 1100 } 1101 1102 // Form the template argument list from the deduced template arguments. 1103 TemplateArgumentList *DeducedArgumentList 1104 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); 1105 Info.reset(DeducedArgumentList); 1106 1107 // Enter a new template instantiation context while we instantiate the 1108 // actual function declaration. 1109 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), 1110 FunctionTemplate, Deduced.data(), Deduced.size(), 1111 ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution); 1112 if (Inst) 1113 return TDK_InstantiationDepth; 1114 1115 // Substitute the deduced template arguments into the function template 1116 // declaration to produce the function template specialization. 1117 Specialization = cast_or_null<FunctionDecl>( 1118 InstantiateDecl(FunctionTemplate->getTemplatedDecl(), 1119 FunctionTemplate->getDeclContext(), 1120 *DeducedArgumentList)); 1121 if (!Specialization) 1122 return TDK_SubstitutionFailure; 1123 1124 // If the template argument list is owned by the function template 1125 // specialization, release it. 1126 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList) 1127 Info.take(); 1128 1129 // There may have been an error that did not prevent us from constructing a 1130 // declaration. Mark the declaration invalid and return with a substitution 1131 // failure. 1132 if (Trap.hasErrorOccurred()) { 1133 Specialization->setInvalidDecl(true); 1134 return TDK_SubstitutionFailure; 1135 } 1136 1137 return TDK_Success; 1138 } 1139 1140 static void 1141 MarkDeducedTemplateParameters(Sema &SemaRef, 1142 const TemplateArgument &TemplateArg, 1143 llvm::SmallVectorImpl<bool> &Deduced); 1144 1145 /// \brief Mark the template arguments that are deduced by the given 1146 /// expression. 1147 static void 1148 MarkDeducedTemplateParameters(const Expr *E, 1149 llvm::SmallVectorImpl<bool> &Deduced) { 1150 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); 1151 if (!E) 1152 return; 1153 1154 const NonTypeTemplateParmDecl *NTTP 1155 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); 1156 if (!NTTP) 1157 return; 1158 1159 Deduced[NTTP->getIndex()] = true; 1160 } 1161 1162 /// \brief Mark the template parameters that are deduced by the given 1163 /// type. 1164 static void 1165 MarkDeducedTemplateParameters(Sema &SemaRef, QualType T, 1166 llvm::SmallVectorImpl<bool> &Deduced) { 1167 // Non-dependent types have nothing deducible 1168 if (!T->isDependentType()) 1169 return; 1170 1171 T = SemaRef.Context.getCanonicalType(T); 1172 switch (T->getTypeClass()) { 1173 case Type::ExtQual: 1174 MarkDeducedTemplateParameters(SemaRef, 1175 QualType(cast<ExtQualType>(T)->getBaseType(), 0), 1176 Deduced); 1177 break; 1178 1179 case Type::Pointer: 1180 MarkDeducedTemplateParameters(SemaRef, 1181 cast<PointerType>(T)->getPointeeType(), 1182 Deduced); 1183 break; 1184 1185 case Type::BlockPointer: 1186 MarkDeducedTemplateParameters(SemaRef, 1187 cast<BlockPointerType>(T)->getPointeeType(), 1188 Deduced); 1189 break; 1190 1191 case Type::LValueReference: 1192 case Type::RValueReference: 1193 MarkDeducedTemplateParameters(SemaRef, 1194 cast<ReferenceType>(T)->getPointeeType(), 1195 Deduced); 1196 break; 1197 1198 case Type::MemberPointer: { 1199 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); 1200 MarkDeducedTemplateParameters(SemaRef, MemPtr->getPointeeType(), Deduced); 1201 MarkDeducedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0), 1202 Deduced); 1203 break; 1204 } 1205 1206 case Type::DependentSizedArray: 1207 MarkDeducedTemplateParameters(cast<DependentSizedArrayType>(T)->getSizeExpr(), 1208 Deduced); 1209 // Fall through to check the element type 1210 1211 case Type::ConstantArray: 1212 case Type::IncompleteArray: 1213 MarkDeducedTemplateParameters(SemaRef, 1214 cast<ArrayType>(T)->getElementType(), 1215 Deduced); 1216 break; 1217 1218 case Type::Vector: 1219 case Type::ExtVector: 1220 MarkDeducedTemplateParameters(SemaRef, 1221 cast<VectorType>(T)->getElementType(), 1222 Deduced); 1223 break; 1224 1225 case Type::DependentSizedExtVector: { 1226 const DependentSizedExtVectorType *VecType 1227 = cast<DependentSizedExtVectorType>(T); 1228 MarkDeducedTemplateParameters(SemaRef, VecType->getElementType(), Deduced); 1229 MarkDeducedTemplateParameters(VecType->getSizeExpr(), Deduced); 1230 break; 1231 } 1232 1233 case Type::FunctionProto: { 1234 const FunctionProtoType *Proto = cast<FunctionProtoType>(T); 1235 MarkDeducedTemplateParameters(SemaRef, Proto->getResultType(), Deduced); 1236 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I) 1237 MarkDeducedTemplateParameters(SemaRef, Proto->getArgType(I), Deduced); 1238 break; 1239 } 1240 1241 case Type::TemplateTypeParm: 1242 Deduced[cast<TemplateTypeParmType>(T)->getIndex()] = true; 1243 break; 1244 1245 case Type::TemplateSpecialization: { 1246 const TemplateSpecializationType *Spec 1247 = cast<TemplateSpecializationType>(T); 1248 if (TemplateDecl *Template = Spec->getTemplateName().getAsTemplateDecl()) 1249 if (TemplateTemplateParmDecl *TTP 1250 = dyn_cast<TemplateTemplateParmDecl>(Template)) 1251 Deduced[TTP->getIndex()] = true; 1252 1253 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) 1254 MarkDeducedTemplateParameters(SemaRef, Spec->getArg(I), Deduced); 1255 1256 break; 1257 } 1258 1259 // None of these types have any deducible parts. 1260 case Type::Builtin: 1261 case Type::FixedWidthInt: 1262 case Type::Complex: 1263 case Type::VariableArray: 1264 case Type::FunctionNoProto: 1265 case Type::Record: 1266 case Type::Enum: 1267 case Type::Typename: 1268 case Type::ObjCInterface: 1269 case Type::ObjCQualifiedInterface: 1270 case Type::ObjCObjectPointer: 1271 #define TYPE(Class, Base) 1272 #define ABSTRACT_TYPE(Class, Base) 1273 #define DEPENDENT_TYPE(Class, Base) 1274 #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 1275 #include "clang/AST/TypeNodes.def" 1276 break; 1277 } 1278 } 1279 1280 /// \brief Mark the template parameters that are deduced by this 1281 /// template argument. 1282 static void 1283 MarkDeducedTemplateParameters(Sema &SemaRef, 1284 const TemplateArgument &TemplateArg, 1285 llvm::SmallVectorImpl<bool> &Deduced) { 1286 switch (TemplateArg.getKind()) { 1287 case TemplateArgument::Null: 1288 case TemplateArgument::Integral: 1289 break; 1290 1291 case TemplateArgument::Type: 1292 MarkDeducedTemplateParameters(SemaRef, TemplateArg.getAsType(), Deduced); 1293 break; 1294 1295 case TemplateArgument::Declaration: 1296 if (TemplateTemplateParmDecl *TTP 1297 = dyn_cast<TemplateTemplateParmDecl>(TemplateArg.getAsDecl())) 1298 Deduced[TTP->getIndex()] = true; 1299 break; 1300 1301 case TemplateArgument::Expression: 1302 MarkDeducedTemplateParameters(TemplateArg.getAsExpr(), Deduced); 1303 break; 1304 case TemplateArgument::Pack: 1305 assert(0 && "FIXME: Implement!"); 1306 break; 1307 } 1308 } 1309 1310 /// \brief Mark the template parameters can be deduced by the given 1311 /// template argument list. 1312 /// 1313 /// \param TemplateArgs the template argument list from which template 1314 /// parameters will be deduced. 1315 /// 1316 /// \param Deduced a bit vector whose elements will be set to \c true 1317 /// to indicate when the corresponding template parameter will be 1318 /// deduced. 1319 void 1320 Sema::MarkDeducedTemplateParameters(const TemplateArgumentList &TemplateArgs, 1321 llvm::SmallVectorImpl<bool> &Deduced) { 1322 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) 1323 ::MarkDeducedTemplateParameters(*this, TemplateArgs[I], Deduced); 1324 } 1325