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