SemaTemplateDeduction.cpp revision 16134c62ef3d146e0dd0c76aafb906ff12c0a15d
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 22namespace 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 45using namespace clang; 46 47static Sema::TemplateDeductionResult 48DeduceTemplateArguments(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. 58static 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. 70static Sema::TemplateDeductionResult 71DeduceNonTypeTemplateArgument(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 125static Sema::TemplateDeductionResult 126DeduceNonTypeTemplateArgument(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 153static Sema::TemplateDeductionResult 154DeduceTemplateArguments(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. 204static Sema::TemplateDeductionResult 205DeduceTemplateArguments(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 597static Sema::TemplateDeductionResult 598DeduceTemplateArguments(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 672static Sema::TemplateDeductionResult 673DeduceTemplateArguments(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. 691static 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. 737static 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]. 749Sema::TemplateDeductionResult 750Sema::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. 857static 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. 893Sema::TemplateDeductionResult 894Sema::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 if (Inst) 944 return TDK_InstantiationDepth; 945 946 if (CheckTemplateArgumentList(FunctionTemplate, 947 SourceLocation(), SourceLocation(), 948 ExplicitTemplateArgs, 949 NumExplicitTemplateArgs, 950 SourceLocation(), 951 true, 952 Builder) || Trap.hasErrorOccurred()) 953 return TDK_InvalidExplicitArguments; 954 955 // Form the template argument list from the explicitly-specified 956 // template arguments. 957 TemplateArgumentList *ExplicitArgumentList 958 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); 959 Info.reset(ExplicitArgumentList); 960 961 // Instantiate the types of each of the function parameters given the 962 // explicitly-specified template arguments. 963 for (FunctionDecl::param_iterator P = Function->param_begin(), 964 PEnd = Function->param_end(); 965 P != PEnd; 966 ++P) { 967 QualType ParamType = InstantiateType((*P)->getType(), 968 *ExplicitArgumentList, 969 (*P)->getLocation(), 970 (*P)->getDeclName()); 971 if (ParamType.isNull() || Trap.hasErrorOccurred()) 972 return TDK_SubstitutionFailure; 973 974 ParamTypes.push_back(ParamType); 975 } 976 977 // C++ [temp.arg.explicit]p2: 978 // Trailing template arguments that can be deduced (14.8.2) may be 979 // omitted from the list of explicit template- arguments. If all of the 980 // template arguments can be deduced, they may all be omitted; in this 981 // case, the empty template argument list <> itself may also be omitted. 982 // 983 // Take all of the explicitly-specified arguments and put them into the 984 // set of deduced template arguments. 985 Deduced.reserve(TemplateParams->size()); 986 for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) 987 Deduced.push_back(ExplicitArgumentList->get(I)); 988 } else { 989 // Just fill in the parameter types from the function declaration. 990 for (unsigned I = 0; I != CheckArgs; ++I) 991 ParamTypes.push_back(Function->getParamDecl(I)->getType()); 992 } 993 994 // Deduce template arguments from the function parameters. 995 Deduced.resize(TemplateParams->size()); 996 for (unsigned I = 0; I != CheckArgs; ++I) { 997 QualType ParamType = ParamTypes[I]; 998 QualType ArgType = Args[I]->getType(); 999 1000 // C++ [temp.deduct.call]p2: 1001 // If P is not a reference type: 1002 QualType CanonParamType = Context.getCanonicalType(ParamType); 1003 bool ParamWasReference = isa<ReferenceType>(CanonParamType); 1004 if (!ParamWasReference) { 1005 // - If A is an array type, the pointer type produced by the 1006 // array-to-pointer standard conversion (4.2) is used in place of 1007 // A for type deduction; otherwise, 1008 if (ArgType->isArrayType()) 1009 ArgType = Context.getArrayDecayedType(ArgType); 1010 // - If A is a function type, the pointer type produced by the 1011 // function-to-pointer standard conversion (4.3) is used in place 1012 // of A for type deduction; otherwise, 1013 else if (ArgType->isFunctionType()) 1014 ArgType = Context.getPointerType(ArgType); 1015 else { 1016 // - If A is a cv-qualified type, the top level cv-qualifiers of A’s 1017 // type are ignored for type deduction. 1018 QualType CanonArgType = Context.getCanonicalType(ArgType); 1019 if (CanonArgType.getCVRQualifiers()) 1020 ArgType = CanonArgType.getUnqualifiedType(); 1021 } 1022 } 1023 1024 // C++0x [temp.deduct.call]p3: 1025 // If P is a cv-qualified type, the top level cv-qualifiers of P’s type 1026 // are ignored for type deduction. 1027 if (CanonParamType.getCVRQualifiers()) 1028 ParamType = CanonParamType.getUnqualifiedType(); 1029 if (const ReferenceType *ParamRefType = ParamType->getAsReferenceType()) { 1030 // [...] If P is a reference type, the type referred to by P is used 1031 // for type deduction. 1032 ParamType = ParamRefType->getPointeeType(); 1033 1034 // [...] If P is of the form T&&, where T is a template parameter, and 1035 // the argument is an lvalue, the type A& is used in place of A for 1036 // type deduction. 1037 if (isa<RValueReferenceType>(ParamRefType) && 1038 ParamRefType->getAsTemplateTypeParmType() && 1039 Args[I]->isLvalue(Context) == Expr::LV_Valid) 1040 ArgType = Context.getLValueReferenceType(ArgType); 1041 } 1042 1043 // C++0x [temp.deduct.call]p4: 1044 // In general, the deduction process attempts to find template argument 1045 // values that will make the deduced A identical to A (after the type A 1046 // is transformed as described above). [...] 1047 unsigned TDF = 0; 1048 1049 // - If the original P is a reference type, the deduced A (i.e., the 1050 // type referred to by the reference) can be more cv-qualified than 1051 // the transformed A. 1052 if (ParamWasReference) 1053 TDF |= TDF_ParamWithReferenceType; 1054 // - The transformed A can be another pointer or pointer to member 1055 // type that can be converted to the deduced A via a qualification 1056 // conversion (4.4). 1057 if (ArgType->isPointerType() || ArgType->isMemberPointerType()) 1058 TDF |= TDF_IgnoreQualifiers; 1059 // - If P is a class and P has the form simple-template-id, then the 1060 // transformed A can be a derived class of the deduced A. Likewise, 1061 // if P is a pointer to a class of the form simple-template-id, the 1062 // transformed A can be a pointer to a derived class pointed to by 1063 // the deduced A. 1064 if (isSimpleTemplateIdType(ParamType) || 1065 (isa<PointerType>(ParamType) && 1066 isSimpleTemplateIdType( 1067 ParamType->getAsPointerType()->getPointeeType()))) 1068 TDF |= TDF_DerivedClass; 1069 1070 if (TemplateDeductionResult Result 1071 = ::DeduceTemplateArguments(Context, TemplateParams, 1072 ParamType, ArgType, Info, Deduced, 1073 TDF)) 1074 return Result; 1075 1076 // FIXME: C++ [temp.deduct.call] paragraphs 6-9 deal with function 1077 // pointer parameters. 1078 } 1079 1080 // C++ [temp.deduct.type]p2: 1081 // [...] or if any template argument remains neither deduced nor 1082 // explicitly specified, template argument deduction fails. 1083 TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size()); 1084 for (unsigned I = 0, N = Deduced.size(); I != N; ++I) { 1085 if (Deduced[I].isNull()) { 1086 Decl *Param 1087 = const_cast<Decl *>(TemplateParams->getParam(I)); 1088 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param)) 1089 Info.Param = TTP; 1090 else if (NonTypeTemplateParmDecl *NTTP 1091 = dyn_cast<NonTypeTemplateParmDecl>(Param)) 1092 Info.Param = NTTP; 1093 else 1094 Info.Param = cast<TemplateTemplateParmDecl>(Param); 1095 return TDK_Incomplete; 1096 } 1097 1098 Builder.Append(Deduced[I]); 1099 } 1100 1101 // Form the template argument list from the deduced template arguments. 1102 TemplateArgumentList *DeducedArgumentList 1103 = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); 1104 Info.reset(DeducedArgumentList); 1105 1106 // Enter a new template instantiation context while we instantiate the 1107 // actual function declaration. 1108 InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), 1109 FunctionTemplate, Deduced.data(), Deduced.size()); 1110 if (Inst) 1111 return TDK_InstantiationDepth; 1112 1113 // Substitute the deduced template arguments into the function template 1114 // declaration to produce the function template specialization. 1115 Specialization = cast_or_null<FunctionDecl>( 1116 InstantiateDecl(FunctionTemplate->getTemplatedDecl(), 1117 FunctionTemplate->getDeclContext(), 1118 *DeducedArgumentList)); 1119 if (!Specialization) 1120 return TDK_SubstitutionFailure; 1121 1122 // If the template argument list is owned by the function template 1123 // specialization, release it. 1124 if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList) 1125 Info.take(); 1126 1127 // There may have been an error that did not prevent us from constructing a 1128 // declaration. Mark the declaration invalid and return with a substitution 1129 // failure. 1130 if (Trap.hasErrorOccurred()) { 1131 Specialization->setInvalidDecl(true); 1132 return TDK_SubstitutionFailure; 1133 } 1134 1135 return TDK_Success; 1136} 1137 1138static void 1139MarkDeducedTemplateParameters(Sema &SemaRef, 1140 const TemplateArgument &TemplateArg, 1141 llvm::SmallVectorImpl<bool> &Deduced); 1142 1143/// \brief Mark the template arguments that are deduced by the given 1144/// expression. 1145static void 1146MarkDeducedTemplateParameters(const Expr *E, 1147 llvm::SmallVectorImpl<bool> &Deduced) { 1148 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); 1149 if (!E) 1150 return; 1151 1152 const NonTypeTemplateParmDecl *NTTP 1153 = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); 1154 if (!NTTP) 1155 return; 1156 1157 Deduced[NTTP->getIndex()] = true; 1158} 1159 1160/// \brief Mark the template parameters that are deduced by the given 1161/// type. 1162static void 1163MarkDeducedTemplateParameters(Sema &SemaRef, QualType T, 1164 llvm::SmallVectorImpl<bool> &Deduced) { 1165 // Non-dependent types have nothing deducible 1166 if (!T->isDependentType()) 1167 return; 1168 1169 T = SemaRef.Context.getCanonicalType(T); 1170 switch (T->getTypeClass()) { 1171 case Type::ExtQual: 1172 MarkDeducedTemplateParameters(SemaRef, 1173 QualType(cast<ExtQualType>(T)->getBaseType(), 0), 1174 Deduced); 1175 break; 1176 1177 case Type::Pointer: 1178 MarkDeducedTemplateParameters(SemaRef, 1179 cast<PointerType>(T)->getPointeeType(), 1180 Deduced); 1181 break; 1182 1183 case Type::BlockPointer: 1184 MarkDeducedTemplateParameters(SemaRef, 1185 cast<BlockPointerType>(T)->getPointeeType(), 1186 Deduced); 1187 break; 1188 1189 case Type::LValueReference: 1190 case Type::RValueReference: 1191 MarkDeducedTemplateParameters(SemaRef, 1192 cast<ReferenceType>(T)->getPointeeType(), 1193 Deduced); 1194 break; 1195 1196 case Type::MemberPointer: { 1197 const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); 1198 MarkDeducedTemplateParameters(SemaRef, MemPtr->getPointeeType(), Deduced); 1199 MarkDeducedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0), 1200 Deduced); 1201 break; 1202 } 1203 1204 case Type::DependentSizedArray: 1205 MarkDeducedTemplateParameters(cast<DependentSizedArrayType>(T)->getSizeExpr(), 1206 Deduced); 1207 // Fall through to check the element type 1208 1209 case Type::ConstantArray: 1210 case Type::IncompleteArray: 1211 MarkDeducedTemplateParameters(SemaRef, 1212 cast<ArrayType>(T)->getElementType(), 1213 Deduced); 1214 break; 1215 1216 case Type::Vector: 1217 case Type::ExtVector: 1218 MarkDeducedTemplateParameters(SemaRef, 1219 cast<VectorType>(T)->getElementType(), 1220 Deduced); 1221 break; 1222 1223 case Type::DependentSizedExtVector: { 1224 const DependentSizedExtVectorType *VecType 1225 = cast<DependentSizedExtVectorType>(T); 1226 MarkDeducedTemplateParameters(SemaRef, VecType->getElementType(), Deduced); 1227 MarkDeducedTemplateParameters(VecType->getSizeExpr(), Deduced); 1228 break; 1229 } 1230 1231 case Type::FunctionProto: { 1232 const FunctionProtoType *Proto = cast<FunctionProtoType>(T); 1233 MarkDeducedTemplateParameters(SemaRef, Proto->getResultType(), Deduced); 1234 for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I) 1235 MarkDeducedTemplateParameters(SemaRef, Proto->getArgType(I), Deduced); 1236 break; 1237 } 1238 1239 case Type::TemplateTypeParm: 1240 Deduced[cast<TemplateTypeParmType>(T)->getIndex()] = true; 1241 break; 1242 1243 case Type::TemplateSpecialization: { 1244 const TemplateSpecializationType *Spec 1245 = cast<TemplateSpecializationType>(T); 1246 if (TemplateDecl *Template = Spec->getTemplateName().getAsTemplateDecl()) 1247 if (TemplateTemplateParmDecl *TTP 1248 = dyn_cast<TemplateTemplateParmDecl>(Template)) 1249 Deduced[TTP->getIndex()] = true; 1250 1251 for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) 1252 MarkDeducedTemplateParameters(SemaRef, Spec->getArg(I), Deduced); 1253 1254 break; 1255 } 1256 1257 // None of these types have any deducible parts. 1258 case Type::Builtin: 1259 case Type::FixedWidthInt: 1260 case Type::Complex: 1261 case Type::VariableArray: 1262 case Type::FunctionNoProto: 1263 case Type::Record: 1264 case Type::Enum: 1265 case Type::Typename: 1266 case Type::ObjCInterface: 1267 case Type::ObjCQualifiedInterface: 1268 case Type::ObjCObjectPointer: 1269#define TYPE(Class, Base) 1270#define ABSTRACT_TYPE(Class, Base) 1271#define DEPENDENT_TYPE(Class, Base) 1272#define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 1273#include "clang/AST/TypeNodes.def" 1274 break; 1275 } 1276} 1277 1278/// \brief Mark the template parameters that are deduced by this 1279/// template argument. 1280static void 1281MarkDeducedTemplateParameters(Sema &SemaRef, 1282 const TemplateArgument &TemplateArg, 1283 llvm::SmallVectorImpl<bool> &Deduced) { 1284 switch (TemplateArg.getKind()) { 1285 case TemplateArgument::Null: 1286 case TemplateArgument::Integral: 1287 break; 1288 1289 case TemplateArgument::Type: 1290 MarkDeducedTemplateParameters(SemaRef, TemplateArg.getAsType(), Deduced); 1291 break; 1292 1293 case TemplateArgument::Declaration: 1294 if (TemplateTemplateParmDecl *TTP 1295 = dyn_cast<TemplateTemplateParmDecl>(TemplateArg.getAsDecl())) 1296 Deduced[TTP->getIndex()] = true; 1297 break; 1298 1299 case TemplateArgument::Expression: 1300 MarkDeducedTemplateParameters(TemplateArg.getAsExpr(), Deduced); 1301 break; 1302 case TemplateArgument::Pack: 1303 assert(0 && "FIXME: Implement!"); 1304 break; 1305 } 1306} 1307 1308/// \brief Mark the template parameters can be deduced by the given 1309/// template argument list. 1310/// 1311/// \param TemplateArgs the template argument list from which template 1312/// parameters will be deduced. 1313/// 1314/// \param Deduced a bit vector whose elements will be set to \c true 1315/// to indicate when the corresponding template parameter will be 1316/// deduced. 1317void 1318Sema::MarkDeducedTemplateParameters(const TemplateArgumentList &TemplateArgs, 1319 llvm::SmallVectorImpl<bool> &Deduced) { 1320 for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) 1321 ::MarkDeducedTemplateParameters(*this, TemplateArgs[I], Deduced); 1322} 1323