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