SemaTemplateInstantiate.cpp revision 9151c11836f5fbb36cedfe4d22df7e00e77a1d42
1//===------- SemaTemplateInstantiate.cpp - C++ Template Instantiation ------===/ 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 instantiation. 10// 11//===----------------------------------------------------------------------===/ 12 13#include "clang/Sema/SemaInternal.h" 14#include "TreeTransform.h" 15#include "clang/Sema/DeclSpec.h" 16#include "clang/Sema/Lookup.h" 17#include "clang/Sema/Template.h" 18#include "clang/Sema/TemplateDeduction.h" 19#include "clang/AST/ASTConsumer.h" 20#include "clang/AST/ASTContext.h" 21#include "clang/AST/Expr.h" 22#include "clang/AST/DeclTemplate.h" 23#include "clang/Basic/LangOptions.h" 24 25using namespace clang; 26using namespace sema; 27 28//===----------------------------------------------------------------------===/ 29// Template Instantiation Support 30//===----------------------------------------------------------------------===/ 31 32/// \brief Retrieve the template argument list(s) that should be used to 33/// instantiate the definition of the given declaration. 34/// 35/// \param D the declaration for which we are computing template instantiation 36/// arguments. 37/// 38/// \param Innermost if non-NULL, the innermost template argument list. 39/// 40/// \param RelativeToPrimary true if we should get the template 41/// arguments relative to the primary template, even when we're 42/// dealing with a specialization. This is only relevant for function 43/// template specializations. 44/// 45/// \param Pattern If non-NULL, indicates the pattern from which we will be 46/// instantiating the definition of the given declaration, \p D. This is 47/// used to determine the proper set of template instantiation arguments for 48/// friend function template specializations. 49MultiLevelTemplateArgumentList 50Sema::getTemplateInstantiationArgs(NamedDecl *D, 51 const TemplateArgumentList *Innermost, 52 bool RelativeToPrimary, 53 const FunctionDecl *Pattern) { 54 // Accumulate the set of template argument lists in this structure. 55 MultiLevelTemplateArgumentList Result; 56 57 if (Innermost) 58 Result.addOuterTemplateArguments(Innermost); 59 60 DeclContext *Ctx = dyn_cast<DeclContext>(D); 61 if (!Ctx) 62 Ctx = D->getDeclContext(); 63 64 while (!Ctx->isFileContext()) { 65 // Add template arguments from a class template instantiation. 66 if (ClassTemplateSpecializationDecl *Spec 67 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx)) { 68 // We're done when we hit an explicit specialization. 69 if (Spec->getSpecializationKind() == TSK_ExplicitSpecialization && 70 !isa<ClassTemplatePartialSpecializationDecl>(Spec)) 71 break; 72 73 Result.addOuterTemplateArguments(&Spec->getTemplateInstantiationArgs()); 74 75 // If this class template specialization was instantiated from a 76 // specialized member that is a class template, we're done. 77 assert(Spec->getSpecializedTemplate() && "No class template?"); 78 if (Spec->getSpecializedTemplate()->isMemberSpecialization()) 79 break; 80 } 81 // Add template arguments from a function template specialization. 82 else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(Ctx)) { 83 if (!RelativeToPrimary && 84 Function->getTemplateSpecializationKind() 85 == TSK_ExplicitSpecialization) 86 break; 87 88 if (const TemplateArgumentList *TemplateArgs 89 = Function->getTemplateSpecializationArgs()) { 90 // Add the template arguments for this specialization. 91 Result.addOuterTemplateArguments(TemplateArgs); 92 93 // If this function was instantiated from a specialized member that is 94 // a function template, we're done. 95 assert(Function->getPrimaryTemplate() && "No function template?"); 96 if (Function->getPrimaryTemplate()->isMemberSpecialization()) 97 break; 98 } 99 100 // If this is a friend declaration and it declares an entity at 101 // namespace scope, take arguments from its lexical parent 102 // instead of its semantic parent, unless of course the pattern we're 103 // instantiating actually comes from the file's context! 104 if (Function->getFriendObjectKind() && 105 Function->getDeclContext()->isFileContext() && 106 (!Pattern || !Pattern->getLexicalDeclContext()->isFileContext())) { 107 Ctx = Function->getLexicalDeclContext(); 108 RelativeToPrimary = false; 109 continue; 110 } 111 } else if (CXXRecordDecl *Rec = dyn_cast<CXXRecordDecl>(Ctx)) { 112 if (ClassTemplateDecl *ClassTemplate = Rec->getDescribedClassTemplate()) { 113 QualType T = ClassTemplate->getInjectedClassNameSpecialization(); 114 const TemplateSpecializationType *TST 115 = cast<TemplateSpecializationType>(Context.getCanonicalType(T)); 116 Result.addOuterTemplateArguments(TST->getArgs(), TST->getNumArgs()); 117 if (ClassTemplate->isMemberSpecialization()) 118 break; 119 } 120 } 121 122 Ctx = Ctx->getParent(); 123 RelativeToPrimary = false; 124 } 125 126 return Result; 127} 128 129bool Sema::ActiveTemplateInstantiation::isInstantiationRecord() const { 130 switch (Kind) { 131 case TemplateInstantiation: 132 case DefaultTemplateArgumentInstantiation: 133 case DefaultFunctionArgumentInstantiation: 134 return true; 135 136 case ExplicitTemplateArgumentSubstitution: 137 case DeducedTemplateArgumentSubstitution: 138 case PriorTemplateArgumentSubstitution: 139 case DefaultTemplateArgumentChecking: 140 return false; 141 } 142 143 return true; 144} 145 146Sema::InstantiatingTemplate:: 147InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, 148 Decl *Entity, 149 SourceRange InstantiationRange) 150 : SemaRef(SemaRef), 151 SavedInNonInstantiationSFINAEContext( 152 SemaRef.InNonInstantiationSFINAEContext) 153{ 154 Invalid = CheckInstantiationDepth(PointOfInstantiation, 155 InstantiationRange); 156 if (!Invalid) { 157 ActiveTemplateInstantiation Inst; 158 Inst.Kind = ActiveTemplateInstantiation::TemplateInstantiation; 159 Inst.PointOfInstantiation = PointOfInstantiation; 160 Inst.Entity = reinterpret_cast<uintptr_t>(Entity); 161 Inst.TemplateArgs = 0; 162 Inst.NumTemplateArgs = 0; 163 Inst.InstantiationRange = InstantiationRange; 164 SemaRef.InNonInstantiationSFINAEContext = false; 165 SemaRef.ActiveTemplateInstantiations.push_back(Inst); 166 } 167} 168 169Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, 170 SourceLocation PointOfInstantiation, 171 TemplateDecl *Template, 172 const TemplateArgument *TemplateArgs, 173 unsigned NumTemplateArgs, 174 SourceRange InstantiationRange) 175 : SemaRef(SemaRef), 176 SavedInNonInstantiationSFINAEContext( 177 SemaRef.InNonInstantiationSFINAEContext) 178{ 179 Invalid = CheckInstantiationDepth(PointOfInstantiation, 180 InstantiationRange); 181 if (!Invalid) { 182 ActiveTemplateInstantiation Inst; 183 Inst.Kind 184 = ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation; 185 Inst.PointOfInstantiation = PointOfInstantiation; 186 Inst.Entity = reinterpret_cast<uintptr_t>(Template); 187 Inst.TemplateArgs = TemplateArgs; 188 Inst.NumTemplateArgs = NumTemplateArgs; 189 Inst.InstantiationRange = InstantiationRange; 190 SemaRef.InNonInstantiationSFINAEContext = false; 191 SemaRef.ActiveTemplateInstantiations.push_back(Inst); 192 } 193} 194 195Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, 196 SourceLocation PointOfInstantiation, 197 FunctionTemplateDecl *FunctionTemplate, 198 const TemplateArgument *TemplateArgs, 199 unsigned NumTemplateArgs, 200 ActiveTemplateInstantiation::InstantiationKind Kind, 201 sema::TemplateDeductionInfo &DeductionInfo, 202 SourceRange InstantiationRange) 203 : SemaRef(SemaRef), 204 SavedInNonInstantiationSFINAEContext( 205 SemaRef.InNonInstantiationSFINAEContext) 206{ 207 Invalid = CheckInstantiationDepth(PointOfInstantiation, 208 InstantiationRange); 209 if (!Invalid) { 210 ActiveTemplateInstantiation Inst; 211 Inst.Kind = Kind; 212 Inst.PointOfInstantiation = PointOfInstantiation; 213 Inst.Entity = reinterpret_cast<uintptr_t>(FunctionTemplate); 214 Inst.TemplateArgs = TemplateArgs; 215 Inst.NumTemplateArgs = NumTemplateArgs; 216 Inst.DeductionInfo = &DeductionInfo; 217 Inst.InstantiationRange = InstantiationRange; 218 SemaRef.InNonInstantiationSFINAEContext = false; 219 SemaRef.ActiveTemplateInstantiations.push_back(Inst); 220 221 if (!Inst.isInstantiationRecord()) 222 ++SemaRef.NonInstantiationEntries; 223 } 224} 225 226Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, 227 SourceLocation PointOfInstantiation, 228 ClassTemplatePartialSpecializationDecl *PartialSpec, 229 const TemplateArgument *TemplateArgs, 230 unsigned NumTemplateArgs, 231 sema::TemplateDeductionInfo &DeductionInfo, 232 SourceRange InstantiationRange) 233 : SemaRef(SemaRef), 234 SavedInNonInstantiationSFINAEContext( 235 SemaRef.InNonInstantiationSFINAEContext) 236{ 237 Invalid = false; 238 239 ActiveTemplateInstantiation Inst; 240 Inst.Kind = ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution; 241 Inst.PointOfInstantiation = PointOfInstantiation; 242 Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec); 243 Inst.TemplateArgs = TemplateArgs; 244 Inst.NumTemplateArgs = NumTemplateArgs; 245 Inst.DeductionInfo = &DeductionInfo; 246 Inst.InstantiationRange = InstantiationRange; 247 SemaRef.InNonInstantiationSFINAEContext = false; 248 SemaRef.ActiveTemplateInstantiations.push_back(Inst); 249 250 assert(!Inst.isInstantiationRecord()); 251 ++SemaRef.NonInstantiationEntries; 252} 253 254Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, 255 SourceLocation PointOfInstantiation, 256 ParmVarDecl *Param, 257 const TemplateArgument *TemplateArgs, 258 unsigned NumTemplateArgs, 259 SourceRange InstantiationRange) 260 : SemaRef(SemaRef), 261 SavedInNonInstantiationSFINAEContext( 262 SemaRef.InNonInstantiationSFINAEContext) 263{ 264 Invalid = CheckInstantiationDepth(PointOfInstantiation, InstantiationRange); 265 266 if (!Invalid) { 267 ActiveTemplateInstantiation Inst; 268 Inst.Kind 269 = ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation; 270 Inst.PointOfInstantiation = PointOfInstantiation; 271 Inst.Entity = reinterpret_cast<uintptr_t>(Param); 272 Inst.TemplateArgs = TemplateArgs; 273 Inst.NumTemplateArgs = NumTemplateArgs; 274 Inst.InstantiationRange = InstantiationRange; 275 SemaRef.InNonInstantiationSFINAEContext = false; 276 SemaRef.ActiveTemplateInstantiations.push_back(Inst); 277 } 278} 279 280Sema::InstantiatingTemplate:: 281InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, 282 NamedDecl *Template, 283 NonTypeTemplateParmDecl *Param, 284 const TemplateArgument *TemplateArgs, 285 unsigned NumTemplateArgs, 286 SourceRange InstantiationRange) 287 : SemaRef(SemaRef), 288 SavedInNonInstantiationSFINAEContext( 289 SemaRef.InNonInstantiationSFINAEContext) 290{ 291 Invalid = false; 292 293 ActiveTemplateInstantiation Inst; 294 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution; 295 Inst.PointOfInstantiation = PointOfInstantiation; 296 Inst.Template = Template; 297 Inst.Entity = reinterpret_cast<uintptr_t>(Param); 298 Inst.TemplateArgs = TemplateArgs; 299 Inst.NumTemplateArgs = NumTemplateArgs; 300 Inst.InstantiationRange = InstantiationRange; 301 SemaRef.InNonInstantiationSFINAEContext = false; 302 SemaRef.ActiveTemplateInstantiations.push_back(Inst); 303 304 assert(!Inst.isInstantiationRecord()); 305 ++SemaRef.NonInstantiationEntries; 306} 307 308Sema::InstantiatingTemplate:: 309InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, 310 NamedDecl *Template, 311 TemplateTemplateParmDecl *Param, 312 const TemplateArgument *TemplateArgs, 313 unsigned NumTemplateArgs, 314 SourceRange InstantiationRange) 315 : SemaRef(SemaRef), 316 SavedInNonInstantiationSFINAEContext( 317 SemaRef.InNonInstantiationSFINAEContext) 318{ 319 Invalid = false; 320 ActiveTemplateInstantiation Inst; 321 Inst.Kind = ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution; 322 Inst.PointOfInstantiation = PointOfInstantiation; 323 Inst.Template = Template; 324 Inst.Entity = reinterpret_cast<uintptr_t>(Param); 325 Inst.TemplateArgs = TemplateArgs; 326 Inst.NumTemplateArgs = NumTemplateArgs; 327 Inst.InstantiationRange = InstantiationRange; 328 SemaRef.InNonInstantiationSFINAEContext = false; 329 SemaRef.ActiveTemplateInstantiations.push_back(Inst); 330 331 assert(!Inst.isInstantiationRecord()); 332 ++SemaRef.NonInstantiationEntries; 333} 334 335Sema::InstantiatingTemplate:: 336InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation, 337 TemplateDecl *Template, 338 NamedDecl *Param, 339 const TemplateArgument *TemplateArgs, 340 unsigned NumTemplateArgs, 341 SourceRange InstantiationRange) 342 : SemaRef(SemaRef), 343 SavedInNonInstantiationSFINAEContext( 344 SemaRef.InNonInstantiationSFINAEContext) 345{ 346 Invalid = false; 347 348 ActiveTemplateInstantiation Inst; 349 Inst.Kind = ActiveTemplateInstantiation::DefaultTemplateArgumentChecking; 350 Inst.PointOfInstantiation = PointOfInstantiation; 351 Inst.Template = Template; 352 Inst.Entity = reinterpret_cast<uintptr_t>(Param); 353 Inst.TemplateArgs = TemplateArgs; 354 Inst.NumTemplateArgs = NumTemplateArgs; 355 Inst.InstantiationRange = InstantiationRange; 356 SemaRef.InNonInstantiationSFINAEContext = false; 357 SemaRef.ActiveTemplateInstantiations.push_back(Inst); 358 359 assert(!Inst.isInstantiationRecord()); 360 ++SemaRef.NonInstantiationEntries; 361} 362 363void Sema::InstantiatingTemplate::Clear() { 364 if (!Invalid) { 365 if (!SemaRef.ActiveTemplateInstantiations.back().isInstantiationRecord()) { 366 assert(SemaRef.NonInstantiationEntries > 0); 367 --SemaRef.NonInstantiationEntries; 368 } 369 SemaRef.InNonInstantiationSFINAEContext 370 = SavedInNonInstantiationSFINAEContext; 371 SemaRef.ActiveTemplateInstantiations.pop_back(); 372 Invalid = true; 373 } 374} 375 376bool Sema::InstantiatingTemplate::CheckInstantiationDepth( 377 SourceLocation PointOfInstantiation, 378 SourceRange InstantiationRange) { 379 assert(SemaRef.NonInstantiationEntries <= 380 SemaRef.ActiveTemplateInstantiations.size()); 381 if ((SemaRef.ActiveTemplateInstantiations.size() - 382 SemaRef.NonInstantiationEntries) 383 <= SemaRef.getLangOptions().InstantiationDepth) 384 return false; 385 386 SemaRef.Diag(PointOfInstantiation, 387 diag::err_template_recursion_depth_exceeded) 388 << SemaRef.getLangOptions().InstantiationDepth 389 << InstantiationRange; 390 SemaRef.Diag(PointOfInstantiation, diag::note_template_recursion_depth) 391 << SemaRef.getLangOptions().InstantiationDepth; 392 return true; 393} 394 395/// \brief Prints the current instantiation stack through a series of 396/// notes. 397void Sema::PrintInstantiationStack() { 398 // Determine which template instantiations to skip, if any. 399 unsigned SkipStart = ActiveTemplateInstantiations.size(), SkipEnd = SkipStart; 400 unsigned Limit = Diags.getTemplateBacktraceLimit(); 401 if (Limit && Limit < ActiveTemplateInstantiations.size()) { 402 SkipStart = Limit / 2 + Limit % 2; 403 SkipEnd = ActiveTemplateInstantiations.size() - Limit / 2; 404 } 405 406 // FIXME: In all of these cases, we need to show the template arguments 407 unsigned InstantiationIdx = 0; 408 for (llvm::SmallVector<ActiveTemplateInstantiation, 16>::reverse_iterator 409 Active = ActiveTemplateInstantiations.rbegin(), 410 ActiveEnd = ActiveTemplateInstantiations.rend(); 411 Active != ActiveEnd; 412 ++Active, ++InstantiationIdx) { 413 // Skip this instantiation? 414 if (InstantiationIdx >= SkipStart && InstantiationIdx < SkipEnd) { 415 if (InstantiationIdx == SkipStart) { 416 // Note that we're skipping instantiations. 417 Diags.Report(Active->PointOfInstantiation, 418 diag::note_instantiation_contexts_suppressed) 419 << unsigned(ActiveTemplateInstantiations.size() - Limit); 420 } 421 continue; 422 } 423 424 switch (Active->Kind) { 425 case ActiveTemplateInstantiation::TemplateInstantiation: { 426 Decl *D = reinterpret_cast<Decl *>(Active->Entity); 427 if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(D)) { 428 unsigned DiagID = diag::note_template_member_class_here; 429 if (isa<ClassTemplateSpecializationDecl>(Record)) 430 DiagID = diag::note_template_class_instantiation_here; 431 Diags.Report(Active->PointOfInstantiation, DiagID) 432 << Context.getTypeDeclType(Record) 433 << Active->InstantiationRange; 434 } else if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { 435 unsigned DiagID; 436 if (Function->getPrimaryTemplate()) 437 DiagID = diag::note_function_template_spec_here; 438 else 439 DiagID = diag::note_template_member_function_here; 440 Diags.Report(Active->PointOfInstantiation, DiagID) 441 << Function 442 << Active->InstantiationRange; 443 } else { 444 Diags.Report(Active->PointOfInstantiation, 445 diag::note_template_static_data_member_def_here) 446 << cast<VarDecl>(D) 447 << Active->InstantiationRange; 448 } 449 break; 450 } 451 452 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: { 453 TemplateDecl *Template = cast<TemplateDecl>((Decl *)Active->Entity); 454 std::string TemplateArgsStr 455 = TemplateSpecializationType::PrintTemplateArgumentList( 456 Active->TemplateArgs, 457 Active->NumTemplateArgs, 458 Context.PrintingPolicy); 459 Diags.Report(Active->PointOfInstantiation, 460 diag::note_default_arg_instantiation_here) 461 << (Template->getNameAsString() + TemplateArgsStr) 462 << Active->InstantiationRange; 463 break; 464 } 465 466 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: { 467 FunctionTemplateDecl *FnTmpl 468 = cast<FunctionTemplateDecl>((Decl *)Active->Entity); 469 Diags.Report(Active->PointOfInstantiation, 470 diag::note_explicit_template_arg_substitution_here) 471 << FnTmpl 472 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(), 473 Active->TemplateArgs, 474 Active->NumTemplateArgs) 475 << Active->InstantiationRange; 476 break; 477 } 478 479 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution: 480 if (ClassTemplatePartialSpecializationDecl *PartialSpec 481 = dyn_cast<ClassTemplatePartialSpecializationDecl>( 482 (Decl *)Active->Entity)) { 483 Diags.Report(Active->PointOfInstantiation, 484 diag::note_partial_spec_deduct_instantiation_here) 485 << Context.getTypeDeclType(PartialSpec) 486 << getTemplateArgumentBindingsText( 487 PartialSpec->getTemplateParameters(), 488 Active->TemplateArgs, 489 Active->NumTemplateArgs) 490 << Active->InstantiationRange; 491 } else { 492 FunctionTemplateDecl *FnTmpl 493 = cast<FunctionTemplateDecl>((Decl *)Active->Entity); 494 Diags.Report(Active->PointOfInstantiation, 495 diag::note_function_template_deduction_instantiation_here) 496 << FnTmpl 497 << getTemplateArgumentBindingsText(FnTmpl->getTemplateParameters(), 498 Active->TemplateArgs, 499 Active->NumTemplateArgs) 500 << Active->InstantiationRange; 501 } 502 break; 503 504 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: { 505 ParmVarDecl *Param = cast<ParmVarDecl>((Decl *)Active->Entity); 506 FunctionDecl *FD = cast<FunctionDecl>(Param->getDeclContext()); 507 508 std::string TemplateArgsStr 509 = TemplateSpecializationType::PrintTemplateArgumentList( 510 Active->TemplateArgs, 511 Active->NumTemplateArgs, 512 Context.PrintingPolicy); 513 Diags.Report(Active->PointOfInstantiation, 514 diag::note_default_function_arg_instantiation_here) 515 << (FD->getNameAsString() + TemplateArgsStr) 516 << Active->InstantiationRange; 517 break; 518 } 519 520 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: { 521 NamedDecl *Parm = cast<NamedDecl>((Decl *)Active->Entity); 522 std::string Name; 523 if (!Parm->getName().empty()) 524 Name = std::string(" '") + Parm->getName().str() + "'"; 525 526 TemplateParameterList *TemplateParams = 0; 527 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template)) 528 TemplateParams = Template->getTemplateParameters(); 529 else 530 TemplateParams = 531 cast<ClassTemplatePartialSpecializationDecl>(Active->Template) 532 ->getTemplateParameters(); 533 Diags.Report(Active->PointOfInstantiation, 534 diag::note_prior_template_arg_substitution) 535 << isa<TemplateTemplateParmDecl>(Parm) 536 << Name 537 << getTemplateArgumentBindingsText(TemplateParams, 538 Active->TemplateArgs, 539 Active->NumTemplateArgs) 540 << Active->InstantiationRange; 541 break; 542 } 543 544 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: { 545 TemplateParameterList *TemplateParams = 0; 546 if (TemplateDecl *Template = dyn_cast<TemplateDecl>(Active->Template)) 547 TemplateParams = Template->getTemplateParameters(); 548 else 549 TemplateParams = 550 cast<ClassTemplatePartialSpecializationDecl>(Active->Template) 551 ->getTemplateParameters(); 552 553 Diags.Report(Active->PointOfInstantiation, 554 diag::note_template_default_arg_checking) 555 << getTemplateArgumentBindingsText(TemplateParams, 556 Active->TemplateArgs, 557 Active->NumTemplateArgs) 558 << Active->InstantiationRange; 559 break; 560 } 561 } 562 } 563} 564 565llvm::Optional<TemplateDeductionInfo *> Sema::isSFINAEContext() const { 566 using llvm::SmallVector; 567 if (InNonInstantiationSFINAEContext) 568 return llvm::Optional<TemplateDeductionInfo *>(0); 569 570 for (SmallVector<ActiveTemplateInstantiation, 16>::const_reverse_iterator 571 Active = ActiveTemplateInstantiations.rbegin(), 572 ActiveEnd = ActiveTemplateInstantiations.rend(); 573 Active != ActiveEnd; 574 ++Active) 575 { 576 switch(Active->Kind) { 577 case ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation: 578 case ActiveTemplateInstantiation::TemplateInstantiation: 579 // This is a template instantiation, so there is no SFINAE. 580 return llvm::Optional<TemplateDeductionInfo *>(); 581 582 case ActiveTemplateInstantiation::DefaultTemplateArgumentInstantiation: 583 case ActiveTemplateInstantiation::PriorTemplateArgumentSubstitution: 584 case ActiveTemplateInstantiation::DefaultTemplateArgumentChecking: 585 // A default template argument instantiation and substitution into 586 // template parameters with arguments for prior parameters may or may 587 // not be a SFINAE context; look further up the stack. 588 break; 589 590 case ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution: 591 case ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution: 592 // We're either substitution explicitly-specified template arguments 593 // or deduced template arguments, so SFINAE applies. 594 assert(Active->DeductionInfo && "Missing deduction info pointer"); 595 return Active->DeductionInfo; 596 } 597 } 598 599 return llvm::Optional<TemplateDeductionInfo *>(); 600} 601 602/// \brief Retrieve the depth and index of a parameter pack. 603static std::pair<unsigned, unsigned> 604getDepthAndIndex(NamedDecl *ND) { 605 if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND)) 606 return std::make_pair(TTP->getDepth(), TTP->getIndex()); 607 608 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND)) 609 return std::make_pair(NTTP->getDepth(), NTTP->getIndex()); 610 611 TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND); 612 return std::make_pair(TTP->getDepth(), TTP->getIndex()); 613} 614 615//===----------------------------------------------------------------------===/ 616// Template Instantiation for Types 617//===----------------------------------------------------------------------===/ 618namespace { 619 class TemplateInstantiator : public TreeTransform<TemplateInstantiator> { 620 const MultiLevelTemplateArgumentList &TemplateArgs; 621 SourceLocation Loc; 622 DeclarationName Entity; 623 624 public: 625 typedef TreeTransform<TemplateInstantiator> inherited; 626 627 TemplateInstantiator(Sema &SemaRef, 628 const MultiLevelTemplateArgumentList &TemplateArgs, 629 SourceLocation Loc, 630 DeclarationName Entity) 631 : inherited(SemaRef), TemplateArgs(TemplateArgs), Loc(Loc), 632 Entity(Entity) { } 633 634 /// \brief Determine whether the given type \p T has already been 635 /// transformed. 636 /// 637 /// For the purposes of template instantiation, a type has already been 638 /// transformed if it is NULL or if it is not dependent. 639 bool AlreadyTransformed(QualType T); 640 641 /// \brief Returns the location of the entity being instantiated, if known. 642 SourceLocation getBaseLocation() { return Loc; } 643 644 /// \brief Returns the name of the entity being instantiated, if any. 645 DeclarationName getBaseEntity() { return Entity; } 646 647 /// \brief Sets the "base" location and entity when that 648 /// information is known based on another transformation. 649 void setBase(SourceLocation Loc, DeclarationName Entity) { 650 this->Loc = Loc; 651 this->Entity = Entity; 652 } 653 654 bool TryExpandParameterPacks(SourceLocation EllipsisLoc, 655 SourceRange PatternRange, 656 const UnexpandedParameterPack *Unexpanded, 657 unsigned NumUnexpanded, 658 bool &ShouldExpand, 659 bool &RetainExpansion, 660 llvm::Optional<unsigned> &NumExpansions) { 661 return getSema().CheckParameterPacksForExpansion(EllipsisLoc, 662 PatternRange, Unexpanded, 663 NumUnexpanded, 664 TemplateArgs, 665 ShouldExpand, 666 RetainExpansion, 667 NumExpansions); 668 } 669 670 void ExpandingFunctionParameterPack(ParmVarDecl *Pack) { 671 SemaRef.CurrentInstantiationScope->MakeInstantiatedLocalArgPack(Pack); 672 } 673 674 TemplateArgument ForgetPartiallySubstitutedPack() { 675 TemplateArgument Result; 676 if (NamedDecl *PartialPack 677 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){ 678 MultiLevelTemplateArgumentList &TemplateArgs 679 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs); 680 unsigned Depth, Index; 681 llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack); 682 if (TemplateArgs.hasTemplateArgument(Depth, Index)) { 683 Result = TemplateArgs(Depth, Index); 684 TemplateArgs.setArgument(Depth, Index, TemplateArgument()); 685 } 686 } 687 688 return Result; 689 } 690 691 void RememberPartiallySubstitutedPack(TemplateArgument Arg) { 692 if (Arg.isNull()) 693 return; 694 695 if (NamedDecl *PartialPack 696 = SemaRef.CurrentInstantiationScope->getPartiallySubstitutedPack()){ 697 MultiLevelTemplateArgumentList &TemplateArgs 698 = const_cast<MultiLevelTemplateArgumentList &>(this->TemplateArgs); 699 unsigned Depth, Index; 700 llvm::tie(Depth, Index) = getDepthAndIndex(PartialPack); 701 TemplateArgs.setArgument(Depth, Index, Arg); 702 } 703 } 704 705 /// \brief Transform the given declaration by instantiating a reference to 706 /// this declaration. 707 Decl *TransformDecl(SourceLocation Loc, Decl *D); 708 709 /// \brief Transform the definition of the given declaration by 710 /// instantiating it. 711 Decl *TransformDefinition(SourceLocation Loc, Decl *D); 712 713 /// \bried Transform the first qualifier within a scope by instantiating the 714 /// declaration. 715 NamedDecl *TransformFirstQualifierInScope(NamedDecl *D, SourceLocation Loc); 716 717 /// \brief Rebuild the exception declaration and register the declaration 718 /// as an instantiated local. 719 VarDecl *RebuildExceptionDecl(VarDecl *ExceptionDecl, 720 TypeSourceInfo *Declarator, 721 IdentifierInfo *Name, 722 SourceLocation Loc); 723 724 /// \brief Rebuild the Objective-C exception declaration and register the 725 /// declaration as an instantiated local. 726 VarDecl *RebuildObjCExceptionDecl(VarDecl *ExceptionDecl, 727 TypeSourceInfo *TSInfo, QualType T); 728 729 /// \brief Check for tag mismatches when instantiating an 730 /// elaborated type. 731 QualType RebuildElaboratedType(SourceLocation KeywordLoc, 732 ElaboratedTypeKeyword Keyword, 733 NestedNameSpecifierLoc QualifierLoc, 734 QualType T); 735 736 TemplateName TransformTemplateName(CXXScopeSpec &SS, 737 TemplateName Name, 738 SourceLocation NameLoc, 739 QualType ObjectType = QualType(), 740 NamedDecl *FirstQualifierInScope = 0); 741 742 ExprResult TransformPredefinedExpr(PredefinedExpr *E); 743 ExprResult TransformDeclRefExpr(DeclRefExpr *E); 744 ExprResult TransformCXXDefaultArgExpr(CXXDefaultArgExpr *E); 745 ExprResult TransformTemplateParmRefExpr(DeclRefExpr *E, 746 NonTypeTemplateParmDecl *D); 747 ExprResult TransformSubstNonTypeTemplateParmPackExpr( 748 SubstNonTypeTemplateParmPackExpr *E); 749 750 QualType TransformFunctionProtoType(TypeLocBuilder &TLB, 751 FunctionProtoTypeLoc TL); 752 ParmVarDecl *TransformFunctionTypeParam(ParmVarDecl *OldParm, 753 llvm::Optional<unsigned> NumExpansions); 754 755 /// \brief Transforms a template type parameter type by performing 756 /// substitution of the corresponding template type argument. 757 QualType TransformTemplateTypeParmType(TypeLocBuilder &TLB, 758 TemplateTypeParmTypeLoc TL); 759 760 /// \brief Transforms an already-substituted template type parameter pack 761 /// into either itself (if we aren't substituting into its pack expansion) 762 /// or the appropriate substituted argument. 763 QualType TransformSubstTemplateTypeParmPackType(TypeLocBuilder &TLB, 764 SubstTemplateTypeParmPackTypeLoc TL); 765 766 ExprResult TransformCallExpr(CallExpr *CE) { 767 getSema().CallsUndergoingInstantiation.push_back(CE); 768 ExprResult Result = 769 TreeTransform<TemplateInstantiator>::TransformCallExpr(CE); 770 getSema().CallsUndergoingInstantiation.pop_back(); 771 return move(Result); 772 } 773 }; 774} 775 776bool TemplateInstantiator::AlreadyTransformed(QualType T) { 777 if (T.isNull()) 778 return true; 779 780 if (T->isDependentType() || T->isVariablyModifiedType()) 781 return false; 782 783 getSema().MarkDeclarationsReferencedInType(Loc, T); 784 return true; 785} 786 787Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) { 788 if (!D) 789 return 0; 790 791 if (TemplateTemplateParmDecl *TTP = dyn_cast<TemplateTemplateParmDecl>(D)) { 792 if (TTP->getDepth() < TemplateArgs.getNumLevels()) { 793 // If the corresponding template argument is NULL or non-existent, it's 794 // because we are performing instantiation from explicitly-specified 795 // template arguments in a function template, but there were some 796 // arguments left unspecified. 797 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(), 798 TTP->getPosition())) 799 return D; 800 801 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition()); 802 803 if (TTP->isParameterPack()) { 804 assert(Arg.getKind() == TemplateArgument::Pack && 805 "Missing argument pack"); 806 807 assert(getSema().ArgumentPackSubstitutionIndex >= 0); 808 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size()); 809 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex]; 810 } 811 812 TemplateName Template = Arg.getAsTemplate(); 813 assert(!Template.isNull() && Template.getAsTemplateDecl() && 814 "Wrong kind of template template argument"); 815 return Template.getAsTemplateDecl(); 816 } 817 818 // Fall through to find the instantiated declaration for this template 819 // template parameter. 820 } 821 822 return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D), TemplateArgs); 823} 824 825Decl *TemplateInstantiator::TransformDefinition(SourceLocation Loc, Decl *D) { 826 Decl *Inst = getSema().SubstDecl(D, getSema().CurContext, TemplateArgs); 827 if (!Inst) 828 return 0; 829 830 getSema().CurrentInstantiationScope->InstantiatedLocal(D, Inst); 831 return Inst; 832} 833 834NamedDecl * 835TemplateInstantiator::TransformFirstQualifierInScope(NamedDecl *D, 836 SourceLocation Loc) { 837 // If the first part of the nested-name-specifier was a template type 838 // parameter, instantiate that type parameter down to a tag type. 839 if (TemplateTypeParmDecl *TTPD = dyn_cast_or_null<TemplateTypeParmDecl>(D)) { 840 const TemplateTypeParmType *TTP 841 = cast<TemplateTypeParmType>(getSema().Context.getTypeDeclType(TTPD)); 842 843 if (TTP->getDepth() < TemplateArgs.getNumLevels()) { 844 // FIXME: This needs testing w/ member access expressions. 845 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getIndex()); 846 847 if (TTP->isParameterPack()) { 848 assert(Arg.getKind() == TemplateArgument::Pack && 849 "Missing argument pack"); 850 851 if (getSema().ArgumentPackSubstitutionIndex == -1) 852 return 0; 853 854 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size()); 855 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex]; 856 } 857 858 QualType T = Arg.getAsType(); 859 if (T.isNull()) 860 return cast_or_null<NamedDecl>(TransformDecl(Loc, D)); 861 862 if (const TagType *Tag = T->getAs<TagType>()) 863 return Tag->getDecl(); 864 865 // The resulting type is not a tag; complain. 866 getSema().Diag(Loc, diag::err_nested_name_spec_non_tag) << T; 867 return 0; 868 } 869 } 870 871 return cast_or_null<NamedDecl>(TransformDecl(Loc, D)); 872} 873 874VarDecl * 875TemplateInstantiator::RebuildExceptionDecl(VarDecl *ExceptionDecl, 876 TypeSourceInfo *Declarator, 877 IdentifierInfo *Name, 878 SourceLocation Loc) { 879 VarDecl *Var = inherited::RebuildExceptionDecl(ExceptionDecl, Declarator, 880 Name, Loc); 881 if (Var) 882 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var); 883 return Var; 884} 885 886VarDecl *TemplateInstantiator::RebuildObjCExceptionDecl(VarDecl *ExceptionDecl, 887 TypeSourceInfo *TSInfo, 888 QualType T) { 889 VarDecl *Var = inherited::RebuildObjCExceptionDecl(ExceptionDecl, TSInfo, T); 890 if (Var) 891 getSema().CurrentInstantiationScope->InstantiatedLocal(ExceptionDecl, Var); 892 return Var; 893} 894 895QualType 896TemplateInstantiator::RebuildElaboratedType(SourceLocation KeywordLoc, 897 ElaboratedTypeKeyword Keyword, 898 NestedNameSpecifierLoc QualifierLoc, 899 QualType T) { 900 if (const TagType *TT = T->getAs<TagType>()) { 901 TagDecl* TD = TT->getDecl(); 902 903 SourceLocation TagLocation = KeywordLoc; 904 905 // FIXME: type might be anonymous. 906 IdentifierInfo *Id = TD->getIdentifier(); 907 908 // TODO: should we even warn on struct/class mismatches for this? Seems 909 // like it's likely to produce a lot of spurious errors. 910 if (Keyword != ETK_None && Keyword != ETK_Typename) { 911 TagTypeKind Kind = TypeWithKeyword::getTagTypeKindForKeyword(Keyword); 912 if (!SemaRef.isAcceptableTagRedeclaration(TD, Kind, TagLocation, *Id)) { 913 SemaRef.Diag(TagLocation, diag::err_use_with_wrong_tag) 914 << Id 915 << FixItHint::CreateReplacement(SourceRange(TagLocation), 916 TD->getKindName()); 917 SemaRef.Diag(TD->getLocation(), diag::note_previous_use); 918 } 919 } 920 } 921 922 return TreeTransform<TemplateInstantiator>::RebuildElaboratedType(KeywordLoc, 923 Keyword, 924 QualifierLoc, 925 T); 926} 927 928TemplateName TemplateInstantiator::TransformTemplateName(CXXScopeSpec &SS, 929 TemplateName Name, 930 SourceLocation NameLoc, 931 QualType ObjectType, 932 NamedDecl *FirstQualifierInScope) { 933 if (TemplateTemplateParmDecl *TTP 934 = dyn_cast_or_null<TemplateTemplateParmDecl>(Name.getAsTemplateDecl())) { 935 if (TTP->getDepth() < TemplateArgs.getNumLevels()) { 936 // If the corresponding template argument is NULL or non-existent, it's 937 // because we are performing instantiation from explicitly-specified 938 // template arguments in a function template, but there were some 939 // arguments left unspecified. 940 if (!TemplateArgs.hasTemplateArgument(TTP->getDepth(), 941 TTP->getPosition())) 942 return Name; 943 944 TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition()); 945 946 if (TTP->isParameterPack()) { 947 assert(Arg.getKind() == TemplateArgument::Pack && 948 "Missing argument pack"); 949 950 if (getSema().ArgumentPackSubstitutionIndex == -1) { 951 // We have the template argument pack to substitute, but we're not 952 // actually expanding the enclosing pack expansion yet. So, just 953 // keep the entire argument pack. 954 return getSema().Context.getSubstTemplateTemplateParmPack(TTP, Arg); 955 } 956 957 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size()); 958 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex]; 959 } 960 961 TemplateName Template = Arg.getAsTemplate(); 962 assert(!Template.isNull() && Template.getAsTemplateDecl() && 963 "Wrong kind of template template argument"); 964 return Template; 965 } 966 } 967 968 if (SubstTemplateTemplateParmPackStorage *SubstPack 969 = Name.getAsSubstTemplateTemplateParmPack()) { 970 if (getSema().ArgumentPackSubstitutionIndex == -1) 971 return Name; 972 973 const TemplateArgument &ArgPack = SubstPack->getArgumentPack(); 974 assert(getSema().ArgumentPackSubstitutionIndex < (int)ArgPack.pack_size() && 975 "Pack substitution index out-of-range"); 976 return ArgPack.pack_begin()[getSema().ArgumentPackSubstitutionIndex] 977 .getAsTemplate(); 978 } 979 980 return inherited::TransformTemplateName(SS, Name, NameLoc, ObjectType, 981 FirstQualifierInScope); 982} 983 984ExprResult 985TemplateInstantiator::TransformPredefinedExpr(PredefinedExpr *E) { 986 if (!E->isTypeDependent()) 987 return SemaRef.Owned(E); 988 989 FunctionDecl *currentDecl = getSema().getCurFunctionDecl(); 990 assert(currentDecl && "Must have current function declaration when " 991 "instantiating."); 992 993 PredefinedExpr::IdentType IT = E->getIdentType(); 994 995 unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); 996 997 llvm::APInt LengthI(32, Length + 1); 998 QualType ResTy = getSema().Context.CharTy.withConst(); 999 ResTy = getSema().Context.getConstantArrayType(ResTy, LengthI, 1000 ArrayType::Normal, 0); 1001 PredefinedExpr *PE = 1002 new (getSema().Context) PredefinedExpr(E->getLocation(), ResTy, IT); 1003 return getSema().Owned(PE); 1004} 1005 1006ExprResult 1007TemplateInstantiator::TransformTemplateParmRefExpr(DeclRefExpr *E, 1008 NonTypeTemplateParmDecl *NTTP) { 1009 // If the corresponding template argument is NULL or non-existent, it's 1010 // because we are performing instantiation from explicitly-specified 1011 // template arguments in a function template, but there were some 1012 // arguments left unspecified. 1013 if (!TemplateArgs.hasTemplateArgument(NTTP->getDepth(), 1014 NTTP->getPosition())) 1015 return SemaRef.Owned(E); 1016 1017 TemplateArgument Arg = TemplateArgs(NTTP->getDepth(), NTTP->getPosition()); 1018 if (NTTP->isParameterPack()) { 1019 assert(Arg.getKind() == TemplateArgument::Pack && 1020 "Missing argument pack"); 1021 1022 if (getSema().ArgumentPackSubstitutionIndex == -1) { 1023 // We have an argument pack, but we can't select a particular argument 1024 // out of it yet. Therefore, we'll build an expression to hold on to that 1025 // argument pack. 1026 QualType TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs, 1027 E->getLocation(), 1028 NTTP->getDeclName()); 1029 if (TargetType.isNull()) 1030 return ExprError(); 1031 1032 return new (SemaRef.Context) SubstNonTypeTemplateParmPackExpr(TargetType, 1033 NTTP, 1034 E->getLocation(), 1035 Arg); 1036 } 1037 1038 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size()); 1039 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex]; 1040 } 1041 1042 // The template argument itself might be an expression, in which 1043 // case we just return that expression. 1044 if (Arg.getKind() == TemplateArgument::Expression) 1045 return SemaRef.Owned(Arg.getAsExpr()); 1046 1047 if (Arg.getKind() == TemplateArgument::Declaration) { 1048 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); 1049 1050 // Find the instantiation of the template argument. This is 1051 // required for nested templates. 1052 VD = cast_or_null<ValueDecl>( 1053 getSema().FindInstantiatedDecl(E->getLocation(), 1054 VD, TemplateArgs)); 1055 if (!VD) 1056 return ExprError(); 1057 1058 // Derive the type we want the substituted decl to have. This had 1059 // better be non-dependent, or these checks will have serious problems. 1060 QualType TargetType; 1061 if (NTTP->isExpandedParameterPack()) 1062 TargetType = NTTP->getExpansionType( 1063 getSema().ArgumentPackSubstitutionIndex); 1064 else if (NTTP->isParameterPack() && 1065 isa<PackExpansionType>(NTTP->getType())) { 1066 TargetType = SemaRef.SubstType( 1067 cast<PackExpansionType>(NTTP->getType())->getPattern(), 1068 TemplateArgs, E->getLocation(), 1069 NTTP->getDeclName()); 1070 } else 1071 TargetType = SemaRef.SubstType(NTTP->getType(), TemplateArgs, 1072 E->getLocation(), NTTP->getDeclName()); 1073 assert(!TargetType.isNull() && "type substitution failed for param type"); 1074 assert(!TargetType->isDependentType() && "param type still dependent"); 1075 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg, 1076 TargetType, 1077 E->getLocation()); 1078 } 1079 1080 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg, 1081 E->getSourceRange().getBegin()); 1082} 1083 1084ExprResult 1085TemplateInstantiator::TransformSubstNonTypeTemplateParmPackExpr( 1086 SubstNonTypeTemplateParmPackExpr *E) { 1087 if (getSema().ArgumentPackSubstitutionIndex == -1) { 1088 // We aren't expanding the parameter pack, so just return ourselves. 1089 return getSema().Owned(E); 1090 } 1091 1092 const TemplateArgument &ArgPack = E->getArgumentPack(); 1093 unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex; 1094 assert(Index < ArgPack.pack_size() && "Substitution index out-of-range"); 1095 1096 const TemplateArgument &Arg = ArgPack.pack_begin()[Index]; 1097 if (Arg.getKind() == TemplateArgument::Expression) 1098 return SemaRef.Owned(Arg.getAsExpr()); 1099 1100 if (Arg.getKind() == TemplateArgument::Declaration) { 1101 ValueDecl *VD = cast<ValueDecl>(Arg.getAsDecl()); 1102 1103 // Find the instantiation of the template argument. This is 1104 // required for nested templates. 1105 VD = cast_or_null<ValueDecl>( 1106 getSema().FindInstantiatedDecl(E->getParameterPackLocation(), 1107 VD, TemplateArgs)); 1108 if (!VD) 1109 return ExprError(); 1110 1111 QualType T; 1112 NonTypeTemplateParmDecl *NTTP = E->getParameterPack(); 1113 if (NTTP->isExpandedParameterPack()) 1114 T = NTTP->getExpansionType(getSema().ArgumentPackSubstitutionIndex); 1115 else if (const PackExpansionType *Expansion 1116 = dyn_cast<PackExpansionType>(NTTP->getType())) 1117 T = SemaRef.SubstType(Expansion->getPattern(), TemplateArgs, 1118 E->getParameterPackLocation(), NTTP->getDeclName()); 1119 else 1120 T = E->getType(); 1121 return SemaRef.BuildExpressionFromDeclTemplateArgument(Arg, T, 1122 E->getParameterPackLocation()); 1123 } 1124 1125 return SemaRef.BuildExpressionFromIntegralTemplateArgument(Arg, 1126 E->getParameterPackLocation()); 1127} 1128 1129ExprResult 1130TemplateInstantiator::TransformDeclRefExpr(DeclRefExpr *E) { 1131 NamedDecl *D = E->getDecl(); 1132 if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) { 1133 if (NTTP->getDepth() < TemplateArgs.getNumLevels()) 1134 return TransformTemplateParmRefExpr(E, NTTP); 1135 1136 // We have a non-type template parameter that isn't fully substituted; 1137 // FindInstantiatedDecl will find it in the local instantiation scope. 1138 } 1139 1140 return TreeTransform<TemplateInstantiator>::TransformDeclRefExpr(E); 1141} 1142 1143ExprResult TemplateInstantiator::TransformCXXDefaultArgExpr( 1144 CXXDefaultArgExpr *E) { 1145 assert(!cast<FunctionDecl>(E->getParam()->getDeclContext())-> 1146 getDescribedFunctionTemplate() && 1147 "Default arg expressions are never formed in dependent cases."); 1148 return SemaRef.BuildCXXDefaultArgExpr(E->getUsedLocation(), 1149 cast<FunctionDecl>(E->getParam()->getDeclContext()), 1150 E->getParam()); 1151} 1152 1153QualType TemplateInstantiator::TransformFunctionProtoType(TypeLocBuilder &TLB, 1154 FunctionProtoTypeLoc TL) { 1155 // We need a local instantiation scope for this function prototype. 1156 LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true); 1157 return inherited::TransformFunctionProtoType(TLB, TL); 1158} 1159 1160ParmVarDecl * 1161TemplateInstantiator::TransformFunctionTypeParam(ParmVarDecl *OldParm, 1162 llvm::Optional<unsigned> NumExpansions) { 1163 return SemaRef.SubstParmVarDecl(OldParm, TemplateArgs, 1164 NumExpansions); 1165} 1166 1167QualType 1168TemplateInstantiator::TransformTemplateTypeParmType(TypeLocBuilder &TLB, 1169 TemplateTypeParmTypeLoc TL) { 1170 const TemplateTypeParmType *T = TL.getTypePtr(); 1171 if (T->getDepth() < TemplateArgs.getNumLevels()) { 1172 // Replace the template type parameter with its corresponding 1173 // template argument. 1174 1175 // If the corresponding template argument is NULL or doesn't exist, it's 1176 // because we are performing instantiation from explicitly-specified 1177 // template arguments in a function template class, but there were some 1178 // arguments left unspecified. 1179 if (!TemplateArgs.hasTemplateArgument(T->getDepth(), T->getIndex())) { 1180 TemplateTypeParmTypeLoc NewTL 1181 = TLB.push<TemplateTypeParmTypeLoc>(TL.getType()); 1182 NewTL.setNameLoc(TL.getNameLoc()); 1183 return TL.getType(); 1184 } 1185 1186 TemplateArgument Arg = TemplateArgs(T->getDepth(), T->getIndex()); 1187 1188 if (T->isParameterPack()) { 1189 assert(Arg.getKind() == TemplateArgument::Pack && 1190 "Missing argument pack"); 1191 1192 if (getSema().ArgumentPackSubstitutionIndex == -1) { 1193 // We have the template argument pack, but we're not expanding the 1194 // enclosing pack expansion yet. Just save the template argument 1195 // pack for later substitution. 1196 QualType Result 1197 = getSema().Context.getSubstTemplateTypeParmPackType(T, Arg); 1198 SubstTemplateTypeParmPackTypeLoc NewTL 1199 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(Result); 1200 NewTL.setNameLoc(TL.getNameLoc()); 1201 return Result; 1202 } 1203 1204 assert(getSema().ArgumentPackSubstitutionIndex < (int)Arg.pack_size()); 1205 Arg = Arg.pack_begin()[getSema().ArgumentPackSubstitutionIndex]; 1206 } 1207 1208 assert(Arg.getKind() == TemplateArgument::Type && 1209 "Template argument kind mismatch"); 1210 1211 QualType Replacement = Arg.getAsType(); 1212 1213 // TODO: only do this uniquing once, at the start of instantiation. 1214 QualType Result 1215 = getSema().Context.getSubstTemplateTypeParmType(T, Replacement); 1216 SubstTemplateTypeParmTypeLoc NewTL 1217 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result); 1218 NewTL.setNameLoc(TL.getNameLoc()); 1219 return Result; 1220 } 1221 1222 // The template type parameter comes from an inner template (e.g., 1223 // the template parameter list of a member template inside the 1224 // template we are instantiating). Create a new template type 1225 // parameter with the template "level" reduced by one. 1226 QualType Result 1227 = getSema().Context.getTemplateTypeParmType(T->getDepth() 1228 - TemplateArgs.getNumLevels(), 1229 T->getIndex(), 1230 T->isParameterPack(), 1231 T->getName()); 1232 TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result); 1233 NewTL.setNameLoc(TL.getNameLoc()); 1234 return Result; 1235} 1236 1237QualType 1238TemplateInstantiator::TransformSubstTemplateTypeParmPackType( 1239 TypeLocBuilder &TLB, 1240 SubstTemplateTypeParmPackTypeLoc TL) { 1241 if (getSema().ArgumentPackSubstitutionIndex == -1) { 1242 // We aren't expanding the parameter pack, so just return ourselves. 1243 SubstTemplateTypeParmPackTypeLoc NewTL 1244 = TLB.push<SubstTemplateTypeParmPackTypeLoc>(TL.getType()); 1245 NewTL.setNameLoc(TL.getNameLoc()); 1246 return TL.getType(); 1247 } 1248 1249 const TemplateArgument &ArgPack = TL.getTypePtr()->getArgumentPack(); 1250 unsigned Index = (unsigned)getSema().ArgumentPackSubstitutionIndex; 1251 assert(Index < ArgPack.pack_size() && "Substitution index out-of-range"); 1252 1253 QualType Result = ArgPack.pack_begin()[Index].getAsType(); 1254 Result = getSema().Context.getSubstTemplateTypeParmType( 1255 TL.getTypePtr()->getReplacedParameter(), 1256 Result); 1257 SubstTemplateTypeParmTypeLoc NewTL 1258 = TLB.push<SubstTemplateTypeParmTypeLoc>(Result); 1259 NewTL.setNameLoc(TL.getNameLoc()); 1260 return Result; 1261} 1262 1263/// \brief Perform substitution on the type T with a given set of template 1264/// arguments. 1265/// 1266/// This routine substitutes the given template arguments into the 1267/// type T and produces the instantiated type. 1268/// 1269/// \param T the type into which the template arguments will be 1270/// substituted. If this type is not dependent, it will be returned 1271/// immediately. 1272/// 1273/// \param TemplateArgs the template arguments that will be 1274/// substituted for the top-level template parameters within T. 1275/// 1276/// \param Loc the location in the source code where this substitution 1277/// is being performed. It will typically be the location of the 1278/// declarator (if we're instantiating the type of some declaration) 1279/// or the location of the type in the source code (if, e.g., we're 1280/// instantiating the type of a cast expression). 1281/// 1282/// \param Entity the name of the entity associated with a declaration 1283/// being instantiated (if any). May be empty to indicate that there 1284/// is no such entity (if, e.g., this is a type that occurs as part of 1285/// a cast expression) or that the entity has no name (e.g., an 1286/// unnamed function parameter). 1287/// 1288/// \returns If the instantiation succeeds, the instantiated 1289/// type. Otherwise, produces diagnostics and returns a NULL type. 1290TypeSourceInfo *Sema::SubstType(TypeSourceInfo *T, 1291 const MultiLevelTemplateArgumentList &Args, 1292 SourceLocation Loc, 1293 DeclarationName Entity) { 1294 assert(!ActiveTemplateInstantiations.empty() && 1295 "Cannot perform an instantiation without some context on the " 1296 "instantiation stack"); 1297 1298 if (!T->getType()->isDependentType() && 1299 !T->getType()->isVariablyModifiedType()) 1300 return T; 1301 1302 TemplateInstantiator Instantiator(*this, Args, Loc, Entity); 1303 return Instantiator.TransformType(T); 1304} 1305 1306TypeSourceInfo *Sema::SubstType(TypeLoc TL, 1307 const MultiLevelTemplateArgumentList &Args, 1308 SourceLocation Loc, 1309 DeclarationName Entity) { 1310 assert(!ActiveTemplateInstantiations.empty() && 1311 "Cannot perform an instantiation without some context on the " 1312 "instantiation stack"); 1313 1314 if (TL.getType().isNull()) 1315 return 0; 1316 1317 if (!TL.getType()->isDependentType() && 1318 !TL.getType()->isVariablyModifiedType()) { 1319 // FIXME: Make a copy of the TypeLoc data here, so that we can 1320 // return a new TypeSourceInfo. Inefficient! 1321 TypeLocBuilder TLB; 1322 TLB.pushFullCopy(TL); 1323 return TLB.getTypeSourceInfo(Context, TL.getType()); 1324 } 1325 1326 TemplateInstantiator Instantiator(*this, Args, Loc, Entity); 1327 TypeLocBuilder TLB; 1328 TLB.reserve(TL.getFullDataSize()); 1329 QualType Result = Instantiator.TransformType(TLB, TL); 1330 if (Result.isNull()) 1331 return 0; 1332 1333 return TLB.getTypeSourceInfo(Context, Result); 1334} 1335 1336/// Deprecated form of the above. 1337QualType Sema::SubstType(QualType T, 1338 const MultiLevelTemplateArgumentList &TemplateArgs, 1339 SourceLocation Loc, DeclarationName Entity) { 1340 assert(!ActiveTemplateInstantiations.empty() && 1341 "Cannot perform an instantiation without some context on the " 1342 "instantiation stack"); 1343 1344 // If T is not a dependent type or a variably-modified type, there 1345 // is nothing to do. 1346 if (!T->isDependentType() && !T->isVariablyModifiedType()) 1347 return T; 1348 1349 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, Entity); 1350 return Instantiator.TransformType(T); 1351} 1352 1353static bool NeedsInstantiationAsFunctionType(TypeSourceInfo *T) { 1354 if (T->getType()->isDependentType() || T->getType()->isVariablyModifiedType()) 1355 return true; 1356 1357 TypeLoc TL = T->getTypeLoc().IgnoreParens(); 1358 if (!isa<FunctionProtoTypeLoc>(TL)) 1359 return false; 1360 1361 FunctionProtoTypeLoc FP = cast<FunctionProtoTypeLoc>(TL); 1362 for (unsigned I = 0, E = FP.getNumArgs(); I != E; ++I) { 1363 ParmVarDecl *P = FP.getArg(I); 1364 1365 // TODO: currently we always rebuild expressions. When we 1366 // properly get lazier about this, we should use the same 1367 // logic to avoid rebuilding prototypes here. 1368 if (P->hasDefaultArg()) 1369 return true; 1370 } 1371 1372 return false; 1373} 1374 1375/// A form of SubstType intended specifically for instantiating the 1376/// type of a FunctionDecl. Its purpose is solely to force the 1377/// instantiation of default-argument expressions. 1378TypeSourceInfo *Sema::SubstFunctionDeclType(TypeSourceInfo *T, 1379 const MultiLevelTemplateArgumentList &Args, 1380 SourceLocation Loc, 1381 DeclarationName Entity) { 1382 assert(!ActiveTemplateInstantiations.empty() && 1383 "Cannot perform an instantiation without some context on the " 1384 "instantiation stack"); 1385 1386 if (!NeedsInstantiationAsFunctionType(T)) 1387 return T; 1388 1389 TemplateInstantiator Instantiator(*this, Args, Loc, Entity); 1390 1391 TypeLocBuilder TLB; 1392 1393 TypeLoc TL = T->getTypeLoc(); 1394 TLB.reserve(TL.getFullDataSize()); 1395 1396 QualType Result = Instantiator.TransformType(TLB, TL); 1397 if (Result.isNull()) 1398 return 0; 1399 1400 return TLB.getTypeSourceInfo(Context, Result); 1401} 1402 1403ParmVarDecl *Sema::SubstParmVarDecl(ParmVarDecl *OldParm, 1404 const MultiLevelTemplateArgumentList &TemplateArgs, 1405 llvm::Optional<unsigned> NumExpansions) { 1406 TypeSourceInfo *OldDI = OldParm->getTypeSourceInfo(); 1407 TypeSourceInfo *NewDI = 0; 1408 1409 TypeLoc OldTL = OldDI->getTypeLoc(); 1410 if (isa<PackExpansionTypeLoc>(OldTL)) { 1411 PackExpansionTypeLoc ExpansionTL = cast<PackExpansionTypeLoc>(OldTL); 1412 1413 // We have a function parameter pack. Substitute into the pattern of the 1414 // expansion. 1415 NewDI = SubstType(ExpansionTL.getPatternLoc(), TemplateArgs, 1416 OldParm->getLocation(), OldParm->getDeclName()); 1417 if (!NewDI) 1418 return 0; 1419 1420 if (NewDI->getType()->containsUnexpandedParameterPack()) { 1421 // We still have unexpanded parameter packs, which means that 1422 // our function parameter is still a function parameter pack. 1423 // Therefore, make its type a pack expansion type. 1424 NewDI = CheckPackExpansion(NewDI, ExpansionTL.getEllipsisLoc(), 1425 NumExpansions); 1426 } 1427 } else { 1428 NewDI = SubstType(OldDI, TemplateArgs, OldParm->getLocation(), 1429 OldParm->getDeclName()); 1430 } 1431 1432 if (!NewDI) 1433 return 0; 1434 1435 if (NewDI->getType()->isVoidType()) { 1436 Diag(OldParm->getLocation(), diag::err_param_with_void_type); 1437 return 0; 1438 } 1439 1440 ParmVarDecl *NewParm = CheckParameter(Context.getTranslationUnitDecl(), 1441 NewDI, NewDI->getType(), 1442 OldParm->getIdentifier(), 1443 OldParm->getLocation(), 1444 OldParm->getStorageClass(), 1445 OldParm->getStorageClassAsWritten()); 1446 if (!NewParm) 1447 return 0; 1448 1449 // Mark the (new) default argument as uninstantiated (if any). 1450 if (OldParm->hasUninstantiatedDefaultArg()) { 1451 Expr *Arg = OldParm->getUninstantiatedDefaultArg(); 1452 NewParm->setUninstantiatedDefaultArg(Arg); 1453 } else if (OldParm->hasUnparsedDefaultArg()) { 1454 NewParm->setUnparsedDefaultArg(); 1455 UnparsedDefaultArgInstantiations[OldParm].push_back(NewParm); 1456 } else if (Expr *Arg = OldParm->getDefaultArg()) 1457 NewParm->setUninstantiatedDefaultArg(Arg); 1458 1459 NewParm->setHasInheritedDefaultArg(OldParm->hasInheritedDefaultArg()); 1460 1461 // FIXME: When OldParm is a parameter pack and NewParm is not a parameter 1462 // pack, we actually have a set of instantiated locations. Maintain this set! 1463 if (OldParm->isParameterPack() && !NewParm->isParameterPack()) { 1464 // Add the new parameter to 1465 CurrentInstantiationScope->InstantiatedLocalPackArg(OldParm, NewParm); 1466 } else { 1467 // Introduce an Old -> New mapping 1468 CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm); 1469 } 1470 1471 // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext 1472 // can be anything, is this right ? 1473 NewParm->setDeclContext(CurContext); 1474 1475 return NewParm; 1476} 1477 1478/// \brief Substitute the given template arguments into the given set of 1479/// parameters, producing the set of parameter types that would be generated 1480/// from such a substitution. 1481bool Sema::SubstParmTypes(SourceLocation Loc, 1482 ParmVarDecl **Params, unsigned NumParams, 1483 const MultiLevelTemplateArgumentList &TemplateArgs, 1484 llvm::SmallVectorImpl<QualType> &ParamTypes, 1485 llvm::SmallVectorImpl<ParmVarDecl *> *OutParams) { 1486 assert(!ActiveTemplateInstantiations.empty() && 1487 "Cannot perform an instantiation without some context on the " 1488 "instantiation stack"); 1489 1490 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, 1491 DeclarationName()); 1492 return Instantiator.TransformFunctionTypeParams(Loc, Params, NumParams, 0, 1493 ParamTypes, OutParams); 1494} 1495 1496/// \brief Perform substitution on the base class specifiers of the 1497/// given class template specialization. 1498/// 1499/// Produces a diagnostic and returns true on error, returns false and 1500/// attaches the instantiated base classes to the class template 1501/// specialization if successful. 1502bool 1503Sema::SubstBaseSpecifiers(CXXRecordDecl *Instantiation, 1504 CXXRecordDecl *Pattern, 1505 const MultiLevelTemplateArgumentList &TemplateArgs) { 1506 bool Invalid = false; 1507 llvm::SmallVector<CXXBaseSpecifier*, 4> InstantiatedBases; 1508 for (ClassTemplateSpecializationDecl::base_class_iterator 1509 Base = Pattern->bases_begin(), BaseEnd = Pattern->bases_end(); 1510 Base != BaseEnd; ++Base) { 1511 if (!Base->getType()->isDependentType()) { 1512 InstantiatedBases.push_back(new (Context) CXXBaseSpecifier(*Base)); 1513 continue; 1514 } 1515 1516 SourceLocation EllipsisLoc; 1517 TypeSourceInfo *BaseTypeLoc; 1518 if (Base->isPackExpansion()) { 1519 // This is a pack expansion. See whether we should expand it now, or 1520 // wait until later. 1521 llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded; 1522 collectUnexpandedParameterPacks(Base->getTypeSourceInfo()->getTypeLoc(), 1523 Unexpanded); 1524 bool ShouldExpand = false; 1525 bool RetainExpansion = false; 1526 llvm::Optional<unsigned> NumExpansions; 1527 if (CheckParameterPacksForExpansion(Base->getEllipsisLoc(), 1528 Base->getSourceRange(), 1529 Unexpanded.data(), Unexpanded.size(), 1530 TemplateArgs, ShouldExpand, 1531 RetainExpansion, 1532 NumExpansions)) { 1533 Invalid = true; 1534 continue; 1535 } 1536 1537 // If we should expand this pack expansion now, do so. 1538 if (ShouldExpand) { 1539 for (unsigned I = 0; I != *NumExpansions; ++I) { 1540 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, I); 1541 1542 TypeSourceInfo *BaseTypeLoc = SubstType(Base->getTypeSourceInfo(), 1543 TemplateArgs, 1544 Base->getSourceRange().getBegin(), 1545 DeclarationName()); 1546 if (!BaseTypeLoc) { 1547 Invalid = true; 1548 continue; 1549 } 1550 1551 if (CXXBaseSpecifier *InstantiatedBase 1552 = CheckBaseSpecifier(Instantiation, 1553 Base->getSourceRange(), 1554 Base->isVirtual(), 1555 Base->getAccessSpecifierAsWritten(), 1556 BaseTypeLoc, 1557 SourceLocation())) 1558 InstantiatedBases.push_back(InstantiatedBase); 1559 else 1560 Invalid = true; 1561 } 1562 1563 continue; 1564 } 1565 1566 // The resulting base specifier will (still) be a pack expansion. 1567 EllipsisLoc = Base->getEllipsisLoc(); 1568 Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, -1); 1569 BaseTypeLoc = SubstType(Base->getTypeSourceInfo(), 1570 TemplateArgs, 1571 Base->getSourceRange().getBegin(), 1572 DeclarationName()); 1573 } else { 1574 BaseTypeLoc = SubstType(Base->getTypeSourceInfo(), 1575 TemplateArgs, 1576 Base->getSourceRange().getBegin(), 1577 DeclarationName()); 1578 } 1579 1580 if (!BaseTypeLoc) { 1581 Invalid = true; 1582 continue; 1583 } 1584 1585 if (CXXBaseSpecifier *InstantiatedBase 1586 = CheckBaseSpecifier(Instantiation, 1587 Base->getSourceRange(), 1588 Base->isVirtual(), 1589 Base->getAccessSpecifierAsWritten(), 1590 BaseTypeLoc, 1591 EllipsisLoc)) 1592 InstantiatedBases.push_back(InstantiatedBase); 1593 else 1594 Invalid = true; 1595 } 1596 1597 if (!Invalid && 1598 AttachBaseSpecifiers(Instantiation, InstantiatedBases.data(), 1599 InstantiatedBases.size())) 1600 Invalid = true; 1601 1602 return Invalid; 1603} 1604 1605/// \brief Instantiate the definition of a class from a given pattern. 1606/// 1607/// \param PointOfInstantiation The point of instantiation within the 1608/// source code. 1609/// 1610/// \param Instantiation is the declaration whose definition is being 1611/// instantiated. This will be either a class template specialization 1612/// or a member class of a class template specialization. 1613/// 1614/// \param Pattern is the pattern from which the instantiation 1615/// occurs. This will be either the declaration of a class template or 1616/// the declaration of a member class of a class template. 1617/// 1618/// \param TemplateArgs The template arguments to be substituted into 1619/// the pattern. 1620/// 1621/// \param TSK the kind of implicit or explicit instantiation to perform. 1622/// 1623/// \param Complain whether to complain if the class cannot be instantiated due 1624/// to the lack of a definition. 1625/// 1626/// \returns true if an error occurred, false otherwise. 1627bool 1628Sema::InstantiateClass(SourceLocation PointOfInstantiation, 1629 CXXRecordDecl *Instantiation, CXXRecordDecl *Pattern, 1630 const MultiLevelTemplateArgumentList &TemplateArgs, 1631 TemplateSpecializationKind TSK, 1632 bool Complain) { 1633 bool Invalid = false; 1634 1635 CXXRecordDecl *PatternDef 1636 = cast_or_null<CXXRecordDecl>(Pattern->getDefinition()); 1637 if (!PatternDef) { 1638 if (!Complain) { 1639 // Say nothing 1640 } else if (Pattern == Instantiation->getInstantiatedFromMemberClass()) { 1641 Diag(PointOfInstantiation, 1642 diag::err_implicit_instantiate_member_undefined) 1643 << Context.getTypeDeclType(Instantiation); 1644 Diag(Pattern->getLocation(), diag::note_member_of_template_here); 1645 } else { 1646 Diag(PointOfInstantiation, diag::err_template_instantiate_undefined) 1647 << (TSK != TSK_ImplicitInstantiation) 1648 << Context.getTypeDeclType(Instantiation); 1649 Diag(Pattern->getLocation(), diag::note_template_decl_here); 1650 } 1651 return true; 1652 } 1653 Pattern = PatternDef; 1654 1655 // \brief Record the point of instantiation. 1656 if (MemberSpecializationInfo *MSInfo 1657 = Instantiation->getMemberSpecializationInfo()) { 1658 MSInfo->setTemplateSpecializationKind(TSK); 1659 MSInfo->setPointOfInstantiation(PointOfInstantiation); 1660 } else if (ClassTemplateSpecializationDecl *Spec 1661 = dyn_cast<ClassTemplateSpecializationDecl>(Instantiation)) { 1662 Spec->setTemplateSpecializationKind(TSK); 1663 Spec->setPointOfInstantiation(PointOfInstantiation); 1664 } 1665 1666 InstantiatingTemplate Inst(*this, PointOfInstantiation, Instantiation); 1667 if (Inst) 1668 return true; 1669 1670 // Enter the scope of this instantiation. We don't use 1671 // PushDeclContext because we don't have a scope. 1672 ContextRAII SavedContext(*this, Instantiation); 1673 EnterExpressionEvaluationContext EvalContext(*this, 1674 Sema::PotentiallyEvaluated); 1675 1676 // If this is an instantiation of a local class, merge this local 1677 // instantiation scope with the enclosing scope. Otherwise, every 1678 // instantiation of a class has its own local instantiation scope. 1679 bool MergeWithParentScope = !Instantiation->isDefinedOutsideFunctionOrMethod(); 1680 LocalInstantiationScope Scope(*this, MergeWithParentScope); 1681 1682 // Pull attributes from the pattern onto the instantiation. 1683 InstantiateAttrs(TemplateArgs, Pattern, Instantiation); 1684 1685 // Start the definition of this instantiation. 1686 Instantiation->startDefinition(); 1687 1688 Instantiation->setTagKind(Pattern->getTagKind()); 1689 1690 // Do substitution on the base class specifiers. 1691 if (SubstBaseSpecifiers(Instantiation, Pattern, TemplateArgs)) 1692 Invalid = true; 1693 1694 TemplateDeclInstantiator Instantiator(*this, Instantiation, TemplateArgs); 1695 llvm::SmallVector<Decl*, 4> Fields; 1696 for (RecordDecl::decl_iterator Member = Pattern->decls_begin(), 1697 MemberEnd = Pattern->decls_end(); 1698 Member != MemberEnd; ++Member) { 1699 // Don't instantiate members not belonging in this semantic context. 1700 // e.g. for: 1701 // @code 1702 // template <int i> class A { 1703 // class B *g; 1704 // }; 1705 // @endcode 1706 // 'class B' has the template as lexical context but semantically it is 1707 // introduced in namespace scope. 1708 if ((*Member)->getDeclContext() != Pattern) 1709 continue; 1710 1711 if ((*Member)->isInvalidDecl()) { 1712 Invalid = true; 1713 continue; 1714 } 1715 1716 Decl *NewMember = Instantiator.Visit(*Member); 1717 if (NewMember) { 1718 if (FieldDecl *Field = dyn_cast<FieldDecl>(NewMember)) 1719 Fields.push_back(Field); 1720 else if (NewMember->isInvalidDecl()) 1721 Invalid = true; 1722 } else { 1723 // FIXME: Eventually, a NULL return will mean that one of the 1724 // instantiations was a semantic disaster, and we'll want to set Invalid = 1725 // true. For now, we expect to skip some members that we can't yet handle. 1726 } 1727 } 1728 1729 // Finish checking fields. 1730 ActOnFields(0, Instantiation->getLocation(), Instantiation, 1731 Fields.data(), Fields.size(), SourceLocation(), SourceLocation(), 1732 0); 1733 CheckCompletedCXXClass(Instantiation); 1734 if (Instantiation->isInvalidDecl()) 1735 Invalid = true; 1736 else { 1737 // Instantiate any out-of-line class template partial 1738 // specializations now. 1739 for (TemplateDeclInstantiator::delayed_partial_spec_iterator 1740 P = Instantiator.delayed_partial_spec_begin(), 1741 PEnd = Instantiator.delayed_partial_spec_end(); 1742 P != PEnd; ++P) { 1743 if (!Instantiator.InstantiateClassTemplatePartialSpecialization( 1744 P->first, 1745 P->second)) { 1746 Invalid = true; 1747 break; 1748 } 1749 } 1750 } 1751 1752 // Exit the scope of this instantiation. 1753 SavedContext.pop(); 1754 1755 if (!Invalid) { 1756 Consumer.HandleTagDeclDefinition(Instantiation); 1757 1758 // Always emit the vtable for an explicit instantiation definition 1759 // of a polymorphic class template specialization. 1760 if (TSK == TSK_ExplicitInstantiationDefinition) 1761 MarkVTableUsed(PointOfInstantiation, Instantiation, true); 1762 } 1763 1764 return Invalid; 1765} 1766 1767namespace { 1768 /// \brief A partial specialization whose template arguments have matched 1769 /// a given template-id. 1770 struct PartialSpecMatchResult { 1771 ClassTemplatePartialSpecializationDecl *Partial; 1772 TemplateArgumentList *Args; 1773 }; 1774} 1775 1776bool 1777Sema::InstantiateClassTemplateSpecialization( 1778 SourceLocation PointOfInstantiation, 1779 ClassTemplateSpecializationDecl *ClassTemplateSpec, 1780 TemplateSpecializationKind TSK, 1781 bool Complain) { 1782 // Perform the actual instantiation on the canonical declaration. 1783 ClassTemplateSpec = cast<ClassTemplateSpecializationDecl>( 1784 ClassTemplateSpec->getCanonicalDecl()); 1785 1786 // Check whether we have already instantiated or specialized this class 1787 // template specialization. 1788 if (ClassTemplateSpec->getSpecializationKind() != TSK_Undeclared) { 1789 if (ClassTemplateSpec->getSpecializationKind() == 1790 TSK_ExplicitInstantiationDeclaration && 1791 TSK == TSK_ExplicitInstantiationDefinition) { 1792 // An explicit instantiation definition follows an explicit instantiation 1793 // declaration (C++0x [temp.explicit]p10); go ahead and perform the 1794 // explicit instantiation. 1795 ClassTemplateSpec->setSpecializationKind(TSK); 1796 1797 // If this is an explicit instantiation definition, mark the 1798 // vtable as used. 1799 if (TSK == TSK_ExplicitInstantiationDefinition) 1800 MarkVTableUsed(PointOfInstantiation, ClassTemplateSpec, true); 1801 1802 return false; 1803 } 1804 1805 // We can only instantiate something that hasn't already been 1806 // instantiated or specialized. Fail without any diagnostics: our 1807 // caller will provide an error message. 1808 return true; 1809 } 1810 1811 if (ClassTemplateSpec->isInvalidDecl()) 1812 return true; 1813 1814 ClassTemplateDecl *Template = ClassTemplateSpec->getSpecializedTemplate(); 1815 CXXRecordDecl *Pattern = 0; 1816 1817 // C++ [temp.class.spec.match]p1: 1818 // When a class template is used in a context that requires an 1819 // instantiation of the class, it is necessary to determine 1820 // whether the instantiation is to be generated using the primary 1821 // template or one of the partial specializations. This is done by 1822 // matching the template arguments of the class template 1823 // specialization with the template argument lists of the partial 1824 // specializations. 1825 typedef PartialSpecMatchResult MatchResult; 1826 llvm::SmallVector<MatchResult, 4> Matched; 1827 llvm::SmallVector<ClassTemplatePartialSpecializationDecl *, 4> PartialSpecs; 1828 Template->getPartialSpecializations(PartialSpecs); 1829 for (unsigned I = 0, N = PartialSpecs.size(); I != N; ++I) { 1830 ClassTemplatePartialSpecializationDecl *Partial = PartialSpecs[I]; 1831 TemplateDeductionInfo Info(Context, PointOfInstantiation); 1832 if (TemplateDeductionResult Result 1833 = DeduceTemplateArguments(Partial, 1834 ClassTemplateSpec->getTemplateArgs(), 1835 Info)) { 1836 // FIXME: Store the failed-deduction information for use in 1837 // diagnostics, later. 1838 (void)Result; 1839 } else { 1840 Matched.push_back(PartialSpecMatchResult()); 1841 Matched.back().Partial = Partial; 1842 Matched.back().Args = Info.take(); 1843 } 1844 } 1845 1846 // If we're dealing with a member template where the template parameters 1847 // have been instantiated, this provides the original template parameters 1848 // from which the member template's parameters were instantiated. 1849 llvm::SmallVector<const NamedDecl *, 4> InstantiatedTemplateParameters; 1850 1851 if (Matched.size() >= 1) { 1852 llvm::SmallVector<MatchResult, 4>::iterator Best = Matched.begin(); 1853 if (Matched.size() == 1) { 1854 // -- If exactly one matching specialization is found, the 1855 // instantiation is generated from that specialization. 1856 // We don't need to do anything for this. 1857 } else { 1858 // -- If more than one matching specialization is found, the 1859 // partial order rules (14.5.4.2) are used to determine 1860 // whether one of the specializations is more specialized 1861 // than the others. If none of the specializations is more 1862 // specialized than all of the other matching 1863 // specializations, then the use of the class template is 1864 // ambiguous and the program is ill-formed. 1865 for (llvm::SmallVector<MatchResult, 4>::iterator P = Best + 1, 1866 PEnd = Matched.end(); 1867 P != PEnd; ++P) { 1868 if (getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, 1869 PointOfInstantiation) 1870 == P->Partial) 1871 Best = P; 1872 } 1873 1874 // Determine if the best partial specialization is more specialized than 1875 // the others. 1876 bool Ambiguous = false; 1877 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(), 1878 PEnd = Matched.end(); 1879 P != PEnd; ++P) { 1880 if (P != Best && 1881 getMoreSpecializedPartialSpecialization(P->Partial, Best->Partial, 1882 PointOfInstantiation) 1883 != Best->Partial) { 1884 Ambiguous = true; 1885 break; 1886 } 1887 } 1888 1889 if (Ambiguous) { 1890 // Partial ordering did not produce a clear winner. Complain. 1891 ClassTemplateSpec->setInvalidDecl(); 1892 Diag(PointOfInstantiation, diag::err_partial_spec_ordering_ambiguous) 1893 << ClassTemplateSpec; 1894 1895 // Print the matching partial specializations. 1896 for (llvm::SmallVector<MatchResult, 4>::iterator P = Matched.begin(), 1897 PEnd = Matched.end(); 1898 P != PEnd; ++P) 1899 Diag(P->Partial->getLocation(), diag::note_partial_spec_match) 1900 << getTemplateArgumentBindingsText( 1901 P->Partial->getTemplateParameters(), 1902 *P->Args); 1903 1904 return true; 1905 } 1906 } 1907 1908 // Instantiate using the best class template partial specialization. 1909 ClassTemplatePartialSpecializationDecl *OrigPartialSpec = Best->Partial; 1910 while (OrigPartialSpec->getInstantiatedFromMember()) { 1911 // If we've found an explicit specialization of this class template, 1912 // stop here and use that as the pattern. 1913 if (OrigPartialSpec->isMemberSpecialization()) 1914 break; 1915 1916 OrigPartialSpec = OrigPartialSpec->getInstantiatedFromMember(); 1917 } 1918 1919 Pattern = OrigPartialSpec; 1920 ClassTemplateSpec->setInstantiationOf(Best->Partial, Best->Args); 1921 } else { 1922 // -- If no matches are found, the instantiation is generated 1923 // from the primary template. 1924 ClassTemplateDecl *OrigTemplate = Template; 1925 while (OrigTemplate->getInstantiatedFromMemberTemplate()) { 1926 // If we've found an explicit specialization of this class template, 1927 // stop here and use that as the pattern. 1928 if (OrigTemplate->isMemberSpecialization()) 1929 break; 1930 1931 OrigTemplate = OrigTemplate->getInstantiatedFromMemberTemplate(); 1932 } 1933 1934 Pattern = OrigTemplate->getTemplatedDecl(); 1935 } 1936 1937 bool Result = InstantiateClass(PointOfInstantiation, ClassTemplateSpec, 1938 Pattern, 1939 getTemplateInstantiationArgs(ClassTemplateSpec), 1940 TSK, 1941 Complain); 1942 1943 return Result; 1944} 1945 1946/// \brief Instantiates the definitions of all of the member 1947/// of the given class, which is an instantiation of a class template 1948/// or a member class of a template. 1949void 1950Sema::InstantiateClassMembers(SourceLocation PointOfInstantiation, 1951 CXXRecordDecl *Instantiation, 1952 const MultiLevelTemplateArgumentList &TemplateArgs, 1953 TemplateSpecializationKind TSK) { 1954 for (DeclContext::decl_iterator D = Instantiation->decls_begin(), 1955 DEnd = Instantiation->decls_end(); 1956 D != DEnd; ++D) { 1957 bool SuppressNew = false; 1958 if (FunctionDecl *Function = dyn_cast<FunctionDecl>(*D)) { 1959 if (FunctionDecl *Pattern 1960 = Function->getInstantiatedFromMemberFunction()) { 1961 MemberSpecializationInfo *MSInfo 1962 = Function->getMemberSpecializationInfo(); 1963 assert(MSInfo && "No member specialization information?"); 1964 if (MSInfo->getTemplateSpecializationKind() 1965 == TSK_ExplicitSpecialization) 1966 continue; 1967 1968 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, 1969 Function, 1970 MSInfo->getTemplateSpecializationKind(), 1971 MSInfo->getPointOfInstantiation(), 1972 SuppressNew) || 1973 SuppressNew) 1974 continue; 1975 1976 if (Function->hasBody()) 1977 continue; 1978 1979 if (TSK == TSK_ExplicitInstantiationDefinition) { 1980 // C++0x [temp.explicit]p8: 1981 // An explicit instantiation definition that names a class template 1982 // specialization explicitly instantiates the class template 1983 // specialization and is only an explicit instantiation definition 1984 // of members whose definition is visible at the point of 1985 // instantiation. 1986 if (!Pattern->hasBody()) 1987 continue; 1988 1989 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation); 1990 1991 InstantiateFunctionDefinition(PointOfInstantiation, Function); 1992 } else { 1993 Function->setTemplateSpecializationKind(TSK, PointOfInstantiation); 1994 } 1995 } 1996 } else if (VarDecl *Var = dyn_cast<VarDecl>(*D)) { 1997 if (Var->isStaticDataMember()) { 1998 MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); 1999 assert(MSInfo && "No member specialization information?"); 2000 if (MSInfo->getTemplateSpecializationKind() 2001 == TSK_ExplicitSpecialization) 2002 continue; 2003 2004 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, 2005 Var, 2006 MSInfo->getTemplateSpecializationKind(), 2007 MSInfo->getPointOfInstantiation(), 2008 SuppressNew) || 2009 SuppressNew) 2010 continue; 2011 2012 if (TSK == TSK_ExplicitInstantiationDefinition) { 2013 // C++0x [temp.explicit]p8: 2014 // An explicit instantiation definition that names a class template 2015 // specialization explicitly instantiates the class template 2016 // specialization and is only an explicit instantiation definition 2017 // of members whose definition is visible at the point of 2018 // instantiation. 2019 if (!Var->getInstantiatedFromStaticDataMember() 2020 ->getOutOfLineDefinition()) 2021 continue; 2022 2023 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); 2024 InstantiateStaticDataMemberDefinition(PointOfInstantiation, Var); 2025 } else { 2026 Var->setTemplateSpecializationKind(TSK, PointOfInstantiation); 2027 } 2028 } 2029 } else if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(*D)) { 2030 // Always skip the injected-class-name, along with any 2031 // redeclarations of nested classes, since both would cause us 2032 // to try to instantiate the members of a class twice. 2033 if (Record->isInjectedClassName() || Record->getPreviousDeclaration()) 2034 continue; 2035 2036 MemberSpecializationInfo *MSInfo = Record->getMemberSpecializationInfo(); 2037 assert(MSInfo && "No member specialization information?"); 2038 2039 if (MSInfo->getTemplateSpecializationKind() 2040 == TSK_ExplicitSpecialization) 2041 continue; 2042 2043 if (CheckSpecializationInstantiationRedecl(PointOfInstantiation, TSK, 2044 Record, 2045 MSInfo->getTemplateSpecializationKind(), 2046 MSInfo->getPointOfInstantiation(), 2047 SuppressNew) || 2048 SuppressNew) 2049 continue; 2050 2051 CXXRecordDecl *Pattern = Record->getInstantiatedFromMemberClass(); 2052 assert(Pattern && "Missing instantiated-from-template information"); 2053 2054 if (!Record->getDefinition()) { 2055 if (!Pattern->getDefinition()) { 2056 // C++0x [temp.explicit]p8: 2057 // An explicit instantiation definition that names a class template 2058 // specialization explicitly instantiates the class template 2059 // specialization and is only an explicit instantiation definition 2060 // of members whose definition is visible at the point of 2061 // instantiation. 2062 if (TSK == TSK_ExplicitInstantiationDeclaration) { 2063 MSInfo->setTemplateSpecializationKind(TSK); 2064 MSInfo->setPointOfInstantiation(PointOfInstantiation); 2065 } 2066 2067 continue; 2068 } 2069 2070 InstantiateClass(PointOfInstantiation, Record, Pattern, 2071 TemplateArgs, 2072 TSK); 2073 } else { 2074 if (TSK == TSK_ExplicitInstantiationDefinition && 2075 Record->getTemplateSpecializationKind() == 2076 TSK_ExplicitInstantiationDeclaration) { 2077 Record->setTemplateSpecializationKind(TSK); 2078 MarkVTableUsed(PointOfInstantiation, Record, true); 2079 } 2080 } 2081 2082 Pattern = cast_or_null<CXXRecordDecl>(Record->getDefinition()); 2083 if (Pattern) 2084 InstantiateClassMembers(PointOfInstantiation, Pattern, TemplateArgs, 2085 TSK); 2086 } 2087 } 2088} 2089 2090/// \brief Instantiate the definitions of all of the members of the 2091/// given class template specialization, which was named as part of an 2092/// explicit instantiation. 2093void 2094Sema::InstantiateClassTemplateSpecializationMembers( 2095 SourceLocation PointOfInstantiation, 2096 ClassTemplateSpecializationDecl *ClassTemplateSpec, 2097 TemplateSpecializationKind TSK) { 2098 // C++0x [temp.explicit]p7: 2099 // An explicit instantiation that names a class template 2100 // specialization is an explicit instantion of the same kind 2101 // (declaration or definition) of each of its members (not 2102 // including members inherited from base classes) that has not 2103 // been previously explicitly specialized in the translation unit 2104 // containing the explicit instantiation, except as described 2105 // below. 2106 InstantiateClassMembers(PointOfInstantiation, ClassTemplateSpec, 2107 getTemplateInstantiationArgs(ClassTemplateSpec), 2108 TSK); 2109} 2110 2111StmtResult 2112Sema::SubstStmt(Stmt *S, const MultiLevelTemplateArgumentList &TemplateArgs) { 2113 if (!S) 2114 return Owned(S); 2115 2116 TemplateInstantiator Instantiator(*this, TemplateArgs, 2117 SourceLocation(), 2118 DeclarationName()); 2119 return Instantiator.TransformStmt(S); 2120} 2121 2122ExprResult 2123Sema::SubstExpr(Expr *E, const MultiLevelTemplateArgumentList &TemplateArgs) { 2124 if (!E) 2125 return Owned(E); 2126 2127 TemplateInstantiator Instantiator(*this, TemplateArgs, 2128 SourceLocation(), 2129 DeclarationName()); 2130 return Instantiator.TransformExpr(E); 2131} 2132 2133bool Sema::SubstExprs(Expr **Exprs, unsigned NumExprs, bool IsCall, 2134 const MultiLevelTemplateArgumentList &TemplateArgs, 2135 llvm::SmallVectorImpl<Expr *> &Outputs) { 2136 if (NumExprs == 0) 2137 return false; 2138 2139 TemplateInstantiator Instantiator(*this, TemplateArgs, 2140 SourceLocation(), 2141 DeclarationName()); 2142 return Instantiator.TransformExprs(Exprs, NumExprs, IsCall, Outputs); 2143} 2144 2145NestedNameSpecifierLoc 2146Sema::SubstNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS, 2147 const MultiLevelTemplateArgumentList &TemplateArgs) { 2148 if (!NNS) 2149 return NestedNameSpecifierLoc(); 2150 2151 TemplateInstantiator Instantiator(*this, TemplateArgs, NNS.getBeginLoc(), 2152 DeclarationName()); 2153 return Instantiator.TransformNestedNameSpecifierLoc(NNS); 2154} 2155 2156/// \brief Do template substitution on declaration name info. 2157DeclarationNameInfo 2158Sema::SubstDeclarationNameInfo(const DeclarationNameInfo &NameInfo, 2159 const MultiLevelTemplateArgumentList &TemplateArgs) { 2160 TemplateInstantiator Instantiator(*this, TemplateArgs, NameInfo.getLoc(), 2161 NameInfo.getName()); 2162 return Instantiator.TransformDeclarationNameInfo(NameInfo); 2163} 2164 2165TemplateName 2166Sema::SubstTemplateName(NestedNameSpecifierLoc QualifierLoc, 2167 TemplateName Name, SourceLocation Loc, 2168 const MultiLevelTemplateArgumentList &TemplateArgs) { 2169 TemplateInstantiator Instantiator(*this, TemplateArgs, Loc, 2170 DeclarationName()); 2171 CXXScopeSpec SS; 2172 SS.Adopt(QualifierLoc); 2173 return Instantiator.TransformTemplateName(SS, Name, Loc); 2174} 2175 2176bool Sema::Subst(const TemplateArgumentLoc *Args, unsigned NumArgs, 2177 TemplateArgumentListInfo &Result, 2178 const MultiLevelTemplateArgumentList &TemplateArgs) { 2179 TemplateInstantiator Instantiator(*this, TemplateArgs, SourceLocation(), 2180 DeclarationName()); 2181 2182 return Instantiator.TransformTemplateArguments(Args, NumArgs, Result); 2183} 2184 2185llvm::PointerUnion<Decl *, LocalInstantiationScope::DeclArgumentPack *> * 2186LocalInstantiationScope::findInstantiationOf(const Decl *D) { 2187 for (LocalInstantiationScope *Current = this; Current; 2188 Current = Current->Outer) { 2189 2190 // Check if we found something within this scope. 2191 const Decl *CheckD = D; 2192 do { 2193 LocalDeclsMap::iterator Found = Current->LocalDecls.find(CheckD); 2194 if (Found != Current->LocalDecls.end()) 2195 return &Found->second; 2196 2197 // If this is a tag declaration, it's possible that we need to look for 2198 // a previous declaration. 2199 if (const TagDecl *Tag = dyn_cast<TagDecl>(CheckD)) 2200 CheckD = Tag->getPreviousDeclaration(); 2201 else 2202 CheckD = 0; 2203 } while (CheckD); 2204 2205 // If we aren't combined with our outer scope, we're done. 2206 if (!Current->CombineWithOuterScope) 2207 break; 2208 } 2209 2210 // If we didn't find the decl, then we either have a sema bug, or we have a 2211 // forward reference to a label declaration. Return null to indicate that 2212 // we have an uninstantiated label. 2213 assert(isa<LabelDecl>(D) && "declaration not instantiated in this scope"); 2214 return 0; 2215} 2216 2217void LocalInstantiationScope::InstantiatedLocal(const Decl *D, Decl *Inst) { 2218 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D]; 2219 if (Stored.isNull()) 2220 Stored = Inst; 2221 else if (Stored.is<Decl *>()) { 2222 assert(Stored.get<Decl *>() == Inst && "Already instantiated this local"); 2223 Stored = Inst; 2224 } else 2225 LocalDecls[D].get<DeclArgumentPack *>()->push_back(Inst); 2226} 2227 2228void LocalInstantiationScope::InstantiatedLocalPackArg(const Decl *D, 2229 Decl *Inst) { 2230 DeclArgumentPack *Pack = LocalDecls[D].get<DeclArgumentPack *>(); 2231 Pack->push_back(Inst); 2232} 2233 2234void LocalInstantiationScope::MakeInstantiatedLocalArgPack(const Decl *D) { 2235 llvm::PointerUnion<Decl *, DeclArgumentPack *> &Stored = LocalDecls[D]; 2236 assert(Stored.isNull() && "Already instantiated this local"); 2237 DeclArgumentPack *Pack = new DeclArgumentPack; 2238 Stored = Pack; 2239 ArgumentPacks.push_back(Pack); 2240} 2241 2242void LocalInstantiationScope::SetPartiallySubstitutedPack(NamedDecl *Pack, 2243 const TemplateArgument *ExplicitArgs, 2244 unsigned NumExplicitArgs) { 2245 assert((!PartiallySubstitutedPack || PartiallySubstitutedPack == Pack) && 2246 "Already have a partially-substituted pack"); 2247 assert((!PartiallySubstitutedPack 2248 || NumArgsInPartiallySubstitutedPack == NumExplicitArgs) && 2249 "Wrong number of arguments in partially-substituted pack"); 2250 PartiallySubstitutedPack = Pack; 2251 ArgsInPartiallySubstitutedPack = ExplicitArgs; 2252 NumArgsInPartiallySubstitutedPack = NumExplicitArgs; 2253} 2254 2255NamedDecl *LocalInstantiationScope::getPartiallySubstitutedPack( 2256 const TemplateArgument **ExplicitArgs, 2257 unsigned *NumExplicitArgs) const { 2258 if (ExplicitArgs) 2259 *ExplicitArgs = 0; 2260 if (NumExplicitArgs) 2261 *NumExplicitArgs = 0; 2262 2263 for (const LocalInstantiationScope *Current = this; Current; 2264 Current = Current->Outer) { 2265 if (Current->PartiallySubstitutedPack) { 2266 if (ExplicitArgs) 2267 *ExplicitArgs = Current->ArgsInPartiallySubstitutedPack; 2268 if (NumExplicitArgs) 2269 *NumExplicitArgs = Current->NumArgsInPartiallySubstitutedPack; 2270 2271 return Current->PartiallySubstitutedPack; 2272 } 2273 2274 if (!Current->CombineWithOuterScope) 2275 break; 2276 } 2277 2278 return 0; 2279} 2280